【问题标题】:Trying to output user input with only getchar/putchar尝试仅使用 getchar/putchar 输出用户输入
【发布时间】:2014-03-21 21:54:15
【问题描述】:

我必须用 C 语言编写一个简单的计算器,它只使用 getchar/putchar。我需要回显用户输入,然后执行数学运算(只需要使用 +、/、%、* 和 -)。现在,我只关心我做错了什么,只要将值存储到我的输入变量中。程序的其余部分应该很容易自己完成。

在我的 while 循环中,我尝试小心地将 if/else 嵌套(主要用于错误检查)仅在我的“else if”中。我希望我的循环忽略空格,然后将数字、数学运算符和其他数字分别分配给 input1、input2 和 input3。现在,如果我输入“55 * 66”之类的内容,我会返回“*0”之类的内容。

感谢收看。

更新(2014 年 3 月 22 日): 我越来越近了。我现在的问题是,只有当我在每个数字和操作数之后输入一个空格时,程序才能工作(即“2 + 4”有效,但任何没有空格或多个空格的东西都不能)。我也不太明白如何输入数字以输出它们。我使用了 printf,因此我至少可以同时拥有一个工作程序。 谢谢。

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

int add(int input1,char operand, int input2);
int subtract(int input1,char operand, int input2);
int mod(int input1,char operand, int input2);
int multiply(int input1,char operand, int input2);
int divide(int input1,char operand, int input2);

int main()
{

    int answer = 0;
    int ch = 0;
    int input1 = 0;
    char operand = 0;
    int input2 = 0;
    int function = 0;

    printf("\nPlease enter a calculation to be made.\n");

    while (((ch = getchar()) != ' ') && (ch != '\n')){

    if (ch == '-') {
        printf("\nError: no negatives allowed.\n");
    }

    else if (!isdigit(ch)){
    printf("\nError: number not inputted (first number).\n");
    }

    else {
        input1 = (input1 * 10) + (ch - '0');
        }
}

    while (((ch = getchar()) != ' ') && (ch != '\n')){

        switch(ch){

        case '+':
        operand = '+';
        break;

        case '-':
        operand = '-';
        break;

        case '%':
        operand = '%';
        break;

        case '*':
        operand = '*';
        break;

        case '/':
        operand = '/';
        break;

        default:
        printf("Error: input is not one of the allowed operands.");
        break;

        }

    }

    while (((ch = getchar()) != ' ') && (ch != '\n')){


    if (ch == '-') {
        printf("\nError: no negatives allowed.\n");
    }

    else if (!isdigit(ch)){
    printf("\nError: number not inputted (second number).\n");
    }

    else {
        input2 = (input2 * 10) + (ch - '0');
        }
}

printf("%d", input1);
putchar(' ');

printf("%c", operand);
putchar(' ');

printf("%d", input2);

putchar(' ');
putchar('=');
putchar(' ');

if (operand == '+'){
answer = add(input1, operand, input2);
printf("%d", answer);
}
else if (operand == '-'){
answer = subtract(input1, operand, input2);
printf("%d", answer);
}
else if (operand == '%'){
answer = mod(input1, operand, input2);
printf("%d", answer);
}
else if (operand == '*'){
answer = multiply(input1, operand, input2);
printf("%d", answer);
}
else if (operand == '/'){
answer = divide(input1, operand, input2);
printf("%d", answer);
}

    return 0;
}

int add(int input1,char operand, int input2){

return input1 + input2;

}

int subtract(int input1,char operand, int input2){

return input1 - input2;

}

int mod(int input1,char operand, int input2){

return input1 % input2;

}

int multiply(int input1,char operand, int input2){

return input1 * input2;

}

int divide(int input1,char operand, int input2){

return input1/input2;

}

【问题讨论】:

  • 您有两个案例测试isdigit(ch)。第一个不做任何事情,并且将始终支持将ch 存储在input3 中的第二个。
  • 您可以使用isspace 来检查空格。另外,在确定ch 是一个数字之后,您希望通过比较ch &gt;= 0 来实现什么?不要忘记ch 是数字字符的 ASCII 值,而不是数字本身的数值。
  • ch &gt;= 0 不会测试数字是否大于或等于 0。它将测试输入的字符的编码是否 >= 0。不是你想要的。也许你的意思是ch &gt;= '0',但这也没什么用。只需使用isdigit()。请记住,如果用户输入负数,您将首先读取-
  • 感谢您的回复 - 关于检查 ch >= 0,此代码的一部分是我要检查并排除负数。我应该说清楚的。我还添加了“input1 = ch;”在第一个 else if 中,我不确定为什么它不存在。不过,我仍然遇到问题。
  • 你手上的是单个数字字符的 ASCII 值,而不是带符号的多位整数。您将不得不编写一个更复杂的解析器来读取可选的 '-' 后跟多个数字字符,将每个数字从 ASCII 转换为实际数值,并将它们合并为某个数基中的单个整数(通常,我们使用基数 10!)。

标签: c getchar putchar


【解决方案1】:

最简单的做法是按顺序解析每个表达式:

int main(int argc, char **argv)
{
    int ch;
    int a;
    char op;
    int b;
    int negate;

    // start with a character of lookahead
    ch = getchar();

    // skip leading spaces
    while (ch != EOF && isspace(ch) && ch != '\n') ch = getchar();

    // read A operand
    a = 0;
    if ((negative = (ch == '-'))) ch = getchar();
    if (ch == EOF || !isdigit(ch)) {
        // error, expecting a digit or '-' here
    }
    do {
        // convert digit from ASCII, and "shift" into accumulator
        a = (a * 10) + (ch - '0');
        ch = getchar();
    }
    while (ch != EOF && isdigit(ch));
    if (negative) a = -a;

    // skip spaces
    while (ch != EOF && isspace(ch) && ch != '\n') ch = getchar();

    // read operator
    if (ch == EOF || (ch != '+' && ch != '-' && ...)) {
        // error, expecting an operator
    }
    op = ch;
    ch = getchar();

等等……

【讨论】:

  • 非常感谢您在所有这些过程中提供的帮助。我正在尽力自己做这件事,但目前,我的代码不能在没有空格或多个空格的情况下工作。我试图从你的代码中实现一些东西,但还没有完全工作。
  • 我整理了一个工作版本here。它将在 2 周内到期。确保您了解它在做什么。
  • 非常感谢您的帮助。
猜你喜欢
  • 2021-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多