string expressions to integer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mido22trika
    New Member
    • Jun 2010
    • 4

    string expressions to integer

    i want to know how to enter
    expressions like : ((1+3)*5)
    in string
    then calculate its result ?????
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    Use stack for performing the operation. And you must evaluate your expression into postfix notation.

    Regards
    Dheeraj Joshi
    Last edited by Dheeraj Joshi; Jun 14 '10, 11:16 AM. Reason: Removed one line and replaced it with another

    Comment

    • mido22trika
      New Member
      • Jun 2010
      • 4

      #3
      plzexample

      becz

      if it is operator i cant make operation on to elements

      explain for me in this example

      ((1+2)*3)

      Comment

      • Dheeraj Joshi
        Recognized Expert Top Contributor
        • Jul 2009
        • 1129

        #4
        Postfix expression of above statement will be
        Code:
        12+3*
        push 1
        push 2
        Now input is +
        pop 1
        pop 2
        calculate sum 1+2 = 3
        push 3
        Now input is *
        pop 3
        pop 3
        calculate 3*3 = 9
        Now no more inputs and stack top is -1, so stop

        Regards
        Dheeraj Joshi

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          What precisely is your assignment?

          Are the four basic arithmetic functions (add, subtract, multiply, divide) sufficient or do you need to support other functions (such as exponentiation) ?

          The purpose of the postfix stack is to conform to the arithmetic order of operations. You need to have a clear understanding of the proper order of operations. Note that "order of operations" is a mathematical concept -- it has nothing to do with computer programming.

          Comment

          • mido22trika
            New Member
            • Jun 2010
            • 4

            #6
            i need adding + and multiplication * only
            and there are parentheses

            but the way by stack is hardly to made!!!!!

            Comment

            • mido22trika
              New Member
              • Jun 2010
              • 4

              #7
              how i push 1 then 2 before founding +

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                You should read about postfix notation. Evaluating a postfix expression is easy (that's the allure). The trick is to translate your input expression from infix notation to postfix notation.

                Look at the example in your original post: ((1+3)*5)
                The postfix equivalent is 1,3,+,5,*

                This can be read as ...
                1. Put the number "1" in your left hand.
                2. Put the number "3" in your right hand.
                3. Add the numbers you're holding, putting the result in your left hand and dropping what's in your right hand.
                4. Put the number "5" in your right hand.
                5. Multiply the numbers you're holding, putting the result in your left hand and dropping what's in your right hand.


                This distinction between "right hand" and "left hand" is not important for symmetric operators like add and multiply; but it is vital for asymmetric operators like subtract, divide, and exponentiate.

                Writing an infix-to-postfix translator is a pretty common problem for introductory/intermediate programming courses. There is no point trying to write a program until you understand what postfix notation is, why it works, and how to do the translations with pencil and paper.

                Comment

                • johny10151981
                  Top Contributor
                  • Jan 2010
                  • 1059

                  #9
                  try post fix notation
                  read the algorithm,

                  Comment

                  Working...