题目描述

给你一个简单的四则运算表达式,包含两个实数和一个运算符,实数与运算符用空格隔开,请编程计算出结果。

输入

表达式的格式为:s1 op s2, s1和s2是两个实数,op表示的是运算符(+,-,*,/),也可能是其他字符。

输出

 如果运算符合法,输出表达式的值;若运算符不合法或进行除法运算时除数是0(浮点类型的数绝对值小于1e-10,默认为0),则输出"Wrong input!"。最后结果小数点后保留两位。

样例输入

1.0 + 1.0

样例输出

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

int main() {
    double a, c;
    char b;
    scanf("%lf %c %lf", &a, &b, &c);
    if (b == '+')
        printf("%.2lf", a + c);
    else if (b == '-')
        printf("%.2lf", a - c);
    else if (b == '*')
        printf("%.2lf", a * c);
    else if (b == '/')
        if (fabs(c) < 1e-10)//----------------------浮点类型数据比较要用fabs
            printf("Wrong input!");
        else
            printf("%.2lf", a / c);
    else
        printf("Wrong input!");
    return 0;
}

更多推荐

四则运算(C语言)