c程序 输入一个英文句子,统计各单词出现的次数,并按要求排序。要求如下,谢谢。

对一个英文句子统计每个单词出现的次数。
提示用户选择(1:按出现次数降序排列, 2:按单词字典顺序升序排列),显示排序结果,单词统一转换成为小写,输出格式参考例子。
英文句子中可能包含的标点符号有:,.;:
要求:
1 排除掉单词后面的逗号、分号、冒号
2 统计次数时,不区分大小写。
3 英文句子直接写在程序当中。

例如,英文句子为
The president told the audit authorities to keep their thoughts and actions in line with the CPC Central Committee, resolutely safeguard the centralized and unified leadership of the committee and implement the requirements of the committee.

则程序输出为:
按出现次数降序排列:
the,7
and,3
committee,3
of,2
actions,1
audit,1
authorities,1
central,1
centralized,1
cpc,1
implement,1
in,1
keep,1
leadership,1
line,1
president,1
requirements,1
resolutely,1
safeguard,1
their,1
thoughts,1
to,1
told,1
unified,1
with,1

按单词字典顺序升序排列:
actions,1
and,3
audit,1
authorities,1
central,1
centralized,1
committee,3
cpc,1
implement,1
in,1
keep,1
leadership,1
line,1
of,2
president,1
requirements,1
resolutely,1
safeguard,1
the,7
their,1
thoughts,1
to,1
told,1
unified,1
with,1

试编写代码如下:

#include<stdio.h>
#include<string.h>
#include<ctype.h>

struct word
{  
   char str[30];  //单词  
   int num;       //单词出现的次数
}words[500];

int sum;

void chuli(char s[])
{  
    int i,j;  
    int flag=0; 

    for(i=0;i<=sum;i++)  
    {   
       if(strcmpi(words[i].str,s)==0)   
       {             
          words[i].num++;    
          flag=1;      
       }     
    }  

    if(flag==0)  
    {   
       strcpy(words[sum].str,s);

       words[sum].num++;   
       sum++;  
    }       
}

void paixu1()
{    
   int i,j;    
   struct word a;  

   for(i=0;i<sum;i++)  
   {   
     for(j=i+1;j<sum;j++)      
        if(strcmp(words[i].str,words[j].str) > 0)   
        {     
           a=words[j];          
           words[j]=words[i];
           words[i]=a; 
        }
  }
}

