代码之家  ›  专栏  ›  技术社区  ›  Telefang

计算C语言中的字符、单词、行和段落

  •  1
  • Telefang  · 技术社区  · 10 年前

    我正在计算stdin中C语言的字符、单词、行和段落。

    有些东西不起作用,我不知道为什么。

    #include <stdio.h>
    
    int main(int argc, char const *argv[])
    {
        int pCount=0, parCount=0, cCount=0, lCount=0;
        double prom=0;
        char c;
        int newln_cnt=0;
        while ((c=getchar())!=EOF){ 
            cCount++;
            switch (c)
            {
                case '\n':
                    newln_cnt++;
                    lCount++;
                    if (newln_cnt == 2)
                    {
                        parCount++;
                        newln_cnt = 0;
                    }
                    break;
                case ' ':
                    pCount++;
                    break;
            }               
        }
        prom = (cCount / pCount);
        printf("Total caracteres: %d \n", cCount);
        printf("Cantidad palabras: %d \n", pCount);
        printf("Cantidad líneas: %d \n", lCount);
        printf("Cantidad párrafos: %d \n", parCount);
        printf("Promedio longitud palabra: %f \n", prom);
        return 0;
    }
    

    它有点适合角色(它显示的少了一个)。但其余的都很糟糕。

    输入:

    Oid, mortales, el grito sagrado:
    "Libertad, libertad, libertad!"
    
    Oid el ruido de rotas cadenas,
    ved en trono a la noble igualdad.
    
    Ya su trono dignisimo abrieron
    las Provincias Unidas del Sud
    
    y los libres del mundo responden:
    "Al gran pueblo argentino, salud!
    Al gran pueblo argentino, salud!"
    
    Y los libres del mundo responden:
    "Al gran pueblo argentino, salud!"
    
    Sean eternos los laureles
    que supimos conseguir,
    que supimos conseguir.
    
    Coronados de gloria vivamos...
    o juremos con gloria morir!,
    o juremos con gloria morir!,
    
    o juremos con gloria morir!
    

    预期输出:

    Total caracteres: 558
    Cantidad palabras: 87
    Cantidad líneas: 25
    Cantidad párrafos: 8
    Promedio longitud palabra: 4.966
    

    我的输出:

    Total caracteres: 557
    Cantidad palabras: 69
    Cantidad líneas: 24
    Cantidad párrafos: 12
    Promedio longitud palabra: 8.000
    

    程序统计字符、单词、行和段落的数量(两个连续的“\n”)。和平均字长。

    3 回复  |  直到 10 年前
        1
  •  2
  •   BLUEPIXY    10 年前

    每个计数条件都是错误的。
    修复如下:

    #include <stdio.h>
    #include <ctype.h>
    
    int main(void){
        int pCount=0, parCount=0, cCount=0, lCount=0;//word, paragraph, character, line
        int abCount = 0;//alphabet 
        double prom=0;
        int c;//It should be int.
        char pprev = '\n', prev = '\n';
    
        while ((c=getchar())!=EOF){
            ++cCount;
            if(isalpha(c))
                ++abCount;
            if(isspace(c)){
                if(c == '\n'){
                    ++lCount;
                }
            } else if(isspace(prev)){//isspace(prev) && !isspace(c) : edge of top of word
                ++pCount;
                if(pprev == '\n' && prev == '\n'){//edge of top of paragraph
                    ++parCount;
                }
            }
            pprev = prev;
            prev = c;
        }
        if(prev != '\n'){//If the file is not terminated by newline
            ++lCount;
        }
    
        prom = (double)abCount / pCount;//(cCount - spcCount - punctCount) / pCount
        printf("Total caracteres: %d \n", cCount);
        printf("Cantidad palabras: %d \n", pCount);
        printf("Cantidad lineas: %d \n", lCount);
        printf("Cantidad parrafos: %d \n", parCount);
        printf("Promedio longitud palabra: %.3f \n", prom);
        return 0;
    }
    
        2
  •  0
  •   Mathieu Shrikanth M D    10 年前

    我在您的代码中看到了几个问题:

    1. 段落计数:不设置 newln_cnt 如果读取字符不同于 \n 。每两段计算一段 \n个 已读取。

    2. 空间数:您只考虑 ' ' 字符,您可能会错过其他空白字符,如不可中断的空格。考虑使用 isspace() 作用

    3. 平均线长度:将两个整数相除得到一个浮点数,考虑强制转换:

      prom = (float)cCount / (flao)pCount;
      

    我的建议:从一个简短的文本(每行3个单词,5行)和一个调试器开始。

        3
  •  -2
  •   Niklas Rosencrantz    10 年前

    由于类型转换错误,它没有编译,但您可以对所有内容使用浮点,它将编译:

    #include <stdio.h>
    
    int main(int argc, char const *argv[])
    {
        double pCount=0, parCount=0, cCount=0, lCount=0;
        double prom=0;
        char c;
        int newln_cnt=0;
        while ((c=getchar())!=EOF){ 
            switch (c)
            {
                case '\n':
                    newln_cnt++;
                    lCount++;
                    if (newln_cnt == 2)
                    {
                        parCount++;
                        newln_cnt = 0;
                    }
                    break;
                case ' ':
                    pCount++;
                    break;
            }               
        }
        prom = (cCount / pCount);
        printf("Total caracteres: %f \n", cCount);
        printf("Cantidad palabras: %f \n", pCount);
        printf("Cantidad líneas: %f \n", lCount);
        printf("Cantidad párrafos: %f \n", parCount);
        printf("Promedio longitud palabra: %f \n", prom);
        return 0;
    }
    

    既然程序已经编译,你可以调整到最适合你的程序类型,你甚至可以有自己的类型。

    一个与您的程序类似的著名程序是 wc -单词计数,是标准Unix库的一部分。