/*统计一个C源程序中关键字的个数*/
# include <stdio.h> # include <string.h> # include <stdlib.h> # define SIZE 32 void main(int argc,char *argv[]) { int i,j,sign; char ch; static int stop=1,number=0;//前者控制输出,后者保存个数 char s[SIZE][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 { for(i=0;i<SIZE;i++) { ch=fgetc(fp);//读取字符 while(!feof(fp)) { sign=1; for(j=0;j<strlen(s[i]);j++) { if(s[i][j]!=ch) { sign=0; break; } } if(sign==1) { number++; ch=fgetc(fp); } } rewind(fp); printf("The number of \"%s\" is %d\n",s[i],number);//10行停一次 if((i-1)==10*stop) { printf("*****Any Key to Continue*****\n"); getchar(); stop++; } } } fclose(fp); }