查找出现频率最多的字

#include<iostream.h>
#include<string.h>
#include<fstream.h>

struct word{
    char name[30];
    int num;
    struct word *next;
};
void readfile(struct word*&head)
{
    struct word*fp;

    if((fp=fopen("in.txt","r"))==NULL)
    { 
        cout<<"无法打开此文件!<<endl;
        break;
    }
    char ch;
	char temp[30];
    struct word *p;
    while(!feof(fp))
    {
        int i=0;
        ch=fgetc(fp);
        temp[0]=' '; 
        while((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||temp[0]==' ')
        {
            if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
            {
                temp[i]=ch;
                i++;
            }
            ch=fgetc(fp);
            if(feof(fp)) 
				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;
        }
    }
}
void puts(struct word*&head)
{
	struct word *q;
	q=q->name; 
}
void sort(struct word*&head)
{    
    struct word *ptr;
    int a[10],i;
    for(i=0;i<10;i++)
        a[i]=0;
    cout<<"文章中出现频率最高的十个单词如下:<<endl;
    for(i=0;i<10;i++)
    { 
        ptr=head;
        while(ptr!=NULL)
        { 
            if(ptr->num>a[i])
                a[i]=ptr->num;
            else
                ptr=ptr->next;
        } 
        ptr=head;
        while(ptr!=NULL)
        { 
            if(a[i]==ptr->num)
            { 
                ptr->num=0;
                cout<<"出现频率:cout<<a[i]<<endl;
                puts(ptr->name);
                break;
            } 
            else 
                ptr=ptr->next;
        }
    }
}

void main()
{
    struct word *head;
    head=new word;
    head->next=NULL;
    readfile(head);
    sort(head);
}

 

转载于:https://www.cnblogs.com/fk15690320339/p/3578141.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值