请高人指点代码

hb0800092 2009-03-13 06:06:13
高手帮忙指点下
这段代码用来删除c程序里的注释
怎么样才能让它更简洁点
程序在命令行输入三个参数 或连个参数都可以运行
#include<stdio.h>
#define MAXSTACK 100
#define OUT 0
#define IN 1
typedef struct{
int stacksave[MAXSTACK];
int top;
}stack;
stack s;
void check_file(char** file)
{
char ch,ch1;
FILE* sourcesfile = NULL, *objfile = NULL;
if ((sourcesfile = fopen(file[1],"r")) == NULL)
{
printf("can't open the file %s\n",file[1]);
exit(1);
}
if ((objfile = fopen(file[2],"w")) == NULL)
{
printf("can't open the file %s\n",file[2]);
exit(1);
}
while ((ch = fgetc(sourcesfile)) != EOF)
{
if (ch == 34 )
{
fputc(ch,objfile);
while ((ch = fgetc(sourcesfile)) != 34)
fputc(ch,objfile);
}
else if (ch == 39)
{
fputc(ch,objfile);
while ((ch = fgetc(sourcesfile)) != 39)
fputc(ch,objfile);
}
else if (ch == '/')
{
if ((ch1 = fgetc(sourcesfile)) == '*')
{
s.stacksave[s.top] = IN;
s.top++;
}
else
{
fputc(ch,objfile);
fputc(ch1,objfile);
}
}
else if (ch == '*')
{
if ((ch1 = fgetc(sourcesfile)) == '/')
{
if (s.stacksave[s.top-1] == IN)
s.top--;
}
else
{
fputc(ch,objfile);
fputc(ch1,objfile);
}
}
else if (s.top == 0)
fputc(ch,objfile);
}
}
int main(int argc, char** argv)
{
if (argc < 2)
{
printf("To few parmete in the cmd\n");
exit(0);
}
if (argc == 2)
{
argv[2] = "out.c";
}
s.top = 0;
check_file(argv);
return 0;
}
...全文
41 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