An additional comments, format your code! The messier it is, the more mistakes you'll make.
Learn to put braces around EVERY block. In
if
,
while
,
case
and
for
blocks it is so easy to get this wrong, if you always brace, even for one line, you'll prevent a headache later.
Avoid putting multiple commands on a single line:
case '+':gotOper(ch, 1); break;
case '+': {
gotOper(ch, 1);
break;
}
Notice the brace at the end of the line beginning the block, not on the next line.
If you use a proper IDE - Netbeans or Eclipse - you can set it to automatically provide the braces. I suggest you do so.
Another good practice is to use JavaDoc comments liberally and to write them BEFORE you write the method. This is good practice as you must write in English - or whatever you prefer - what the method will do before you add any code. After writing the code, then check that the JavaDoc is correct.
Your code, formatted and properly braced:
package itp2;
import java.util.*;
import java.io.*;
class StackX {
private int maxSize;
private char[] stackArray;
private int[] stackArray1;
private int top;
public StackX(int s) {
maxSize = s;
stackArray = new char[maxSize];
top = -1;
}
public void push(char j) {
stackArray[++top] = j;
}
public void push1(int j) {
stackArray1[++top] =j;
}
public char pop() {
return stackArray[top--];
}
public int pop1() {
return stackArray[top--];
}
public char peek() {
return stackArray[top];
}
public boolean isEmpty() {
return (top == -1);
}
public int size() {
return top+1;
}
public char peekN(int n) {
return stackArray[n];
}
}
class InToPost {
private StackX theStack;
private String input;
private String output = "";
public InToPost(String in) {
input = in;
int stackSize = input.length();
theStack = new StackX(stackSize);
}
public String doTrans() {
for(int j=0; j<input.length(); j++) {
char ch = input.charAt(j);
switch(ch) {
case '+': {
gotOper(ch, 1);
break;
}
case '-': {
gotOper(ch, 1);
break;
}
case '*': {
gotOper(ch, 2);
break;
}
case '/': {
gotOper(ch, 2);
break;
}
default : {
output = output + ch;
break;
}
}
}
while( !theStack.isEmpty() )
{
output = output + theStack.pop();
}
return output;
}
public void gotOper(char opThis, int prec1)
{
while (!theStack.isEmpty()) {
char opTop = theStack.pop();
if(1==1 ) {
int prec2;
if (opTop=='+' || opTop=='-') {
prec2 = 1;
} else {
prec2 = 2;
}
if(prec2 < prec1) {
theStack.push(opTop);
break;
} else {
output = output + opTop;
}
}
}
theStack.push(opThis);
}
}
public class ITP2 {
public static void main(String[] args)
throws IOException {
String output;
String input;
while(true) {
System.out.print("Enter infix: ");
System.out.flush();
input = getString();
if( input.equals("") ) {
break;
}
InToPost theTrans = new InToPost(input);
output = theTrans.doTrans();
System.out.println("Evaluated expression: " + (output));
System.out.println("Postfix is " + evaluate(output)) ;
}
}
public static String getString()
throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
class Eval {
private StackX operatorStack ;
private StackX operandStack;
private String output;
Eval () {
}
public int evaluate( String output) {
StringTokenizer s = new StringTokenizer(output);
int value;
String symbol;
while (s.hasMoreTokens()) {
symbol = s.nextToken();
if (Character.isDigit(symbol.charAt(0))) {
Integer operand = new Integer(Integer.parseInt(symbol));
operandStack.push1(operand);
} else {
int op2 = ((Integer)operandStack.pop1()).intValue();
int op1 = ((Integer)operandStack.pop1()).intValue();
int result = 0;
switch(symbol.charAt(0)) {
case '*': {
result = op1 * op2;
break;
}
case '+': {
result = op1 + op2;
break;
}
case '-': {
result = op1 - op2;
break;
}
case '/': {
result = op1 / op2;
break;
}
case '%': {
result = op1 % op2;
break;
}
}
Integer operand = new Integer(result);
operandStack.push1(operand);
}
}
value = ((Integer)operandStack.pop1()).intValue();
return value;
}
}
}