[英]Stack problem java. Postfix Evaluation
嗨,我在运行程序时遇到问题。 在PostfixEvaluate()方法中,它接收字符串并解决postfix问题并返回它。 好吧,当我去运行它时,我得到了一堆随机数(有些重复),我快要疯了,因为我不知道还能尝试什么,而我为此花了更多的时间。采取。
这是PostfixEvaluate方法:
public int PostfixEvaluate(String e){
//String Operator = "";
int number1;
int number2;
int result=0;
char c;
//number1 = 0;
//number2 = 0;
for(int j = 0; j < e.length(); j++){
c = e.charAt(j);
if (c != '+'&& c!= '*' && c!= '-' && c!= '/') {
//if (c == Integer.parseInt(e)) {
s.push(c);
}
else {
number1 = s.pop();
number2 = s.pop();
switch(c) {
case '+':
result = number1 + number2;
break;
case '-':
result = number1 - number2;
break;
case '*':
result = number1 * number2;
break;
case '/':
result = number1 / number2;
break;
} s.push(result);
}
System.out.println(result);
}
return s.pop();
}
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 3 + 9 *");
TestingClass clas = new TestingClass(st);
clas.test(y);
clas.PostfixEvaluate(x);
}
}
这是堆栈类:
public class Stacked {
int top;
char stack[];
int maxLen;
public Stacked(int max) {
top = -1;
maxLen = max;
stack = new char[maxLen];
}
public void push(int result) {
top++;
stack[top] = (char)result;
}
public int pop() {
int x;
x = stack[top];
//top = top - 1;
top--;
return x;
}
public boolean isStackEmpty() {
if(top == -1) {
System.out.println("Stack is empty " + "Equation Good");
return true;
}
else
System.out.println("Equation is No good");
return false;
}
public void reset() {
top = -1;
}
public void showStack() {
System.out.println(" ");
System.out.println("Stack Contents...");
for(int j = top; j > -1; j--){
System.out.println(stack[j]);
}
System.out.println(" ");
}
public void showStack0toTop() {
System.out.println(" ");
System.out.println("Stack Contents...");
for(int j=0; j>=top; j++){
System.out.println(stack[j]);
}
System.out.println(" ");
}
}
在我看来,您根本不处理空格。
这意味着当您放置一个空格时,它将在操作过程中将其从堆栈中弹出时,会将字符空间隐式转换为它的ascii值(32)。 同样,您似乎假设所有数字/结果都将是一位数字,并且将char从int强制转换为int,这不是您想要执行的操作,因为那样会将char转换为char的ascii值,即'' -> 32,“ 3”-> 51,依此类推。
如果我是你,我将在PostfixEvaluate中为您的循环执行此操作:
while(!e.equals("")){
string c;
int space = e.indexOf(' ');
if(space!=-1){
c = e.substring(0,space);
e = e.substring(space+2);
} else{
c = e;
e = "";
}
if (!c.equals("+")&& !c.equal("*") && !c.equals("-") && !c.equals("/")) {
//...
}
并更改堆栈以容纳字符串或整数。
问题是您将char
作为int
推送到堆栈上,因此无意中使用了数字的ascii
表示形式,这不是数字的实际值。
不用执行这种复杂的字符移动,而是使用String.split()
将输入字符串标记化。 例:
String[] tokens = e.split(" ");
for(String token:tokens){
if (!"+".equals(token) && !"*".equals(token) && !"-".equals(token) && !"/".equals(token)) {
s.push(Integer.parseInt(token));
} else {
....
}
}
您需要先将字符串拆分为令牌:
/* Splits the expression up into several Strings,
* all of which are either a number or and operator,
* none of which have spaces in them. */
String [] expressionAsTokens = e.split(" ");
然后,您需要确保比较字符串,而不是字符:
//compare strings instead of chars
String token = expressionAsTokens[j];
if (!"+".equals(token) && !"*".equals(token) && !"-".equals(token) && !"/".equals(token)) {
s.push(Integer.parseInt(token));
} else {
//same code as you had before
}
另外,是否有任何理由将所有内容都作为char
数组存储在Stacked
类中? 您的pop()
方法返回并返回整数,但所有内容都存储为char
。
对于此应用程序,所有内容都应存储为整数:
public class Stacked {
int stack[]; // array is of type integer
int top;
int maxLen;
// constructor
public void push() {/*...*/}
public int pop() {/*...*/} //pop returns an int as before
//...
}
最后一点:注意加减数字的顺序。我不记得后缀运算数是左优先还是右优先,但是请确保按正确的顺序获取它们。 就像您现在拥有的那样, 2 3 - 4 *
计算结果为4 * (3 - 2)
而我认为应该是(2 - 3) * 4
。 这与加法和乘法无关,而与减法和除法无关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.