1000 REMark TUTOR6_BAS
1005 REMark :
1010 REMark Action routines added for digits & decimal point.
1015 REMark INITIALISE amended to cope with new functions etc.
1017 :
1018 REMark $$asmb=flp1_calc_cde,4,82
1020 :
1025 initialise
1030 REPeat pointer_loop
1035   action = MCALL(#3 ; \0)
1040   SELect ON action
1045     = 0         : EXIT pointer_loop : REMark ESC
1050     = -10 TO -1 : digit action      : REMark DIGIT key
1055     = -11       : decimal           : REMark DECIMAL POINT
1060     = -15 TO -12: arithmetic action : REMark Function
1065     = -16       : equal             : REMark EQUALS
1070   END SELect 
1075   SELect ON action
1080     = -16 TO -1: action = MSTAT%(#3, action TO 0)
1085   END SELect 
1090 END REPeat pointer_loop
1095 MCLEAR #3
1100 CLAMP
1105 CLOSE #3
1110 CLOSE #4
1115 STOP
1120 :
1125 :
1130 DEFine PROCedure initialise
1135   OPEN #3,'con_'   : REMark Main console channel
1140   DIM display$(11) : REMark Current number being input
1145   DIM operator$(1) : REMark Arithmetic function to carry out
1150   decimal_point = 0: REMark Assume no DP yet in the number.
1155   OPEN #4,'scr_'   : REMark Display window channel
1160   INK #4,7         : REMark White ink for the display
1165   CSIZE #4,2,0     : REMark Big digits for the display
1170   :
1175   MDRAW#3, 'calc', 0, 0
1180   :
1185   show_answer '0'  : REMark Initialise the display
1190   operator$ = ''   : REMark No previous operation
1195   display$ = ''    : REMark No current input number
1200   running_total = 0: REMark The running total so far !
1205 END DEFine initialise
1210 :
1215 :
1220 DEFine FuNction right_justify$(n$)
1225   :
1230   REMark This function takes a string and returns it
1235   REMark right justified in 11 characters.
1240   :
1245   RETurn FILL$(' ', 11 - LEN(n$)) & n$
1250 END DEFine right_justify$
1255 :
1260 :
1265 DEFine PROCedure show_answer(text$)
1270   :
1275   REMark Print the current answer up on the display
1280   REMark but make it right justified first
1285   :
1290   MWLINK #3, 1, #4 : REMark Overlay #4 on application window 1
1295   PRINT #4, right_justify$(text$)
1300 END DEFine show_answer
1305 :
1310 :
1315 DEFine PROCedure digit(loose_item)
1320   LOCal number
1325   :
1330   REMark Add a single digit to the front of the number
1335   REMark currently being entered. This is held in
1340   REMark DISPLAY$ until an arithmetic action is entered.
1345   :
1350   number = ABS(loose_item) -1: REMark Convert to a digit 0 - 9
1355   IF LEN(display$) < 11
1360      display$ = display$ & number
1365      show_answer display$
1370   ELSE 
1375      BEEP 1000, 5
1380   END IF 
1385 END DEFine digit
1390 :
1395 :
1400 DEFine PROCedure decimal
1405   :
1410   REMark Add a decimal point to the current number
1415   REMark but only if there is not one already.
1420   :
1425   IF NOT (decimal_point)
1430      IF LEN (display$) < 11
1435         decimal_point = 1
1440         display$ = display$ & '.'
1445         show_answer display$
1450      ELSE 
1455         BEEP 1000, 5
1460      END IF 
1465   ELSE 
1470      BEEP 1000, 5
1475   END IF 
1480 END DEFine decimal
1485 :
1490 :
1495 DEFine PROCedure arithmetic(loose_item)
1500   LOCal answer$(12)
1505   :
1510   REMark This procedure caters for an arithmetic function
1515   REMark key being pressed, HIT or DO'd
1520   :
1525   REMark Cater for a leading minus first of all
1530   IF loose_item = -14 AND display$ = ''
1535      display$ = '-'
1540      show_answer display$
1545      RETurn 
1550   END IF 
1555   :
1560   REMark Calculate the answer so far
1565   answer$ = compute$(display$)
1570   :
1575   REMark Now set up the 'pending' operation
1580   SELect ON loose_item
1585     = -12: operator$ = '/'
1590     = -13: operator$ = '*'
1595     = -14: operator$ = '-'
1600     = -15: operator$ = '+'
1605   END SELect 
1610   :
1615   REMark display the answer so far
1620   show_answer answer$
1625   :
1630   REMark Get ready for next number
1635   display$ = ''
1640   decimal_point = 0
1645 END DEFine arithmetic
1650 :
1655 :
1660 DEFine FuNction compute$(o$)
1665   LOCal calc, number$(11)
1670   :
1675   REMark Calculate the total so far using the PREVIOUS
1680   REMark arithmetic function
1685   :
1690   number$ = o$
1695   REMark First check for special cases or invalid numbers
1700   IF o$ = '.' THEN number$ = '0.0': END IF 
1705   IF o$ = '-' THEN number$ = '': END IF 
1710   IF number$ <> ''
1715      calc = CODE(operator$)
1720      SELect ON calc
1725        = CODE('') : running_total = number$
1730                     display$ = ''
1735        = CODE('+'): running_total = running_total + number$
1740        = CODE('-'): running_total = running_total - number$
1745        = CODE('*'): running_total = running_total * number$
1750        = CODE('/'): IF number$ <> 0
1755                        running_total = running_total / number$
1760                     ELSE 
1765                        BEEP 1000,5
1770                        RETurn 'Divide by 0'
1775                     END IF 
1780      END SELect 
1785   END IF 
1790   RETurn running_total
1795 END DEFine compute$
1800 :
1805 :
1810 DEFine PROCedure equal
1815   LOCal answer$(12)
1820   :
1825   REMark Finish the previous calculation and display the
1830   REMark final result. Reset all totals etc for the next calc
1835   :
1840   REMark Finish off the final 'pending' operation
1845   answer$ = compute$(display$)
1850   :
1855   REMark Display the final result then stuff it !
1860   show_answer answer$
1865   HOT_STUFF answer$
1870   :
1875   REMark Reset the calculator
1880   running_total = 0
1885   decimal_point = 0
1890   display$ = ''
1895   operator$ = ''
1900 END DEFine equal
