问题描述
我正在编写波兰语符号的评估代码。当我克服 ClassCastException 时,我得到 EmptyStack,当我解决 Emptystack 时,我得到 ClassCast 我在一个循环中。 以下是我想要评估波兰语符号的方式:
首先我从用户那里得到一个字符串,然后把它放到一个堆栈中。为了评估,我使用了第二个堆栈。为什么? 因为: 一个示例字符串:“* + * + 1 2 + 3 4 5 6” 这里的第一个操作是将 3 和 4 相加,但是我要用 5 和 6 做什么?我把它们放在另一个堆栈中。让我们说stack2。我开始弹出直到找到一个运算符,在这种情况下,我将 6 5 4 3 推入 stack2 并找到了一个加号运算符。然后我弹出 2 个我推送到 stack2 的数字(前两个数字是 3 和 4),将它们相加,然后再次将它们推送到堆栈。 我应该评估,但似乎我错过了一点,或者起初这不是一个好主意。 这是我的代码:
public class Main {
public static void stack(String srt){ // this function puts the string in srt stack
String arr[] = srt.split(" "); // I am recognizing if there is a 2 or more digit number
int size= arr.length; // with receiving prefix with " " btw every number and operator
Stack stack= new Stack();
for (String s : arr) {
// System.out.println(s);
stack.push(s);
}
// for (int i = 0; i < size; i++) {
// System.out.println(stack.pop()); // I checked to see if any
// problems first Now I dont start this
// }
evaluate(stack); // then calls the evaluate function
}
public static void evaluate(Stack stack){
Stack stack2= new Stack();
stack2.push(stack.pop());// cuz of there cant be an operator there I pop first 2 number
stack2.push(stack.pop());
for (int i = 0; i < (stack.capacity()*2); i++) { // I had a hard time calculating how many times
// I should cycle through this for and leave it 2 times as stack capacity
if(stack.peek().toString().equals("*") || stack.peek().toString().equals("-") || stack.peek().toString().equals("/") || stack.peek().toString().equals("+")){
System.out.println("Checkpoint1");
// System.out.println(stack2.peek());
int s2= Integer.parseInt((String) stack2.pop());
int s1= Integer.parseInt((String) stack2.pop());
double s3;
String c = (String) stack.pop();
switch (c) {
case "+":
s3=s1+s2;
stack2.push(s3);
case "-":
s3=s1-s2;
stack2.push(s3);
case "*":
s3=s1*s2;
stack2.push(s3);
case "/":
s3=s1/s2;
stack2.push(s3);
}
}else{
System.out.println("Checkpoint 2");
stack2.push(Integer.parseInt((String) stack.pop()));
// System.out.println(stack.peek());
}
}
// System.out.println(stack.peek());
// System.out.println(stack2.peek());
}
public static void main(String[] args) {
String examplestr ="* + * + 1 2 + 3 4 5 6";
stack(examplestr);
}
}
感谢您的帮助!!!
解决方法
结果是 switch-case 没有正确运行,因为我忘记了一个基本部分“break
”。对于 EmptyStackException,我做了一个 try-catch 并将它们放入其中,因此当它返回空堆栈时,我知道评估已完成并打印结果。但我仍然不知道如何处理这些小问题。感觉好像我没有正确解决它们。还得加班。谢谢。
编辑:我做了一些更多的调整,这是我最终的工作代码。我还没有在结果中检测到错误。
这是工作代码;
import java.util.EmptyStackException;
import java.util.Stack;
public class Main2 {
public static void stack(String srt) {
String arr[] = srt.split(" ");
int size = arr.length;
Stack stack = new Stack();
for (int i = 0; i < size; i++) {
if (arr[i].toString().equals("*") || arr[i].toString().equals("-") || arr[i].toString().equals("/") || arr[i].toString().equals("+"))
stack.push(arr[i]);
else
stack.push(Double.parseDouble(arr[i]));
}
// for (int i = 0; i < size; i++) {
// System.out.println(stack.pop());
// }
evaluate(stack);
}
public static void evaluate(Stack stack) {
Stack stack1 = new Stack();
stack1.push(stack.pop());
stack1.push(stack.pop());
for (int i = 0; i < stack1.capacity(); i++) {
try {
if (stack.peek().toString().equals("*") || stack.peek().toString().equals("-") || stack.peek().toString().equals("/") || stack.peek().toString().equals("+")) {
// System.out.println("Checkpoint1");
double s1 = (double) stack1.pop();
double s2 = (double) stack1.pop();
double s3;
String operator = String.valueOf(stack.pop());
switch (operator) {
case "+":
s3 = s1 + s2;
stack1.push(s3);
break;
case "-":
s3 = s1 - s2;
stack1.push(s3);
break;
case "*":
s3 = s1 * s2;
stack1.push(s3);
break;
case "/":
s3 = s1 / s2;
stack1.push(s3);
break;
}
} else {
// System.out.println("Checkpoint 2");
stack1.push(Double.parseDouble(String.valueOf(stack.pop())));
}
} catch (EmptyStackException e) {
System.out.println("Notasyonun sonucu: " + stack1.peek());
}
}
}
public static void main(String[] args) {
String notasyon = scanner.nextLine();
stack(notasyon);
}
}