Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to perform Expression Evaluation

How to perform Expression Evaluation

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialdata-structures
10 Posts 7 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 12365995
    wrote on last edited by
    #1

    Write a simple expression evaluator that takes a string like the following as input: 1+2*(3+4).
    -Only integers need to be supported
    -The following operators should be supported: +, -, *.
    -Expressions within parenthesis should be evaluated first.
    -Then the resulting expressions should be evaluated from left to right (no operator precedence)
    -Example:
    4+(4-(2*3)+1)+4 = 7

    The code should be written in such a way to support additional operators.

    Assume the existence of a function tokenise, which could take a string and return an array of strings, for instance

    tokenise(‘1+(2*3)+4’) would return [‘1’,’+’,’(‘,’2’,’*’,’3’,’)’,’+’,’4’]

    and the functions isNumber and asNumber which would return a Boolean and integer respectively on a single string input.

    D S C P B 6 Replies Last reply
    0
    • U User 12365995

      Write a simple expression evaluator that takes a string like the following as input: 1+2*(3+4).
      -Only integers need to be supported
      -The following operators should be supported: +, -, *.
      -Expressions within parenthesis should be evaluated first.
      -Then the resulting expressions should be evaluated from left to right (no operator precedence)
      -Example:
      4+(4-(2*3)+1)+4 = 7

      The code should be written in such a way to support additional operators.

      Assume the existence of a function tokenise, which could take a string and return an array of strings, for instance

      tokenise(‘1+(2*3)+4’) would return [‘1’,’+’,’(‘,’2’,’*’,’3’,’)’,’+’,’4’]

      and the functions isNumber and asNumber which would return a Boolean and integer respectively on a single string input.

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Okay, and????? :confused: :rolleyes:

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      1 Reply Last reply
      0
      • U User 12365995

        Write a simple expression evaluator that takes a string like the following as input: 1+2*(3+4).
        -Only integers need to be supported
        -The following operators should be supported: +, -, *.
        -Expressions within parenthesis should be evaluated first.
        -Then the resulting expressions should be evaluated from left to right (no operator precedence)
        -Example:
        4+(4-(2*3)+1)+4 = 7

        The code should be written in such a way to support additional operators.

        Assume the existence of a function tokenise, which could take a string and return an array of strings, for instance

        tokenise(‘1+(2*3)+4’) would return [‘1’,’+’,’(‘,’2’,’*’,’3’,’)’,’+’,’4’]

        and the functions isNumber and asNumber which would return a Boolean and integer respectively on a single string input.

        S Offline
        S Offline
        Sascha Lefevre
        wrote on last edited by
        #3

        Please don't post the same message in multiple forums.

        If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

        1 Reply Last reply
        0
        • U User 12365995

          Write a simple expression evaluator that takes a string like the following as input: 1+2*(3+4).
          -Only integers need to be supported
          -The following operators should be supported: +, -, *.
          -Expressions within parenthesis should be evaluated first.
          -Then the resulting expressions should be evaluated from left to right (no operator precedence)
          -Example:
          4+(4-(2*3)+1)+4 = 7

          The code should be written in such a way to support additional operators.

          Assume the existence of a function tokenise, which could take a string and return an array of strings, for instance

          tokenise(‘1+(2*3)+4’) would return [‘1’,’+’,’(‘,’2’,’*’,’3’,’)’,’+’,’4’]

          and the functions isNumber and asNumber which would return a Boolean and integer respectively on a single string input.

          C Offline
          C Offline
          Chris Losinger
          wrote on last edited by
          #4

          do i get your degree if i do your homework ?

          image processing toolkits | batch image processing

          P 1 Reply Last reply
          0
          • U User 12365995

            Write a simple expression evaluator that takes a string like the following as input: 1+2*(3+4).
            -Only integers need to be supported
            -The following operators should be supported: +, -, *.
            -Expressions within parenthesis should be evaluated first.
            -Then the resulting expressions should be evaluated from left to right (no operator precedence)
            -Example:
            4+(4-(2*3)+1)+4 = 7

            The code should be written in such a way to support additional operators.

            Assume the existence of a function tokenise, which could take a string and return an array of strings, for instance

            tokenise(‘1+(2*3)+4’) would return [‘1’,’+’,’(‘,’2’,’*’,’3’,’)’,’+’,’4’]

            and the functions isNumber and asNumber which would return a Boolean and integer respectively on a single string input.

            P Offline
            P Offline
            Patrice T
            wrote on last edited by
            #5

            And you tried something or you just want us to do your HomeWork ?

            Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein

            1 Reply Last reply
            0
            • C Chris Losinger

              do i get your degree if i do your homework ?

              image processing toolkits | batch image processing

              P Offline
              P Offline
              Patrice T
              wrote on last edited by
              #6

              Chris Losinger wrote:

              do i get your degree if i do your homework ?

              I fear he will keep the degree for himself, and even claim tour work is his. ;)

              Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein

              1 Reply Last reply
              0
              • U User 12365995

                Write a simple expression evaluator that takes a string like the following as input: 1+2*(3+4).
                -Only integers need to be supported
                -The following operators should be supported: +, -, *.
                -Expressions within parenthesis should be evaluated first.
                -Then the resulting expressions should be evaluated from left to right (no operator precedence)
                -Example:
                4+(4-(2*3)+1)+4 = 7

                The code should be written in such a way to support additional operators.

                Assume the existence of a function tokenise, which could take a string and return an array of strings, for instance

                tokenise(‘1+(2*3)+4’) would return [‘1’,’+’,’(‘,’2’,’*’,’3’,’)’,’+’,’4’]

                and the functions isNumber and asNumber which would return a Boolean and integer respectively on a single string input.

                B Offline
                B Offline
                Bram van Kampen
                wrote on last edited by
                #7

                Well, We do not do exam questions here. You see, Like it or not, whether you get your degree or diploma, or not, the real test is: 'Can you do the Job.' Degrees and diploma's are there to sort the corn from the chaff. I Myself have no degree in Computer Science, for the simple reason that in the 1970's when I went to university, computers where things that only large government institutions could afford, the cost of each of them involved billions, and no one was allowing students access to expensive things like that. I studied pure mathematics instead. You are damn lucky that you have access to an affordable computer, with a power that is a large number of magnitudes greater than the machines costing billions some 40 odd years ago! What happened? Did you miss a Semester at College, or what? Also, Is this an exercise in What? It could de C or CPP (My Guess) Or, if it is MFC, did you write the Interface, and are now stuck for 'Where the Rubber meets the Road' For you, and, Only for Once, just a bit of a leg up, to do my bit for humanity. I Suggest you start searching for 'Reverse Polish Notation' (RPN) to at least get you started. As a Further bit of help, the question does not ask you do the harder part, (writing the Tokeniser)you only have to write code to evaluate it's result. Should not take too much effort. On pain of being blackballed by fellow members, I remark that you are requested to implement an abstract RPN execution stack. Success :)

                Bram van Kampen

                D 1 Reply Last reply
                0
                • B Bram van Kampen

                  Well, We do not do exam questions here. You see, Like it or not, whether you get your degree or diploma, or not, the real test is: 'Can you do the Job.' Degrees and diploma's are there to sort the corn from the chaff. I Myself have no degree in Computer Science, for the simple reason that in the 1970's when I went to university, computers where things that only large government institutions could afford, the cost of each of them involved billions, and no one was allowing students access to expensive things like that. I studied pure mathematics instead. You are damn lucky that you have access to an affordable computer, with a power that is a large number of magnitudes greater than the machines costing billions some 40 odd years ago! What happened? Did you miss a Semester at College, or what? Also, Is this an exercise in What? It could de C or CPP (My Guess) Or, if it is MFC, did you write the Interface, and are now stuck for 'Where the Rubber meets the Road' For you, and, Only for Once, just a bit of a leg up, to do my bit for humanity. I Suggest you start searching for 'Reverse Polish Notation' (RPN) to at least get you started. As a Further bit of help, the question does not ask you do the harder part, (writing the Tokeniser)you only have to write code to evaluate it's result. Should not take too much effort. On pain of being blackballed by fellow members, I remark that you are requested to implement an abstract RPN execution stack. Success :)

                  Bram van Kampen

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  Bram van Kampen wrote:

                  ...implement an abstract RPN execution stack.

                  Did this in undergraduate school. Was a fun exercise.

                  "One man's wage rise is another man's price increase." - Harold Wilson

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                  B 1 Reply Last reply
                  0
                  • D David Crow

                    Bram van Kampen wrote:

                    ...implement an abstract RPN execution stack.

                    Did this in undergraduate school. Was a fun exercise.

                    "One man's wage rise is another man's price increase." - Harold Wilson

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                    B Offline
                    B Offline
                    Bram van Kampen
                    wrote on last edited by
                    #9

                    Well Indeed, The Real Fun and Complication was to build the RPN instruction List in the first place. This OP was not asked to do that.

                    Bram van Kampen

                    1 Reply Last reply
                    0
                    • U User 12365995

                      Write a simple expression evaluator that takes a string like the following as input: 1+2*(3+4).
                      -Only integers need to be supported
                      -The following operators should be supported: +, -, *.
                      -Expressions within parenthesis should be evaluated first.
                      -Then the resulting expressions should be evaluated from left to right (no operator precedence)
                      -Example:
                      4+(4-(2*3)+1)+4 = 7

                      The code should be written in such a way to support additional operators.

                      Assume the existence of a function tokenise, which could take a string and return an array of strings, for instance

                      tokenise(‘1+(2*3)+4’) would return [‘1’,’+’,’(‘,’2’,’*’,’3’,’)’,’+’,’4’]

                      and the functions isNumber and asNumber which would return a Boolean and integer respectively on a single string input.

                      A Offline
                      A Offline
                      Alan Balkany
                      wrote on last edited by
                      #10

                      The "official" way to do expression evaluation involves parse trees and reverse Polish notation, but I found a simpler more practical approach: Repeatedly process your list of tokens, making it one step simpler each time by applying a series of rules: 1. If you see the sequence '(', 'number', ')', replace it with just 'number'. 2. For each operator in decreasing precedence, look for the sequence 'number', 'operator', 'number' and replace it with the result of the operation. If you end up with a single number, that's your answer. Anything else indicates a syntax error in the input expression.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups