Results 1 to 4 of 4

Thread: Arithematic operations

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2002
    Posts
    16

    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.

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2002
    Posts
    16
    Is there any other way apart from using stacks for it ?

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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
  •  



Click Here to Expand Forum to Full Width