PTA:7-3 表达式求值_1

 

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

输入格式:

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

输出格式:

共一行,为表达式的值。

输入样例:

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

1+(3+2)*(7^2+6*9)/(2)

输出样例:

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

258

 java代码如下

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

public class Main {
    static boolean tiaoshi=true;
    public static void main(String[] args) {
        tiaoshi=false;//调试开关,观察结构
        Scanner in = new Scanner(System.in);
        String input = in.next();
        Stack<Object> stack=new Stack<>();
        Vector<Object> output=new Vector<>();
        for(int i=0;i<input.length();i++){
            int type=getType(input.toCharArray()[i]);
            if(type>=0){
                int num=type;
                while (i<input.length()-1&&getType(input.toCharArray()[i+1])>=0){
                    num*=10;
                    num+=getType(input.toCharArray()[++i]);
                }
                output.add(num);
            }else if(stack.size()==0||input.toCharArray()[i]=='('){
                stack.push(input.toCharArray()[i]);
            }else if(type>-5){
                if(type>getType((char)stack.peek())){
                    stack.push(input.toCharArray()[i]);
                }else{
                    while(stack.size()!=0&&type<=getType((char)stack.peek())){
                        output.add(stack.pop());
                    }
                    stack.push(input.toCharArray()[i]);
                }
            }else if(input.toCharArray()[i]==')'){
                while(stack.size()!=0){
                    char c=(char)stack.pop();
                    if(c=='(')break;
                    else output.add(c);
                }
            }
        }
        while(stack.size()>0){
            output.add(stack.pop());
        }
        if(tiaoshi)
        System.out.println(output);
        stack=new Stack<>();
        for(int i=0;i<output.size();i++){
            if(output.get(i) instanceof Integer){
                stack.push(output.get(i));
                if(tiaoshi){
                    printStack(stack);
                }
            }else{
                int b=(int)stack.pop(),a=(int)stack.pop();
                int c=0;
                switch ((char)output.get(i)) {
                    case '+':
                        c=a+b;
                        break;
                    case '-':
                        c=a-b;
                        break;
                    case '*':
                        c=a*b;
                        break;
                    case '/':
                        c=a/b;
                        break;
                    case '^':
                        c=(int)Math.pow(a,b);
                        break;
                }
                stack.push(c);
                if(tiaoshi){
                    System.out.println("[操作符]"+output.get(i));
                    System.out.println("[a]"+a+" [b]"+b+" [c]"+c);
                    printStack(stack);
                }
            }
        }
        if(tiaoshi){
            System.out.println("[stack长度]"+stack.size());
            printStack(stack);
        }
        System.out.println(stack.pop());
        in.close();
    }
    static int getType(char c){
        if(c=='('||c==')')return -10086;
        if(c=='^')return -2;
        if(c=='*'||c=='/')return -3;
        if(c=='+'||c=='-')return -4;
        return c-'0';
    }
    static void printStack(Stack stack){
        String out="[stack]";
        for(int i=0;i<stack.size();i++){
            out+=stack.peek();
        }
        //System.out.println(out);
    }
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
算数表达式求值的一个重要应用之一。算数表达式通常由数字、运算符和括号组成,例如: (3 + 4) * 5 - 6 / 2 为了求出这个表达式的值,我们需要遵循一定的计算规则,通常是先计算括号内的表达式,再依次计算乘、除、加、减运算。使用可以较为方便地实现这个过程。 具体操作步骤如下: 1. 建立两个,一个用于存储数字,另一个用于存储运算符。 2. 从左到右遍历表达式的每一个字符,遇到数字则直接压入数字中,遇到运算符则判断其优先级,如果其优先级高于顶运算符,则直接将其压入运算符中;否则,将顶运算符弹出,与数字中的两个数字进行运算,将结果压回数字中,然后继续比较当前运算符与新的顶运算符的优先级。 3. 当表达式遍历完成后,依次弹出运算符中的运算符,并与数字中的两个数字进行运算,将结果压回数字中,直到运算符为空。 4. 最终,数字中仅剩一个数字,即为表达式的值。 以表达式 (3 + 4) * 5 - 6 / 2 为例,具体操作过程如下: 1. 遍历到字符 '3',将其压入数字中。 2. 遍历到字符 '+',由于运算符为空,直接将其压入运算符中。 3. 遍历到字符 '4',将其压入数字中。 4. 遍历到字符 ')',弹出运算符中的运算符 '+',并弹出数字中的两个数字 3 和 4,计算出结果 7,将其压入数字中。 5. 遍历到字符 '*',由于运算符中的运算符优先级高于当前运算符,直接将其压入运算符中。 6. 遍历到字符 '5',将其压入数字中。 7. 遍历到字符 '-',弹出运算符中的运算符 '*',并弹出数字中的两个数字 7 和 5,计算出结果 35,将其压入数字中。 8. 遍历到字符 '6',将其压入数字中。 9. 遍历到字符 '/',由于运算符中的运算符优先级低于当前运算符,弹出数字中的两个数字 35 和 6,计算出结果 5,将其压入数字中。 10. 遍历结束,弹出运算符中的运算符 '-',并弹出数字中的两个数字 35 和 5,计算出结果 30,即为表达式的值。 综上,使用可以较为方便地实现算数表达式的求值。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水夸夸的梁郭先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值