This content has been marked as final.
Show 25 replies

1. Re: Help Please! How do i Convert a string to math operator?
807605 Jun 24, 2007 10:04 PM (in response to 807605)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?
807605 Jun 24, 2007 10:27 PM (in response to 807605)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?
796440 Jun 24, 2007 10:38 PM (in response to 807605)I have to use a stack for this. So i used
Okay, and...?
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.... 
4. Re: Help Please! How do i Convert a string to math operator?
807605 Jun 24, 2007 10:54 PM (in response to 796440)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?
807605 Jun 24, 2007 10:57 PM (in response to 796440)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?
807605 Jun 24, 2007 11:12 PM (in response to 807605)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?
807605 Jun 25, 2007 12:25 PM (in response to 807605)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?
807605 Jun 25, 2007 1:34 PM (in response to 807605)> what about an expression like...
Convert it to postfix notation.
~ 
9. Re: Help Please! How do i Convert a string to math operator?
807605 Jun 25, 2007 1:41 PM (in response to 807605)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?
807605 Jun 25, 2007 2:21 PM (in response to 807605)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?
807605 Jun 25, 2007 2:26 PM (in response to 807605)> 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?
807605 Jun 25, 2007 2:28 PM (in response to 807605)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?
807605 Jun 25, 2007 2:30 PM (in response to 807605)> Aaahh the above code will evaluate it using a stackonce it is in postfix notation.... hhmm.... now to
figure out how to convert it into postfix
notation......
Reply #11.
~ 
14. Re: Help Please! How do i Convert a string to math operator?
807605 Jun 25, 2007 2:31 PM (in response to 807605)Ok i kind of get that but it looks like it will only
It's a recursive parsing problem and you have to keep in mind the orders of operation and work from inside to outside.
work in certain situations. what about an expression
like... 5 + 6 * 3 * 6 * ( 4 + 3 + 2 ) + 12
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.