C语言——统计文件中出现最多的前5个字母

统计附件文件"news.txt"中出现的英文字母的次数,并按次数从高到低的排序打印出现次数最多的5个字母。
统一转化为小写字母进行统计。
(提交包括源代码和可执行文件)

文件news.txt是

The dominant sequence transduction models are based on complex recurrent

or convolutional neural networks in an encoder

decoder configuration.
The best performing models also connect the encoder and decoder through
an attention mechanism. We propose a new
simple network architecture,
the Transformer, based solely on attention mechanisms, dispensing with
recurrence and convolutions entirely. Experiments on two machine
translation tasks show these models to be superior in quality while
being more parallelizab
le and requiring significantly less time to train.

Our model achieves 28.4 BLEU on the WMT 2014 English

to

German
translation task, improving over the existing best results, including

ensembles by over 2 BLEU. On the WMT 2014 English

to

French translation

task, our model establishes a new single

model state

of

the

art BLEU
score of 41.8 after training for 3.5 days on eight GPUs, a small fraction
of the training costs of the best models from the literature. We show
that the Transformer generalizes well to ot
her tasks by applying it
successfully to English constituency parsing both with large and limited
training data.

点击我回答左上角的采纳,采纳本回答和 https://ask.csdn.net/questions/763805 后,可以继续回答你其它问题。

图片说明

// Q763806.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#define _CRT_SECURE_NO_WARNINGS

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

int arr[26];
int order[26];

int cmp(const void* a, const void* b)
{
    return arr[*((int*)b)] - arr[*((int*)a)];
}

void stat(char* line)
{
    while (*line != '\0')
    {
        if (*line >= 'A' && *line <= 'Z')
            arr[*line - 'A']++;
        if (*line >= 'a' && *line <= 'z')
            arr[*line - 'a']++;
        line++;
    }
}

int main()
{
    char buf[1000];
    FILE* fp;
    memset(arr, 0, sizeof(int) * 26);
    for (int i = 0; i < 26; i++) order[i] = i;
    if ((fp = fopen("..\\news.txt", "r")) != NULL)
    {
        while (fgets(buf, 1000, fp) != NULL)
            stat(buf);
    }
    qsort(order, 26, sizeof(int), cmp);
    for (int i = 0; i < 5; i++)
        printf("top %d: %c %d次\n", i + 1, order[i] + 'a', arr[order[i]]);
    return 0;
}

top 1: e 116次
top 2: t 88次
top 3: n 84次
top 4: o 73次
top 5: s 67次

完整代码和可执行文件在你采纳并留下email 后发给你

其他人如果也需要,下载:https://download.csdn.net/download/caozhy/11217493

https://download.csdn.net/download/sinat_15738233/7396589

代码写好了,你看看吧:

#include<stdio.h>
#include<math.h>
#include<string.h>
struct words
{
    int n;
    char c[30];
}w[10000];
int main()
{
    FILE *fp;
    char b[30], ch;
    int i = 0, m = 1, j = 0, k = 0, t = 0, flag = 0;
    fp = fopen("news.txt", "r");
    while ((ch = fgetc(fp)) != EOF)
    {
        if ('A' <= ch&&ch <= 'Z') ch = ch + 32;//转小写
        if ('a' <= ch && ch <= 'z')                //字母
        {
            b[i] = ch; i++; flag = 1;//开始写入b
        }
        else
        {
            if (ch == '-' && (ch = fgetc(fp)) == '\n')//非字母 非空格
            {
                flag = 0;
            }
            else
            {
                if (flag == 1)                       //空格
                {
                    b[i] = '\0'; i = 0; flag = 0; m = 0;//写入b完成+'\0'
                    for (j = 0; j < k; j++)
                    {
                        if (strcmp(b, w[j].c) == 0)
                        {
                            m = 1;//标志 前有单词相同
                            break;
                        }
                    }
                    if (m) w[j].n++;//数量加1
                    else  //存入结构体
                    {
                        w[k].n = 1; strcpy(w[k].c, b); k++;
                    }
                }
            }
            /*if ('A' <= ch && ch <= 'Z') ch += 32;
            if ('a' <= ch && ch <= 'z')
            {
            b[i] = ch; i++; flag = 1;
            }*/
        }
    }
    /*输入 前5 */
    for (i = 0; i < k&&i < 5; i++)
    {
        t = 0;
        while (w[t].n == 0) t++;
        for (j = 1; j<k; j++)
        {
            if (w[j].n>w[t].n) t = j;//选 大的
            else
            if (w[j].n == w[t].n)
            {
                if (strcmp(w[j].c, w[t].c) < 0)
                    t = j;
            }
        }
        printf("%s %d\n", w[t].c, w[t].n);
        w[t].n = 0;//t 已输出,令n=0;
    }

    return 0;
}