Page 1 of 1
					
				result of a mathematical formula
				Posted: Mon Dec 11, 2006 3:12 am
				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.
			 
			
					
				
				Posted: Mon Dec 11, 2006 8:38 am
				by Froggerprogger
				
			 
			
					
				
				Posted: Mon Dec 11, 2006 10:50 am
				by PB
				
			 
			
					
				
				Posted: Mon Dec 11, 2006 1:16 pm
				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.
 
			 
			
					
				
				Posted: Mon Dec 11, 2006 1:31 pm
				by Trond
				You forgot CloseProgram()
			 
			
					
				
				Posted: Mon Dec 11, 2006 1:36 pm
				by netmaestro
				Quite right, thanks for pointing it out. Should be reasonably correct now. Also, it doesn't seem to need #PB_Program_Hide.
			 
			
					
				
				Posted: Mon Dec 11, 2006 1:40 pm
				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.
 
			 
			
					
				
				Posted: Mon Dec 11, 2006 1:43 pm
				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 

 
			 
			
					
				
				Posted: Mon Dec 11, 2006 1:48 pm
				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 
			 
			
					
				
				Posted: Mon Dec 11, 2006 1:52 pm
				by netmaestro
				You win. It does have a cleaner feel to it. Updated.
			 
			
					
				
				Posted: Mon Dec 11, 2006 2:03 pm
				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
 
			 
			
					
				
				Posted: Mon Dec 11, 2006 2:40 pm
				by coma
				thank you all for the replies, it will help me a lot.
			 
			
					
				
				Posted: Tue Dec 12, 2006 12:17 am
				by PB
				@TheFool: I changed my link, just for you, to use ImgSpot to avoid the ads.