void paixu2()
{    
    int i,j;    
    struct word a;  

    for(i=0; i<sum; ++i)
{
        for(j=sum-1; j>i; --j)
{
        if(words[j].num>words[j-1].num)    
        {     
           a=words[j];          
           words[j]=words[j-1];
           words[j-1]=a; 
        }
}

}

int main()
{  
   char s[30];  
   int imax,istart,iend,i,j,flag=0;
   
   char str[] = "The president told the audit authorities to keep their thoughts and actions in line with the CPC Central Committee, resolutely safeguard the centralized and unified leadership of the committee and implement the requirements of the committee.";
   
   char delims[] = " ";  
   char *sword = NULL;

   sum=0;

   for(i=0;i<500;i++)
      words[i].num=0;

   sword = strtok(str, delims);  
   while( sword != NULL ) {
        sscanf(sword,"%[a-zA-Z]",s);
        strlwr(s);
        chuli(s);
   
sword = strtok( NULL, delims );  
   }  

    printf("该文章共有:%d个单词\n",sum);

    paixu1();
    printf("\n按单词字典顺序升序排列:\n");
    for(i=0;i<sum;i++)
     printf("%s, %d\n",words[i].str,words[i].num);
      
    paixu2();
    printf("\n按出现次数降序排列:\n");
    for(i=0;i<sum;i++)
     printf("%s, %d\n",words[i].str,words[i].num);

    return 0;
}

实际运行截图:

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-05-24
#include <iostream>
#include <map>
#include <algorithm>
#include <string>
#include <iterator>
#include <vector>
#define N 50
using namespace std;

int cmp(const pair<string, int>& x, const pair<string, int>& y) 
//vector里实现单词出现次数排序
{  
    return x.second > y.second;  
}  
   
void sortMapByValue(map<string, int>& tMap,vector<pair<string, int> >& tVector)
//将map元素转存为vector  
{  
    for (map<string, int>::iterator curr = tMap.begin(); curr != tMap.end(); curr++)   
        tVector.push_back(make_pair(curr->first, curr->second));    
   
    sort(tVector.begin(), tVector.end(), cmp); //对vector对序,实现按单词出现次数排序
}  

int main()
{
    char ch;
    string a;
    string la;
    map <string,int > v;
    map <string,int>::iterator it;

    while(getline(cin,la)){//按行读入,ctrl+z结束循环
        int i=0;
        //if(la=="000")break;//调试用
        while(i!=la.size()){//对读入的字符行逐字符比对
            ch=la[i];
            if(ch>='A' && ch<='Z')//大写字母转换成小写,再构成单词
                a+=char(ch-'A'+'a');
            else if(ch>='a' && ch<='z')//小写字符照抄到单词中
                a+=ch;
            else {//遇到空格或者标点符号
                if(a.size()>0){//如果前面构成的单词非空
                //cout<<a<<endl;//调试用
                    it=v.find(a);//map容器还没有此单词
                    if(it==v.end())
                        v.insert(pair<string,int>(a,1));//插入map容器中
                    else
                        v[a]++;//map容器中已有以此单词为键的项,该键计数器value值加1
                    a.clear();//已加入容器,构成单词a清空,准备下一次构造
                }
            }
            ++i;
     }
    }
    cin.clear();//清除输入流,不然,结束上面大while循环时输入的ctrl+z
    //会影响到整个程序,导致程序结束退出,无法再进行下面输入操作
    cout<<"1:按出现次数降序排列, 2:按单词字典顺序升序排列"<<endl;
    int k;
    cin>>k;//选择输出方式
    if(k==2){//map中默认已自动按key(即单词)排序,直接输出即符合字典顺序
    for(it=v.begin();it!=v.end();++it)
        cout<<(*it).first<<","<<(*it).second<<endl;
    }
    if(k==1){//对map中的value排序:因map特性,不支持sort()算法直接排序
        //所以,通过将map内容转存到vector容器中进行排序
    vector <pair<string,int > > tVector;  
    sortMapByValue(v,tVector);  
    for(int i=0;i<tVector.size();i++)  
        cout<<tVector[i].first<<","<<tVector[i].second<<endl;  
    }
    
    return 0;
}

追问

大哥,我要的是c,不是c++。。。

追答

要纯C语言版,私聊。

本回答被网友采纳
第2个回答  2018-05-25
'把下面代码复制到窗体,添加1个list 和一个command按钮运行就可以了,和一个文本框,数据输入在文本框里

Private Type English
word As String
count As Long
End Type

'判断是不是英文
Private Function En(ByVal s As String) As Boolean
If s = "" Then En = False: Exit Function
s = LCase(s)
For i = 1 To Len(s)
a = Asc(Mid(s, i, 1))
If a < 97 Or a > 122 Then En = False: Exit Function
Next
En = True
End Function

'统计数组内各个英文出现次数
Private Function TongJi(ByVal T_txt As String) As English()
s = Split(T_txt, " ")
n = 0
Dim English() As English
ReDim English(0)
For i = 0 To UBound(s)
If s(i) <> "" And En(s(i)) = True Then
temp = False
For j = 0 To UBound(English)
If English(j).word = "" Then
English(j).word = s(i): English(j).count = 1: Exit For
ElseIf English(j).word = s(i) Then
temp = True: English(j).count = English(j).count + 1: Exit For
End If
Next
If temp = False Then
ReDim Preserve English(n): English(n).word = s(i): English(n).count = 1: n = n + 1
End If
End If
Next
TongJi = English
End Function

Private Sub Command1_Click()
Dim e() As English
e = TongJi(Text1)
Cls
For i = 0 To UBound(e)
If e(i).count > 0 Then List1.AddItem e(i).word & " 出现过: " & e(i).count & " 次"
Next
End Sub
相似回答