/*题目:任给一个C源程序,要求统计出里面关键字的个数*/
# include <stdio.h> # include <string.h>//用strcmp() # include <stdlib.h>//用exit() # include <ctype.h>//用isalpha() void main(int argc,char *argv[])//运行时输入要检测的文件名 { int m,n,i; char ch; static int stop=1; static int num[32]={0};//保存各关键字的个数 static char ss[9]={0};//保存读取的字符 char s[32][9]={{"auto"},{"break"},{"case"},{"char"},{"const"},{"continue"},
{"default"},{"do"},{"double"},{"else"},{"enum"},{"extern"},
{"float"},{"for"},{"goto"},{"if"},{"int"},{"long"},{"register"},
{"return"},{"short"},{"signed"},{"sizeof"},{"static"},
{"struct"},{"switch"},{"typedef"},{"union"},{"unsigned"},
{"void"},{"volatile"}};
FILE *fp; if((fp=fopen(argv[1],"r"))==NULL) { printf("The file \"%s\" cannot be opened!\n",argv[1]); exit(0); } else { ch=fgetc(fp);//从文件读入字符 while(!feof(fp))//遇文件结束则结束循环 { for(m=0;m<9;m++) { if(isalpha(ch)) { ss[m]=ch;//如果读入的是字母,存入数组中 ch=fgetc(fp); } else break;//文件结束时,退出 } for(n=0;n<32;n++) if(strcmp(s[n],ss)==0) { num[n]++;//如果保存读入字符的数组和关键字相同,则对应的关键字的个数加1 break; } } } fclose(fp); for(i=0;i<32;i++) { printf("The number of \"%s\" is %d\n",s[i],num[i]); if((i-1)==10*stop) { printf("*****Any Key to Continue*****\n"); getchar(); stop++; } } }
/*不胜感激*/