## A Reverse Polish Notation calculator for COCO 3

Color BASIC, Extended Color BASIC, CoCo 3 BASIC, and Disk Extended Color BASIC call all be discussed here.

### A Reverse Polish Notation calculator for COCO 3

This is a program that I originally wrote for my Apple II some time ago and just recently ported to my COCO 3.

It is actually a very powerful calculator (precision issues aside) for dealing with very complex calculations. Being an RPN calculator it utilizes a stack. Operands (values/arguments) are placed on the stack and operations are then executed in appropriate order. The result of each operation is placed on the top of the stack and becomes the next available operand. Generally speaking there are two type of operations. Those that only a require a single operand (Square Root, Log, etc.), and those that require 2 (+, -, /, etc.). More than 2 are possible, but no such functions have been implemented in this calculator. As operations are performed operands are removed from the stack and as mentioned before, the result of each operation becomes the next operand on top of the stack. it is important to note that the operands are pulled from the stack right to left. For instance if your stack looks like this:

5
25

Then entering the operation'/' will be equivalent to 25/5 and the result, 5, will be placed on the stack One more example - if your stack looks like this:

3
4

Then entering the operation 'P' (for exponentiation) will be equivalent to 4^3 and the result, 64, will be placed on the stack. So, to enter a formula like 2*(3+(6/4)) you would enter 2, then 3, then 6, then 4, then / then + then * and the result, 9, will remain on the stack. Of course there is more than one way to skin a cat and you could certainly enter that formula differently, but the essential thing is the have your operations performed in the correct order on the appropriate operands.

When run the program displays the implemented operations. In addition 'CE' will pop the top value off the stack, 'CL' will clear the entire stack, 'E' enter Euler's constant (2.71828183), 'PI' will enter Pi (3.14159266) and ZZ will quit the program.

