• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Calculator problems  RSS feed

 
Pj Hughes
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have managed to get a calculator program written although it is not working properly at the moment. For this purpose I have written my own stack and array classes.

I have made a file with only one equation in it:
49+62*61-36

and the program output should be - the second line should be in RPN format:
49+62*61-36
[49+62*61-36]
3795

But I am currently getting:
49+62*61-36
[36]
36.0

I don't see why it is only keeping the last token? all code is shown below. Any help will be much appreciated.

My arraylist class


my stack class


the class where the calculations happen.

 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What algorithm are you using to determine how to change to RPN (I presume that means Reverse Polish Notation, which I usually call postfix)? How do you turn 1 + 2 × 3 into 1 2 3 × +?
 
Pj Hughes
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What algorithm are you using to determine how to change to RPN (I presume that means Reverse Polish Notation, which I usually call postfix)? How do you turn 1 + 2 × 3 into 1 2 3 × +?


Hi

yes it is postfix I want the expression in.

I am using the shunting yard algorithm.

 
Pj Hughes
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have managed to get it working for the file with one equation in it.

I am now getting the following error when trying to read in more than one line?

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at uk.ac.shef.dcs.com262.coa10pjh.TestClass.main(TestClass.java:146)


Is there a way to make it do it line by line?

THE CHANGE MADE WAS TO MOVE THE END BRACKET OF THE WHILE STATEMENT TO LINE 101

UPDATED VERSION JUST IN CASE

 
Pj Hughes
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Managed to get the brackets issue resolved by removing them from the hashmap declaration.
 
Pj Hughes
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have also managed to fix the Array out of bounds issue. I forgot to resize the array after increasing the file size.

I am now getting the same answer for each equation? can somebody point out my error? any advice will be much appreciated.

the output is:
49+62*61-36
15.666666666666668
4/64
15.666666666666668
(53+26)
15.666666666666668
0*72
15.666666666666668
21-85+75-85
15.666666666666668
90*76-50+67
15.666666666666668
46*89-15
15.666666666666668
34/83-38
15.666666666666668
20/76/14+92-15
15.666666666666668
5*10/3-1
15.666666666666668

instead of:

49+62*61-36
3795
4/64
0.0625
(53+26)
79
0*72
0
21-85+75-85
-74
90*76-50+67
6857
46*89-15
4079
34/83-38
-37.5903614458
20/76/14+92-15
77.0187969925
5*10/3-1
15.666666666666668
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!