使用c问题的简单计算器

时间:2017-07-16 11:14:58

标签: c

我是C的新手,我正在尝试制作一个简单的计算器

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

int main() {
    int firstNumber, secondNumber, answer;
    char mathSign[20];

    printf("Enter first number \n");
    scanf("%d", &firstNumber);

    printf("Enter sign \n a - Add \n s = Subtract \n m - Multiply \n d - Divide \n");
    scanf("%s", mathSign);

    printf("Enter second number \n");
    scanf("%d", &secondNumber);

    if (mathSign == 'a') {
        answer = firstNumber + secondNumber;
    } else if (mathSign == 's') {
        answer = firstNumber - secondNumber;
    } else if (mathSign == 'm') {
        answer = firstNumber * secondNumber;
    } else if (mathSign == 'd') {
        answer = firstNumber / secondNumber;
    }

    printf("Your answer is %d", answer);

    return 0;
}

由于某种原因,答案总是以52结尾。我应该将整数翻倍吗?

3 个答案:

答案 0 :(得分:1)

试试这段代码。这应该工作。此外,这个将是优化的。

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

int main() {
    int firstNumber, secondNumber, answer;
    char mathSign;

    printf("1.Enter first number \n");
    scanf("%d", &firstNumber);

    printf("Enter sign \n a - Add \n s = Subtract \n m - Multiply \n d - Divide \n");
    scanf(" %c", &mathSign);

    printf("Enter second number \n");
    scanf("%d", &secondNumber);

    switch (tolower(mathSign)) {
    case 'a':
        answer = firstNumber + secondNumber;
        break;
    case 's':
        answer = firstNumber - secondNumber;
        break;
    case 'm':
        answer = firstNumber * secondNumber;
        break;
    case 'd':
        answer = firstNumber / secondNumber;
        break;
    default:
        printf("Invalid Choice");
        break;
    }

    printf("Your answer is %d", answer);
    return 0;
}

答案 1 :(得分:0)

当你只使用mathSign时,它是一个指向数组的指针。 要访问第一个元素,您必须使用mathSign[0]。

或者,由于您只对一个符号感兴趣,因此您可以声明char mathSign而不是char mathSign[20],然后使用scanf(" %c", mathSign);读取符号字符 在这种情况下,比较mathSign == 'a是正确的。

答案 2 :(得分:0)

您测试mathSign == 'a'mathSign是一个数组,因此您应该编写mathSign[0] == 'a'。编译器应该产生警告。你不应该忽视这些警告,它们表明通常是真正的错误的问题。您可以通过提高警告级别来获得更有用的诊断:gcc -Wall -W -Werrorclang -Weverything -Werror ...

请注意,如果用户键入长度超过19个字符的单词,scanf("%s", mathSign);将导致缓冲区溢出。您应该使用以下命令保护目标数组:

scanf("%19s", mathSign);

此外,您应检查转换是否成功,否则如果无法转换输入并且您使用仍未初始化的值,则可能会有未定义的行为。

您可以使用" %c"来读取单个字符,并使用经典运算符以提高可读性。请注意%c之前的空格:它允许scanf()跳过待处理的空格字符,例如上一个响应中的换行符。

以下是修改后的版本:

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

int main(void) {
    int firstNumber, secondNumber, answer;
    char mathSign;

    printf("Enter first number: ");
    if (scanf("%d", &firstNumber) != 1)
        return 1;

    printf("Enter operation\n + - Add\n - = Subtract\n * - Multiply\n / - Divide\n");
    if (scanf(" %c", &mathSign) != 1)
        return 1;

    printf("Enter second number: ");
    if (scanf("%d", &secondNumber) != 1)
        return 1;

    if (mathSign == '+') {
        answer = firstNumber + secondNumber;
    } else if (mathSign == '-') {
        answer = firstNumber - secondNumber;
    } else if (mathSign == '*') {
        answer = firstNumber * secondNumber;
    } else if (mathSign == '/') {
        answer = firstNumber / secondNumber;
    } else {
        printf("unsupported operation: %c\n", mathSign);
        return 1;
    }

    printf("Your answer is %d\n", answer);

    return 0;
}

您可以使用switch声明进一步简化程序:

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

int main(void) {
    int firstNumber, secondNumber, answer;
    char mathSign;

    printf("Enter a simple expression: ");
    if (scanf("%i %c%i", &firstNumber, &mathSign, &secondNumber) != 3)
        return 1;

    switch (mathSign) {
      case '+': answer = firstNumber + secondNumber; break;
      case '-': answer = firstNumber - secondNumber; break;
      case '*': answer = firstNumber * secondNumber; break;
      case '/': answer = firstNumber / secondNumber; break;
      case '%': answer = firstNumber % secondNumber; break;
      default: 
        printf("unsupported operation: %c\n", mathSign);
        return 1;
    }
    printf("the result is %d\n", answer);
    return 0;
}