Help Please! How do i Convert a string to math operator?

Hi. I am trying to write a calculator that takes a string from the user and calculates the expression. For example,

55 * 4 + ( 55 - 30 ) + 7

I can use the string tokenizer and seperate the string into individual parts, then use parsedouble for the numbers, but is there a way to parse the operators?i cant figure this out.

[341 byte] By [bradaleyxa] at [2007-11-27 8:41:09]
# 1

You can use the equals method of the String class.

if(token.equals("+")) {

} else if(token.equals("-")) {

etc

floundera at 2007-7-12 20:39:58 > top of Java-index,Java Essentials,Java Programming...
# 2
I have to use a stack for this. So i used stringtokenizer and put each seperate string in a stack. I need to somehow translate this to an equation and make sure everything done in parens is done first....
bradaleyxa at 2007-7-12 20:39:58 > top of Java-index,Java Essentials,Java Programming...
# 3
> I have to use a stack for this. So i used> stringtokenizer and put each seperate string in a> stack. I need to somehow translate this to an> equation and make sure everything done in parens is> done first....Okay, and...?
jverda at 2007-7-12 20:39:58 > top of Java-index,Java Essentials,Java Programming...
# 4
Have you converted your equation into postfix notation. If not, do a Google search. Evaluating a postfix notation equation is a lot simpler.
floundera at 2007-7-12 20:39:58 > top of Java-index,Java Essentials,Java Programming...
# 5

And im not really sure how to form a math expression from a stack of strings. I can convert each number to a double but im not sure how to convert each string thats a symbol to a symbol.

If i had 2+4 i would have 3 strings in the stack. "2", "+", and "4"

i guess im just lost on how to turn this into an expression. sry.

bradaleyxa at 2007-7-12 20:39:58 > top of Java-index,Java Essentials,Java Programming...
# 6

You dont have to "turn a symbol to a symbol" It already is one. All you need to do is use the code snippet I provided in reply #1.

Lets say you have the expression 4 + 5 * 6.

Convert it to postfix: 5 6 * 4 +

Now to evaluate you iterate over the values.

If you come to an operand (number) push it onto the stack.

If you come to an operator, pop the two top most operands, perform the operation and push result back onto stack.

At the end, pop your result off the stack. It should be the only value left in the stack.

floundera at 2007-7-12 20:39:58 > top of Java-index,Java Essentials,Java Programming...
# 7
Ok i kind of get that but it looks like it will only work in certain situations. what about an expression like... 5 + 6 * 3 * 6 * ( 4 + 3 + 2 ) + 12
bradaleyxa at 2007-7-12 20:39:58 > top of Java-index,Java Essentials,Java Programming...
# 8
> what about an expression like... Convert it to postfix notation.~
yawmarka at 2007-7-12 20:39:58 > top of Java-index,Java Essentials,Java Programming...
# 9
> what about an expression like... 5 + 6 * 3 * 6 * ( 4 + 3 + 2 ) + 125 6 3 * 6 * 4 3 + 2 + * + 12 +In High School, I had an HP calculator. I love lending it to unsuspecting people: "where is the = key?"
BigDaddyLoveHandlesa at 2007-7-12 20:39:58 > top of Java-index,Java Essentials,Java Programming...
# 10

Ok so i searched google to find out how to convert it to postfix notation, and i cant really find a clear example. Is there a simple way. I saw a 300+ line code that does it. I am not sure if i will be able to use that.

I see this somewhat easy example and it looks like i can just add division and subtraction.Im usually good with my programs and have gotten 100% on all so far... i feel like a deer in headlights... haha

Also, How would this deal with parentheses?

public class Postfix {

public static void main(String[] args) {

Stack<Integer> stack = new Stack<Integer>();

while (!StdIn.isEmpty()) {

String s = StdIn.readString();

if(s.equals("+")) stack.push(stack.pop() + stack.pop());

else if (s.equals("*")) stack.push(stack.pop() * stack.pop());

else stack.push(Integer.parseInt(s));

}

System.out.println(stack.pop());

}

}

bradaleyxa at 2007-7-12 20:39:58 > top of Java-index,Java Essentials,Java Programming...
# 11
> Also, How would this deal with parentheses?It wouldn't. Have a look at this: http://en.wikipedia.org/wiki/Shunting_yard_algorithm~
yawmarka at 2007-7-12 20:39:58 > top of Java-index,Java Essentials,Java Programming...
# 12
Aaahh the above code will evaluate it using a stack once it is in postfix notation.... hhmm.... now to figure out how to convert it into postfix notation......
bradaleyxa at 2007-7-12 20:39:58 > top of Java-index,Java Essentials,Java Programming...
# 13
> Aaahh the above code will evaluate it using a stack> once it is in postfix notation.... hhmm.... now to> figure out how to convert it into postfix> notation......Reply #11.~
yawmarka at 2007-7-12 20:39:58 > top of Java-index,Java Essentials,Java Programming...
# 14

> Ok i kind of get that but it looks like it will only

> work in certain situations. what about an expression

> like... 5 + 6 * 3 * 6 * ( 4 + 3 + 2 ) + 12

It's a recursive parsing problem and you have to keep in mind the orders of operation and work from inside to outside.

1. Find the inner most matching parenthesis.

2. Resolve the expression

3. Replace the expression with the resolution of the expression

4. Repeat until you have no enclosing parenthesis

5. Resolve multiplicative terms (* or /)

6. Replace multiplicative terms with resolution of the term

7. Repeat until all multiplicative terms have been removed

8. Resolve additive terms (+ or -)

9. Replace additive terms with the resolution of the term

10. Repeat until no more additive terms remain

11. Should be done at this point.

This may have holes in it, but it's a first pass at how I would approach it, and it is VERY rudimentary with a lot of needed refactoring.

PS.

puckstopper31a at 2007-7-12 20:39:58 > top of Java-index,Java Essentials,Java Programming...
# 15

Thanks a lot for the link and your help. One more thing. I am taking the input as a string. So The main thing is, i can use parseDouble and change the numbers to numbers and send them to the output. What would i do to change the "*" to a * symbol?

Something like

if(str.nextToken() == "*"){

stack.push(*); // This wont work, erro missing term.

}

? So do i push a string "*" onto the stack?... lame final project......

bradaleyxa at 2007-7-21 22:45:32 > top of Java-index,Java Essentials,Java Programming...
# 16
> What would i do to change the "*" to a * symbol?There's nothing to change. It's already a symbol. I think this was covered earlier in the thread...~
yawmarka at 2007-7-21 22:45:32 > top of Java-index,Java Essentials,Java Programming...
# 17

One more thing... the link to http://en.wikipedia.org/wiki/Shunting_yard_algorithm

tells me to add the numbers to an output queue. what format would this be? since i use a stack for the operators , which way will i store the output?

Ok cool. Thanks a lot guys. I am gonna try to work on this some more and see if i can figure it out. Hopefully i will be able to. I appriciate all the help and responses from you all. wish me luck!!

bradaleyxa at 2007-7-21 22:45:32 > top of Java-index,Java Essentials,Java Programming...
# 18
> str.nextToken() == "*"Tip: don't use == to compare objects, for example to compare strings. Use equals:if (str.nextToken().equals("*"))
BigDaddyLoveHandlesa at 2007-7-21 22:45:32 > top of Java-index,Java Essentials,Java Programming...
# 19
> which way will i store the output?In a queue. First, get familiar with what a queue is: http://en.wikipedia.org/wiki/Queue_%28data_structure%29Then, you can use an implementation of the Queue interface in Java; e.g.: java.util.LinkedList.~
yawmarka at 2007-7-21 22:45:32 > top of Java-index,Java Essentials,Java Programming...
# 20
Don't you just love it when all the help you provide is ignored and others have to come along and repeat it?
floundera at 2007-7-21 22:45:32 > top of Java-index,Java Essentials,Java Programming...
# 21
BTW I found this article has a very good explanation of converting from infix to postfix. http://scriptasylum.com/tutorials/infix_postfix/algorithms/infix-postfix/index.htm
floundera at 2007-7-21 22:45:32 > top of Java-index,Java Essentials,Java Programming...
# 22

Thanks again guys.

Flounder i didnt ignore what you said i actually appriciate it very much. Just a little confused when i googled it and couldnt find the best way... Thanks for your help and ill check ur new link as well.... just got done golfing so lets see if i can knock this parsing calculator out before its due at midnight :) .. thanks a bunch guys

bradaleyxa at 2007-7-21 22:45:32 > top of Java-index,Java Essentials,Java Programming...
# 23
> just got done golfingGlad to see you have your priorities in order.
floundera at 2007-7-21 22:45:32 > top of Java-index,Java Essentials,Java Programming...
# 24

>Glad to see you have your priorities in order.

hehe. Cant work all the time you know. gotta have some fun or whats the point in living. but thanks again. finished the assignment. dont know how clean my convertToPostfix method is but it should be good enough for full credit. This is just java27C (third class in my first java series). really appriciate all the help.

Time to go hit some balls :)

bradaleyxa at 2007-7-21 22:45:32 > top of Java-index,Java Essentials,Java Programming...
# 25

> >Glad to see you have your priorities in order.

>

> hehe. Cant work all the time you know. gotta have

> some fun or whats the point in living.

Agreed.

For what it's worth, you do appear to be making a genuine effort here. But I also understand why certain respondents feel their comments were ignored. It may be that you were overwhelmed, or didn't understand a given comment until somebody else repeated the same basic idea in different terms. That's fine, but in the future, please provide feedback as to what you didn't understand, disagreed with, etc.

jverda at 2007-7-21 22:45:32 > top of Java-index,Java Essentials,Java Programming...