Results 1 to 9 of 9

Thread: Easy Beginner Question

  1. #1
    Dhaner
    Guest

    Question Easy Beginner Question

    I am new to VB and I would like to know if it is possible (cuz I never founded any info on it yet) to convert a string expression entered by the user, such as "5*(5+5)" to a mathematical form so that it can be used to define a variable.

    Ex : Dim dblVariable As Double

    txtObject.Text = 5*(5+5)

    dblVariable = (expression wanted : 5*(5+5)

    Thanks to everybody who will answer without making too much fun of me.

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    I don't think theres an easy way to do that...

    using val function will only return the first character..
    you would have to evaluate the string expression for numbers and operators and then do conditional logic testing to result
    in a correct answer...

    My question to you is are you trying to build a calculator of some sort?

  3. #3
    Dhaner
    Guest
    Exactly, I am building a calculator. I am beginning programming with C++ and I wanted to know how easier it was to do the equivalent on VB (it really is...). It looks like I will have to build someting to analyse the text entered anyway. Are there ressources that would help me on the programming of such analysis?

    What I wanted to do is to permit the use of priority by including ( ) priorities and the possibility to see the whole equation so that the user can modify it and add complex priorities situations.

    I know it is a bit useless to build a calculator when these programs exist in much better version, but I just want to learn the bases in a quick way

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    My guess would be that you would have to search for '(' and assign all text from that operator until you reach the closing brace.
    dim e() as string
    dim d() as double
    inputstring = "5*(6+(3+9))+3"
    'search for high priorites ()
    'when found substract string from original string
    string e(1) = "3+9"
    string e(2) = "6+"

    'search from left hand side for middle priorites *,/,%
    string e(3) = "5*"

    'search from left hand side for low priorties
    string e(4) = "+3"

    calculate operations from e(1) to Ubound e
    'parse thru each dimension of e, calculate, and assign to numeric variable d.
    d(1) = 12
    d(2) = 18
    d(3) = 90
    d(4) = 93

  5. #5
    Dhaner
    Guest
    This is very helpful stuff. Thank you!

    Time to sleep in Quebec!

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    How about something like
    VB Code:
    1. Dim dblVariable As Double
    2. dblVariable = 5*(5+5)
    3. txtTextObject.Text = CStr(dblVariable)

  7. #7
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    That won't get him anywhere...

    He's taking a string object for input, such as a textbox, and trying to convert it into a double expression...

  8. #8
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    How about the Scripting control?
    http://www.vbforums.com/showthread.p...valuate+script
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  9. #9
    Dhaner
    Guest
    This one is exactly what I was looking for! Thanks a lot everybody!

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