在另一个C程序中没有注释的嵌套!
我稍后给出我的答案!
来了!
#include<stdio.h>
void recomment(int c);
void in_comment(void);
void echo_quote(int c);
int
/*romove all comments form a valid c program*/
main()
{
int c;
while((c=getchar())!=EOF)
recomment(c);
return 0;
}
/*recommet :read each character ,remove the commets*/
void recomment(int c)
{
int d;
if(c=='/')
if((d=getchar())=='*')
in_comment();/*beginning comments*/
else
if(d=='/')
{
putchar(c);/*another slash*/
recomment(d);
}
else
{putchar(c);/*not a comment*/
putchar(d);
}
else if(c=='\''||c=='"')
echo_quote(c); /*quote befins*/
else
putchar(c); /*not a comment */
}
/*in_comment:inside of a valid comment */
void in_comment(void)
{
int c,d;
c=getchar(); /*prev character */
d=getchar();/*curr character*/
while(c!='*'||d!='/')/*search for end */
{
c=d ;
d=getchar();
}
}
/*echo_quote:echo characters within quotes*/
void echo_quote(int c)
{
int d;
putchar(c);
while((d=getchar())!=c)/*search for end*/
{
putchar(d);
if(d=='\\')
putchar(getchar());/*ignor escape seq*/
}
putchar(d);
}