Java calculator implementation
am basically attempting to create a calculator using a class hierarchy - the basic function will be:
Convert the String representation into a list of Tokens
Convert the List of Tokens into Polish Notation
Calculate the result on the basis of Polish Notation
I also need to represent objects -
numbers operators and brackets.
Following this i need to convert to polish notation - using a shunting yard algorithm and then compute the answer.
I am ok with understanding the shunting yard but am unsure as to the class hierarchy part.
The precedence is - Addition and Subtraction have a precedence of 2, and Multiplication and Division have a precedence of 3.
I am assuming i need a filereader class and also classes to handle the different objects.
I also need help in creating my own stacks and queues.
Thanks
Re: Java calculator implementation
There are two phases: Conversion from an Infix String to an RPN expression, and evaluation of the RPN expression. There is no theoretical reason why you couldn't do it all-in-one, but by having the intermediate RPN expression available, it would probably lead to more systematic debugging.
For starters:
For conversion to RPN, I would probably use a Stack<Character> object for the operator stack and a StringBuilder object for the output expression. Operator characters and operand numbers will be converted to Strings when you append them to the output expression.
The conversion would use Stack methods push(), pop() and peek() and empty() and the StringBuilder append() method.
For evaluation I would probably use a Stack<Double> object for the operand stack. Split the StringBuilder output expression into an array of Strings for processing, then step through the array. You will need Stack methods push(), pop() and empty(). You can index through the RPN array and see if the next item is a String corresponding to one of the operands.
As you get into implementation details (on paper, before writing code) you may decide that certain functions or data structures can help keep it clean. A hashmap for operators' precedence might be handy (and cleaner) instead of hard-coding procedural precedence comparisons in the RPN conversion process, for example.
Cheers!
Z
Re: Java calculator implementation
Hi thanks for the help.
How would I implement my own stack without using the java utils?
also what sort of hierarchy should i use? I was thinking maybe a filereader class and then one to deal with the objects and then a main to tie it all together?
thanks again
Re: Java calculator implementation
so my thinking now is to have the following classes:
stack class
main - filereading, dealing with the numbers+operands
shunting yard class
Is this an acceptable sort of hierarchy?
thanks
Re: Java calculator implementation
Quote:
Originally Posted by
why_always_me
so my thinking now is to have the following classes:
stack class
main - filereading, dealing with the numbers+operands
shunting yard class
Is this an acceptable sort of hierarchy?
thanks
Hi Guys my new thinking is :
List class
Node class
Shunting yard class
Main class
Main includes the reverse Polich notation, filereader and tokenizer.
My question now is how would i link main to the shunting yard? I was told I should create a list of objects which will be numbers, operands and brackets. Thanks again
Re: Java calculator implementation
you can create instances of you class in your main. then you can use those instances to perform whatever you need to.
i.e.
...main()
{
ShuntingYard var1;
.....
}