繁体   English   中英

文本文件中整数的 C 编程出现

[英]C Programming Occurrences of an Integers in an Text File

问题:编写一个程序,从名为 a.txt 的输入文件中读取 0 到 100 范围内的所有整数,并计算每个文件在文件中出现的次数。 处理完所有输入后,显示输入文件中出现次数的所有值。

注意:程序会忽略小于 0 或大于 100 的任何数字。 注意:如果文件中没有数字,则不要显示零。 提示:大小为 101 的数组就足够了。 文件中的数字起到索引的作用。

例如:假设文件:a.txt的内容如下:

99 2 99 
3 
-12 80 12 33 
3 99 100 1234 84

显示输出为:

2 has occurred: 1 times,
3 has occurred: 2 times,
12 has occurred: 1 times,
33 has occurred: 1 times,
80 has occurred: 1 times,
84 has occurred: 1 times,
99 has occurred: 3 times,
100 has occurred: 1 times

这是我现在拥有的代码:

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

int main(){
    FILE *inFile;
    int count = 1, num[101];

    inFile = fopen("a.txt", "r");
    for(int i = 1; i <= 100; i++) {
        fscanf(inFile, "%d", &num[i]);
    }

    for(int i = 1; i <= 100; i++){
        if(num[i] == i) {
            printf("%i has occurred: %d times\n", i, count);
            count++;
        }
    }

    fclose(inFile);
}

输出:2 已发生:1 次

你好,我正在尝试为我的 C 编程课做这个作业,我的 C 编程课将于周日午夜到期,但我在尝试打印数组中的所有数字时遇到了麻烦。 在我的代码中,我首先声明了 int count 以增加出现次数,如果在文本文件中发现该数字不止一次,并创建了一个大小为 101 的数组。然后,我使用了一个 for 循环来读取文本文件并存储所有将 1-100 之间的数字放入数组中。 第二个 for 循环,后跟一个 if 语句,用于比较数组中的数字。 即使这是一个测试程序,我们也应该能够对所有数据值执行此操作。 希望这是一个足够好的解释,谢谢。

你很近。

您不想将每个值读入num ,而是希望使用num数组来保持文件中看到的每个数字的计数。

int main() {
    FILE* inFile;
    int value = 0;
    int result = 0;
    int num[101] = { 0 }; // zero-init this array

    inFile = fopen("a.txt", "r");

    if (inFile == NULL) {
        printf("unable to open file\n");
        return -1;
    }

    result = fscanf(inFile, "%d", &value);
    while (result == 1) {

        printf("Just read: %d\n", value);

        if ((value >= 0) && (value <= 100)) {
            num[value] = num[value] + 1;  // num[value]++
        }
        result = fscanf(inFile, "%d", &value);
    }

    for (int i = 0; i <= 100; i++) {
        if (num[i] > 0) {
            printf("%i has occurred: %d times\n", i, num[i]);
        }
    }

    fclose(inFile);
}

除了@selbie的好回答之外,从我对您之前的问题How do I get the counter to work... 的回答中,您可以在此处应用相同的原则来填充频率数组 在这种情况下,您只需使用n作为索引而不是计数器。

例如,您的索引n和数组num声明(并初始化为全零),您只需读取文件中的所有整数并检查值n是否介于0 <= n <= 100 ,如果是,则更新值将num数组中的索引n加一,例如num[n]++; . 你可以这样做:

    int n = 0;              /* index */
    int num[NELEM] = {0};   /* array */
    ...
    while (fscanf(myFile, "%d", &n) == 1)   /* read each int */
        if (0 <= n && n <= 100)             /* if 0 <= n <= 100 */
            num[n]++;                       /* increment value at index */

然后对于您的输出,您只需处理num[0]上的特殊检查以确定是否输出该索引,然后从1-NELEM循环输出每个值的出现频率,例如

    if (num[0])     /* check if 0 found, output if so */
        printf ("num[%3d] occurred %3d times\n", 0, num[0]);

    for (int i = 1; i < NELEM; i++) /* output counts for 1-100 */
        printf ("num[%3d] occurred %3d times\n", i, num[i]);

完整的例子可能是:

#include <stdio.h>

#define NELEM 101   /* if you need a constant, #define one (or more) */

int main(int argc, char **argv) {

    int n = 0;              /* index */
    int num[NELEM] = {0};   /* array */
    /* read filename from 1st argument (stdin by default) */
    FILE *myFile = argc > 1 ? fopen (argv[1], "r") : stdin;

    if (!myFile) {  /* validate myfile is open for reading */
        perror ("fopen-myfile");
        return 1;
    }

    while (fscanf(myFile, "%d", &n) == 1)   /* read each int */
        if (0 <= n && n <= 100)             /* if 0 <= n <= 100 */
            num[n]++;                       /* increment value at index */

    if (myFile != stdin)        /* close file if not stdin */
        fclose (myFile);

    if (num[0])     /* check if 0 found, output if so */
        printf ("num[%3d] occurred %3d times\n", 0, num[0]);

    for (int i = 1; i < NELEM; i++) /* output counts for 1-100 */
        printf ("num[%3d] occurred %3d times\n", i, num[i]);
}

示例使用/输出

如果文件中有 500 个随机整数,您将获得类似于以下内容的输出:

$ ./bin/fscanffreq dat/500_rand_0-100.txt
num[  0] occurred   3 times
num[  1] occurred   8 times
num[  2] occurred   7 times
num[  3] occurred   2 times
num[  4] occurred   1 times
num[  5] occurred   4 times
num[  6] occurred   3 times
num[  7] occurred   5 times
num[  8] occurred   6 times
num[  9] occurred   4 times
num[ 10] occurred   6 times
...
num[ 95] occurred   6 times
num[ 96] occurred   4 times
num[ 97] occurred   6 times
num[ 98] occurred   2 times
num[ 99] occurred   5 times
num[100] occurred   6 times

注意:如果num[0]0 ,则不会显示)

仔细检查一下,如果您还有其他问题,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM