|
-
Nov 22nd, 2000, 08:46 AM
#1
Thread Starter
New Member
Does some1 have a function or anything else that solves mathematical expressions?
The function should get a string (the expression) and return a double (the solution)
(4 Example: the string is "2.4-5.3*2-4(5+3)/sin(3.2)" so the function should return 688.66106 - i'm not sure but i think that this is the correct answer)
[Edited by yotam on 11-22-2000 at 08:51 AM]
-
Nov 22nd, 2000, 10:28 AM
#2
Fanatic Member
-
Nov 22nd, 2000, 10:28 AM
#3
a) The Answer is (according to my calculations) : 539,987915420116
b) Try this code:
Code:
Option Explicit
Private Declare Function EbExecuteLine Lib "vba6.dll" (ByVal pStringToExec As Long, ByVal Foo1 As Long, ByVal Foo2 As Long, ByVal fCheckOnly As Long) As Long
' For VB5
'Declare Function EbExecuteLine Lib "vba5.dll" (ByVal pStringToExec As Long, ByVal Foo1 As Long, ByVal Foo2 As Long, ByVal fCheckOnly As Long) As Long
' For Access 97/VBE.dll clients as Word 97 and Excel 97
'Declare Function EbExecuteLine Lib "vba332.dll" (ByVal pStringToExec As Long, ByVal Foo1 As Long, ByVal Foo2 As Long, ByVal fCheckOnly As Long) As Long
Function FExecuteCode(stCode As String, Optional fCheckOnly As Boolean) As Boolean
FExecuteCode = EbExecuteLine(StrPtr(stCode), 0&, 0&, Abs(fCheckOnly)) = 0
End Function
Private Sub Form_Load()
Dim TheQuestion As String
TheQuestion = "2.4-5.3*2-4*(5+3)/sin(3.2)"
FExecuteCode "Q = " & TheQuestion
FExecuteCode "MSGBox ""The answer to the question " & vbCrLf & vbCrLf & TheQuestion & vbCrLf & vbCrLf & "is : "" & vbCrLf & vbcrlf & Q"
End Sub
Now you're basically running an interpreter...
-
Nov 22nd, 2000, 10:32 AM
#4
Fanatic Member
Roblll's code is better then mine, so use that code.
-
Nov 22nd, 2000, 10:34 AM
#5
The anser is 688,661068746521 for the formula: (2.4-5.3*2-4*(5+3))/sin(3.2)
(Just to let you know I know what I'm talkin' 'bout.)
-
Nov 23rd, 2000, 07:21 AM
#6
Thread Starter
New Member
thank u
10x 4 helping me! It was very helpful.
-
Nov 24th, 2000, 12:18 PM
#7
transcendental analytic
Does this work for anyone else, i mean after you compile, it says you need vba6.dll, and when you put it in the same directory it crashes.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Nov 24th, 2000, 02:32 PM
#8
same error
Hi.
It happens the same to me.
numibesi
-
Nov 24th, 2000, 04:07 PM
#9
Dunno... Never compiled it...yet...Lemme try...
The instruction at "0x0fa916cc" referenced memory at "0x000002b0". The memory could not be "read".
Uhm... Servicepack 5????? Maybe you'd have to DIM first???
Nope...Same error...
Lemme try sometin' else...
Code:
Private Sub Form_Load()
' This time without variables...
FExecuteCode "MSGBox ""The answer to the question is : "" & vbCrLf & vbcrlf & "" (2.4-5.3*2-4*(5+3)/sin(3.2))"
End Sub
Doesn't work either... Anyone? I'm out of ideas...
-
Nov 24th, 2000, 04:18 PM
#10
Thread Starter
New Member
I think that the only way is to write a function that searched the string char by char and solve it... :-(
-
Nov 24th, 2000, 04:42 PM
#11
Monday Morning Lunatic
To solve an equation, first you need to move it into Reverse Polish Notation. To do this, first the brackets need to be split, for example:
x = 1 * 6 * (9 + 3 * sin(y))
goes to:
x = 1 * 6 * par_a
par_a = 9 + 3 * par_b
par_b = sin y
Then, you can parse it out (can't remember how, look for "Reverse Polish" on a search engine, or someone here MUST know!). Then substitute the values back in again.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 24th, 2000, 04:50 PM
#12
I know it sounds lame now... But I once had a "parsing and processing" routine that did it.. Can't remember where I left it (maybe on work, couldn't dig it up here), but also I think to recall I found it here... As I remember it was a CLASS...
I'll try to find it monday at work... E-mailed to my job to not forget it... I'll keep you up to date...
Rob.
-
Nov 24th, 2000, 04:59 PM
#13
transcendental analytic
And i was thinking this was exactly what i need, it's tons faster than vbs, but it doesn't work after compilation
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Nov 28th, 2000, 07:48 AM
#14
Thread Starter
New Member
Help!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|