I am not sure if I am doing it right or not. I am trying to convert an infix equation to a postfix equation, for example:
(3 + 4) * 2
in postfix:
4 3 + 2 *
I try to do it all in one way, if possible.
Right now, I am getting an arrayoutofbounds error so that I don't appear in the wrong place or something like that.
The infixtopostfix method is used here.
public void InfixToPostfix(String f) {
Stacked postfix = new Stacked(100);
Stacked op = new Stacked(100);
char c;
String fix = f;
int out = 0;
for (int i = 0; i < fix.length(); i++) {
c = fix.charAt(i);
if (c != '+' || c != '*' || c != '-' || c != '/' || c != '(' || c != ')') {
postfix.push(c);
}
else if (c == '+' || c == '*' || c == '-' || c == '/') {
if (c != ')') {
op.push(c);
} else {
out = (char) op.pop();
s.push(out);
}
out = (char) op.pop();
System.out.println("THE POSTFIX = " + out)
}
}
}
source
share