OK so hello everyone! this is my first time ever using any programming program other than what is available on a Ti84... my programming skils currently are limited to math equations in which each variable has to be prompted for, and given explicite directions to follow in the code itself.
I was wondering if it were at all possible to create a program in which all you had to do was type in an equation, and it would solve it for you. this means im looking for something that would allow you to enter 2+2, and it would give you 4. this is opposed to what I have taught myself today how to do, which is for the program to prompt you on what operation you would like to do, and then for each variable (2 and 2), then would go through the code it has and adds the two variables together... this is extremely complicated, long and difficult for usage... is what im asking for possible?
Completely new and am SO confused
There are complex ways of doing it, but this is quick and dirty.
Code: Select all
' Pack an expression into an envelope, compile, run then get the result
Dim As String expression ' a numerical expression
Input "Enter expression.";expression
Dim As String program = $"C:\FreeBASIC\fbc.exe " ' the FB compiler
' create a FreeBASIC program
Open "scratch.bas" For Output As #1
Print #1, "open "; """scratch.txt"""; " for output as #2"
Print #1, "print #2, "; expression
Print #1, "close #2"
Close #1
' compile the scratch.bas program file
If Exec( program, " scratch.bas" ) <> 0 Then Print "Compile failure"
' run the exe program generated
If Exec( "scratch.exe", "" ) <> 0 Then Print "Execute failure"
' now get the result from the output file
Open "scratch.txt" For Input As #1
Dim As String result
Line Input #1, result
Close #1
Print "Result = ";result
Sleep
Re: Completely new and am SO confused
There are quite a few parsers available in Freebasic for doing this, here is one of them.Huzapro2 wrote:OK so hello everyone! this is my first time ever using any programming program other than what is available on a Ti84... my programming skils currently are limited to math equations in which each variable has to be prompted for, and given explicite directions to follow in the code itself.
I was wondering if it were at all possible to create a program in which all you had to do was type in an equation, and it would solve it for you. this means im looking for something that would allow you to enter 2+2, and it would give you 4. this is opposed to what I have taught myself today how to do, which is for the program to prompt you on what operation you would like to do, and then for each variable (2 and 2), then would go through the code it has and adds the two variables together... this is extremely complicated, long and difficult for usage... is what im asking for possible?
Code: Select all
' *********** START OF _EVAL **********************************
Dim Shared e_input As String
Dim Shared e_tok As String
Dim Shared e_spelling As String
Dim Shared e_error As Integer
Dim Shared x As Double 'x,y or z substitutes as a number in an expression
dim shared y as double
dim shared z as double
FUNCTION SEC(byval x AS DOUBLE) AS DOUBLE
SEC = 1 / COS(x)
END FUNCTION
FUNCTION COSEC(byval x AS DOUBLE) AS DOUBLE
COSEC = 1 / SIN(x)
END FUNCTION
FUNCTION COT(byval x AS DOUBLE) AS DOUBLE
COT = 1 / TAN(x)
END FUNCTION
FUNCTION ARCSIN(byval x AS DOUBLE) AS DOUBLE
ARCSIN = ATN(x / SQR(-x * x + 1))
END FUNCTION
FUNCTION ARCCOS(byval x AS DOUBLE) AS DOUBLE
ARCCOS = ATN(-x / SQR(-x * x + 1)) + 2 * ATN(1)
END FUNCTION
FUNCTION ARCSEC(byval x AS DOUBLE) AS DOUBLE
ARCSEC = ATN(x / SQR(x * x - 1)) + SGN((x) -1) * (2 * ATN(1))
END FUNCTION
FUNCTION ARCCOSEC(byval x AS DOUBLE) AS DOUBLE
ARCCOSEC = ATN(x / SQR(x * x - 1)) + (SGN(x) - 1) * (2 * ATN(1))
END FUNCTION
FUNCTION ARCCOT(byval x AS DOUBLE) AS DOUBLE
ARCCOT = ATN(x) + 2 * ATN(1)
END FUNCTION
FUNCTION ARCTAN(byval x AS DOUBLE) AS DOUBLE
ARCTAN = ATN(x)
END FUNCTION
FUNCTION sinh(byval x AS DOUBLE) AS DOUBLE
sinh = (EXP(x) - EXP(-x)) / 2
END FUNCTION
FUNCTION cosh(byval x AS DOUBLE) AS DOUBLE
cosh = (EXP(x) + EXP(-x)) / 2
END FUNCTION
FUNCTION tanh(byval x AS DOUBLE) AS DOUBLE
tanh = (EXP(x) - EXP(-x)) / (EXP(x) + EXP(-x))
END FUNCTION
FUNCTION sech(byval x AS DOUBLE) AS DOUBLE
sech = 2 / (EXP(x) + EXP(-x))
END FUNCTION
FUNCTION cosech(byval x AS DOUBLE) AS DOUBLE
cosech = 2 / (EXP(x) - EXP(-x))
END FUNCTION
FUNCTION coth(byval x AS DOUBLE) AS DOUBLE
coth = (EXP(x) + EXP(-x)) / (EXP(x) - EXP(-x))
END FUNCTION
FUNCTION arcsinh(byval x AS DOUBLE) AS DOUBLE
arcsinh = LOG(x + SQR(x * x + 1))
END FUNCTION
FUNCTION arccosh(byval x AS DOUBLE) AS DOUBLE
arccosh = LOG(x + SQR(x * x - 1))
END FUNCTION
FUNCTION arctanh(byval x AS DOUBLE) AS DOUBLE
arctanh = LOG((1 + x) / (1 - x)) / 2
END FUNCTION
FUNCTION arcsech(byval x AS DOUBLE) AS DOUBLE
arcsech = LOG((SQR(-x * x + 1) + 1) / x)
END FUNCTION
FUNCTION arccosech(byval x AS DOUBLE) AS DOUBLE
arccosech = LOG((SGN(x) * SQR(x * x + 1) +1) / x)
END FUNCTION
FUNCTION arccoth(byval x AS DOUBLE) AS DOUBLE
arccoth = LOG((x + 1) / (x - 1)) / 2
END FUNCTION
FUNCTION LOGN(byval x AS DOUBLE,byval n AS DOUBLE) AS DOUBLE
LOGN = LOG(x) / LOG(n)
END FUNCTION
FUNCTION HAVERSINE(byval x AS DOUBLE) AS DOUBLE
HAVERSINE = (SIN(x/2))^2
END FUNCTION
Function e_function(fun As String, arg As Double) As Double
Dim n As Double
Select Case Lcase$(fun)
Case "abs": n = Abs(arg)
Case "atn": n = Atn(arg)
Case "cos": n = Cos(arg)
Case "exp": n = Exp(arg)
Case "fix": n = Fix(arg)
Case "int": n = Int(arg)
Case "log": n = Log(arg)
Case "rnd": n = Rnd(arg)
Case "sgn": n = Sgn(arg)
Case "sin": n = Sin(arg)
Case "sqr": n = Sqr(arg)
Case "tan": n = Tan(arg)
case "haversine":n=haversine(arg)
case "cosec":n=cosec(arg)
case "sec":n=sec(arg)
case "cot": n=cot(arg)
case "arcsin":n=arcsin(arg)
case "arccos":n=arccos(arg)
case "arctan":n=arctan(arg)
case "arcsec":n=arcsec(arg)
case "arccosec":n=arccosec(arg)
case "arccot":n=arccot(arg)
case "sinh":n=sinh(arg)
case "cosh":n=cosh(arg)
case "tanh":n=tanh(arg)
case "sech":n=sech(arg)
case "cosech":n=cosech(arg)
case "coth":n=coth(arg)
case "arcsinh":n=arcsinh(arg)
case "arccoth":n=arccoth(arg)
case "arctanh":n=arctanh(arg)
case "arcsech":n=arcsech(arg)
case "arccosech":n=arccosech(arg)
case "arccoth":n=arccoth(arg)
Case Else
If Not e_error Then
'MsgBox("undefined function '" + fun + "'")
print "UNDEFINED FUNCTION " + fun
e_error = -1
End If
End Select
e_function = n
End Function
Sub e_nxt()
Dim is_keyword As Integer
Dim c As String
e_tok = ""
e_spelling = ""
Do
c = Left$(e_input, 1)
e_input = Mid$(e_input, 2)
Loop While c = " " Or c = Chr$(9) Or c = Chr$(13) Or c = Chr$(10)
Select Case Lcase$(c)
Case "0" To "9", ".","x","y","z" 'MODIFIED to support external x,y or z
If c="x" Then c=Str(x)
If c="y" Then c=Str(y)
If c="z" Then c=Str(z)
e_tok = "num"
Do
e_spelling = e_spelling + c
c = Left$(e_input, 1)
e_input = Mid$(e_input, 2)
Loop While (c >= "0" And c <= "9") Or c = "." Or c="x" or c="y" 'ALTERATION *********
e_input = c + e_input
' Identifier or keyword.
Case "a" To "z", "_"
Dim As Integer is_id
e_tok = "id"
Do
e_spelling = e_spelling + c
c = Lcase$(Left$(e_input, 1))
e_input = Mid$(e_input, 2)
is_id = (c >= "a" And c <= "z")
is_id = is_id Or c = "_" Or (c >= "0" And c <= "9")
Loop While is_id
e_input = c + e_input
is_keyword = -1
Select Case Lcase$(e_spelling)
Case "and"
Case "eqv"
Case "imp"
Case "mod"
Case "not"
Case "or"
Case "xor"
Case Else: is_keyword = 0
End Select
If is_keyword Then
e_tok = Lcase$(e_spelling)
End If
Case "<", ">"
e_tok = c
c = Left$(e_input, 1)
If c = "=" Or c = ">" Then
e_tok = e_tok + c
e_input = Mid$(e_input, 2)
End If
Case Else
e_tok = c
End Select
If e_spelling = "" Then
e_spelling = e_tok
End If
End Sub
Sub e_match (token As String)
If Not e_error And e_tok <> token Then
print "EXPECTED " + token + ", got '" + e_spelling + "'"
e_error = -1
End If
e_nxt
End Sub
Function e_prs (p As Integer) As Double
Dim n As Double
Dim fun As String
If e_tok = "num" Then
' number.
n = Val(e_spelling)
e_nxt
Elseif e_tok = "-" Then
e_nxt
n = -e_prs(11)
Elseif e_tok = "not" Then
e_nxt
n = Not e_prs(6)
Elseif e_tok = "(" Then
e_nxt
n = e_prs(1)
e_match(")")
Elseif e_tok = "id" Then
fun = e_spelling
e_nxt
e_match("(")
n = e_prs(1)
e_match(")")
n = e_function(fun, n)
Else
If Not e_error Then
print "syntax error, token = '" + e_spelling + "'"
e_error = -1
End If
End If
Do While Not e_error
If p <= 11 And e_tok = "^" Then
e_nxt: n = n ^ e_prs(12)
Elseif p <= 10 And e_tok = "*" Then
e_nxt: n = n * e_prs(11)
Elseif p <= 10 And e_tok = "/" Then
e_nxt: n = n / e_prs(11)
Elseif p <= 9 And e_tok = "" Then
e_nxt: n = n \ e_prs(10)
Elseif p <= 8 And e_tok = "mod" Then
e_nxt: n = n Mod e_prs(9)
Elseif p <= 7 And e_tok = "+" Then
e_nxt: n = n + e_prs(8)
Elseif p <= 7 And e_tok = "-" Then
e_nxt: n = n - e_prs(8)
Elseif p <= 6 And e_tok = "=" Then
e_nxt: n = n = e_prs(7)
Elseif p <= 6 And e_tok = "<" Then
e_nxt: n = n < e_prs(7)
Elseif p <= 6 And e_tok = ">" Then
e_nxt: n = n > e_prs(7)
Elseif p <= 6 And e_tok = "<>" Then
e_nxt: n = n <> e_prs(7)
Elseif p <= 6 And e_tok = "<=" Then
e_nxt: n = n <= e_prs(7)
Elseif p <= 6 And e_tok = ">=" Then
e_nxt: n = n >= e_prs(7)
Elseif p <= 5 And e_tok = "and" Then
e_nxt: n = n And e_prs(6)
Elseif p <= 4 And e_tok = "or" Then
e_nxt: n = n Or e_prs(5)
Elseif p <= 3 And e_tok = "xor" Then
e_nxt: n = n Xor e_prs(4)
Elseif p <= 2 And e_tok = "eqv" Then
e_nxt: n = n Eqv e_prs(3)
Elseif p <= 1 And e_tok = "imp" Then
e_nxt: n = n Imp e_prs(2)
Else
Exit Do
End If
Loop
e_prs = n
End Function
Function e_eval(Byval s As String,Byref value As Double) As Integer
e_error = 0
e_input = s
e_nxt
value = e_prs(1)
If e_tok <> "" And Not e_error Then
print "syntax error, token = '" + e_spelling + "'"
e_error = -1
End If
e_eval = Not e_error
End Function
function _EVAL(_exp as string) as double
dim ans as double
e_eval(_exp,ans)
return ans
end function
'___________ END OF _EVAL ___________________________
'USAGE
dim as string s
do
input "enter equation (or q to quit) ";s
if s="q" then exit do
print s;" = ";_eval(s)
loop
'part 2
cls
do
print "Example of an equation in the variable x ---> sin(x)+3*cos(3*x)"
print
input "enter an x value) ";x
input "Enter an equation in x (or q to quit) ",s
if s="q" then exit do
print s;" = ";_eval(s);" when x= ";x
loop
correct me if I am wrong, but are you asking how you can type in an equation and get the answer?
many modern programming languages have a built-in parser
For example in Freebasic,
many modern programming languages have a built-in parser
For example in Freebasic,
Code: Select all
Print (2 + 2) / 2
Sleep
@ dafhi.
We had both seen the problem before so we had the code to post. And yes, the FB forum is a dangerous place to post if you want to write your own original code.
There is a fundamental difference between “compile time” and “runtime”.
“Compile time” parsing is trivial in FB, but “Runtime” requires a coded parser.
It appears that Huzapro2 is wanting a “runtime” calculator facility, we gave two possible solutions.
Richard's solution uses the parser within the FB Compiler by writing another small FB program.
Dodicat's solution provides the parser as FB code which is more involved, but probably better in the long run as the FB compiler does not need to remain available once the main.exe is generated.
We had both seen the problem before so we had the code to post. And yes, the FB forum is a dangerous place to post if you want to write your own original code.
There is a fundamental difference between “compile time” and “runtime”.
“Compile time” parsing is trivial in FB, but “Runtime” requires a coded parser.
It appears that Huzapro2 is wanting a “runtime” calculator facility, we gave two possible solutions.
Richard's solution uses the parser within the FB Compiler by writing another small FB program.
Dodicat's solution provides the parser as FB code which is more involved, but probably better in the long run as the FB compiler does not need to remain available once the main.exe is generated.
-
- Posts: 1009
- Joined: Oct 11, 2008 7:42
- Location: ABQ, NM
- Contact:
Oh wow, you're going from Heiroglyphics flavor of 'BASiC' to something far different.my first time ever using any programming program other than what is available on a Ti84
In TI-84, they use triangles, and little 45 degree angle signs, what you might wanna do is look up DARTMOUTH BASIC and go over a *.PDF of what BASIC looks like away from a pocket calculator, then just look up any reference material on any flavor of BASIC.
There are some excellent examples of parsers in the forum already which will evaluate equations, even graph them, so just poke around.
Variable solving, graphing, parsers, parsing, use the SEARCH button and just look around. There's lots to see.
~Kiyote!