【问题标题】:C program to find total number of digits [closed]C程序查找总位数[关闭]
【发布时间】:2024-01-19 11:25:01
【问题描述】:

我编写了这个程序来查找用户输入的一行文本中的总位数。我在使用getchar() 时出错。我似乎无法弄清楚我做错了什么?

#include <stdio.h>

#define MAX_SIZE 100

void main() {
    char c[MAX_SIZE];
    int digit, sum, i;
    digit, i = 0;

    printf("Enter a line of characters>");
    c = getchar();
    while (c[i] != '\n') {
        digit = 0;
        if (c [i] >= '0' && c[i] <= '9') {
            digit++;
        }
    }
    printf("%d\n", digit);
}

我将使用 sum 变量添加我找到的所有数字。但我在getchar() 线上遇到错误。帮忙??

【问题讨论】:

  • c = getchar() - c 是一个数组(因此无论其余部分如何,都不能直接赋值)。 getchar() 返回int。老实说,you don't need an array for this task at all,如果目的只是为了计算数字字符直到行尾或文件结束。
  • getchar 获取单个字符。你应该使用fgets或类似的东西
  • ..然后你不会在循环中增加i
  • 一般情况下,如果您遇到错误,您应该发布错误内容。也就是说,cmets 已经涵盖了这一点。
  • 好吧,根据你们提到的不使用数组我写了一些不使用数组的东西,就像这样:#include void main() { char c;整数位,总和;数字 = 0; printf("输入一行字符>"); c = getchar();而 (c != '\n') { 数字 = 0; if (c >= '0' && c

标签: c printf getchar digit


【解决方案1】:

您可以在不使用数组的情况下输入“文本行”。

#include <stdio.h>
#include <ctype.h>

int main(void) {                    // notice this signature
    int c, digits = 0, sum = 0;
    while((c = getchar()) != '\n' && c != EOF) {
        if(isdigit(c)) {
            digits++;
            sum += c - '0';
        }
    }
    printf("%d digits with sum %d\n", digits, sum);
    return 0;
}

请注意,c 的类型为 int。大多数库的字符函数不使用char 类型。

编辑:添加了数字的总和。

【讨论】:

  • 那应该是另一个问题,但我已经编辑了答案。请注意,'0' 也是 int 类型。
  • (OP 询问“我如何对数字值求和?)
【解决方案2】:

Weather Vane 的答案是最好最简单的答案。但是,为了将来参考,如果要迭代(循环)数组,使用 for 循环会更容易。此外,您的 main 函数应该返回一个 int 并且应该如下所示:int main()。您需要在 main 函数的末尾添加一个return 0;。这是您的程序的修改版本,它使用 for 循环遍历字符数组。我使用gets 函数从控制台读取一行字符。它会等待用户输入字符串。

#include <stdio.h>
#define MAX_SIZE 100

int main()
{
    char c[MAX_SIZE];
    int digit = 0;

    printf("Enter a line of characters>");
    gets(c);

    for (int i = 0; i < MAX_SIZE; i++)
    {
        if (c[i] == '\n') break; // this line checks to see if we have reached the end of the line. If so, exit the for loop (thats what the "break" statment does.)

        //if (isdigit(c[i])) // uncomment this line and comment or delete the one below to use a much easier method to check if a character is a digit.
        if (c [i]>= '0' && c[i] <= '9')
        {
            digit++;
        }
    }

    printf("%d\n", digit);
    return 0;
}

【讨论】:

  • 感谢 Codeon 大师。非常感谢您的帮助。
  • gets 太危险了,已经过时了,所以非常糟糕推荐使用它。而main 是不需要返回值的 one 函数。有人赞成这个答案。我会投反对票,但我从不对我给出的问题的答案投票。
  • 欢迎您。乐于助人。
  • 当然,为了简单起见,我使用它。我很确定 OP 并不关心 gets 容易受到缓冲区溢出攻击等事实。我从来不知道 main 可能是无效的。我知道在 C# 中这是可能的,但我认为你不能在 C++ 中做到这一点。你每天都会学到新东西!
  • 不是main 可以是void,而是编译器可以容忍main 中没有返回值。但仅适用于main
【解决方案3】:

获取整数位数的一个简单方法是使用在 math.h 头文件中定义的 log10() 函数。考虑这个程序

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main (int argc, const char *argv[]) {
    system("clear");

    unsigned int i, sum = 0, numberOfDigits;
    puts("Enter a number");
    scanf("%u", &i);
    numberOfDigits = (int) (log10(x) + 1);

    system("clear");

    while(i != 0) {
        sum += (i % 10);
        i /= 10;
    }

    fprintf(stdout, "The sum is %i\n", sum);
    fflush(stdin);
    return 0;
}

【讨论】: