4.2结对项目

[必做 2] 读取小文本文件A_Tale_of_Two_Cities.txt 或者 大文本文件Gone_with_the_wind.txt,统计某一指定单词在该文本文件中出现的频率。

结对对象:袁晨晖  博客地址:http://www.cnblogs.com/ychhhh/

                         github地址:https://github.com/yuanchenhui/zuoye

                         双方贡献 1:1

 

  1 #include<iostream>
  2 #include<fstream>
  3 #include<cstring>
  4 #include<string>
  5 using namespace std;
  6 
  7 struct Word{
  8     int Count;
  9     char *p;
 10     void exchange(Word&word)
 11     {
 12         string  tStr=word.Str ;
 13         int tCount=word.Count ;
 14         word.Str =Str;
 15         word.Count =Count;
 16         Str=tStr;
 17         Count=tCount;
 18     }
 19 };
 20 Words test[100];
 21 
 22 void lwr(char x[])
 23 {
 24     int k=0;
 25     while(x[k]!='\0')
 26     {
 27         if(x[k]>='A'&&x[k]<='Z')
 28             x[k]=x[k]+32;
 29         k++;
 30     }
 31 }
 32 int identify(char a[])
 33 {
 34     int m=(strlen(a)>=4)?1:0;
 35     int n=(a[0]>='a'&&a[0]<='z')?1:0;
 36     if(!m||!n)
 37         return 0;
 38     else
 39         while(a)
 40         {
 41             for(int i=1;i++)
 42             {
 43                 if(!(a[i]>='a'&&a[i]<='z')||!(a[i]>='0'&&a[i]<='9'))
 44                     return 0;
 45                 else
 46                     return 1;
 47             }
 48         }
 49 }
 50 
 51 void SortWordDown(Word*words,int size)
 52 {
 53     for(int i=0;i<size;i++)
 54     {
 55         for(int j=0;j<size-1;j++)
 56         {
 57             if(words[j].Count<words[j+1].Count )
 58             {
 59                 words[j].exchange (words[j+1]);
 60             }
 61         }
 62     }
 63 }
 64 
 65 int counting(char b[],int num)
 66 {
 67     for(int j=0;j<num;j++)
 68     {
 69         if(!strcmp(b,test[j].p ))
 70             test[j].Count++;
 71         else
 72             return 0;
 73     }
 74 }
 75 
 76 int main(void)
 77 {
 78     char c[100];
 79     char *ptr;
 80     ifstream file("E://A_Tale_of_Two_Cities.txt")
 81         if(!file)
 82         {
 83             cout<<"不能打开文件";
 84         }
 85         while(!file.eof ())
 86         {
 87             file.getline (c,500);
 88         }
 89         file.close ();
 90         int j=0;
 91         while(c[j]!='/0'&&c[j+1]!='/0')
 92         {
 93             if(c[j]>='A'&&c[j]<='Z')
 94             {
 95                 c[j]=c[j]-'A'+'a';
 96                 j++;
 97             }
 98         }
 99         cout<<c;
100         char *sep="";
101         int i=0;
102         ptr=strtok(c,"");
103         while(ptr!=NULL)
104         {
105             if(isword(p)!=false)
106             {
107                 if(judge(p,n)!=false)
108                 {
109                     w[n].s=*p;
110                     n++;
111                 }
112             }
113             ptr=strtok(NULL,"");
114         }
115         int t=0;
116         cout<<"输入统计出现频数的指定单词:\n";
117         char d[500];
118         cin>>d;
119         int t=0;
120         while(!strcmp(test[n].p,d))
121         {
122             t++;
123         }
124         cout<<test[n].p<<":"<<test[n].count<<'\n';
125         return 0;
126 }

运行结果

 

 

基于上次的代码,将输出到result1改成寻找指定单词并输出。修改如下

 1 }
 2         int t=0;
 3         cout<<"输入统计出现频数的指定单词:\n";
 4         char d[500];
 5         cin>>d;
 6         int t=0;
 7         while(!strcmp(test[n].p,d))
 8         {
 9             t++;
10         }
11         cout<<test[n].p<<":"<<test[n].count<<'\n';
12         return 0;
13 }

 

心得:互帮互助,跟高效解决问题。帮扶了我这种不太会的。

posted on 2016-03-29 22:16  蒋易  阅读(169)  评论(0编辑  收藏  举报