您好,登录后才能下订单哦!
代码期间,把代码过程中较好的代码片段做个珍藏,如下的代码内容是关于C++用来检测数据类型的声明工具的代码,应该是对码农有较大用。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define MAXTOKENS 100
#define MAXTOKENLEN 64
struct token{
char type;
char string[MAXTOKENLEN];
};
int top = -1;
struct token stack[MAXTOKENS];
struct token this;
#define pop stack[top--]
#define push(s) stack[++top] = s
#define STRCMP(a,R,b) (strcmp(a,b) R 0)
enum type_tag classify_string(void)
{
if (STRCMP(s,==,"const"))
{
strcpy(s,"read-only");
return QUALIFIER;
}
if (STRCMP(s,==,"volatile")) return QUALIFIER;
if (STRCMP(s,==,"void")) return TYPE;
if (STRCMP(s,==,"char")) return TYPE;
if (STRCMP(s,==,"signed")) return TYPE;
if (STRCMP(s,==,"unsigned")) return TYPE;
if (STRCMP(s,==,"short")) return TYPE;
if (STRCMP(s,==,"int")) return TYPE;
if (STRCMP(s,==,"long")) return TYPE;
if (STRCMP(s,==,"double")) return TYPE;
if (STRCMP(s,==,"float")) return TYPE;
if (STRCMP(s,==,"struct")) return TYPE;
if (STRCMP(s,==,"union")) return TYPE;
if (STRCMP(s,==,"enum")) return TYPE;
return IDENTIFIER;
}
void gettoken(void)
{
{
this.type = (char)classify_string();
return;
}
{
strcpy(this.string,"pointer to");
return;
}
this.string[1] = '';
return;
}
int read_to_first_identifier(void)
{
{
push(this);
gettoken();
}
printf("%s is ",this.string);
return 0;
}
int deal_with_arrays(void)
{
while(this.type == '[')
{
printf("array ");
if (isdigit(this.string[0]))
{
printf("0..%d ",atoi(this.string)-1);
}
printf("of ");
}
return 0;
}
int deal_with_function_args(void)
{
{
gettoken();
}
printf("function returning ");
return 0;
}
int deal_with_pointers(void)
{
{
printf("%s ",pop.string);
}
return 0;
}
int deal_with_declarator(void)
{
switch(this.type)
{
case '[' : deal_with_arrays();break;
case '(' : deal_with_function_args();break;
default : break;
}
while(top >= 0)
{
if (stack[top].type == '(')
{
pop;
}
else
{
printf("%s ",pop.string);
}
}
return 0;
}
int main(void)
{
read_to_first_identifier();
deal_with_declarator();
printf("n");
return 0;
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。