• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 167
  • Last Modified:

Help!!!

I need to know how can I convert string to an arithmetic expression

For example I have this variable:

ABC="10+2-5"

and i want to have the answer shown in a msgbox
0
DImirC
Asked:
DImirC
  • 3
  • 2
  • 2
  • +5
1 Solution
 
mark2150Commented:
This is much harder than it looks.

If your expression is always of th form A+B-C then you can simplify things. But if you want to evaluate any random equation you're looking at a lot of work. You'ld have to generate a parser that will scan the line of text and identify numeric values and operators. You'll then need to perform the indicated operations.

In the old days of BASIC you could place that formula on a numerical input statement and the BASIC itself would parse it out. However it could be corrupted by entering invalid characters, etc.

The relative complexity comes when you start taking all the permutions into account. Are the only functions you need + & -? Or do you need * and / as well? How about trig functions, Sin(), Cos(), Tan()? Powers? Are your numbers only going to be integers? or do you need to handle reals? How about fractions? Are you going to allow $ signs and ,'s? This can get really messy in a hurry.

Search the PAQ's, I've seen code for this posted before.

M
0
 
tsa2Commented:
try the VAL statement
0
 
DImirCAuthor Commented:
if I use this:

ABC="10+2-5"
A=val(ABC)

Then

A=10

I need the result of the arithmetic expression
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
cable4096Commented:
Uh, no, the VAL() function will just give the number until a non-number character is found. So

Val("10 + 2 - 5") will just return 10 and not 7, the correct answer.

Will this just be for Integer numbers, or will you be using Floating Point numbers as well? Will you be using Parenthesis "()" in the formula as well?

This is kind of a complex thing if you want to include support for floating point numbers and parenthesis.

Otherwise you could do a loop that reads the number until it reaches a plus "+", minus "-", multiply "*", or divide "/". Then parse it into an array and sort the array so that the multiplide part is done first, then the division, then the plus, and then the minus.

Say, this isn't for a school assignment, is it? ;)
0
 
tsa2Commented:
Try parsing the string:

dim a$
dim b$
dim c$
a$ = mid$(abc, 1, 2)
b$ = mid$(abc, 3, 1)
c$ = mid$(abc, 6, 1)

ans = val(a$ + b$ - c$)

This is the only way.
There is no other way.
0
 
DImirCAuthor Commented:
tsa2 , the expression can have more numbers, so I dont know how many numbers are there going to be...
0
 
AzraSoundCommented:
http://www.vbweb.co.uk/commands/math_functions.htm

example there that parses the expression and calculates
0
 
Brendt HessSenior DBACommented:
If you can use the MS Scripting control in your VB programs, you can use the EVAL and EXECUTE functions from VBScript.

See:
http://www.thescarms.com/vbasic/Scripting.htm
for a discussion of the scripting control.
0
 
ArkCommented:
Hi
If you need only "+/-" operations (order is not important), it's not a hard code. Using brackets, multiplication and dividing is more difficult.
Cheers
0
 
arcusdCommented:
if ur app is connected to a database you can use select statement
to evaluate this string , using recordset you can access the answer
to the expression


ur code may look like this

ABC = "10+2-5"

sqlstmt = "select" & ABC & "from DummyTable"


set mdb = OPendatabase.....
set mset = mdb.OpenRecordset(sqlstmt,.....)
if not mset.eof then
   answer  = mset.Fields(0)
end if

Note:
DummyTable must exist in your database , this table could only have one field and one row .....


hope this helps
0
 
ArkCommented:
Azra,
I thing, you have to change your comment to answer - class from your link do many math operations, it's new and I just found two links at this class at another VB sites - it means that code is good and popular.
Cheers
0
 
mark2150Commented:
Just use the "Accept comment as answer" option above his response...

M
0
 
DImirCAuthor Commented:
thanx AzraSound
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now