求助,这个C语言程序的作用是?主要里面delcomm函数的作用。
#include"stdio.h"#include<stdlib.h>
void main()
{
void delcomm(FILE * fp1, FILE * fp2);
FILE* fp1, * fp2;
char source, target;
printf("Input source file name:");
gets(source);
printf("Input target file name:");
gets(target);
if ((fp1 = fopen((source), "r")) == NULL)
{
printf("Can't open source file.\n");
exit(0);
}
if ((fp2 = fopen((target), "w")) == NULL)
{
printf("Can't open target file.\n");
exit(0);
}
delcomm(fp1, fp2);
}
void delcomm(FILE* fp1, FILE* fp2)
{
int c, i = 0;
while (c = fgetc(fp1) != EOF)
if (c == '\n')
fprintf(fp2, "\n");
else
switch (i)
{
case 0:if (c == '/') i = 1;
else fprintf(fp2, "%c", c);
break;
case 1:if (c == '*') i = 2;
else
{
fprintf(fp2, "/%c", c);
i = 0;
}
break;
case 2:if (c == '*')i = 3;
break;
case 3:i = (c == '/') ? 0 : 2;
break;
}
} 本帖最后由 我叫MD 于 2020-6-1 22:41 编辑
这程序有问题啊 感觉c的值只有 0 和 1,所以根据条件判断 只会一直执行你所写的第34行代码,然后退出
可能不同的编译器会编译不同的版本,我拿vs2019调试过了c的值只会为1或者0 #include <stdio.h>
#include <stdlib.h>
void main()
{
FILE* fp1, * fp2;
void delcomm(FILE* fp1, FILE* fp2);
char source, target;
printf("Input source file name: ");
gets(source);
printf("Input target file name: ");
gets(target);
if ((fp1 = fopen(source, "r")) == NULL)
{
printf("Can't open FILE.\n");
exit(0);
}
if ((fp2 = fopen(target, "w")) == NULL)
{
printf("Can't create target file.\n");
exit(0);
}
delcomm(fp1, fp2);
fcloseall();
}
void delcomm(FILE* fp1, FILE* fp2)
{
int c, i = 0;
while (((c = fgetc(fp1)) != EOF))
if (c == '\n')
fprintf(fp2, "\n");
else
switch (i)
{
case 0: if (c == '/') i = 1;
else fprintf(fp2, "%c", c);
break;
case 1: if (c == '*') i = 2;
else
{
fprintf(fp2, "/%c", c);
i = 0;
}
break;
case 2: if (c == '*') i = 3;
break;
case 3: i = (c == '/') ? 0 : 2;
break;
}
} 改下代码,这个好像是复制文本的,但是不知道那个自定义的函数是在干嘛
页:
[1]