100 '		Calculator for HDOS or CP/M MBASIC. H-19 or H-89 only.
110 '		By Keith Boone, October 10/81
120 '
130 '		Simulates a simple four function calculator
140 '		on-screen. Originally designed as part of
150 '		a checkbook program which is still unfinished,
160 '		the calculator could be merged into any similar
170 '		program without difficulty.
180 '
190 '		This is a 12 digit calculator with 4 digits
200 '		precision to the right of the decimal place.
210 '		Change the multiplication factor in line
220 '		840 to increase or decrease this. i.e. 2 digits
230 '		only for dollars and cents formating.
240 '
250 ESC$=CHR$(27)		' escape
260 CUH$=ESC$+"H"		' cursor home
270 CLR$=ESC$+"E"		' erase page
280 BELL$=CHR$(7)		' bell
290 EOP$=ESC$+"J"		' erase to end of page
300 ERV$=ESC$+"p"		' enter reverse video
310 XRV$=ESC$+"q"		' exit reverse video
320 EGR$=ESC$+"F"		' enter graphics
330 XGR$=ESC$+"G"		' exit graphics
340 Y$=ESC$+"Y" 		' direct cursor addressing
350 L$=ESC$+"l" 		' erase line
360 CBK$=ESC$+"D"		' cursor back
370 X1$="#########.##"		' print using field
380 ' define function C$ for X,Y cursor positioning
390 DEF FN C$(R,C)=Y$+CHR$(R+31)+CHR$(C+31)
400 PRINT ESC$+"x1"		' enable 25th line
410 GOTO 450			' start main program
420 ' erase 25th line and home cursor
430 PRINT FN C$(25,1);L$;CUH$
440 RETURN
450 ' Calculate
460 GOSUB 420: PRINT CLR$
470 PRINT: PRINT EOP$;EGR$
480 PRINT ESC$+"x5"			' turn cursor off
490 PRINT FN C$(21,31);"faaaaaaaaaaaaac"
500 PRINT FN C$(22,31);"`             `"
510 PRINT FN C$(23,31);"eaaaaaaaaaaaaad"
520 PRINT XGR$;
530 PRINT FN C$(19,18);"The keypad may now be used as a calculator."
540 PRINT FN C$(25,10);
550 PRINT "EOp3тттттттттттттттттттт	фo mulitply, '/' to divide."
560 PRINT FN C$(22,32);"            0": N$=""
570 N$=N$+INPUT$(1)
580 IF ASC(RIGHT$(N$,1))=13 THEN N$="": N#=0: GOTO 560' check for clear/enter
590 IF ASC(RIGHT$(N$,1))=27 THEN 610			' check for escape
600 GOTO 620
610 N$=INPUT$(1): IF N$="Q" THEN PRINT ESC°$+"y5": N#=0: N$="": GOSUB 420:  PRINT CLR$: END ELSE PRINT BELL$: GOTO 560
620 IF RIGHT$(N$,1)="+" OR RIGHT$(N$,1)="-" OR RIGHT$(N$,1)="*" OR RIGHT$(N$,1)="/" OR RIGHT$(N$,1)="=" THEN 670
630 IF RIGHT$(N$,1)="." THEN 780
640 IF RIGHT$(N$,1)<"0" OR RIGHT$(N$,1)>"9" THEN N$=LEFT$(N$,LEN(N$)-1): PRINT BELL$: GOTO 780
650 IF LEN(N$)>12 THEN PRINT BELL$: N$=LEFT$(N$,12): GOTO 800
660 GOTO 780
670 PRINT FN C$(22,45-LEN(LEFT$((N$),LEN(N$)-1)));LEFT$((N$),LEN(N$)-1)
680 IF N#=0 THEN SIGN$="" 
690 IF SIGN$="" THEN N#=VAL(LEFT$((N$),LEN(N$)-1))
700 IF SIGN$="+" THEN N#=N#+VAL(LEFT$((N$),LEN(N$)-1))
710 IF SIGN$="-" THEN N#=N#-VAL(LEFT$((N$),LEN(N$)-1))
720 IF SIGN$="*" THEN N#=N#*VAL(LEFT$((N$),LEN(N$)-1))
730 IF SIGN$="/" AND VAL(LEFT$(N$,LEN(N$)-1))=0 THEN PRINT FN C$(22,36)"ERROR    ";BELL$: N$="": GOTO 570
740 IF SIGN$="/" THEN N#=N#/VAL(LEFT$((N$),LEN(N$)-1))
750 SIGN$=RIGHT$(N$,1):N$=""
760 IF SIGN$="=" THEN 830
770 GOTO 570
780 IF SIGN$="=" THEN SIGN$="": N#=0
790 IF  VAL(RIGHT$(N$,1))>=0  AND  VAL(RIGHT$(N$,1))<=9  THEN PRINT FN C$(22,32)"            " ELSE IF RIGHT$(N$,1)="." THEN PRINT FN C$(22,32)"            "
800 IF N$="" THEN N$="0"
810 PRINT FN C$(22,45-LEN(N$));N$: IF N$="0" THEN N$=""
820 GOTO 570
830 ' calculation finished
840 N$=STR$((INT(N#*10000))/10000)
850 IF LEN(N$)>13 THEN N$=ERV$+"E"+XRV$+LEFT$(N$,11): PRINT BELL$
860 IF LEFT$(N$,1)=ESC$ THEN PRINT FN C$(22,32);N$: GOTO 890
870 PRINT FN C$(22,32)"            "
880 PRINT FN C$(22,45-LEN(N$));N$:N$=""
890 GOTO 570
 P