簡體   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