Expression as INPUT?

Just starting out? Need help? Post your questions and find answers here.
AMpos
Enthusiast
Enthusiast
Posts: 131
Joined: Fri Jun 05, 2020 12:47 am

Expression as INPUT?

Post by AMpos »

Is there an easy way to convert an user expresion input to value?

I mean:

a$=input$
result=expression(a$)
>user types "2+2"
and
RESULT=4

In my old program, I did a procedure to convert this (it only parsed +-/*, and I dont need any more), and perhaps there is some kind of trick to do it automatically...
User avatar
STARGÅTE
Addict
Addict
Posts: 2297
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Expression as INPUT?

Post by STARGÅTE »

One of the easiest ways is using a database query:

Code: Select all

UseSQLiteDatabase()

Procedure.s Eval(String.s)
	Static Database.i
	Protected Result.s
	If Not Database 
		Database = OpenDatabase(#PB_Any, ":memory:", "", "", #PB_Database_SQLite)
	EndIf
	If DatabaseQuery(Database, "Select ("+String+")")
		If NextDatabaseRow(Database)
			Result = GetDatabaseString(Database, 0)
		EndIf
		FinishDatabaseQuery(Database)
	Else
		Result = DatabaseError()
	EndIf
	ProcedureReturn Result
EndProcedure

Debug Eval("2+2")
Debug Eval("3*4-1")
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Expression as INPUT?

Post by Tenaja »

AMpos wrote:Is there an easy way to convert an user expresion input to value?

I mean:

a$=input$
result=expression(a$)
>user types "2+2"
and
RESULT=4

In my old program, I did a procedure to convert this (it only parsed +-/*, and I dont need any more), and perhaps there is some kind of trick to do it automatically...
Stargate has a wonderful cheat to do it. To do it in your code, it all depends on how complicated of an expression you want to permit. There are several examples of other solutions on this forum, look for these keywords: calculator, parser, recursive
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Expression as INPUT?

Post by Tenaja »

And if you want an excellent tutorial, search Google for
Let's Build a Compiler, by Jack Crenshaw

You will only need the first few chapters 8 handle your simply expressions, but you will likely thoroughly understand it once you get through the chapters you need.
BarryG
Addict
Addict
Posts: 4318
Joined: Thu Apr 18, 2019 8:17 am

Re: Expression as INPUT?

Post by BarryG »

STARGÅTE wrote:One of the easiest ways is using a database query
That's short and sweet, but it doesn't support decimal places? Giving it 22/7 only shows 3 instead of 3.14... Can that be done?

Anyway, I've used this code forever (by Danilo) and it works great -> http://forums.purebasic.com/german/view ... =8&t=24256
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Expression as INPUT?

Post by Josh »

BarryG wrote:That's short and sweet, but it doesn't support decimal places?
That's not true. How it is done is exactly one of the two ways, as it is done by all compilers I testet (except Pb has no system and does everything differently). Two integer operands result in an integer value, if one of the operands is a float, then the result is also a float:

Code: Select all

21/7   = 3
21.0/7 = 3.14285714285714
21/7.0 = 3.14285714285714
sorry for my bad english
BarryG
Addict
Addict
Posts: 4318
Joined: Thu Apr 18, 2019 8:17 am

Re: Expression as INPUT?

Post by BarryG »

Josh wrote:That's not true.
Yes it is. I'm referring to STARGÅTE's code. Try for yourself:

Code: Select all

UseSQLiteDatabase()

Procedure.s Eval(String.s)
   Static Database.i
   Protected Result.s
   If Not Database
      Database = OpenDatabase(#PB_Any, ":memory:", "", "", #PB_Database_SQLite)
   EndIf
   If DatabaseQuery(Database, "Select ("+String+")")
      If NextDatabaseRow(Database)
         Result = GetDatabaseString(Database, 0)
      EndIf
      FinishDatabaseQuery(Database)
   Else
      Result = DatabaseError()
   EndIf
   ProcedureReturn Result
EndProcedure

Debug Eval("22/7") ; Shows 3 instead of 3.14
Debug Eval("100/3") ; Shows 3 instead of 3.33
Debug Eval("256/6") ; Shows 42 instead of 42.66
So how do we get it to show decimal places? It currently only returns whole integers.
infratec
Always Here
Always Here
Posts: 7793
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Expression as INPUT?

Post by infratec »

You don't read exactly, or you don't understand what you read :wink:

Code: Select all

Debug Eval("256.0/6") ; Shows 42.6666666667
How should the program (SQLite) know that you don't want integers (which are faster)?
You have to tell him by pointing out that at least one of the values is floating point.
#NULL
Addict
Addict
Posts: 1504
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Expression as INPUT?

Post by #NULL »

BarryG wrote:So how do we get it to show decimal places? It currently only returns whole integers.
Just prepend 1.0 *

Code: Select all

exp.s = "256/6"
Debug Eval(exp) ; 42
exp.s = "1.0*" + exp
Debug Eval(exp) ; 42.66..
BarryG
Addict
Addict
Posts: 4318
Joined: Thu Apr 18, 2019 8:17 am

Re: Expression as INPUT?

Post by BarryG »

I've never used SQLite before, so I didn't know it had to be prepended by "1.0*" to force floats. Thanks for explaining!
User avatar
STARGÅTE
Addict
Addict
Posts: 2297
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Expression as INPUT?

Post by STARGÅTE »

It's not only SQLite, it's in nearly all languages, even in PB:

Code: Select all

Debug 22/7 ; Int / Int = Int
Debug 22.0/7 ; Float / Int = Float
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
mk-soft
Always Here
Always Here
Posts: 6554
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Expression as INPUT?

Post by mk-soft »

Tip!

Add own functions to SQLite -> viewtopic.php?f=12&t=75264
My Projects EventDesigner V3 / ThreadToGUI / OOP-BaseClass / Windows: Module ActiveScript
PB v3.30 / v5.75 - OS Mac Mini - VM Window Pro / Linux Ubuntu
Downloads on my OneDrive
#NULL
Addict
Addict
Posts: 1504
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Expression as INPUT?

Post by #NULL »

You can also use CAST()

Code: Select all

Debug Eval("256/6")                ; 42
Debug Eval("CAST(256/6 as FLOAT)") ; 42.0
Debug Eval("CAST(256 as FLOAT)/6") ; 42.66..
Post Reply