【问题标题】:Counting characters, words, lines an paragraphs in C计算C中的字符,单词,行和段落
【发布时间】:2016-04-27 06:25:50
【问题描述】:

我正在尝试从标准输入计算 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')。和平均字长。

【问题讨论】:

  • char c;应该是int c;
  • 你永远不会增加cCountpCount 实际上是空格数,与单词数不太一样。想象一下,如果你输入 3 行没有任何空格会发生什么,那么pcount 会是什么?
  • 您的问题取得了一些进展,但您的输入样本对于调试来说太长了。
  • 我把它粘贴到一个 .txt 文件中并像 cat text.txt 一样执行程序 | ./程序
  • 好的,现在我得到了和你一样的输出。您确实应该考虑使用更短且简单的输入文件来测试和调试您的程序。也许是时候学习如何使用调试器了。

标签: c io count words getchar


【解决方案1】:

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

#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】:

我在您的代码中发现了几个问题:

  1. 段落计数:如果读取的字符与 \n 不同,则不要将 newln_cnt 设置为 0。每次读取两个\n 时,这将计为一个段落。

  2. 空格数:您只考虑' ' 字符,您可能会错过其他空格字符,例如\t ou 不可破坏的空格。考虑使用isspace() 函数。

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

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

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

【讨论】:

    【解决方案3】:

    由于类型转换错误,它没有编译,但你可以对所有东西使用浮点数,它会编译:

    #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 库的一部分。

    【讨论】:

    • 为什么在这里使用浮点类型比整数更好?
    • @dreamlax "Promedio longitud palabra" 表示平均
    • @Programmer400 平均字长
    • @Programmer400:我的观点是 prom 可以使用 (double)cCount / pCount 分配平均值,而无需将 所有内容 转换为浮点数。很少需要将浮点类型用作计数器。
    • @dreamlax 是的,我只是想编译这个东西,现在我们可以同意我们是否应该有演员表,我们可能应该这样做。我添加了一个指向wc 程序的链接,该程序很短并且不打印平均 AFAIK。
    猜你喜欢
    • 1970-01-01
    • 2021-08-22
    • 2020-11-16
    • 2018-06-02
    • 1970-01-01
    • 2023-03-12
    • 2013-07-22
    • 1970-01-01
    • 2014-02-20
    相关资源
    最近更新 更多