我试图在不使用java的内置堆栈的情况下这样做,所以我创建了一个带有函数的类(如下所示)。我无法弄清楚我的生活中我的逻辑错误导致了错误。
package ass5;
import java.util.*;
public class ass5main{
public static void main(String[] args) {
ass5methods pfix = new ass5methods();
HashMap<Character, Integer> compare = new HashMap<Character, Integer>();
compare.put('-',1);
compare.put('+',1);
compare.put('*',2);
compare.put('/',2);
compare.put('(', 3);
compare.put('{', 3);
HashMap<Integer, String> errorMsg = new HashMap<Integer, String>();
errorMsg.put(0, "bracket does not match");
errorMsg.put(1, "parethenses do not match");
errorMsg.put(2, "order of brackets and parentheses is mixed");
errorMsg.put(3, "please hardcode an input");
String[] Statement = new String[5];
Statement[0] = "(1-1)";
Statement[1] = "(2+3)";
Statement[2] = "(3x9) {2+4}";
Statement[3] = "(2(3*6)}";
Statement[4] = "{34(26)+10-3}";
for (Integer i = 0; i <= 4; i++) {
String postt = "";
if(Statement[i]!=null) {
String expression = Statement[i];
boolean correct = true;
System.out.println("expression" + (i +1));
for (Integer j = 0; j < expression.length() && correct ; j++) {
char c = expression.charAt(j);
if (c >= '0' && c <= '9') {
postt +=c;
}
if (c == '+' || c == '-' || c == '*' || c == '/' ) {
if (pfix.stackTop == -1 || compare.get(pfix.read()) <= compare.get(c)) {
pfix.push(c);
}
else if(pfix.stackTop > -1 && compare.get(pfix.read()) >= compare.get(c)){
postt += pfix.pop();
pfix.push(c);
} }
else if (c == '(' || c == '{' ) {
pfix.push(c);
}
if(c == ')') {
//This while loop is where eclipse says the error is
while(!((pfix.topOfStack()) == '(')) {
postt += pfix.pop();
}
pfix.pop();
}
else if(c == '}') {
while(!(pfix.read() == '{')) {
postt += pfix.pop();
}
pfix.pop();
}
}
}
System.out.println(postt);
}
}
}
package ass5;
import java.util.*;
public class ass5methods {
int stackTop;
public ArrayList <Character> stack2;
public ass5methods() {
stackTop = -1;
stack2 = new ArrayList <Character>();
}
public void push(Character c) {
stackTop++;
stack2.add(c);
}
public char pop() {
char c3 = stack2.remove(stackTop);
stackTop--;
return c3;
}
public char topOfStack() {
char c = stack2.get(stackTop);
return c;
}
public char read() {
char c = stack2.get(stackTop);
return c;
}
public boolean isEmpty() {
if (stackTop<0) {
return true;
}
else {
return false;
}
}
}
public static void main(String[] args) {
Stack<Character> operators = new Stack();
HashMap<Character, Integer> compare = new HashMap<Character, Integer>();
compare.put('-',1);
compare.put('+',1);
compare.put('*',2);
compare.put('/',2);
compare.put('(', 3);
compare.put('{', 3);
HashMap<Integer, String> errorMsg = new HashMap<Integer, String>();
errorMsg.put(0, "bracket does not match");
errorMsg.put(1, "parathenses does not match");
errorMsg.put(2, "order of brackets and paranthese is mixed");
errorMsg.put(3, "please hardcode an input");
String[] Statement = new String[5];
Statement[0] = "(1-1)";
Statement[1] = "(2+3)";
Statement[2] = "(3*9) {2+4}";
Statement[3] = "(2*(3*6)}";
Statement[4] = "{34*(26)+10-3}";
for (Integer i = 0; i <= 4; i++) {
String postt = "";
if(Statement[i]!=null) {
String expression = Statement[i];
boolean correct = true;
System.out.println("expression" + (i +1));
for (Integer j = 0; j < expression.length() && correct ; j++) {
char c = expression.charAt(j);
if (c >= '0' && c <= '9') {
postt +=c;
}
if (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == '{' ) {
operators.push(c);
}
if(c == ')' || c == '}') {
while(!operators.isEmpty()) {
char op = operators.pop();
if(op == '(' || op == '{' ) {
break;
}
postt += op;
}
}
}
}
System.out.println(postt);
}
}
这不是一个完整的解决方案,我改为打印表达式的后缀。我使用了Stack lib类。你可以更新你的选择。刚刚在您的实施更新。这将有助于您更好地发展。可以用这种方式可以试试问题。