更新时间:2022-10-12 12:45:18
#include <stdio.h> #include<string.h> #include<stdlib.h> int appear(char*s1,char*s2); int main( ) { char line[256]; char main_fun[8]="main()"; int main_num=0;//初时,尚未发现 //将文件中的数据读入到字符数组中 FILE *fp = fopen("source.c","r"); //以输入的方式打开文件 if(fp==NULL) //测试是否成功打开 { printf("source code read error!\n"); exit(1); } while(!feof(fp)) { fgets(line,256, fp); main_num+=appear(line,main_fun); if (main_num>1) //多于1个,没有必要再去读取 break; } fclose(fp); //识别结论 if(main_num==0) printf("error: no main().\n"); else if (main_num==1) printf("right: a main() be exist.\n"); else printf("error: more than one main().\n"); return 0; } //返回s2在s1中出现了几次 int appear(char*s1,char*s2) { int n=0,flag; char *p,*q; for(; *s1!='\0'; s1++) { if (*s2==*s1) /*判断字符串中是否有和要判断的字串首字符相同的字符*/ { flag=1; p=s1 ; /*s1 p 为第一个相同字符的地址*/ q=s2; for(; *q!='\0';) /*如果有则判断接下去的几个字符是否相同*/ { if (*q++!=*p++) { flag=0; break; } } if (flag==1) n++; } } return(n); }
#include <stdio.h> #include<string.h> #include<stdlib.h> int appear(char*s1,char*s2); int main( ) { char line[256]; int if_num=0, while_num, for_num=0; //将文件中的数据读入到字符数组中 FILE *fp = fopen("test.c","r"); //以输入的方式打开文件 if(fp==NULL) //测试是否成功打开 { printf("source code read error!\n"); exit(1); } while(!feof(fp)) { fgets(line,256, fp); //读入一行 if_num+=appear(line,"if"); while_num+=appear(line,"while"); for_num+=appear(line,"for"); } fclose(fp); //识别结论 printf("if: %d\n", if_num); printf("while: %d\n", while_num); printf("for: %d\n", for_num); return 0; } //返回s2在s1中出现了几次 int appear(char*s1,char*s2) { int n=0,flag; char *p,*q; for(; *s1!='\0'; s1++) { if (*s2==*s1) /*判断字符串中是否有和要判断的字串首字符相同的字符*/ { flag=1; p=s1 ; /*s1 p 为第一个相同字符的地址*/ q=s2; for(; *q!='\0';) /*如果有则判断接下去的几个字符是否相同*/ { if (*q++!=*p++) { flag=0; break; } } if (flag==1) n++; } } return(n); }注:在真正的IDE中进行词分析时,需要找到if、for、while等关键词,还要识别是否符合语法规定的要求,并且这样的工作,就在一次“扫描”中完成,采用的技术,已经不是这种简单地进行字符串匹配了。
#include <stdio.h> #include<string.h> #include<stdlib.h> void formatPrograme(char *sourcefile, char *targetfile); void outprogram(char *filename); int main( ) { formatPrograme("source.c", "target.c"); printf("经过处理后的程序是:\n"); outprogram("target.c"); return 0; } void formatPrograme(char *sourcefile, char *targetfile) { char ch1, ch2; //将文件中的数据读入到字符数组中 FILE *fpin = fopen(sourcefile,"r"); //以输入的方式打开文件 if(fpin==NULL) //测试是否成功打开 { printf("source code read error!\n"); exit(0); } FILE *fpout = fopen(targetfile,"w"); //以输入的方式打开文件 if(fpout==NULL) //测试是否成功打开 { printf("source code write error!\n"); exit(0); } ch1='\0'; while(!feof(fpin)) { ch2= fgetc(fpin); //读到了花括号,且前一个符号不是换行,应该加入一个换行 if((ch2=='{'||ch2=='}')&&(ch1!='\n')) fputc('\n', fpout); else //当前读到的不是换行,但前一个是花括号,此时也该加 if((ch1=='{'||ch1=='}')&&(ch2!='\n')) fputc('\n', fpout); fputc(ch2, fpout); //输出当前读入的符号 ch1=ch2; } fclose(fpout); fclose(fpin); } void outprogram(char *filename) { char line[256]; int n = 1; //将文件中的数据读入到字符数组中 FILE *fp = fopen(filename,"r"); //以输入的方式打开文件 if(fp==NULL) //测试是否成功打开 { printf("source code read error!\n"); exit(1); } while(!feof(fp)) { fgets(line,256, fp); //读入一行 printf("%d\t%s\n", n, line); n++; } fclose(fp); return; }
#include <stdio.h> #include<string.h> #include<stdlib.h> void formatPrograme(char *sourcefile, char *targetfile); void outprogram(char *filename); int main( ) { formatPrograme("source.c", "target.c"); printf("任务完成!\n"); return 0; } void formatPrograme(char *sourcefile, char *targetfile) { int m, n; char line[256]; //将文件中的数据读入到字符数组中 FILE *fpin = fopen(sourcefile,"r"); //以输入的方式打开文件 if(fpin==NULL) //测试是否成功打开 { printf("source code read error!\n"); exit(0); } FILE *fpout = fopen(targetfile,"w"); //以输入的方式打开文件 if(fpout==NULL) //测试是否成功打开 { printf("source code write error!\n"); exit(0); } printf("您要将第m行开始的n行代码作为注释,请输入m和n:"); scanf("%d %d", &m, &n); int n1=0; while(!feof(fpin)) { fgets(line,255,fpin); n1++; if(n1>=m&&n1<m+n) fputs("//",fpout); //指定行内,本行始加注释 fputs(line, fpout); } fclose(fpout); fclose(fpin); }