• 写了挺久了,也看了好多遍!就是不知道问题在哪0 0


    大概的思想是先打开文件,然后读取所有单词,单词读取一个在设置一个数组,当出现第二次出现该单词的时候数组该位+1。最后冒泡排序输出最后10个数字对应的单词

    #include<iostream>
    #include<fstream>
    using namespace std;
    //定义单词的结构体
    struct word
    {
    char w[20];
    int num;
    struct word *next;
    };
    void sort(struct word *head)
    {
    int i;
    int a[10];
    struct word *q;
    for(i=0;i<10;i++) //初始化数组
    a[i]=0;
    //对统计后的单词频率进行排序并输出
    cout<<"频率最高的十个单词是"<<endl;
    for(i=0;i<10;i++)
    {
    q=head;
    while(q!=NULL)
    {
    if(q->num>a[i])
    a[i]=q->num;
    else
    q=q->next;
    }
    q=head;
    while(q!=NULL)
    {
    if(a[i]==q->num)
    {
    q->num=0;
    cout<<q->w;
    cout<<" "<<a[i]<<" "<<endl;
    break;
    }
    else
    q=q->next;
    }
    }
    }
    void main()
    {
    FILE *fp;
    int i;
    int a[10];
    char b;
    struct word *head=NULL;
    struct word *q;
    for(i=0;i<10;i++) //初始化数组
    a[i]=0;
    if((fp=fopen("WZ.txt","r"))==NULL)
    {
    cout<<"无法打开此文件!"<<endl;
    }
    //统计单词的出现频率
    while(!feof(fp))
    {
    char *p=new char;
    fscanf(fp,"%s",p);
    if(head==NULL)
    {
    struct word *t=new word;
    strcpy(t->w,p);
    t->num=1;
    t->next=NULL;
    head=t;
    }
    else
    {
    struct word *d=head;
    while(d!=NULL)
    {
    if(strcmp(d->w,p)==0)
    {
    int number= d->num;
    number++;
    d->num = number;
    break;
    }
    d=d->next;
    }
    if(d==NULL)
    {
    struct word *t=new word;
    strcpy(t->w, p);
    t->num=1;
    t->next=head;
    head=t;
    }
    }
    }
    sort(head);

    }

  • 相关阅读:
    API接口服务端
    phpredis扩展
    PHP之-json转数组,支持多层嵌套json
    瀑布流
    ERROR 1130: Host xxx is not allowed to connect to this MySQL server
    让IE支持CSS3 Media Query实现响应式Web设计
    Sublime Text快捷键:
    最简单的linux内存清理方法
    16: vue + crypto-js + python前后端加密解密
    16: mint-ui移动端
  • 原文地址:https://www.cnblogs.com/xiaocongjiejie/p/3602806.html
Copyright © 2020-2023  润新知