7-36 求解简单的四则运算表达式 (10 分)

输入一个四则运算表达式,输出运算结果,要求对除数为0的情况作特别处理。题目保证输入与输出均在双精度范围内。

输入格式:

输入在一行中依次输入操作数1、运算符、操作数2,其间没有空格,操作数的数据类型为实型。

输出格式:

在一行中输出表达式的运算结果,保留两位小数。

如果除法分母为0,则输出错误信息“Divisor can not be 0!”;如果输入了除加减乘除以外的非法运算符,则输出错误信息“Unknown operator!”。

输入样例1:

3.1+4.8

结尾无空行

输出样例1:

7.90

结尾无空行

输入样例2:

98/0

结尾无空行

输出样例2:

Divisor can not be 0!

结尾无空行

输入样例1:

123%3

结尾无空行

输出样例1:

Unknown operator!

结尾无空行

这个题目比较好理解,就是注意一些细节,比如浮点小数等。

#include<stdio.h>
int main(void){
   double a=0,b=0;
    char c=0;
    scanf("%lf%c%lf",&a,&c,&b);

    if(c=='+')
        printf("%.2lf",a+b);
    else if(c=='-')
        printf("%.2lf",a-b);
    else if(c=='/'){
        if(b==0)
            printf("Divisor can not be 0!");
        else printf("%.2lf",a/b);
    }
    else if(c=='*')
        printf("%.2lf",a*b);
    else if(c!='+'&&c!='-'&&c!='/'&&c!='*')
        printf("Unknown operator!");
    return 0;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PTA(Programming Test and Assessment)平台上有很多简单四则运算表达式题目,可以练习基本的算术运算和表达式求值。下面是一个例子: 题目描述: 给定一个只包含加、减、乘、除四种基本运算的表达式,求表达式的值。 输入格式: 第一行包含一个整数 T,表示共有 T 组测试数据。 每组数据占一行,包含一个长度不超过 50 的只包含数字与加、减、乘、除四种运算符的表达式。 输出格式: 对于每组数据,输出表达式的值,保留两位小数。 样例输入: 2 1+2*3-4/5 3+5*8-6/2 样例输出: 6.20 43.00 代码示例(C++): ```cpp #include <iostream> #include <stack> #include <string> #include <iomanip> using namespace std; int main() { int T; cin >> T; while (T--) { string s; cin >> s; stack<double> nums; stack<char> ops; for (int i = 0; i < s.size(); i++) { if (isdigit(s[i])) { double num = s[i] - '0'; while (i + 1 < s.size() && isdigit(s[i + 1])) { num = num * 10 + s[i + 1] - '0'; i++; } nums.push(num); } else if (s[i] == '(') { ops.push('('); } else if (s[i] == ')') { while (ops.top() != '(') { double b = nums.top(); nums.pop(); double a = nums.top(); nums.pop(); char op = ops.top(); ops.pop(); if (op == '+') nums.push(a + b); else if (op == '-') nums.push(a - b); else if (op == '*') nums.push(a * b); else if (op == '/') nums.push(a / b); } ops.pop(); } else if (s[i] == '+' || s[i] == '-') { while (!ops.empty() && (ops.top() == '+' || ops.top() == '-' || ops.top() == '*' || ops.top() == '/')) { double b = nums.top(); nums.pop(); double a = nums.top(); nums.pop(); char op = ops.top(); ops.pop(); if (op == '+') nums.push(a + b); else if (op == '-') nums.push(a - b); else if (op == '*') nums.push(a * b); else if (op == '/') nums.push(a / b); } ops.push(s[i]); } else if (s[i] == '*' || s[i] == '/') { while (!ops.empty() && (ops.top() == '*' || ops.top() == '/')) { double b = nums.top(); nums.pop(); double a = nums.top(); nums.pop(); char op = ops.top(); ops.pop(); if (op == '+') nums.push(a + b); else if (op == '-') nums.push(a - b); else if (op == '*') nums.push(a * b); else if (op == '/') nums.push(a / b); } ops.push(s[i]); } } while (!ops.empty()) { double b = nums.top(); nums.pop(); double a = nums.top(); nums.pop(); char op = ops.top(); ops.pop(); if (op == '+') nums.push(a + b); else if (op == '-') nums.push(a - b); else if (op == '*') nums.push(a * b); else if (op == '/') nums.push(a / b); } cout << fixed << setprecision(2) << nums.top() << endl; } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值