读取英文文档,找出出现次数前十的单词

#include<iostream> #include<fstream> #include<string> #define MAX 32 using namespace std; struct word { char name[MAX]; int num; struct word *next; }; void read(struct word*&head) {     ifstream infile("test.txt");     infile>>noskipws;     if(!infile){   cout<<"can not open!"<<endl;   return;  }     char a,temp[MAX];     struct word *p;     while(infile)     {         int i=0;            infile.get(a);         temp[0]=' ';         while((a>='a'&&a<='z')||(a>='A'&&a<='Z')||temp[0]==' ')         {             if(a>='a'&&a<='z'||a>='A'&&a<='Z')             {                 temp[i]=a;                 i++;             }             infile.get(a);             if(infile.eof())     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;         }     }     infile.close(); } void out(struct word*head,int n) {     struct word *p;     p=head->next;     for(int i=0;i<n;i++)     {         cout<<"单词:"<<p->name<<"   出现的次数:"<<p->num<<endl;         p=p->next;     } } void output(struct word*&head) { ofstream output("out.txt"); struct word *p; p=head->next; while(p)     {         output<<"单词"<<p->name<<"   出现的次数"<<p->num<<endl;         p=p->next;     }

}

void sort(struct word*&head) {     struct word *p,*q,*s,*l;     q=head;     p=head->next;     s=p->next;     p->next=NULL;     while(s)     {         while(p&&p->num>s->num)         {             q=p;             p=p->next;         }         q->next=s;         l=s->next;         s->next=p;         s=l;         p=head->next;         q=head;     } } void main() { struct word *head; head=new word; head->next=NULL; read(head); sort(head); out(head,10); output(head); }

文档:

   Many of the women who have joined the American workforce since the 1970s have the computer revolution to thank, according to a new study.
  A researcher, Dr Weinberg at Ohio State University, estimated that increased computer use in the workplace explains about 55 percent of the increase in the demand for women workers since the mid-1970s. Many of the new jobs are in blue-collar industries.
  “Computers have opened up a lot of job opportunities for women that weren’t available before,” said Bruce Weinberg, author of the study and assistant professor of economics at Ohio State University.

posted on 2014-03-03 12:56  guowei123  阅读(274)  评论(2编辑  收藏  举报