PTA练习题:表达式求值_1

在一个表达式中,只有“(”,“)”,“0-9”,“+”,“-”,“*”,“/”,“^”,请求出表达式的值。(“/”用整数除法)。

输入格式:

共1 行,为一个算式。 (算式长度<=30 其中所有数据在 0~2^31-1的范围内)。

输出格式:

共一行,为表达式的值。

输入样例:

在这里给出一组输入。例如:

1+(3+2)(7^2+69)/(2)
输出样例:

在这里给出相应的输出。例如:

258

import java.util.Scanner;
import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String a = in.next();
        char[] e = a.toCharArray();
        Stack<Character> s1 = new Stack<>();
        Stack<Integer> s2 = new Stack<>();
        int x1;
        int x2;
        for(int i=0;i<e.length;i++)
        {
            if(e[i] >= '0' && e[i] <= '9')
            {
                int num;
                num = e[i]-'0';
                while(i<e.length-1)
                {
                    if(!(e[i+1] >= '0' && e[i+1] <= '9'))
                    {
                        break;
                    }
                    
                    num *= 10;
                    num += (e[i+1] - '0');
                    i++;
                }
                s2.push(num);
            }
            else if(s1.isEmpty())
            {
                s1.push(e[i]);
            }
            else if(e[i] == '(')
            {
                s1.push(e[i]);
            }
            else if(e[i] == ')')
            {
                char c3;
                while(!s1.isEmpty() && (c3 = s1.pop()) != '(')
                {
                    x2 = s2.pop();
                    x1 = s2.pop();
                    s2.push(calculate(x1,x2,c3));
                }
            }
            else if(e[i] == '^')
            {
                if(s1.peek() == '^')
                {
                    char c3;
                    c3 = s1.pop();
                    x2 = s2.pop();
                    x1 = s2.pop();
                    s2.push(calculate(x1,x2,c3));
                }
                s1.push(e[i]);
            }
            else if(e[i] == '+' || e[i] == '-')
            {
                if(s1.peek() != '(')
                {
                    char c3;
                    while(!s1.isEmpty() && s1.peek() != '(')
                    {
                        c3 = s1.pop();
                        x2 = s2.pop();
                        x1 = s2.pop();
                        s2.push(calculate(x1,x2,c3));
                    }
                }
                s1.push(e[i]);
            }
            else
            {
                char c3;
                while(!(s1.peek() == '+' || s1.peek() == '-') && !s1.isEmpty() && s1.peek() != '(')
                {
                    c3 = s1.pop();
                    x2 = s2.pop();
                    x1 = s2.pop();
                    s2.push(calculate(x1,x2,c3));
                }
                s1.push(e[i]);
            }
        }
        while(!s1.isEmpty())
        {
            char c3;
            c3 = s1.pop();
            x2 = s2.pop();
            x1 = s2.pop();
            s2.push(calculate(x1,x2,c3));
        }
        System.out.println(s2.pop());
        in.close();
    }
    public static int calculate(int x1,int x2,char c)
    {
        int sum = 0;
        switch (c)
        {
            case '+':sum = x1+x2;break;
            case '-':sum = x1-x2;break;
            case '*':sum = x1*x2;break;
            case '/':sum = x1/x2;break;
            case '^':sum = (int) Math.pow(x1,x2);break;
        }
        return sum;
    }
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值