' evaluate expression routine ' the following operators are recognized in order: ' () ^ */ +- ' ' globals: ' expr\$ = string containing expression ' f\$ = element of expression ' pos% = position in string containing expression ' e! = true: error occured ' REPEAT LINE INPUT "Enter an expression: ";expr\$ IF expr\$<>"" expr\$=UPPER\$(expr\$) pos%=1 e!=FALSE a=@expr IF e!=FALSE AND (pos%-1)=LEN(expr\$) PRINT a ELSE PRINT "error in expression" ENDIF ENDIF UNTIL expr\$="" ' FUNCTION expr LOCAL a ' IF LEN(expr\$)=0 RETURN 0 ELSE a=@muldiv WHILE f\$="+" OR f\$="-" IF f\$="+" ADD a,@muldiv ELSE SUB a,@muldiv ENDIF WEND RETURN a ENDIF ENDFUNC ' FUNCTION muldiv LOCAL a ' a=@power WHILE f\$="*" OR f\$="/" IF f\$="*" MUL a,@factor ELSE DIV a,@factor ENDIF @getelt WEND RETURN a ENDFUNC ' FUNCTION power LOCAL a ' a=@factor @getelt WHILE f\$="^" a=a^@factor @getelt WEND RETURN a ENDFUNC ' FUNCTION factor LOCAL a ' @getelt IF f\$=")" RETURN 0 ELSE IF @checknum(f\$)=TRUE RETURN VAL(f\$) ELSE IF f\$="(" a=@expr IF f\$<>")" e!=TRUE RETURN 0 ELSE RETURN a ENDIF ELSE e!=TRUE RETURN 0 ENDIF ENDFUNC ' FUNCTION checknum(f\$) LOCAL decimal!,x\$ ' decimal!=FALSE IF LEFT\$(f\$)="-" t%=2 ELSE t%=1 ENDIF FOR t%=t% TO LEN(f\$) x\$=MID\$(f\$,t%,1) IF INSTR("0123456789.",x\$)=0 RETURN FALSE ENDIF IF x\$="." IF decimal!=TRUE RETURN FALSE ELSE decimal!=TRUE ENDIF ENDIF NEXT t% RETURN TRUE ENDFUNC ' PROCEDURE getelt LOCAL l%,x\$,x1\$,fn\$ ' f\$="" l%=LEN(expr\$) x\$=MID\$(expr\$,pos%,1) WHILE x\$=" " AND pos%<=l% INC pos% x\$=MID\$(expr\$,pos%,1) WEND prev\$=MID\$(expr\$,pos%-1,1) next\$=MID\$(expr\$,pos%+1,1) fn\$=MID\$(expr\$,pos%,4) IF fn\$="SIN(" ADD pos%,4 f\$=STR\$(SIN(@expr)) ELSE IF fn\$="COS(" ADD pos%,4 f\$=STR\$(COS(@expr)) ELSE IF fn\$="TAN(" ADD pos%,4 f\$=STR\$(TAN(@expr)) ELSE IF fn\$="ATN(" ADD pos%,4 f\$=STR\$(ATN(@expr)) ELSE IF fn\$="DEG(" ADD pos%,4 f\$=STR\$(DEG(@expr)) ELSE IF fn\$="RAD(" ADD pos%,4 f\$=STR\$(RAD(@expr)) ELSE IF fn\$="EXP(" ADD pos%,4 f\$=STR\$(EXP(@expr)) ELSE IF fn\$="LOG(" ADD pos%,4 f\$=STR\$(LOG(@expr)) ELSE IF fn\$="SQR(" ADD pos%,4 f\$=STR\$(SQR(@expr)) ELSE IF x\$="-" AND INSTR("0123456789",next\$)<>0 AND INSTR("0123456789",prev\$)=0 f\$=x\$ INC pos% x\$=next\$ ENDIF IF INSTR("()^*/+-",x\$)<>0 f\$=x\$ INC pos% ELSE WHILE INSTR("()^*/+- ",x\$)=0 AND pos%<=l% f\$=f\$+x\$ INC pos% x\$=MID\$(expr\$,pos%,1) WEND ENDIF ENDIF RETURN