Sync On : Set display mode 800,600,32
 
Type coor
   x#
   y#
EndType
Dim ops$(4,4)
ops$(1,1)="c"
ops$(1,2)="s"
ops$(1,3)="t"
ops$(1,4)="a"
ops$(2,1)="^"
ops$(3,1)="*"
ops$(3,2)="/"
ops$(4,1)="+"
ops$(4,2)="-"
enter$=""
Dim geq$(10)
Dim graph(10000,2) as coor
Dim eqs$(50)
Dim var#(50)
Dim op$(50)
global string$=""
global in$=""
global enter$=""
global error$=""
 
Do
 
   CLS
 
   rem Menu Prompt
   Ink RGB(255,255,255),0
   Text 10,10,"What would you like to do?"
   Text 10,30,"1)Equation Solver"
   Text 10,50,"2)Grapher"
   Text 10,70,"3)Exit"
 
   Ink RGB(255,0,0),0
   Text 10,110,"> "+in$
 
   Gosub handle_input
   If enter$<>"" Then Gosub handle_action
 
   Sync
Loop
 
`Handle the input
handle_input:
 
   `Input
   If ReturnKey()=1 and hold=0 Then hold=1
   If ReturnKey()=0
      If hold=1
         hold=0
         Clear entry buffer
         in$=lower$(in$)
         enter$=in$
         in$=""
      Else
         in$=entry$(1)
      Endif
   Endif
   If Scancode()=0 and hold=1 Then hold=0
 
Return
 
