1231564

  1 #include<iostream>
  2 #include<algorithm>
  3 #include<cstdio>
  4 #include<cstdlib>
  5 #include<cmath>
  6 #include<cstring>
  7 #include<queue>
  8 #include<vector>
  9 #include<map>
 10 using namespace std;
 11 #define llg long long
 12 #define maxn 150010
 13 #define yyj(a) freopen(a".txt","r",stdin);
 14 
 15 inline llg getint()
 16 {
 17     llg w=0,q=0; char c=getchar();
 18     while((c<'0' || c>'9') && c!='-') c=getchar();
 19     if (c=='-')  q=1, c=getchar(); while (c>='0' && c<='9') w=w*10+c-'0', c=getchar();
 20     return q ? -w : w;
 21 }
 22 
 23 llg tot,dif,siz;
 24 string s,S;
 25 char c;
 26 map<string,llg>ma ;
 27 vector<string>a;
 28 
 29 void qsort()
 30 {
 31     for (llg i=0;i<siz;i++)
 32         for (llg j=i+1;j<siz;j++)
 33             if (a[i].compare(a[j])>0)
 34                 swap(a[i],a[j]);
 35 }
 36 
 37 void work1()
 38 {
 39     for (llg i=0;i<siz;i++)
 40     {
 41         cout<<i+1<<".";
 42         for (llg j=1;j<=20-a[i].size();j++) cout<<" ";
 43         cout<<a[i];
 44         for (llg j=1;j<=20;j++) cout<<" ";
 45         cout<<"    It appeared "<<ma[a[i]]<<" times in the article,the frequency is ";
 46         double V=ma[a[i]];
 47         V/=tot;
 48         printf("%.8lf\n",V);
 49     }
 50 }
 51 
 52 void work()
 53 {
 54     cout<<"请输入您需要查询的单词:";
 55     string w;
 56     cin>>w;
 57     cout<<endl;
 58     cout<<"----------------------------------------------------------"<<endl;
 59     cout<<"    It appeared "<<ma[w]<<" times in the article,the frequency is ";
 60     double V=ma[w];
 61     V/=tot;
 62     printf("%.8lf\n",V);
 63     cout<<"----------------------------------------------------------"<<endl;
 64 }
 65 
 66 int main()
 67 {
 68 //    cout<<"请将所需要分析的文本保存到work.txt文件中"<<endl;
 69     cout<<"请输入所需要分析的文本,并以$符号结束"<<endl;
 70     system("pause");
 71 //    yyj("work");
 72     ma.clear();
 73     while((c=cin.get())!='$')
 74     {
 75         S=S+c;
 76     }
 77     for (llg i=0;i<S.size();i++)
 78     {
 79         if (S[i]==' ' || S[i]=='!' || S[i]=='.' || S[i]==',' || S[i]=='?' || S[i]=='\n' || S[i]=='\'' || S[i]=='"' || S[i]==':')
 80         { 
 81             if (S=="") continue;
 82             if (ma[s]==0) 
 83             {
 84                 dif++;
 85                 a.push_back(s);
 86             }
 87             ma[s]++;
 88             tot++;
 89             s="";
 90         }
 91         else
 92         {
 93             s+=S[i];
 94         }
 95     }
 96     siz=a.size();
 97     qsort();
 98     cout<<"There are "<<dif<<" different words"<<endl;
 99 //    cout<<"Let's Sort them in dictionary order and output as follows"<<endl<<endl<<endl<<endl;     
100     llg T;
101     while (1)
102     {
103         cout<<endl<<endl;
104         cout<<"请输入一个数字表示您要进行的操作"<<endl;
105         cout<<"1表示查询某个单词的出现频率"<<endl;
106         cout<<"2表示输出按字典序所有单词的出现频率"<<endl;
107         cout<<"3表示结束程序"<<endl;
108         T=getint();
109         if (T==3) break;
110         if (T==2) work1();
111         if (T==1) work();
112     }
113 
114     fclose(stdin);
115     return 0;
116 } 

 

 

  1 #include<iostream>
  2 #include<algorithm>
  3 #include<cstdio>
  4 #include<cstdlib>
  5 #include<cmath>
  6 #include<cstring>
  7 #include<queue>
  8 #include<vector>
  9 #include<map>
 10 using namespace std;
 11 #define llg long long
 12 #define maxn 150010
 13 #define yyj(a) freopen(a".txt","r",stdin),freopen("out.txt","w",stdout);
 14 
 15 inline llg getint()
 16 {
 17     llg w=0,q=0; char c=getchar();
 18     while((c<'0' || c>'9') && c!='-') c=getchar();
 19     if (c=='-')  q=1, c=getchar(); while (c>='0' && c<='9') w=w*10+c-'0', c=getchar();
 20     return q ? -w : w;
 21 }
 22 
 23 llg tot,dif,siz;
 24 string s,S;
 25 char c;
 26 map<string,llg>ma ;
 27 vector<string>a;
 28 
 29 void qsort()
 30 {
 31     for (llg i=0;i<siz;i++)
 32         for (llg j=i+1;j<siz;j++)
 33             if (a[i].compare(a[j])>0)
 34                 swap(a[i],a[j]);
 35 }
 36 
 37 void work1()
 38 {
 39     for (llg i=0;i<siz;i++)
 40     {
 41         cout<<i+1<<".";
 42         for (llg j=1;j<=20-a[i].size();j++) cout<<" ";
 43         cout<<a[i];
 44         for (llg j=1;j<=20;j++) cout<<" ";
 45         cout<<"    It appeared "<<ma[a[i]]<<" times in the article,the frequency is ";
 46         double V=ma[a[i]];
 47         V/=tot;
 48         printf("%.8lf\n",V);
 49     }
 50 }
 51 
 52 void work()
 53 {
 54     cout<<"请输入您需要查询的单词:";
 55     string w;
 56     cin>>w;
 57     cout<<endl;
 58     cout<<"----------------------------------------------------------"<<endl;
 59     cout<<"    It appeared "<<ma[w]<<" times in the article,the frequency is ";
 60     double V=ma[w];
 61     V/=tot;
 62     printf("%.8lf\n",V);
 63     cout<<"----------------------------------------------------------"<<endl;
 64 }
 65 
 66 int main()
 67 {
 68     cout<<"请将所需要分析的文本保存到work.txt文件中"<<endl;
 69     system("pause");
 70     cout<<"文件分析已经完成,请在out.txt文件中查看"<<endl;
 71     system("pause");
 72     yyj("work");
 73     ma.clear();
 74     while((c=cin.get())!=EOF)
 75     {
 76         S=S+c;
 77     }
 78     for (llg i=0;i<S.size();i++)
 79     {
 80         if (S[i]==' ' || S[i]=='!' || S[i]=='.' || S[i]==',' || S[i]=='?' || S[i]=='\n' || S[i]=='\'' || S[i]=='"' || S[i]==':')
 81         { 
 82             if (S=="") continue;
 83             if (ma[s]==0) 
 84             {
 85                 dif++;
 86                 a.push_back(s);
 87             }
 88             ma[s]++;
 89             tot++;
 90             s="";
 91         }
 92         else
 93         {
 94             s+=S[i];
 95         }
 96     }
 97     siz=a.size();
 98     qsort();
 99     cout<<"There are "<<dif<<" different words"<<endl;
100     work1();
101     return 0;
102 } 

 

posted @ 2019-01-06 10:32  №〓→龙光←  阅读(229)  评论(0编辑  收藏  举报