1 2 Previous Next 25 Replies Latest reply on Jun 26, 2007 10:45 PM by 796440

# 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.
• ###### 1. Re: Help Please! How do i Convert a string to math operator?
You can use the equals method of the String class.
``````if(token.equals("+")) {

} else if(token.equals("-")) {
etc``````
• ###### 2. Re: Help Please! How do i Convert a string to math operator?
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....
• ###### 3. Re: Help Please! How do i Convert a string to math operator?
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...?
• ###### 4. Re: Help Please! How do i Convert a string to math operator?
Have you converted your equation into postfix notation. If not, do a Google search. Evaluating a postfix notation equation is a lot simpler.
• ###### 5. Re: Help Please! How do i Convert a string to math operator?
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.
• ###### 6. Re: Help Please! How do i Convert a string to math operator?
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.
• ###### 7. Re: Help Please! How do i Convert a string to math operator?
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
• ###### 8. Re: Help Please! How do i Convert a string to math operator?
> what about an expression like...

Convert it to postfix notation.

~
• ###### 9. Re: Help Please! How do i Convert a string to math operator?
what about an expression like... 5 + 6 * 3 * 6 * ( 4 + 3 + 2 ) + 12
5 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?"
• ###### 10. Re: Help Please! How do i Convert a string to math operator?
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());
}
}
• ###### 11. Re: Help Please! How do i Convert a string to math operator?
> Also, How would this deal with parentheses?

It wouldn't. Have a look at this:

http://en.wikipedia.org/wiki/Shunting_yard_algorithm

~
• ###### 12. Re: Help Please! How do i Convert a string to math operator?
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......
• ###### 13. Re: Help Please! How do i Convert a string to math operator?
> 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......

~
• ###### 14. Re: Help Please! How do i Convert a string to math operator?
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.
1 2 Previous Next