编写一个删除C语言程序中所有注释语句
#include<stdio.h>voidrcomment(intc);voidin_comment(void);voidecho_quote(intc);voidmai...
#include<stdio.h>
void rcomment(int c);
void in_comment(void);
void echo_quote(int c);
void main()
{
int c, d;
while ((c = getchar()) != EOF)
rcomment(c);
return 0;
}
void rcomment(int c)
{
int d;
if (c == '/')
if ((d = getchar()) == '*')
in_comment();
else if (d == '/')
{
putchar(c);
rcomment(d);
}
else
{
putchar(c);
putchar(d);
}
else if (c == '\'' || c == '"')
echo_quote(c);
else
putchar(c);
}
void in_comment(void)
{
int c, d;
c = getchar();
d = getchar();
while (c != '*' || d != '/')
{
c = d;
d = getchar();
}
}
void echo_quote(int c)
{
int d;
putchar(c);
while ((d = getchar()) != c)
{
putchar(d);
if (d == '\\')
putchar(getchar());
}
putchar(d);
}
求讲解 展开
void rcomment(int c);
void in_comment(void);
void echo_quote(int c);
void main()
{
int c, d;
while ((c = getchar()) != EOF)
rcomment(c);
return 0;
}
void rcomment(int c)
{
int d;
if (c == '/')
if ((d = getchar()) == '*')
in_comment();
else if (d == '/')
{
putchar(c);
rcomment(d);
}
else
{
putchar(c);
putchar(d);
}
else if (c == '\'' || c == '"')
echo_quote(c);
else
putchar(c);
}
void in_comment(void)
{
int c, d;
c = getchar();
d = getchar();
while (c != '*' || d != '/')
{
c = d;
d = getchar();
}
}
void echo_quote(int c)
{
int d;
putchar(c);
while ((d = getchar()) != c)
{
putchar(d);
if (d == '\\')
putchar(getchar());
}
putchar(d);
}
求讲解 展开
1个回答
展开全部
#include <stdio.h>
int main()
{
FILE *fp,*fp1;
char str[99]="";
int i=0;
fp=fopen("test.c","r"); //要打开的源文件
fp1=fopen("new.c","w"); //另存为
while(fgets(str,99,fp)!=NULL)
{
for(i=0;i<99;i++)
if(str[i]=='/'&&str[i-1]=='/'){str[i-1]='\n';str[i]='\0';break;}
fputs(str,fp1);
}
fcloseall();
return 0;
}
如果有/* */块注释的话,用这个
#include <stdio.h>
#include <string.h>
int main()
{
FILE *fp,*fp1;
char str[99]="",str1[99]="";
int i,j,no=0;
fp=fopen("test.c","r"); //要打开的源文件
fp1=fopen("new.c","w"); //另存为
while(fgets(str,99,fp)!=NULL)
{
for(i=0;i<99;i++)
{
if(str[i]=='/'&&str[i-1]=='/')
{str[i-1]='\n';str[i]='\0';}
if(str[i]=='*'&&str[i-1]=='/')
{str[i-1]='\0';no=1;fputs(str,fp1);}
if(str[i]=='/'&&str[i-1]=='*')
{
for(j=0;j<98-i;j++){str[j]=str[i+j+1];}
str[j]='\0';
no=0;
}
}
if(no==0)fputs(str,fp1);
}
fcloseall();
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询