Have fun...
Code: Select all
`10 REM RPN CALCULATOR20 REM30 REM Eventually I want to extend this with additional40 REM   transcendental functions, especially relating to trigonometry.50 REM   However, as is, it is quite powerful with respect to60 REM   dealing with complex calculations.  A property of stack70 REM   oriented calculators.80 REM90 REM Ported from my original Applesoft version: 2017/11/1499 WIDTH 80100 REM110 REM *** DEFINE CONSTANTS ***120 REM130 SV=10140 SH=4150 PV=7160 PH=3170 EV=10180 EH=19190 N=22-SV300 REM310 REM *** INIT. VARS AND ARRAYS ***320 REM330 I=0340 DIM ST(256)500 REM510 REM *** PAINT INITIAL SCREEN ***520 REM540 WIDTH 80550 PRINT "AVAILABLE OPERATIONS"560 PRINT "+       %               LN (NATUAL LOG)   SIN"570 PRINT "-       P (EXPONENT)    LOG               COS"580 PRINT "*       EX (NAT EXP)    ! (FACTORIAL)     TAN"590 PRINT "/       S (SQR ROOT)    N (NEGATE)"1000 REM1010 REM *** TOKEN INPUT LOOP ***1020 REM1030 OP\$="N"1040 LOCATE PH, PV:PRINT "                  ":LOCATE PH, PV:INPUT A\$1050 IF A\$="CE" THEN GOSUB 4060:GOTO 12201060 IF A\$="CL" THEN GOSUB 4090:GOTO 12201070 IF A\$="+" THEN GOSUB 20301080 IF A\$="-" THEN GOSUB 20501090 IF A\$="*" THEN GOSUB 20701100 IF A\$="/" THEN GOSUB 20901110 IF A\$="P" THEN GOSUB 21201120 IF A\$="%" THEN GOSUB 21321125 IF A\$="EX" THEN GOSUB 21401130 IF A\$="COS" THEN GOSUB 21601140 IF A\$="SIN" THEN GOSUB 22101150 IF A\$="S" THEN GOSUB 21801160 IF A\$="TAN" THEN GOSUB 22301170 IF A\$="!" THEN GOSUB 22601172 IF A\$="LN" THEN GOSUB 23001173 IF A\$="LOG" THEN GOSUB 24001175 IF A\$="N" THEN ST(I)=-ST(I):OP\$="Y"1180 IF A\$="E" THEN A=2.71828183:GOSUB 4030:OP\$="Y"1190 IF A\$="PI" THEN A=3.14159266:GOSUB 4030:OP\$="Y"1200 IF A\$="ZZ" THEN CLS:END1210 IF OP\$="N" THEN A=VAL(A\$):GOSUB 40301220 GOSUB 60301230 GOTO 10302000 REM2010 REM *** FUNCTION AND OPERATION IMPLEMENTATION ***2020 REM2030 IF I<2 THEN GOTO 50302040 R=ST(I-1) + ST(I):GOTO 40402050 IF I<2 THEN GOTO 50302060 R=ST(I-1)-ST(I):GOTO 40402070 IF I<2 THEN GOTO 50302080 R=ST(I-1)*ST(I):GOTO 40402090 IF I<2 THEN GOTO 50302100 IF ST(I)=0 THEN GOTO 50602110 R=ST(I-1)/ST(I):GOTO 40402120 IF I<2 THEN GOTO 50302130 R=ST(I-1)^ST(I):GOTO 40402132 IF I<2 THEN GOTO 50302134 R=ST(I-1)/(ST(I)/100):GOTO 40402140 IF I<1 THEN GOTO 50302150 R=2.7182813^ST(I):GOTO 40502160 IF I<1 THEN GOTO 50302170 R=COS(ST(I)):GOTO 40502180 IF I<1 THEN GOTO 50302190 IF ST(I)<0 THEN GOTO 50602200 R=SQR(ST(I)):GOTO 40502210 IF I<1 THEN GOTO 50302220 R=SIN(ST(I)):GOTO 40502230 IF I<1 THEN GOTO 50302250 R=TAN(ST(I)):GOTO 40502260 IF I<1 THEN GOTO 50302270 IF ST(I)-ABS(INT(ST(I)))<>0 THEN GOTO 50402280 IF I>33 THEN GOTO 50502290 R=1:FOR Q=1 TO ST(I):R=R*Q:NEXT Q:GOTO 40502300 IF I<1 THEN GOTO 50302310 IF ST(I)<0 THEN GOTO 50602320 R=LOG(ST(I)):GOTO 40502400 IF I<1 THEN GOTO 50302410 IF ST(I)<0 THEN GOTO 50602420 R=LOG(ST(I))/LOG(10):GOTO 40504000 REM4010 REM *** STACK OPERATIONS ***4020 REM4030 I=I+1:ST(I)=A:RETURN4040 I=I-14050 ST(I)=R:GOTO 41004060 ST(I)=0:IF I>0 THEN I=I-1:GOTO 41004090 FOR I=256 TO 1 STEP -1:ST(I)=0:NEXT I4100 OP\$="Y":RETURN5000 REM5010 REM *** ERROR HANDLING ***5020 REM5030 MS\$="INSUFFICIENT VALUES FOR OPERATION":GOTO 50705040 MS\$="OPERAND MUST BE A POSITIVE INTEGER":GOTO 50705050 MS\$="OPERAND TOO LARGE":GOTO 50705060 MS\$=STR\$(ST(I))+" IS NOT A VALID OPERAND"5070 LOCATE EH, EV:PRINT MS\$:GOSUB 7030:LOCATE EH, EV:PRINT "                                   ";:OP\$="Y":RETURN6000 REM6010 REM *** SCREEN MANAGEMENT ***6020 REM6030 FOR J=SV TO 22:LOCATE SH, J:PRINT "                ";:NEXT J6040 K=SV-1:M=1:IF I>N THEN M=I-N6050 IF I>0 THEN FOR J=I TO M STEP -1:K=K+1:LOCATE SH, K:PRINT ST(J):NEXT J6070 RETURN7000 REM7010 REM *** TIMERS ***7020 REM7030 FOR T=0 TO 2000:NEXT T:RETURN`

BillO

Posts: 18
Joined: Wed Mar 07, 2018 9:19 am
Location: The deep woods of Central Ontario