1037: 四则运算

来源:互联网 发布:js数组地址指向 编辑:程序博客网 时间:2024/06/10 01:12

1037: 四则运算

Time Limit: 1 Sec  Memory Limit: 30 MB
Submit: 30796  Solved: 7964

SubmitStatusWeb Board

Description

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

Input

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

Output

如果运算符合法,输出表达式的值;若运算符不合法或进行除法运算时除数是0,则输出"Wrong input!"。最后结果小数点后保留两位。

Sample Input

1.0 + 1.0

Sample Output

2.00

HINT

除数是0,用|s2|<1e-10(即10的-10次方)判断


Source

**


#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespace std;int main(){    char x;    double s1,s2;    scanf("%lf %c %lf",&s1,&x,&s2);    switch(x)    {    case '+':        printf("%.2f\n",s1+s2);        break;    case '-':        printf("%.2f\n",s1-s2);        break;    case '*':        printf("%.2f\n",s1*s2);        break;    case '/':        if(fabs(s2)<1e-10) printf("Wrong input!\n");        else printf("%.2f\n",s1/s2);        break;    default:       printf("Wrong input!\n");    }    return 0;}