【水文】实现四则运算的简易计算器

代码:

#include 

int main() {
    double num1, num2;
    char operator, ch;

    printf("请输入两个操作数和运算符(空格分隔):");
    scanf("%lf %lf %c", &num1, &num2, &operator);

    switch (operator) {
        case '+':
            printf("%.2f %c %.2f = %.2f\n", num1, operator, num2, num1 + num2);
            break;
        case '-':
            printf("%.2f %c %.2f = %.2f\n", num1, operator, num2, num1 - num2);
            break;
        case '*':
            printf("%.2f %c %.2f = %.2f\n", num1, operator, num2, num1 * num2);
            break;
        case '/':
            if (num2 != 0) {
                printf("%.2f %c %.2f = %.2f\n", num1, operator, num2, num1 / num2);
            } else {
                printf("除数不能为0\n");
            }
            break;
        default:
            printf("无效的运算符\n");
            break;
    }

    return 0;
}

你可能感兴趣的:(C)