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#
  4. convert an expresion in a result

convert an expresion in a result

Scheduled Pinned Locked Moved C#
question
7 Posts 5 Posters 0 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.
  • X Offline
    X Offline
    xtremetechnology
    wrote on last edited by
    #1

    i have in a textbox "1+2+3". How can i convert this string to find the result aka 6 ?

    J J L S 4 Replies Last reply
    0
    • X xtremetechnology

      i have in a textbox "1+2+3". How can i convert this string to find the result aka 6 ?

      J Offline
      J Offline
      Jun Du
      wrote on last edited by
      #2

      "1+2+3" from the textbox is just a string. How to interpret it is entirely up to you. So, in order to obtain the result, you need to write your own parsing rules and calculating logic.

      Best, Jun

      X 1 Reply Last reply
      0
      • J Jun Du

        "1+2+3" from the textbox is just a string. How to interpret it is entirely up to you. So, in order to obtain the result, you need to write your own parsing rules and calculating logic.

        Best, Jun

        X Offline
        X Offline
        xtremetechnology
        wrote on last edited by
        #3

        ok! But how could i interpret "+" like + operator ? I am thinking to split the string "1+2+3", 1,2 and 3 are numbers, integer, but "+" what type it is?

        J 1 Reply Last reply
        0
        • X xtremetechnology

          ok! But how could i interpret "+" like + operator ? I am thinking to split the string "1+2+3", 1,2 and 3 are numbers, integer, but "+" what type it is?

          J Offline
          J Offline
          Jun Du
          wrote on last edited by
          #4

          That type of string can be split to two categories: operands and operators. Operands are simply numbers. With operators, you cast them one by one. Pseudo code may be like this:

          //...
          switch(operator1)
          {
          case "+":
          result = operand1+operand2;
          break;
          case "-":
          result = operand1-operand2;
          break;
          //...
          }

          I am not aware of any shortcuts in this regard.

          Best, Jun

          1 Reply Last reply
          0
          • X xtremetechnology

            i have in a textbox "1+2+3". How can i convert this string to find the result aka 6 ?

            J Offline
            J Offline
            Jaiprakash M Bankolli
            wrote on last edited by
            #5

            First you need to split "+" into an array Then each of the array value needs to be converted or Parsed and added

            Regards, Jaiprakash M Bankolli jaiprakash.bankolli@gmail.com http://jaiprakash.blog.com/

            1 Reply Last reply
            0
            • X xtremetechnology

              i have in a textbox "1+2+3". How can i convert this string to find the result aka 6 ?

              L Offline
              L Offline
              Leslie Sanford
              wrote on last edited by
              #6

              You'll need to parse the strings into tokens and go from there. One approach is to use a recursive decent parser[^].

              1 Reply Last reply
              0
              • X xtremetechnology

                i have in a textbox "1+2+3". How can i convert this string to find the result aka 6 ?

                S Offline
                S Offline
                sherifffruitfly
                wrote on last edited by
                #7

                Google "lex yacc".

                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