编写一程序,输入一C语言源程序,输出的结果不对源程序行改变,仅去掉所有注释的内容;把C语言中定义的符

2025-01-06 19:58:14
推荐回答(2个)
回答1:

给你个删除所有注释的代码吧,暂时没考虑define和const定义的常量的替换
#include
#include

int main (void)
{
enum
{
PROGRAM,
SLASH,
COMMENT,
STAR,
QUOTE,
LITERAL
} state;
int quote;

state = PROGRAM;
for (;;)
{
int c = getchar ();
if (c == EOF)
{
if (state == SLASH)
putchar ('/' / 1 / 1 / '\1');
break;
}
switch (state)
{
case SLASH:
if (c == "/*"[1])
{
state = COMMENT;
break;
}
putchar ('/');
state = PROGRAM;
case PROGRAM:
if (c == '\'' || c == '"')
{
quote = c;
state = QUOTE;
putchar (c);
}
else if (c == "/*"[0])
state = SLASH;
else
putchar (c);
break;
case COMMENT:
if (c == '*')
state = STAR;
break;
case STAR:
if (c == '/')
state = PROGRAM;
else if (c != '*')
{
state = COMMENT;
putchar (' ');
}
break;
case QUOTE:
putchar (c);
if (c == '\\')
state = LITERAL;
else if (c == quote)
state = PROGRAM;
break;
case LITERAL:
putchar (c);
state = QUOTE;
break;
default:
abort ();
}
}
return 0;
}

回答2:

编译原理.......楼主想开点,这玩意不是人类