| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 423 人关注过本帖
标题:初学者,求下面程序大部分语句的注解,越详细越好,、、
只看楼主 加入收藏
我猜你不懂
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-6-24
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
初学者,求下面程序大部分语句的注解,越详细越好,、、




#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;

}

搜索更多相关主题的帖子: 单词 count include 
2013-06-26 08:54
pauljames
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:千里冰封
威 望:9
帖 子:1555
专家分:10000
注 册:2011-5-8
收藏
得分:10 
程序本身不复杂,关键是一些库函数怎么用,所以你要查一下文件操作和字符串操作函数的使用方法

经常不在线不能及时回复短消息,如有c/单片机/运动控制/数据采集等方面的项目难题可加qq1921826084。
2013-06-28 08:07
浪漫横行
Rank: 4
等 级:业余侠客
威 望:1
帖 子:127
专家分:291
注 册:2013-4-30
收藏
得分:10 
你这样问没用,还不如自己网上查查,或看书。。。
2013-06-29 15:33
快速回复:初学者,求下面程序大部分语句的注解,越详细越好,、、
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015486 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved