【问题标题】:How can I count the frequency of each digit in a file?如何计算文件中每个数字的频率?
【发布时间】:2017-11-04 18:37:42
【问题描述】:

对于我的 C 类,我必须编写一个程序来计算文件中每个数字 (0-9) 的频率。我已经用 Java 编写了代码,但无法将其翻译成 C。任何帮助将不胜感激。我将在下面添加到目前为止的内容。文本文件的一部分被添加到代码的末尾以显示它的样子,一个随机的符号和数字字符串。

 #include<stdio.h>
    #define m 100
    #define n 60

    void main() {
    char file_name[m]; FILE *input_file;
    char symb, symb0, symb1, symb2, symb3, symb4, symb5, symb6;
    char symb7, symb8, symb9;
    int i;

    printf("Enter name of the input file: "); scanf("%s", file_name);
    input_file = fopen(file_name, "r");

    while (input_file == NULL) {
    printf("Error: There is no file \"%s\"\n", file_name);
    printf("Enter file name (or \".\" to exit): "); scanf("%s", file_name);
    if (strcmp(file_name, ".") == 0) return;
    input_file = fopen(file_name, "r");}

    //read each char of the file till the end
    while ((symb=getc(input_file))!=EOF){
             // if the char is between '0'-'9' print it
            if(symb >= '0' && symb <='9'){
                //printf("%c", symb);
                switch(symb){
                    case '0' :
                    symb0++;
                    break;
                    case '1' :
                    symb1++;
                    break;
                    case '2' :
                    symb2++;
                    break;
                    case '3' :
                    symb3++;
                    break;
                    case '4' :
                    symb4++;
                    break;
                    case '5' :
                    symb5++;
                    break;
                    case '6' :
                    symb6++;
                    break;
                    case '7' :
                    symb7++;
                    break;
                    case '8' :
                    symb8++;
                    break;
                    case '9' :
                    symb9++;
                    break;
                }
                            printf("%c", symb3);
                }
    }

    printf("\n-= Count the Thisles in =-");

    fclose(input_file);
    }
    //...1..1.'`.1.........2..2..2...../\......./%%%%\/%%\.3...4..
    //......'............../\........./%%\../\./%%%%%%/\%%\33..4..
    //...'.../\.........../%%\.../\.../%%\./%%\%/\%/\/%%\%/\......
    //.'..../%%\./\......./%%\../%%\./%%%%\/%%\/%%\%%\%%%/%%\.55..

【问题讨论】:

  • 一个由十个元素组成的数组怎么样,每个数字一个。将每个元素初始化为零,找到数字时增加。请记住,例如'2' - '0' == 2.
  • 1) 你知道symb1symb9 的初始值是什么吗?它们是未定义的 2)您正在增加 char 变量,这可能不是您的想法
  • 一点也不差...差不多。将symb 声明为int 以便能够检查EOF。其他符号也必须是整数并初始化symb0= 0symb9= 0。那么应该可以工作了。
  • symb3 是一个 int(或至少用作 int),使用:printf("%d", symb3);
  • 次要:void main() ==> int main(void)

标签: c arrays file frequency


【解决方案1】:

收集所有的cmets:

#include<stdio.h>
#define m 100
#define n 60

int main(void) {
    char file_name[m]; FILE *input_file;
    int symb, symbcount[10]={0};
    int i;

    printf("Enter name of the input file: "); scanf("%s", file_name);
    input_file = fopen(file_name, "r");

     while (input_file == NULL) {
        printf("Error: There is no file \"%s\"\n", file_name);
        printf("Enter file name (or \".\" to exit): "); scanf("%s", file_name);
        if (strcmp(file_name, ".") == 0) return 1;
        input_file = fopen(file_name, "r");
    }

    while ((symb=getc(input_file))!=EOF){
        if (symb >= '0' && symb <='9')
            symbcount[symb-'0']++;
    }
    fclose(input_file);

    for (i=0; i<10; i++)
        printf("%d: %d\n", i, symbcount[i]);

    return 0;
}

【讨论】:

  • 谢谢你们!我认为我拥有完成代码所需的一切,这比我想象的要容易得多。我唯一的问题是:为什么使用 int main(void) 而不是 void main()
  • if (strcmp(file_name, ".") == 0) return; --> if (strcmp(file_name, ".") == 0) return 0;
  • @SheaShea 以便您的程序可以返回退出代码。 stackoverflow.com/questions/204476/…。例如,在我上面的评论中,如果您想返回“0=成功”和“1=用户无法提供有效名称”的代码,那么您可以在我上面的评论中return 1
  • @SheaShea C 标准是这么说的。 5.1.2.2.1 程序启动 1 程序启动时调用的函数名为main。实现没有声明这个函数的原型。它应该用int 的返回类型定义并且没有参数:int main(void) { /* ... */ } 或有两个参数(这里称为argcargv,尽管可以使用任何名称,因为它们是本地的声明它们的函数):int main(int argc, char *argv[]) { /* ... */ } 或等效项;或以其他一些实现定义的方式。最后是说嵌入式应用没有回报。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 2012-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多