C语言 统计数字的出现频率,并将出现频率最高的前100个数字按照降序排列后写入到文件中

完整的C语言代码,用于从名为spn.txt的文本文件中读取数字,统计在16到500范围内数字的出现频率,并将出现频率最高的前100个数字按照降序排列后写入到spnexport.txt文件中。我们需要编写一个C程序来读取文件、解析数字、统计次数、排序并写入结果。

环境:Linux Ubuntu,gcc编译器

cpp 复制代码
#include <stdio.h>  

#include <stdlib.h>  

#include <string.h>  

  

#define MAX_NUMBERS 500 - 16 + 1  

#define TOP_COUNT 100  

#define INPUT_FILE "spn.txt"  

#define OUTPUT_FILE "spnexport.txt"  

  

typedef struct {  

    int number;  

    int count;  

} NumberCount;  

  

// 比较函数,用于qsort降序排序  

int compare(const void *a, const void *b) {  

    NumberCount *numA = (NumberCount *)a;  

    NumberCount *numB = (NumberCount *)b;  

    return numB->count - numA->count; // 降序  

}  

  

int main() {  

    FILE *inputFile = fopen(INPUT_FILE, "r");  

    if (!inputFile) {  

        perror("Error opening input file");  

        return 1;  

    }  

  

    FILE *outputFile = fopen(OUTPUT_FILE, "w");  

    if (!outputFile) {  

        perror("Error opening output file");  

        fclose(inputFile);  

        return 1;  

    }  

  

    NumberCount counts[MAX_NUMBERS] = {0};  

    char line[1024];  

    int number;  

  

    // 读取文件并统计数字频率  

    while (fgets(line, sizeof(line), inputFile)) {  

        char *token = strtok(line, " \n\t\r\f\v");  

        while (token != NULL) {  

            number = atoi(token);  

            if (number >= 16 && number <= 500) {  

                counts[number - 16].number = number;  

                counts[number - 16].count++;  

            }  

            token = strtok(NULL, " \n\t\r\f\v");  

        }  

    }  

    fclose(inputFile);  

  

    // 找出出现频率最高的前TOP_COUNT个数字  

    NumberCount topCounts[TOP_COUNT];  

    int topIndex = 0;  

    for (int i = 0; i < MAX_NUMBERS; ++i) {  

        if (counts[i].count > 0) {  

            if (topIndex < TOP_COUNT) {  

                topCounts[topIndex++] = counts[i];  

            } else {  

                // 如果当前数字的频率大于topCounts中最小频率的数字,则替换之  

                int minIndex = 0;  

                for (int j = 1; j < topIndex; ++j) {  

                    if (topCounts[j].count < topCounts[minIndex].count) {  

                        minIndex = j;  

                    }  

                }  

                if (counts[i].count > topCounts[minIndex].count) {  

                    topCounts[minIndex] = counts[i];  

                }  

            }  

        }  

    }  

  

    // 对topCounts数组进行降序排序  

    qsort(topCounts, topIndex, sizeof(NumberCount), compare);  

  

    // 将结果写入输出文件  

    for (int i = 0; i < topIndex; ++i) {  

        fprintf(outputFile, "%d %d\n", topCounts[i].number, topCounts[i].count);  

    }  

  

    fclose(outputFile);  

    printf("Top %d numbers exported to %s\n", topIndex, OUTPUT_FILE);  

    return 0;  

}

运行结果

cpp 复制代码
100 236
20 187
25 152
22 150
110 110
190 109
105 105
92 101
102 95
183 94
16 92
175 90
174 77
108 76
158 76
91 75
18 74
17 67
19 65
94 59
168 55
51 51
111 50
173 47
106 47
98 46
171 45
97 44
172 43
157 42
109 42
52 40
21 38
101 37
70 31
24 30
23 26
29 26
50 25
189 22
60 19
485 18
166 18
107 18
232 16
30 15
81 15
96 15
61 14
191 13
176 12
28 11
31 11
441 11
103 11
48 10
69 10
99 10
112 10
27 9
42 9
46 9
47 9
170 9
86 9
32 8
34 8
177 8
167 8
120 8
132 8
95 8
39 7
44 7
62 7
156 7
450 6
37 6
40 6
53 6
54 6
159 6
26 5
33 5
59 5
127 5
76 5
79 5
84 5
442 5
45 4
49 4
55 4
57 4
68 4
83 4
85 4
444 4
89 4
161 4
相关推荐
USER_A00123 分钟前
【C语言】第五期——函数
c语言
计算机小白一个5 小时前
蓝桥杯 Java B 组之设计 LRU 缓存
java·算法·蓝桥杯
万事可爱^6 小时前
HDBSCAN:密度自适应的层次聚类算法解析与实践
算法·机器学习·数据挖掘·聚类·hdbscan
李白同学6 小时前
【C语言】结构体内存对齐问题
c语言·开发语言
楼台的春风7 小时前
【MCU驱动开发概述】
c语言·驱动开发·单片机·嵌入式硬件·mcu·自动驾驶·嵌入式
大数据追光猿8 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Dream it possible!8 小时前
LeetCode 热题 100_在排序数组中查找元素的第一个和最后一个位置(65_34_中等_C++)(二分查找)(一次二分查找+挨个搜索;两次二分查找)
c++·算法·leetcode
夏末秋也凉8 小时前
力扣-回溯-46 全排列
数据结构·算法·leetcode
南宫生8 小时前
力扣每日一题【算法学习day.132】
java·学习·算法·leetcode
柠石榴8 小时前
【练习】【回溯No.1】力扣 77. 组合
c++·算法·leetcode·回溯