词频统计

本文介绍了一个使用C++实现的单词频率统计程序。程序通过结构体存储单词及其出现频率,读取用户输入的单词,直到遇到特殊标记停止。然后,程序对单词进行排序并输出每个单词及其频率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

#include <iostream>
#include<string>
#include<cstring>
using namespace std;
struct WordList
{
    char word[20];
    int freq;
};
int main()
{
    WordList list[1000];
    int N = 0;
    int i,j,k;
    char tmp[20];
    cin>>tmp;
    while(strcmp(tmp,"xyz") != 0)
    {
        for(i = 0 ; i < N ; i++)
        {
            if(strcmp(list[i].word , tmp) == 0)
            {
                list[i].freq++;
                break;
            }
        }
        if(i >= N)
        {
            strcpy(list[i].word , tmp);
            list[i].freq = 1;
            N++;
        }
        cin>>tmp;
    }
    for(i = 0 ; i < N - 1 ; i++)
    {
        k = i;
        for(i = i + 1 ; j < N ; j++)
        {
            if(strcmp(list[j].word , list[k].word) < 0)
            {
                k = j;
            }
        }
        if(k != j)
        {
            WordList tmp;
            tmp = list[i];
            list[i] = list[k];
            list[k] = tmp;
        }
    }
    cout<<"统计结果如下:"<<endl;
    for(i = 0 ; i < N ; i++)
    {
        cout<<list[i].word<<"\t"<<list[i].freq<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值