初学者,求下面程序大部分语句的注解,越详细越好,、、
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define N 200
#define M 20
static int sum=0;/*sum是用来记录目标单词总共出现的次数,必须用static型*/
void checkword(char* p1,char* objectword)
{
char temp[M];/*这个数组是用来存取从句子中提取出来的单词*/
int i=0,j,count=0; 初始值
while(p1[i]!='\0')
{
j=0;
while(isalpha(p1[i]))
{/*用来检测p1[i]是不是字母*/
temp[j]=p1[i];
i++;
j++;
}
temp[j]='\0';
if(strcmp(temp,objectword)==0)/*判断提取出来的单词是否与要查找的单词一致*/
count++;
while(!isalpha(p1[i])&&p1[i]!='\0')
{
i++;
}
}
if(count!=0)
{
sum+=count;
}
}
void getsentence(char* p1,FILE* p2,char* objectword)
{
int i;
char ch;
ch=fgetc(p2); 调用fgetc()函数,括号内为函数的参数,p2为指针
while(ch!=EOF) 读入的字符不是结束标志
{
i=0;
while(ch!='.'&&ch!=';'&&ch!=EOF) 一直输入字符直到遇到空格或者结束符为止
{
p1[i]=ch;
i++;
ch=fgetc(p2);
}
if(ch=='.'||ch==';')
{
p1[i]=ch;
p1[i+1]='\0';
checkword(p1,objectword);
ch=fgetc(p2);
}
}
}
int main()
{
char str1[N],str2[M],filename[M];
char ch;
FILE* fp;
printf("Please input the name of the file:\n");
scanf("%s",filename);
printf("Please input the word which you want to search:\n");
scanf("%s",str2);
fp=fopen(filename,"r");
if(fp==NULL)
{
printf("cannot open the file\n");
exit(0);
}
getsentence(str1,fp,str2);
printf("The word which you want to search in the file has been appeared %d times in total!\n",sum);
fclose(fp);
system("pause");
return 0;
}
。