计算字符,单词,行C中的段落

时间:2016-04-27 06:25:50

标签: c io count words getchar

我试图从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

该程序计算字符,单词,行和段落的数量(连续两个&#39; \ n&#39;)。和平均字长。

3 个答案:

答案 0 :(得分:2)

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

#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;
}

答案 1 :(得分:0)

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

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

  2. 空格数:您只考虑' '个字符,您可能会错过其他空白字符,例如\ n \ n \ n \ n \ n \ n \ n \ n \ n \ n无法破碎的空格。考虑使用isspace()函数。

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

    prom = (float)cCount / (flao)pCount;
    
  4. 我的建议:从短文本(每行3个字,5行)和调试器开始。

答案 2 :(得分:-2)

由于类型转换错误而无法编译,但您可以使用浮点数来处理所有内容并进行编译:

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

相关问题