gpt4 book ai didi

java - PostFix 评估结果错误

转载 作者:行者123 更新时间:2023-11-30 08:13:57 25 4
gpt4 key购买 nike

public static int postFixEvaluation(int[] numValues, String postfix){
Stack<Integer> evaluateStack = new Stack<Integer>();
char[] chars = postfix.toCharArray();
int length = chars.length;
int currentNumValue =0;
int currentLocation =0;
for(int i = 0; i < length; i++){
char currentChar = chars[i];
if(Character.isLetter(currentChar))//checks to see if character is a letter
{
//replace all letters with values
currentLocation = charToNum(currentChar);//this retrieves the location of specific letter
currentNumValue= (numValues[currentLocation]);//retrieves the value of that location
evaluateStack.push(currentNumValue);//get the number value of that variable and push it on stack
System.out.println(Arrays.toString(evaluateStack.toArray()));//prints out stack elements

}

else if(isOperator(currentChar)){//checks if character is an operator
switch(currentChar){//switches evaluation according to operator
case '+': evaluateStack.push(evaluateStack.pop() + evaluateStack.pop()); break;
case '*': evaluateStack.push(evaluateStack.pop() * evaluateStack.pop()); break;
case '-': evaluateStack.push(evaluateStack.pop() - evaluateStack.pop()); break;
case '/': evaluateStack.push(evaluateStack.pop() / evaluateStack.pop()); break;
}
}

}
if (!evaluateStack.isEmpty()) //as long as the stack is not empty
return evaluateStack.pop();//returns the result
else
return 0;//if it is empty returns zero
}

输入:

C = -13
X = 5
H = 25
D = 4
$PART2
C-(B+(C+(A-E)))-X
(D+C)-(H*X-C)
(A-H)/(D+B)
A*H+C Infix: C-(B+(C+(A-E)))-X
Postfix: CBCAE-++-X-
[-13]
[-13, 2]
[-13, 2, -13]
[-13, 2, -13, 1]
[-13, 2, -13, 1, 5]
[6, 5]
Result: -1**Result should be: -3**

Infix: (D+C)-(H*X-C)
Postfix: DC+HX*C--
[4]
[4, -13]
[-9, 25]
[-9, 25, 5]
[-9, 125, -13]
Result: -129 **Result should be: -147**

Infix: (A-H)/(D+B)
Postfix: AH-DB+/
[1]
[1, 25]
[24, 4]
[24, 4, 2]
Result: 0**Result should be: -4**


Infix: A*H+C
Postfix: AH*C+
[1]
[1, 25]
[25, -13]
Result: 12**Correct Result**

我不明白为什么我的 postEvaluation 不会产生正确的输出。如果有人可以请帮忙!我在我的代码中进行了大量评论,所以如果我能澄清任何事情,请告诉我。谢谢!

最佳答案

我手边没有电脑,只有旧式的纸笔、手机和一些关于调车场算法的知识。

我认为你的错误是这样的(只在纸上测试过):

您不是在评估 A-E,而是在进行 E-A。其他运营商也是如此。

错误:

evaluateStack.push(evaluateStack.pop() + evaluateStack.pop());

正确:

//Don't change pop order here!
int righthand = evaluateStack.pop();
int lefthand = evaluateStack.pop();

evaluateStack.push(lefthand + righthand);

这是我更正的内容:

public static int postFixEvaluation(int[] numValues, String postfix){
Stack<Integer> evaluateStack = new Stack<Integer>();
char[] chars = postfix.toCharArray();
int length = chars.length;
int currentNumValue =0;
int currentLocation =0;

for (int i = 0; i < length; i++){
char currentChar = chars[i];

//checks to see if character is a letter
if (Character.isLetter(currentChar)){
//replace all letters with values
currentLocation = charToNum(currentChar);//this retrieves the location of specific letter
currentNumValue = (numValues[currentLocation]);//retrieves the value of that location
evaluateStack.push(currentNumValue);//get the number value of that variable and push it on stack
System.out.println(Arrays.toString(evaluateStack.toArray()));//prints out stack elements
}


//checks if character is an operator
if (isOperator(currentChar)){
int righthand = evaluateStack.pop();
int lefthand = evaluateStack.pop();
switch (currentChar){
//switches evaluation according to operator
case '+': evaluateStack.push(lefthand + righthand); break;
case '*': evaluateStack.push(lefthand * righthand); break;
case '-': evaluateStack.push(lefthand - righthand); break;
case '/': evaluateStack.push(lefthand / righthand); break;
}
}
}
if (!evaluateStack.isEmpty()){ //as long as the stack is not empty
return evaluateStack.pop();//returns the result
} else {
return 0; //if it is empty returns zero
}
}

一些额外的提示:您应该已经添加了您的辅助函数以及代码在算法方面对您的问题的作用。这可能会吸引更多人来回答您的问题。

关于java - PostFix 评估结果错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29759203/

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