result of a mathematical formula
result of a mathematical formula
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.
			
			
									
									
						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

 - Posts: 423
 - Joined: Fri Apr 25, 2003 5:22 pm
 - Contact:
 
Have a look at these three. keyword: expression parser:
http://www.purebasic.fr/english/viewtopic.php?t=15762
http://www.purebasic.fr/english/viewtopic.php?t=17371
http://www.purebasic.fr/english/viewtopic.php?t=15845
			
			
									
									http://www.purebasic.fr/english/viewtopic.php?t=15762
http://www.purebasic.fr/english/viewtopic.php?t=17371
http://www.purebasic.fr/english/viewtopic.php?t=15845
%1>>1+1*1/1-1!1|1&1<<$1=1
						And there's also these two shorter versions:
http://www.purebasic.fr/english/viewtopic.php?t=24836
http://www.purebasic.fr/english/viewtopic.php?t=24921
			
			
									
									http://www.purebasic.fr/english/viewtopic.php?t=24836
http://www.purebasic.fr/english/viewtopic.php?t=24921
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
						"PureBasic won't be object oriented, period" - Fred.
- netmaestro
 - PureBasic Bullfrog

 - Posts: 8452
 - Joined: Wed Jul 06, 2005 5:42 am
 - Location: Fort Nelson, BC, Canada
 
You can also tap into the windows scripting host:
which is probably the shortest and most powerful solution. The scripting host can do anything VBScript is capable of, which is quite a lot.
			
			
													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")
					Last edited by netmaestro on Mon Dec 11, 2006 1:54 pm, edited 5 times in total.
									
			
									BERESHEIT
						- netmaestro
 - PureBasic Bullfrog

 - Posts: 8452
 - Joined: Wed Jul 06, 2005 5:42 am
 - Location: Fort Nelson, BC, Canada
 
> 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.
			
			
									
									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.
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.
						"PureBasic won't be object oriented, period" - Fred.
- netmaestro
 - PureBasic Bullfrog

 - Posts: 8452
 - Joined: Wed Jul 06, 2005 5:42 am
 - Location: Fort Nelson, BC, Canada
 
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 
			
			
									
									BERESHEIT
						> 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
			
			
													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.
						"PureBasic won't be object oriented, period" - Fred.
- netmaestro
 - PureBasic Bullfrog

 - Posts: 8452
 - Joined: Wed Jul 06, 2005 5:42 am
 - Location: Fort Nelson, BC, Canada
 
Not nice, but the result is correct (PB 4.0 or higher):
Gruss
Helle
			
			
									
									
						Code: Select all
Debug ValD(StrD((ValD("47") + ValD("2") * ValD("10") / ValD("3"))))
Helle
