Results 1 to 10 of 10

Thread: [RESOLVED] Is It Possible To Calculate Maths From A String?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Resolved [RESOLVED] Is It Possible To Calculate Maths From A String?

    Is there a method to calculate an equation from a string that looks something like "(1 + 1) * 2"?

  2. #2
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: Is It Possible To Calculate Maths From A String?

    Here's a very good article on the algorithm you need:

    http://en.wikipedia.org/wiki/Shunting_yard_algorithm
    From my burrow, 2 feet under.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Re: Is It Possible To Calculate Maths From A String?

    I've heard of that before, it's quite interesting the way it works, but what about implementing error detection? For example a sum such as "3 ** 4".

  4. #4
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: Is It Possible To Calculate Maths From A String?

    That's something you'll have to test for as you go along. The algorithm is there to show you how it works.
    From my burrow, 2 feet under.

  5. #5
    Hyperactive Member Runesmith's Avatar
    Join Date
    Oct 2008
    Posts
    399

    Re: Is It Possible To Calculate Maths From A String?

    Quote Originally Posted by Louix View Post
    Is there a method to calculate an equation from a string that looks something like "(1 + 1) * 2"?
    If I understood your question correct, you want to evaluate a string? Then use
    Code:
    AnswerIS=VAL("(1+1)*2")
    If there is an error in the string it will throw an exeption. You can use regular expressions to check if your string is properly formatted, but that would severly limit the creativity of whoever inputs that string. It's better to catch the exception and show a message (eg. "Cannot compute. Processor overloaded. Self-destruction process initiated").
    Runesmith

    The key to getting the right answer is asking the right question

    I just realized: good health is merely the slowest possible rate at which one can die

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Re: Is It Possible To Calculate Maths From A String?

    Hmm, interesting function, but I tried your example and it's returning 0. I've also tried a less complex sum such as 1 + 1, but that returns 1.

  7. #7
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Is It Possible To Calculate Maths From A String?

    Unless Val has changed drastically since I last looked it will purely convert a string representation of a single number into its numerical equivalent, not evaluate a numerical equation.

  8. #8
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Is It Possible To Calculate Maths From A String?

    You will need to parse the string into 'tokens' and then use one of the standard evaluation algorithms, either shunting yard or reverse polish (do they teach either of these in computer science classes?).

    Alternatively, I believe the Windows Scripting Host has an Evaluate function? I could be wrong.

    Additionally, DBasnett wrote an 'evaluation' routine: http://www.vbforums.com/showthread.php?t=559071
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  9. #9
    Hyperactive Member Runesmith's Avatar
    Join Date
    Oct 2008
    Posts
    399

    Re: Is It Possible To Calculate Maths From A String?

    Quote Originally Posted by keystone_paul View Post
    Unless Val has changed drastically since I last looked it will purely convert a string representation of a single number into its numerical equivalent, not evaluate a numerical equation.
    You are right.

    To make up for my lapse, I searched MSDN and came up with this convoluted but effective method - using excel to evaluate your string for you.

    http://support.microsoft.com/kb/159974
    Runesmith

    The key to getting the right answer is asking the right question

    I just realized: good health is merely the slowest possible rate at which one can die

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Re: Is It Possible To Calculate Maths From A String?

    Well, I found a way, even if it is a bit patchy, it still works.

    In my application, I use MSHTML. This solution is ideal for me, but can work for anyone else.

    I made a WebBrowser 0px in height, 0px in width. I can make it execute JavaScript from within a Navigate call which evaluates the input and makes the page contents the number returned by the evaluation.

    Then when the page has done loading, I get the innerText of the page (IHTMLDocument2), and put it in a variable. Works perfectly!

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