c - 尝试仅使用 getchar/putchar 输出用户输入

标签 c getchar putchar

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

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

感谢您的浏览。

更新(2014 年 3 月 22 日): 我越来越近了。我现在的问题是,只有当我在每个数字和操作数后输入一个空格时,该程序才会工作(即“2 + 4”有效,但任何没有空格或超过一个空格的内容都无效)。我也不太明白如何 putchar 数字来输出它们。我使用了 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;

}

最佳答案

最简单的事情是按顺序解析表达式的每个部分:

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();

等等...

关于c - 尝试仅使用 getchar/putchar 输出用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22570207/

相关文章:

c - Scanf 与 char

c - C 程序中的奇怪输出

c - 理解此递归 C 程序以反向打印字符串所需的解释

c - 如何向 Linux 系统添加/注册唯一的 errno 和相应的错误消息?

c - fcntl() 关于进程id的问题

c - 用 C 中的字符串替换整数

c++ - 为什么我在将 tolower() 的结果流式传输到 std::cout 时得到一个数字,但在我使用 putchar() 时却没有?

c - 关于整数格式转换

c - 在c中使用usleep作为Timer是否合适

c - C 程序中的文件结尾 (EOF)