Infix problem

hi there,

I'm working on some little projects to get me back into the swing of Java programming, and I've written a piece of code to perform infix calculations using Reverse Polish Notation. However there seems to be a bug in my code, and I am jiggered if I can find it.

I was wondering if any of you could see something I couldn't? And if so, please point it out to me.

Many thanks.

The code is as follows:

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

/**

* Class - Infix

*

* Written to work out infix equations using reverse

* polish notations.

*

* @author Emyr Williams

*

*/

public class Infix

{

// we'll have two stacks

SimpleStack infixStack = new SimpleStack(20);

String input;

public Infix(String s)

{

input = s;

}

public Infix()

{}

public int parseSum()

{

char ch;

int j;

int num1, num2, answerInt;

for(j = 0; j < input.length(); j++)

{

ch = input.charAt(j);

infixStack.displayStack(""+ch+" ");

if(ch >= '0' && ch <= '9') // if it's a number

infixStack.push((int)(ch - '0'));

else // it's an operator

{

num2 = infixStack.pop();

num1 = infixStack.pop();

switch(ch)

{

case '+':

answerInt = num1 + num2;

break;

case '-':

answerInt = num1 - num2;

break;

case '*':

answerInt = num1 * num2;

break;

case '/':

answerInt = num1 / num2;

break;

default:

answerInt = 0;

}

infixStack.push(answerInt);

}

}

answerInt = infixStack.pop();

return answerInt;

}

/*

* A method to read input from the keyboard

*/

public void getUserInput() throws IOException

{

String s = null;

BufferedReader userInput = new BufferedReader(

new InputStreamReader(System.in));

System.out.println("Please enter infix equation (In RPN) : ");

try

{

s = userInput.readLine().trim();

input = s;

}

catch(IOException e)

{

e.printStackTrace();

}

}

public static void main(String args[]) throws Exception

{

int output;

Infix fix1 = new Infix();

fix1.getUserInput();

output = fix1.parseSum();

System.out.println("Evaluates to: " + output);

}

}

[2566 byte] By [Emyra] at [2007-10-3 5:19:54]
# 1

Sorry, I forgot to describe the problem,

When I tell the program to add 1 + 1 say,

the out put is something like this

Please enter infix equation (In RPN) :

1+1

1

Stack (bottom > top:)

+

Stack (bottom > top:)

1

1

Stack (bottom > top:)

1

2

Evaluates to: 1

Now I'm not too sure why it's doing that, I've written my own stack as well and that has the standard methods in it. for the display stack method the code is as follows:

public void displayStack(String s)

{

System.out.println(s);

System.out.println("Stack (bottom > top:) ");

for(int j = 0; j < size(); j++)

{

System.out.println(peekN(j));

System.out.println(' ');

}

System.out.println("");

}

any help would be greatly appreciated

egw9 :)

Emyra at 2007-7-14 23:26:50 > top of Java-index,Java Essentials,Java Programming...
# 2

Hi, as there is no SimpleStack in your post.i just tried with ordinary java.util.Stack

Also remember you are checking character by character. so your program will produce correct output for single digit values only.

You just try this modified code:

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class Infix{

java.util.Stack infixStack=new java.util.Stack();

String input;

public Infix(String s){

input = s;

}

public Infix()

{}

public int parseSum(){

char ch;

int j;

int num1, num2, answerInt=0;

for(j = 0; j < input.length(); j++){

ch = input.charAt(j);

if(ch >= '0' && ch <= '9'){

System.out.println("ch="+ch); // if it's a number

infixStack.push(String.valueOf(ch));

}else{

num2 = Integer.parseInt((String)infixStack.pop());

num1 = infixStack.size()>0?Integer.parseInt((String)infixStack.pop()):0;

switch(ch)

{

case '+':

answerInt = num1 + num2;

break;

case '-':

answerInt = Math.abs(num1 - num2);

break;

case '*':

answerInt = num1 * num2;

break;

case '/':

answerInt = num1 / num2;

break;

default:

answerInt = 0;

}

infixStack.push(String.valueOf(new Integer(answerInt)));

}

}

answerInt =Integer.parseInt((String)infixStack.pop());

return answerInt;

}

public void getUserInput() throws IOException{

String s = null;

BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Please enter infix equation (In RPN) : ");

try{

s = userInput.readLine().trim();

input = s;

}

catch(IOException e){

e.printStackTrace();

}

}

public static void main(String args[]) throws Exception{

int output;

Infix fix1 = new Infix();

fix1.getUserInput();

output = fix1.parseSum();

System.out.println("Evaluates to: " + output);

}

}

pravintha at 2007-7-14 23:26:50 > top of Java-index,Java Essentials,Java Programming...