Code: Select all
;Expression Evaluator
;written by Jay Reeve
;July 2001
;Release 7 compliance update by STAZ { 8/9/02 }
Procedure.s evaluate(theStr.s)
Define.s v1, v2, v3, result
Define.l Lparen, Rparen, parenCount, r, r1, n
Define.d x, fact
;-----------------------------------------------------------------
theStr = LCase(theStr)
If Len(theStr)
If FindString(theStr, "(" ,1)
Lparen = FindString(theStr, "(" ,1)
v2 = Mid(theStr, Lparen+1)
parenCount = 1
For r = 0 To Len(v2)-1
If Mid(v2,r,1) = "("
parenCount +1
EndIf
If Mid(v2,r,1) = ")"
parenCount -1
EndIf
If parenCount = 0
Break
EndIf
Next
; r now "string indexes" the right paren
; r+1 is therefore aimed at the right paren in string
v3 = Mid (v2, r+1) ; tail without right parenthesis
v2 = Left(v2, r-1) ; body without either parenthesis
v1 = Left(theStr, Lparen-1) ; head without left parenthesis
result = evaluate(v1 + evaluate(v2) + v3)
;---------------------------------------------------------
ElseIf FindString(theStr,"+",1)
r = FindString(theStr,"+",1)
v1 = evaluate(Left(theStr,r-1))
v2 = evaluate(Mid (theStr,r+1))
result = StrD(ValD(v1) + ValD(v2))
ElseIf FindString(theStr,"-",1)
r = FindString(theStr,"-",1)
v1 = evaluate(Left(theStr,r-1))
v2 = evaluate(Mid (theStr,r+1))
result = StrD(ValD(v1) - ValD(v2))
ElseIf FindString(theStr,"*",1)
r = FindString(theStr,"*",1)
v1 = evaluate(Left(theStr,r-1))
v2 = evaluate(Mid (theStr,r+1))
result = StrD(ValD(v1) * ValD(v2))
ElseIf FindString(theStr,"/",1)
r = FindString(theStr,"/",1)
v1 = evaluate(Left(theStr,r-1))
v2 = evaluate(Mid (theStr,r+1))
result = StrD(ValD(v1) / ValD(v2))
ElseIf FindString(theStr,"^",1)
r = FindString(theStr,"^",1)
v1 = evaluate(Left(theStr,r-1))
v2 = evaluate(Mid (theStr,r+1))
result = StrD(Pow(ValD(v1) , ValD(v2)))
ElseIf FindString(theStr,"!",1)
r = FindString(theStr,"!",1)
v1 = evaluate(Left(theStr,r-1))
n = ValD(v1)
fact = 1
For r = 1 To n
fact * r
Next
result = StrD(fact)
ElseIf FindString(theStr,"sqrt",1)
r = FindString(theStr,"sqrt",1)
v1 = evaluate(Mid (theStr,r+4))
result = StrD(Sqr(ValD(v1)))
ElseIf FindString(theStr,"sqr",1)
r = FindString(theStr,"sqr",1)
v1 = evaluate(Mid (theStr,r+3))
result = StrD(ValD(v1)*ValD(v1))
ElseIf FindString(theStr,"asin",1)
r = FindString(theStr,"asin",1)
v1 = evaluate(Mid (theStr,r+4))
result = StrD(ASin(ValD(v1)))
ElseIf FindString(theStr,"acos",1)
r = FindString(theStr,"acos",1)
v1 = evaluate(Mid (theStr,r+4))
result = StrD(ACos(ValD(v1)))
ElseIf FindString(theStr,"atan",1)
r = FindString(theStr,"atan",1)
v1 = evaluate(Mid (theStr,r+4))
result = StrD(ATan(ValD(v1)))
ElseIf FindString(theStr,"sin",1)
r = FindString(theStr,"sin",1)
v1 = evaluate(Mid (theStr,r+3))
result = StrD(Sin(ValD(v1)))
ElseIf FindString(theStr,"cos",1)
r = FindString(theStr,"cos",1)
v1 = evaluate(Mid (theStr,r+3))
result = StrD(Cos(ValD(v1)))
ElseIf FindString(theStr,"tan",1)
r = FindString(theStr,"tan",1)
v1 = evaluate(Mid (theStr,r+3))
result = StrD(Tan(ValD(v1)))
ElseIf FindString(theStr,"alog",1)
r = FindString(theStr,"alog",1)
v1 = evaluate(Mid (theStr,r+4))
result = StrD(Pow(10,ValD(v1)))
ElseIf FindString(theStr,"log",1)
r = FindString(theStr,"log",1)
v1 = evaluate(Mid (theStr,r+3))
result = StrD(Log(ValD(v1))*0.4342944819032518)
ElseIf FindString(theStr,"ln",1)
r = FindString(theStr,"ln",1)
v1 = evaluate(Mid (theStr,r+2))
result = StrD(Log(ValD(v1)))
ElseIf FindString(theStr,"exp",1)
r = FindString(theStr,"exp",1)
v1 = evaluate(Mid (theStr,r+3))
result = StrD(Pow(2.718281828459045,ValD(v1)))
Else
result = theStr
EndIf
Else
result=""
EndIf
ProcedureReturn result
EndProcedure
OpenConsole()
expr.s = " "
PrintN( "press RETURN by itself to end")
While Len(expr)
Print("enter math expression ")
expr=Input()
PrintN(evaluate( expr ))
Wend
CloseConsole()