PTA 7-99 含有2个运算符的算数表达式(选作)

PTA 7-99 含有2个运算符的算数表达式(选作)

分数 30
作者 叶青
单位 长春理工大学

键盘输入三个整数和两个算术运算组成的表达式,计算结果。
运算符包括:加+,减-乘*,除/

输入格式:
三个整数,两个运算符,组成算数表达式,输入的字符之间没有空格

输出格式:
在表达式的后边输出=计算结果

输入样例:
6+3*11
输出样例:
6+3*11=39
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
代码:
#include <stdio.h>

int main()
{
    int x, y, z;
    char a, b;
    long result;
    scanf("%d%c%d%c%d",&x, &a, &y, &b, &z);
    printf("%d%c%d%c%d=",x, a, y, b, z);
    switch(a)
    {
        case '+'://如果输入的a是+就进入switch(b)根据b再选择哪个case语句
            switch(b)
            {
                case '+':result = x + y + z ; printf("%ld", result); break;//注意break不要忘记了
                case '-':result = x + y - z ; printf("%ld", result); break;
                case '*':result = x + y * z ; printf("%ld", result); break;
                case '/':result = x + y / z ; printf("%ld", result); break;
            }
            break;//注意break不要忘记了
        case '-'://如果输入的a是-就进入switch(b)根据b再选择哪个case语句
            switch(b)
            {
                case '+':result = x - y + z ; printf("%ld", result); break;
                case '-':result = x - y - z ; printf("%ld", result); break;
                case '*':result = x - y * z ; printf("%ld", result); break;
                case '/':result = x - y / z ; printf("%ld", result); break;
            }
            break;
        case '*'://如果输入的a是*就进入switch(b)根据b再选择哪个case语句
            switch(b)
            {
                case '+':result = x * y + z ; printf("%ld", result); break;
                case '-':result = x * y - z ; printf("%ld", result); break;
                case '*':result = x * y * z ; printf("%ld", result); break;
                case '/':result = x * y / z ; printf("%ld", result); break;
            }
            break;
        case '/'://如果输入的a是/就进入switch(b)根据b再选择哪个case语句
            switch(b)
            {
                case '+':result = x / y + z ; printf("%ld", result); break;
                case '-':result = x / y - z ; printf("%ld", result); break;
                case '*':result = x / y * z ; printf("%ld", result); break;
                case '/':result = x / y / z ; printf("%ld", result); break;
            }
            break;
        default:printf("Error Symbols!");
    }
    return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值