result of a mathematical formula

Just starting out? Need help? Post your questions and find answers here.
coma
Enthusiast
Enthusiast
Posts: 164
Joined: Fri Aug 15, 2003 3:46 am
Location: Canada

result of a mathematical formula

Post by coma »

Hi everybody.

In my program I have mathematical expressions in strings, for example a$="200+100/2"

How can I put the result of the operation into a numeric variable ? Does anyone know a procedure to do this ?

It seems very difficult to me, especially because of priority of operators.
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post by Froggerprogger »

%1>>1+1*1/1-1!1|1&1<<$1=1
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

You can also tap into the windows scripting host:

Code: Select all

Procedure.d Evaluate(exp.s)
  If CreateFile(0,"tmp.vbs")
    WriteStringN(0, "wscript.stdout.writeline "+exp) : CloseFile(0)
    prg=RunProgram("wscript"," tmp.vbs",GetCurrentDirectory(),#PB_Program_Open|#PB_Program_Read)
    If prg
      result.d = ValD(ReadProgramString(prg))
      CloseProgram(prg)
      ProcedureReturn result ; success
    Else
      ProcedureReturn 0 ; scripting host failure
    EndIf
  Else
    ProcedureReturn 0 ; CreateFile failure
  EndIf
EndProcedure

Debug Evaluate("47+2*10/3")
which is probably the shortest and most powerful solution. The scripting host can do anything VBScript is capable of, which is quite a lot.
Last edited by netmaestro on Mon Dec 11, 2006 1:54 pm, edited 5 times in total.
BERESHEIT
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

You forgot CloseProgram()
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Quite right, thanks for pointing it out. Should be reasonably correct now. Also, it doesn't seem to need #PB_Program_Hide.
BERESHEIT
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> Should be reasonably correct now

Well, technically the "If prg" block should go inside the "If CreateFile" block,
because if the file was never created then there's no need to do an "If prg"
check. ;) Also, I'd make the code create the file in the TEMP dir, so the user
is never aware of it.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

No, you have to make a separate check, to cover two eventualities: couldn't create the file, couldn't load the scripting host. Agreed on the TEMP dir though, good idea. I'm also looking to see if I can stream the command line into the host, no file needed. Should be doable, if anyone knows how, pls post before I spend a lot of time on it :wink:
BERESHEIT
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> two eventualities: couldn't create the file, couldn't load the scripting host

Yes, but if CreateFile fails, there's no need to check "prg". But if CreateFile
worked, then you can go ahead and check "prg" in the same block, so both
cases are covered with only one decision and not two.

Here's two "Visustin" flowcharts to show what I mean:

http://img15.imgspot.com/?u=/u/06/344/19/Image11165881937.gif
Last edited by PB on Tue Dec 12, 2006 12:15 am, edited 5 times in total.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

You win. It does have a cleaner feel to it. Updated.
BERESHEIT
Helle
Enthusiast
Enthusiast
Posts: 178
Joined: Wed Apr 12, 2006 7:59 pm
Location: Germany
Contact:

Post by Helle »

Not nice, but the result is correct (PB 4.0 or higher):

Code: Select all

Debug ValD(StrD((ValD("47") + ValD("2") * ValD("10") / ValD("3"))))
Gruss
Helle
coma
Enthusiast
Enthusiast
Posts: 164
Joined: Fri Aug 15, 2003 3:46 am
Location: Canada

Post by coma »

thank you all for the replies, it will help me a lot.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

@TheFool: I changed my link, just for you, to use ImgSpot to avoid the ads.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Post Reply