handle_action:
 
   Select enter$
      Case "1":
         string$="Enter Equation (e.g. 2+(3*4)): "
         Gosub prompt
         error$=""
         eq$="("+lower$(fixString(enter$))+")"
         ans#=eSolver(eq$)
         ans$=toString(ans#)
         While Spacekey()=0
            Cls
            Ink RGB(0,255,0),0
            Text 10,10,"The answer is: "+ans$
            Text 10,30,"Hit [SpaceBar] to go back"
            If error$<>"" Then Text 10,50,error$
            Sync
         Endwhile
      Endcase
      Case "2":
         string$="Enter Equation to graph using variable 'x' (e.g. y=2+(x*4)): "
         Gosub prompt
         error$=""
         eq$="("+lower$(fixString(enter$))+")"
         grapher(eq$,"x")
      Endcase
      Case "3":
         time=timer()
         While timer()<time+1000
            Cls : Text 10,10,"Good Bye!"
            Sync
         EndWhile
         End
      Endcase
      Case Default:
      Endcase
   EndSelect
   enter$="" : Clear Entry Buffer : in$=""
 
Return
 
prompt:
 
   enter$=""
   While enter$=""
      Cls
      Ink RGB(255,255,0),0
      Text 10,10,string$
      Ink RGB(255,0,0),0
      Text 10,30,"> "+in$+"_"
      Gosub handle_input
      Sync
   EndWhile
 
Return
 
Function eSolver(eq$)
 
   eqNum=0
   For i=1 to len(eq$)
      s$=mid$(eq$,i)
      If s$="("
         Inc eqNum
         eqs$(eqNum)=""
      Else
         If s$=")"
            If eqNum>1
               If eqNum>0 Then Dec eqNum
               eqs$(eqNum)=eqs$(eqNum)+str$(solve(eqs$(eqNum+1)))
            EndIf
         Else
            eqs$(eqNum)=eqs$(eqNum)+s$
         Endif
      Endif
   Next i
 
   ans#=solve(eqs$(1))
 
EndFunction ans#
 
Function grapher(eq$,var$)
 
   xMin#=-10.0
   xMax#=10.0
   xStep#=1.0
   yMin#=-10.0
   yMax#=10.0
   yStep#=1.0
   gStep#=.01
   geqNum=1
 
   geq$(1)=""
   For i=1 to len(eq$)
      s$=mid$(eq$,i)
      If s$=var$
         Inc geqNum
         geq$(geqNum)=""
      Else
         geq$(geqNum)=geq$(geqNum)+s$
      Endif
   Next i
 
   x#=xMin#
   gNum=((xMax#-xMin#)/gStep#)+1
   For i=1 to gNum
      graph(i).x#=x# : graph(i).y#=graphSolver(geqNum,x#)
      Inc x#,gStep#
   Next i
 
   sw=Screen width()
   sh=Screen height()
   gw#=xMax#-xMin#
   gh#=yMax#-yMin#
   gxr#=sw/gw#
   gyr#=sh/gh#
   enter$="0"
   value#=graphSolver(geqNum,val(enter$))
   bx=(sw/2.0)+val(enter$)*gxr# : by=(sh/2.0)-value#*gyr#
   `Draw graph
   While Spacekey()=0
      CLS
 
      Ink RGB(128,128,128),0
      x#=xMin#
      For i=1 to ((xMax#-xMin#)/xStep#)+1
         Line (sw/2.0)+x#*gxr#,0,(sw/2.0)+x#*gxr#,sh
         Inc x#,xStep#
      Next
      y#=yMin#
      For i=1 to ((yMax#-yMin#)/yStep#)+1
         Line 0,(sh/2.0)+y#*gyr#,sw,(sh/2.0)+y#*gyr#
         Inc y#,yStep#
      Next
 
      Ink RGB(255,255,255),0
      Line sw/2.0,0,sw/2.0,sh
      Line 0,sh/2.0,sw,sh/2.0
 
      Ink RGB(255,255,0),0
      For i=1 to gNum-1
         Line (sw/2.0)+graph(i).x#*gxr#,(sh/2.0)-graph(i).y#*gyr#,(sw/2.0)+graph(i+1).x#*gxr#,(sh/2.0)-graph(i+1).y#*gyr#
      Next i
 
      Ink RGB(0,255,0),0
      If error$<>"" Then Center Text sw/2,5,error$
      Center Text sw/2,sh-30,"Hit [SpaceBar] to go Back"
      Center Text sw/2,sh-15,"Hit [Shift] to enter a new value for 'x', or use Arrow Keys<-->"
      Text 10,10,"X("+enter$+")="+toString(value#)
 
      Ink RGB(255,0,0),0
      Circle bx,by,sh/300.0
 
      If Shiftkey()=1
         Ink RGB(0,255,0),0
         string$="Enter a value for 'x':"
         Gosub prompt
         value#=graphSolver(geqNum,val(enter$))
         bx=(sw/2.0)+val(enter$)*gxr# : by=(sh/2.0)-value#*gyr#
      Endif
      If RightKey()=1 and hold=0
         hold=timer()+100
         enter$=toString(val(enter$)+gStep#)
         value#=graphSolver(geqNum,val(enter$))
         bx=(sw/2.0)+val(enter$)*gxr# : by=(sh/2.0)-value#*gyr#
      Endif
      If LeftKey()=1 and hold=0
         hold=timer()+100
         enter$=toString(val(enter$)-gStep#)
         value#=graphSolver(geqNum,val(enter$))
         bx=(sw/2.0)+val(enter$)*gxr# : by=(sh/2.0)-value#*gyr#
      Endif
      If Scancode()=0 Then hold=0
      If timer()>=hold Then hold=0
 
      Sync
   Endwhile
 
EndFunction
 
Function graphSolver(geqNum,x#)
 
      s$=""
      For t=1 to geqNum
         s$=s$+geq$(t)
         If t<geqNum Then s$=s$+str$(x#)
      Next t
      ans#=eSolver(s$)
 
EndFunction ans#
 
Function solve(eq$)
 
   varNum=0
   opNum=0
   ans#=0
 
   var$=""
   e$=""
   For i=1 to len(eq$)+1
      olde$=e$
      e$=mid$(eq$,i)
 
      If (e$>="0" and e$<="9") or e$="."
         var$=var$+e$
      Else
 
         If var$<>""
            Inc varNum
            var#(varNum)=val(var$)
            var$=""
         Endif
 
         If e$="^" or e$="*" or e$="/" or e$="+" or e$="-" or e$="c" or e$="s" or e$="t" or e$="a"
            If e$="-" and (olde$<"0" or olde$>"9")
               var$="-"
            Else
               Inc opNum
               op$(opNum)=e$
            Endif
         Endif
 
      Endif
   Next i
 
   If opNum+1<>varNum
      error$="Cannot Calculate"
      ExitFunction 0.0
   Endif
 
   For o=1 to 4
      i=1
      While i<=opNum and varNum>1
         If op$(i)=ops$(o,1) or op$(i)=ops$(o,2) or op$(i)=ops$(o,3) or op$(i)=ops$(o,4)
            Select op$(i)
               Case "a":
                  var#(i)=abs(var#(i+1)) : EndCase
               Case "s":
                  var#(i)=sin(var#(i+1)) : EndCase
               Case "c":
                  var#(i)=cos(var#(i+1)) : EndCase
               Case "t":
                  var#(i)=tan(var#(i+1)) : EndCase
               Case "^":
                  var#(i)=var#(i)^var#(i+1) : EndCase
               Case "*":
                  var#(i)=var#(i)*var#(i+1) : EndCase
               Case "/":
                  If var#(i+1)<>0
                     var#(i)=var#(i)/var#(i+1)
                  Else
                     error$="Division By 0 Error"
                     ExitFunction 0.0
                  Endif
               EndCase
               Case "+":
                  var#(i)=var#(i)+var#(i+1) : EndCase
               Case "-":
                  var#(i)=var#(i)-var#(i+1) : EndCase
            EndSelect
            Dec varNum
            For t=i+1 to varNum
               var#(t)=var#(t+1)
            Next t
            Dec opNum
            For t=i to opNum
               op$(t)=op$(t+1)
            Next t
            Dec i
         Endif
         Inc i
      Endwhile
 
      If varNum=1 Then ans#=var#(1) : Exit
   Next o
 
EndFunction ans#
 
Function fixString(s$)
 
   e$=""
   ts$=""
   For i=1 to len(s$)
      oldts$=ts$
      ts$=lower$(mid$(s$,i))
      If (ts$>="0" and ts$<="9") or ts$="a" or ts$="c" or ts$="s" or ts$="t" or ts$="^" or ts$="*" or ts$="/" or ts$="+" or ts$="-" or ts$="(" or ts$=")" or ts$="x" or ts$="p" or ts$="e"
         If (ts$="x" and ((oldts$>="0" and oldts$<="9") or oldts$=")")) or (oldts$="x" and ((ts$>="0" and ts$<="9") or ts$="(")) or (oldts$=")" and ts$>="0" and ts$<="9") or (oldts$>="0" and oldts$<="9" and ts$="(")
            e$=e$+"*"+ts$
         Else
            If ts$="s" or ts$="c" or ts$="t" or ts$="a"
               If oldts$>="0" and oldts$<="9"
                  e$=e$+"*1"+ts$
               Else
                  e$=e$+"1"+ts$
               Endif
            Else
               If ts$="p" or ts$="e"
                  If ts$="p"
                     e$=e$+"3.141592654"
                  Else
                     e$=e$+"2.718281828"
                  Endif
               Else
                  If ts$<>" " Then e$=e$+mid$(s$,i)
               Endif
            Endif
         Endif
      Else
         error$="Unrecognized character(s) found! May not be calculated correctly!"
      Endif
   Next i
 
EndFunction e$
 
Function toString(a#)
 
   If a#=floor(a#) or a#=ceil(a#)
      ans$=str$(a#)
   Else
      ans1$=""
      ans2$=""
      ans3$=""
      ans4$=""
      d=0
      For i=1 to len(str$(a#))
         s$=mid$(str$(a#),i)
         If s$<>"."
            If d=0
               ans1$=ans1$+s$
            Else
               If d=1
                  If s$="0"
                     ans2$=ans2$+s$
                  Else
                     d=2
                  Endif
               Endif
               If s$<>"e"
                  If d=2
                     ans3$=ans3$+s$
                  Else
                     If d=3 Then ans4$=ans4$+s$
                  Endif
               Else
                  d=3
               Endif
            Endif
         Else
            d=1
         Endif
      Next i
      a#=val(ans3$)/(10.0^(len(ans3$)-6+len(ans2$)))
      a=int(a#)
      If len(str$(a))>6-len(ans2$)
         If len(ans2$)>0
            ans2$=left$(ans2$,len(ans2$)-1)
         Else
            ans1$=str$(val(ans1$)+1)
            a=val(right$(str$(a),len(str$(a))-1))
         Endif
      Endif
      If (a#-a)>=.5
         Inc a
         If len(str$(a))>6-len(ans2$)
            If len(ans2$)>0
               ans2$=left$(ans2$,len(ans2$)-1)
            Else
               ans1$=str$(val(ans1$)+1)
               a=val(right$(str$(a),len(str$(a))-1))
            Endif
         Endif
      Endif
      If len(ans4$)>0
         ans$=ans1$+"."+ans2$+str$(a)+"e"+ans4$
      Else
         ans$=ans1$+"."+ans2$+str$(a)
      Endif
   Endif
 
EndFunction ans$