在C中检查简单计算器时出错

时间:2014-10-14 18:35:38

标签: c

我需要你的错误检查帮助一个简单的计算器。大部分编码已完成,但我遇到两件事情有困难。 首先 - 当用户输入45号空格时,5 + 5应该是一个错误,因为在45和5之间有一个空格,如果2个数字之间有空格错误则与第二个数字相同。 第二 - 我需要将其转为“do while”循环。我试过但没有成功。任何帮助表示赞赏。

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

int addition(int num1, int num2);
int subtract(int num1, int num2);
int multi(int num1, int num2);
int div(int num1, int num2);
int modul(int num1, int num2);

int main(void){


    int ch, sum = 0, num1 = 0, num2 = 0,operand =0, opcount =0, numcount1 = 0,numcount2 = 0;


        while (((ch = getchar()) != EOF) && ch != '\n') {
            if ((ch >= '0') && (ch <= '9')) { // digits
                if (operand == 0) {
                    numcount1++;
                    num1 = ((num1 * 10) + (ch - '0')); // no operand so use num1
                }
                else {
                    numcount2++;
                    num2 = ((num2 * 10) + (ch - '0')); // operand has been assigned
                }
            }
            else { // non digits
                if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%'){
                    opcount++;
                    if (operand == 0) { // do not re-assign operand
                        operand = ch; // assign operand
                    }
                }
            }

    } /* End of the while loop*/

    /*printf("\n Result of num1 %d \n", num1);      
    printf("\n Result of oper %c \n", operand);    
    printf("\n Result of num2 %d \n", num2); */     

    if (operand == '+' && opcount == 1 && numcount1 > 0 && numcount2 > 0)
        printf("\n Result of  %d + %d = %d \n",num1,num2, addition(num1,num2));
    else if (operand == '-' && opcount == 1 && numcount1 > 0 && numcount2 > 0)
        printf("\n Result of  %d - %d = %d \n", num1, num2, subtract(num1, num2));
    else if (operand == '*' && opcount == 1 && numcount1 > 0 && numcount2 > 0)
        printf("\n Result of  %d * %d = %d \n", num1, num2, multi(num1, num2));
    else if (operand == '/' && opcount == 1 && num2 != 0 && numcount1 > 0 && numcount2 > 0)
        printf("\n Result of  %d / %d = %d \n", num1, num2, div(num1, num2));
    else if (operand == '%' && opcount == 1 && num2 != 0 && numcount1 > 0 && numcount2 > 0)
        printf("\n Result of  %d %% %d = %d \n", num1, num2, modul(num1, num2));
    else printf("\n Expression Error \n");

} /* End of main*/


/********************************** FUNCTIONS ********************************************/

int addition(num1, num2){
    int sum = 0;
    sum = num1 + num2;
    return sum;
}

int subtract(num1, num2){
    int sub = 0;
    sub = num1 - num2;
    return sub;
}

int multi(num1, num2){
    int mul = 0;
    mul = num1 * num2;
    return mul;
}

int div(num1, num2){
    int d = 0;
    d = num1 / num2;
    return d;
}

int modul(num1, num2){
    int m = 0;
    m = num1 % num2;
    return m;
}

1 个答案:

答案 0 :(得分:0)

如果整数输入中包含空格,则此while循环应报告错误

while (((ch = getchar()) != EOF) && ch != '\n') {
    if ((ch >= '0') && (ch <= '9')) { // digits
        if (operand == 0) {
            if ( whitespace && numcount1) {
                printf ( "error spaces between digits not allowed\n");
                return 1;
            }
            num1 = ((num1 * 10) + (ch - '0')); // no operand so use num1
            numcount1++;
            whitespace = 0;
        }
        else {
            if ( whitespace && numcount2) {
                printf ( "error spaces between digits not allowed\n");
                return 1;
            }
            num2 = ((num2 * 10) + (ch - '0')); // operand has been assigned
            numcount2++;
            whitespace = 0;
        }
    }
    else { // non digits
        if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%'){
            if ( operand == 0) { // do not re-assign operand
                operand = ch; // assign operand
                whitespace = 0;
            }
            else {
                printf ( "multiple operators not allowed\n");
                return 1;
            }
        }
        whitespace = 1;
    }
}