gpt4 book ai didi

java - 使用堆栈的后缀评估。

转载 作者:行者123 更新时间:2023-11-29 10:20:18 24 4
gpt4 key购买 nike

我要疯了..我已经很接近让这段代码按照我想要的方式工作了,我就是想不通。我正在尝试求解 ex 的后缀方程。 3 2 + ,这等于 5。当我举个例子mainmethod 中的“3 2 +”它工作正常,但是一旦我输入第 3 个数字,如“3 2 + 2 *”(等于 10),我就会得到一个与 number2 = s.pop() 相关的 arrayoutofboundserror请参阅下面的代码。任何帮助是极大的赞赏。

这是后缀方法:

   public int PostfixEvaluate(String e){
int number1;
int number2;
int result=0;

String[] tokens = e.split(" ");

for(int j = 0; j < tokens.length; j++){
String token = tokens[j];
if (!"+".equals(token) && !"*".equals(token) && !"-".equals(token) && !"/".equals(token)) {
s.push(Integer.parseInt(token));

} else {
String Operator = tokens[j];
number1 = s.pop();
number2 = s.pop();
if (Operator.equals("/")){
result = number1 / number2;}
else if(Operator.equals("*")){
result = number1 * number2;}
else if(Operator.equals("+")){
result = number1 + number2;}
else if(Operator.equals("-")){
result = number1 - number2;}
else System.out.println("Illeagal symbol");
}
s.push(result);

s.pop();
}


//s.pop();
System.out.println("Postfix Evauation = " + result);

return result;
}

public static void main(String[] args) {
Stacked st = new Stacked(100);
//String y = new String("((z * j)/(b * 8) ^2");
String x = new String("2 2 2 * +");
TestingClass clas = new TestingClass(st);

//clas.test(y);
clas.PostfixEvaluate(x);

}

最佳答案

/**
* Evaluate postfix arithmetic expression
*
* @example "1 12 23 + * 4 5 / -" => 34.2
* @author Yong Su
*/
import java.util.Stack;

class PostfixEvaluation {

public static void main(String[] args) {
String postfix = "1 12 23 + * 4 5 / -";
Double value = evaluate(postfix);
System.out.println(value);
}

/**
* Evaluate postfix expression
*
* @param postfix The postfix expression
*/
public static Double evaluate(String postfix) {
// Use a stack to track all the numbers and temporary results
Stack<Double> s = new Stack<Double>();

// Convert expression to char array
char[] chars = postfix.toCharArray();

// Cache the length of expression
int N = chars.length;

for (int i = 0; i < N; i++) {
char ch = chars[i];

if (isOperator(ch)) {
// Operator, simply pop out two numbers from stack and perfom operation
// Notice the order of operands
switch (ch) {
case '+': s.push(s.pop() + s.pop()); break;
case '*': s.push(s.pop() * s.pop()); break;
case '-': s.push(-s.pop() + s.pop()); break;
case '/': s.push(1 / s.pop() * s.pop()); break;
}
} else if(Character.isDigit(ch)) {
// Number, push to the stack
s.push(0.0);
while (Character.isDigit(chars[i]))
s.push(10.0 * s.pop() + (chars[i++] - '0'));
}
}

// The final result should be located in the bottom of stack
// Otherwise return 0.0
if (!s.isEmpty())
return s.pop();
else
return 0.0;
}

/**
* Check if the character is an operator
*/
private static boolean isOperator(char ch) {
return ch == '*' || ch == '/' || ch == '+' || ch == '-';
}
}

关于java - 使用堆栈的后缀评估。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7450815/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com