写了个示例,你看看吧,如有问题再提 int main(void) { int Printflag=1,Stringflag=-1; int Flag0=1,Flag1=1; char *ptr; char *MAIN_string= "#include<stdio.h>\n" "int main(void)\n" "{\n" " long i,j,k,a=0;//定义变量\n" " for (i=1;i<=360;i++) /*可以证明i是小于等于360的*/\n" " for/*测试*/ (j=1;j<500;j++)\n" " for (k=1;k<=500;k++)//TEST//测试\n" " if (i*i+j*j==k*k) \n" " a++; \n" "printf(\"Total:%d\\n\",a);\n" "printf(\"删除注释示例// /**/ \\n\");\n" "}";
ptr=MAIN_string;
while(*ptr)printf("%c",*ptr++); printf("\n\n\n"); ptr=MAIN_string;
while(*ptr) {
if(*ptr=='\"'&&*(ptr-1)!='\\')Stringflag*=-1; if(Stringflag == -1) { if(*ptr=='/'&&*(ptr+1)=='*')Flag0=0; if(*ptr=='*'&&*(ptr+1)=='/')Flag0=1,ptr+=2; if(*ptr=='/'&&*(ptr+1)=='/')Flag1=0; if(*ptr=='\n')Flag1=1; if(Flag0&&Flag1)Printflag=1; else Printflag =0;
} else { Printflag=1; // printf("%c",*ptr++); } if(Printflag)printf("%c",*ptr++); else ptr++;
}
}