Help with Simple Integer Expression

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wazzup
    New Member
    • Feb 2007
    • 25

    Help with Simple Integer Expression

    My task is Create a program to read a simple integer expression and display the result. "Simple" means no parentheses are allowed and only the +, -, *, and / operators are allowed.

    I think expression would be Multiplications , then Division, then Addition and Subtraction.

    My program gave me a wrong result after addition, take a look to my code you will understand. Please help me to develop this.

    Here is my Code
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    Having looked at your code, I'm a little confused as to what you are trying to achieve....perh aps if you attached the output it is givving we might be able to work out what's going on

    Comment

    • wazzup
      New Member
      • Feb 2007
      • 25

      #3
      Ok, First, This is my input as a string 12+34-34+11*4*11/11
      Then, convert them to integer and calc them.

      My program gave me result is -32
      But Correct must be 56

      My algorithm is get the string input then create an Array and put them into the Array. For ex input above: 12 is the first element and + is second elements of the array ...etc

      Then I check the the Array if there is * then get the element before that time with elements after that. For ex input above: 11*4 . Then, get the result replace to position of * in array. Keep doing like that until no more *. Same thing with /, + and -

      Comment

      • wazzup
        New Member
        • Feb 2007
        • 25

        #4
        This is my output of the program:

        Code:
        Original Input is 12 + 34 - 34 + 11 * 4 * 11 / 1
        Input after trimmed is 12+34-34+11*4*11/11
        Number elements of Array is 13
        Original Values of Elements of the Array
        A[0] = 12
        A[1] = +
        A[2] = 34
        A[3] = -
        A[4] = 34
        A[5] = +
        A[6] = 11
        A[7] = *
        A[8] = 4
        A[9] = *
        A[10] = 11
        A[11] = /
        A[12] = 11
        
        Begin Calculate ...............
        
        Expression: 11 * 4 = 44
        Expression: 44 * 11 = 484
        
        Values of Elements after Multiplications.
        A[0] = 12
        A[1] = +
        A[2] = 34
        A[3] = -
        A[4] = 34
        A[5] = +
        A[6] =
        A[7] =
        A[8] =
        A[9] = 484
        A[10] =
        A[11] = /
        A[12] = 11
        
        Expression: 484 / 11 = 44
        
        Values of Elements after Divisions.
        A[0] = 12
        A[1] = +
        A[2] = 34
        A[3] = -
        A[4] = 34
        A[5] = +
        A[6] =
        A[7] =
        A[8] =
        A[9] =
        A[10] =
        A[11] = 44
        A[12] =
        
        Expression: 12 + 34 = 46
        Expression: 34 + 44 = 78
        
        Values of Elements after Additions.
        A[0] =
        A[1] = 46
        A[2] =
        A[3] = -
        A[4] =
        A[5] = 78
        A[6] =
        A[7] =
        A[8] =
        A[9] =
        A[10] =
        A[11] =
        A[12] =
        
        Expression: 46 - 78 = -32
        
        Final Answer is -32
        Correct answer is: 56

        Comment

        • wazzup
          New Member
          • Feb 2007
          • 25

          #5
          I got it! Computer does the * and / first (left to right) then do the + and - (left to right). Thanks for the help!

          Comment

          • DeMan
            Top Contributor
            • Nov 2006
            • 1799

            #6
            That was easy....

            Comment

            Working...