|
-
Jul 5th, 2002, 11:14 PM
#1
Thread Starter
Junior Member
Arithematic operations
I want to evaluate a user entered string like the one below but how do I do that? I am not understanding how do I perform the arithematic operations maintaining the operators precedence.
Here's the problem :
34*45+56/(54*12)-25
some sort of like this.
-
Jul 6th, 2002, 08:01 AM
#2
Monday Morning Lunatic
Look up (on google) converting between postfix and infix expressions.
Although most of the time I'd just get the user to do that and make them put it in in the form:
34 45 * 56 54 12 * / + 25 -
(that *should* produce the right output, I can't test it since I've still got to recover the code for my stack evaluator).
Basically, you have everything on a stack, so you push 34 and 45 on, then use the * operator to replace those with (34*45). And so on. It's a lot easier for the computer to process because there's no way for it to be ambiguous.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jul 8th, 2002, 03:58 AM
#3
Thread Starter
Junior Member
Is there any other way apart from using stacks for it ?
-
Jul 8th, 2002, 05:05 AM
#4
transcendental analytic
probably but stacks would be most efficient, and easiest for that matter
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|