逆波兰表达式求值PTA Java

文章提供了两段Java代码,分别尝试解决根据逆波兰表示法(RPN)计算算式的问题。代码利用栈数据结构处理操作数和运算符,但第一段代码在处理某些测试用例时失败。第二段代码通过将输入按空格分割并逐个处理元素,使用栈进行计算,能正确处理运算和整数。
摘要由CSDN通过智能技术生成

逆波兰表示法是一种将运算符(operator)写在操作数(operand)后面的描述程序(算式)的方法。举个例子,我们平常用中缀表示法描述的算式(1 + 2)*(5 + 4),改为逆波兰表示法之后则是1 2 + 5 4 + *。相较于中缀表示法,逆波兰表示法的优势在于不需要括号。

请输出以逆波兰表示法输入的算式的计算结果。

输入格式:

在一行中输入1个算式。相邻的符号(操作数或运算符)用1个空格隔开。

输出格式:

在一行中输出计算结果。

限制:

2≤算式中操作数的总数≤100

1≤算式中运算符的总数≤99

运算符仅包括“+”、“-”、“*”,操作数、计算过程中的值以及最终的计算结果均在int范围内。

输入样例1:

4 3 + 2 -

输出样例1:

5

输入样例2:

1 2 + 3 4 - *

输出样例2:

-3

我自己写的代码:

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

public class Main {
    public static void main(String[] args) {
        Stack<Integer> s = new Stack<>();
        Scanner scanner = new Scanner(System.in);
        String str;
        str = scanner.nextLine();

        Integer x1;
        Integer x2;
        Integer result = 0;

        int countNum = 2;
        int countSymbol = 1;


//str.charAt() 方法会读入字符串中的空格,因此索引会包括空格 
        int temp = Character.getNumericValue(str.charAt(0));//将char转换成int型,读入第一个操作数
        s.push(temp);
        temp = Character.getNumericValue(str.charAt(2));//读入第二个操作数
        s.push(temp);


        switch (str.charAt(4)) {//先计算第一个操作符
            case '+': {
                x1 = s.pop();
                x2 = s.pop();
                result = x1 + x2;
                s.push(result);
                break;
            }
            case '-': {
                x1 = s.pop();
                x2 = s.pop();
                result = x2 - x1;
                s.push(result);
                break;
            }
            case '*': {
                x1 = s.pop();
                x2 = s.pop();
                result = x1 * x2;
                s.push(result);
                break;
            }
            case '/': {
                x1 = s.pop();
                x2 = s.pop();
                result = x2 / x1;
                s.push(result);
                break;
            }
        }


        for (int i = 6; i < str.length(); i++) {//从第三个操作数开始
            if(countNum <2 && countNum > 100 && countSymbol > 99 && countSymbol <1)
            {
                return;
            }
                switch (str.charAt(i)) {
                    case '+': {
                        x1 = s.pop();
                        x2 = s.pop();
                        result = x1 + x2;
                        s.push(result);
                        break;
                    }
                    case '-': {
                        x1 = s.pop();
                        x2 = s.pop();
                        result = x2 - x1;
                        s.push(result);
                        break;
                    }
                    case '*': {
                        x1 = s.pop();
                        x2 = s.pop();
                        result = x1 * x2;
                        s.push(result);
                        break;
                    }
                    case '/': {
                        x1 = s.pop();
                        x2 = s.pop();
                        result = x2 / x1;
                        s.push(result);
                        break;
                    }
                    case ' ': {
                        break;
                    }
                    default: {
                        temp = Character.getNumericValue(str.charAt(i));
                        s.push(temp);
                        break;
                    }
                }

        }
        result = s.pop();
        System.out.println(result);
    }
}

 但是不知道为什么我写的代码过不了最后一个测试点,拜请路过的大佬指教

下面的代码是CSDN博主

Z-xina
写的,为了学习方便,我放在这里,仅做学习记录,不做他用,若有冒犯,可随时删除

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

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String s=input.nextLine();
        String[] a=s.split(" ");  //将输入的这行数据以空格分割,并存到数组a中
        Stack<Integer> intstack = new Stack<>();  //用于装数据中的整数
        for (int i=0;i<a.length;i++){
            if (judge(a[i])){   //判断是否为整数,如果是则加到栈中
                intstack.push(Integer.parseInt(a[i]));
            }else {   //如果不是整数,即遍历到运算符则开始运算
                int right=intstack.pop();
                int left=intstack.pop();
                switch (a[i]){
                    case "+": {
                        int sum = right+left;
                        intstack.push(sum);
                        break;
                    }
                    case "-": {
                        int sum = left-right;
                        intstack.push(sum);
                        break;
                    }
                    case "*": {
                        int sum = left*right;
                        intstack.push(sum);
                        break;
                    }
                    case "/": {
                        int sum = left/right;
                        intstack.push(sum);
                        break;
                    }
                }
            }
        }
        System.out.println(intstack.peek());    //输出栈顶元素,即最后的运算结果
    }

    static  boolean judge(String s){    //用于判断所输入的值是否为数字,数字则输出true,否则返回false
        try {
            int num=Integer.valueOf(s);
            return true;
        }catch (Exception e){
            return false;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值