首页 > 其他 > 详细

文章单词出现频率

时间:2014-03-04 10:50:12      阅读:500      评论:0      收藏:0      [点我收藏+]

#include<iostream.h> #include<string.h> #include<fstream.h>

struct word

{    

  char name[30];

    int num;

    struct word *next;

};

void readfile(struct word*&head) {

    struct word*fp;

    if((fp=fopen("in.txt","r"))==NULL)

    {

        cout<<"无法打开此文件!<<endl;

        break;

    }   

  char ch;

 char temp[30];

    struct word *p;

    while(!feof(fp))  

   {

        int i=0;

        ch=fgetc(fp);

        temp[0]=‘ ‘;

        while((ch>=‘a‘&&ch<=‘z‘)||(ch>=‘A‘&&ch<=‘Z‘)||temp[0]==‘ ‘)

        {  

           if(ch>=‘a‘&&ch<=‘z‘||ch>=‘A‘&&ch<=‘Z‘)

            {

                temp[i]=ch;

                i++;

            }

            ch=fgetc(fp);

            if(feof(fp))

    break;

        }

        temp[i]=‘\0‘;

        p=head->next;

        while(p)

        {

            if(!_stricmp(temp,p->name))

            {

                p->num++;

    break;

            }

            p=p->next;

        }

        if(!p&&temp[0]!=‘\0‘)

        {

                p=new word;

                strcpy(p->name,temp);

                p->num=1;

                p->next=head->next;

                head->next=p;

        }

    }

}

void puts(struct word*&head) {

 struct word *q;

 q=q->name;

} void sort(struct word*&head) {

        struct word *ptr;

    int a[10],i;

    for(i=0;i<10;i++)

        a[i]=0;

    cout<<"文章中出现频率最高的十个单词如下:<<endl;

    for(i=0;i<10;i++)     {  

       ptr=head;

        while(ptr!=NULL)         {

            if(ptr->num>a[i])  

               a[i]=ptr->num;  

           else

                ptr=ptr->next;

        }

        ptr=head;  

       while(ptr!=NULL)         {   

          if(a[i]==ptr->num)

            {  

               ptr->num=0;

                cout<<"出现频率:cout<<a[i]<<endl;

                puts(ptr->name);  

               break;     

        }  

           else  

               ptr=ptr->next;        

}    

}

}

void main() {   

  struct word *head; 

   head->next=NULL;  

   readfile(head);  

   sort(head);

}

文章单词出现频率,布布扣,bubuko.com

文章单词出现频率

原文:http://www.cnblogs.com/guaihaizi/p/3578937.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!