home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Overload
/
ShartewareOverload.cdr
/
games
/
rpn.zip
/
RPN.BAS
< prev
next >
Wrap
BASIC Source File
|
1986-05-05
|
11KB
|
333 lines
1 REM --- RPN: PROGRAMMABLE RPN CALCULATOR
2 REM WRITTEN BY FRANK LAROSA
3 REM SEARCHLIGHT BBS (516) 724-0971
4 REM
5 REM WRITTEN FOR IBM-PC MICROSOFT BASIC COMPILER
6 REM MAY ALSO BE RUN UNDER INTERPRETED BASIC
7 REM
10 CLEAR:DEFINT A-D,N,I,P:LC=LOG(10):EE=LOG(1)
12 X=0:N1=0:Q=0:B=0:A=0:I=0:FX=4:FX$=STRING$(15-FX,"#")+"."+STRING$(FX,"#")
14 DEF FNR(X,Y)=INT(X*10^Y+.5)/10^Y
20 DIM S(4),A%(10),L%(50),P(1000),R(1000),K(300):AP=0
30 C$="RCIGTOSTOSTILBLRCLGTIGSBGSICONSUMPRDFIXROUDSZ"
40 C$=C$+"ENTADDSUBMPYDIVEXPABSCHS1/XLNXLOGALGALNSINCOSTAN"
50 C$=C$+"ASNACSATNRNDENDINTFRCPI PRXRTNRLDRLUEXCX=YX#YX<Y"
60 C$=C$+"X=0X#0X<0INPPRSNOPSQUSQRCLXCLSCLR":N=0:M$="LGIRSQXDF?"
65 ON ERROR GOTO 1400
70 CLS:PRINT:PRINT "RPN Programmable Calculator v1.4 by Frank LaRosa, 1/84 IBM version 9/85"
80 PRINT:PRINT "Enter ? for help.":PRINT:OL=15:GOTO 500
100 REM
102 REM EXECUTE
104 MD=0:IF Q=0 THEN Q=1
105 PRINT:FL=0:IF K1$<>"" THEN OPEN "O",1,K1$:PRINT "Outputting to file: ";K1$:FL=1
106 IF Q=1 THEN 108:ELSE IF P(Q-1)<OL+1 AND P(Q-2)>OL THEN Q=Q+1
108 N1=Q:GOTO 112
110 N1=N1+1:IF MD=1 THEN RETURN
111 IF INKEY$=CHR$(27) THEN PRINT:PRINT "Break in";N1:RETURN
112 A=P(N1):ON A GOTO 130,154,136,142,150,122,160,164,174,180,390,400,410,420,430,184,188,194,198,202,206,212,216,220,224,228
113 IF A=0 THEN RETURN
114 ON A-26 GOTO 232,236,240,244,248,252,258,264,268,272,276,280,284,287,292,300,308,316,322,330
116 ON A-46 GOTO 336,342,348,354,360,366,372,376,380,440,445,450
118 RETURN
120 REM RCL
122 N1=N1+1:B=P(N1):X=R(B)
124 S(4)=S(3):S(3)=S(2)
126 S(2)=S(1):S(1)=X:GOTO 110
128 REM RCI
130 N1=N1+1:B=P(N1):IF R(B)>-1 AND R(B)<501 THEN 132
131 PRINT "Range error in";N:RETURN
132 X=R(R(B)):GOTO 124
134 REM STO
136 N1=N1+1
138 R(P(N1))=S(1):GOTO 110
140 REM STI
142 N1=N1+1
144 B=R(P(N1)):IF B<0 OR B>500 THEN 131
146 R(B)=S(1):GOTO 110
148 REM LBL
150 N1=N1+1:GOTO 110
152 REM GTO
154 B=P(N1+1):IF B>50 THEN 155 ELSE IF L%(B)<>0 THEN 156
155 PRINT "Branch error in";N-1:RETURN
156 N1=L%(B):MD=2:GOTO 112
158 REM GTI
160 B=R(P(N1+1)):IF L%(B)<>0 THEN 156:ELSE GOTO 155
162 REM GSB
164 IF AP<10 THEN 168
166 PRINT "Too many GSB statements in";N1:RETURN
168 B=P(N1+1):IF L%(B)=0 THEN 155
170 AP=AP+1:A%(AP)=N1+2:GOTO 156
172 REM GSI
174 IF AP>=4 THEN 166
176 B=R(P(N1+1)):IF L%(B)=0 THEN 155:ELSE GOTO 170
178 REM CON
180 N1=N1+2:X=CVS(MKI$(P(N1-1))+MKI$(P(N1))):GOTO 124
182 REM ENT
184 X=S(1):GOTO 124
186 REM ADD
188 S(1)=S(1)+S(2)
190 S(2)=S(3):S(3)=S(4):GOTO 110
192 REM SUB
194 S(1)=S(2)-S(1):GOTO 190
196 REM MPY
198 S(1)=S(1)*S(2):GOTO 190
200 REM DIV
202 S(1)=S(2)/S(1):GOTO 190
204 REM EXP
206 IF S(2)<0 AND S(1)<1 THEN PRINT "Math error in";N1:RETURN
208 S(1)=S(2)^S(1):GOTO 190
210 REM ABS
212 S(1)=ABS(S(1)):GOTO 110
214 REM CHS
216 S(1)=-S(1):GOTO 110
218 REM 1/X
220 S(1)=1/S(1):GOTO 110
222 REM LNX
224 S(1)=LOG(S(1)):GOTO 110
226 REM LOG
228 S(1)=LOG(S(1))/LC:GOTO 110
230 REM ALG
232 S(1)=10^S(1):GOTO 110
234 REM ALN
236 S(1)=EXP(S(1)):GOTO 110
238 REM SIN
240 S(1)=SIN(S(1)):GOTO 110
242 REM COS
244 S(1)=COS(S(1)):GOTO 110
246 REM TAN
248 S(1)=TAN(S(1)):GOTO 110
250 REM ASN
252 X=S(1)
254 S(1)=ATN(X/SQR(-X*X+1)):GOTO 110
256 REM ACS
258 X=S(1)
260 S(1)=-ATN(X/SQR(-X*X+1))+1.5708:GOTO 110
262 REM ATN
264 S(1)=ATN(S(1)):GOTO 110
266 REM RND
268 X=RND(0):GOTO 124
270 REM R/S
272 PRINT:PRINT "END in step";N1:RETURN
274 REM INT
276 S(1)=INT(S(1)):GOTO 110
278 REM FRC
280 S(1)=S(1)-INT(S(1)):GOTO 110
282 REM PI
284 X=3.14159:GOTO 124
286 REM PRX
287 IF FL THEN PRINT #1,"--> ";:PRINT #1,USING FX$;S(1):GOTO 110
288 PRINT "--> ";:PRINT USING FX$;S(1):GOTO 110
290 REM RTN
292 IF AP>0 THEN 296
294 PRINT "RTN without GSB in step";N1:RETURN
296 MD=2:N1=A%(AP):AP=AP-1:IF N1>N THEN RETURN:ELSE 112
298 REM RLD
300 X=S(1)
302 S(1)=S(2):S(2)=S(3):S(3)=S(4)
304 S(4)=X:GOTO 110
306 REM RLU
308 X=S(4)
310 S(4)=S(3):S(3)=S(2):S(2)=S(1)
312 S(1)=X:GOTO 110
314 REM EXC
316 X=S(1):S(1)=S(2)
318 S(2)=X:GOTO 110
320 REM X=Y
322 IF S(1)=S(2) THEN 110
324 N1=N1+1:IF P(N1)<OL+1 THEN N1=N1+1
326 GOTO 110
328 REM X#Y
330 IF S(1)<>S(2) THEN 110
332 GOTO 324
334 REM X<Y
336 IF S(1)<S(2) THEN 110
338 GOTO 324
340 REM X=0
342 IF S(1)=0 THEN 110
344 GOTO 324
346 REM X#0
348 IF S(1)<>0 THEN 110
350 GOTO 324
352 REM X<0
354 IF S(1)<0 THEN 110
356 GOTO 324
358 REM INP
360 LINE INPUT "* ";X$
362 X=VAL(X$):GOTO 124
364 REM PRS
366 FOR I=1 TO 4:IF FL THEN PRINT #1,USING FX$;S(I);:ELSE PRINT USING FX$;S(I);
368 NEXT:IF FL THEN PRINT #1,: ELSE PRINT
369 GOTO 110
370 REM NOP
372 GOTO 110
374 REM SQU
376 S(1)=S(1)*S(1):GOTO 110
378 REM SQR
380 IF S(1)>=0 THEN S(1)=SQR(S(1)):GOTO 110
382 PRINT "Negative SQR in";N1:RETURN
390 REM SUM
392 N1=N1+1:B=P(N1)
394 R(B)=R(B)+S(1):GOTO 110
400 REM PRD
402 N1=N1+1:B=P(N1)
404 R(B)=R(B)*S(1):GOTO 110
410 REM FIX
412 N1=N1+1:FX=P(N1):FX$=STRING$(15-FX,"#")+"."+STRING$(FX,"#")
414 GOTO 110
420 REM ROU
422 N1=N1+1:B=P(N1)
424 S(1)=FNR(S(1),B):GOTO 110
430 REM DSZ
432 N1=N1+1:B=P(N1)
434 R(B)=R(B)-1:IF R(B)<>0 THEN 110
436 N1=N1+1:IF P(N1)>OL THEN 110
438 N1=N1+1:GOTO 110
440 REM CLX
442 S(1)=0:GOTO 110
445 REM CLS
446 FOR I=1 TO 4:S(I)=0:NEXT:GOTO 110
450 REM CLR
452 FOR I=0 TO 500:R(I)=0:NEXT:GOTO 110
499 REM
500 Q=0:Q1=0:PRINT:PRINT "RPN>";:LINE INPUT K$:IF K$="" THEN 500
501 I=INSTR(K$," "):IF I>0 THEN K1$=MID$(K$,I+1) ELSE K1$=""
502 IF LEN(K$)>=2 AND INSTR(C$,LEFT$(K$,2))<>0 THEN 1300
504 IF INSTR("0123456789.-",LEFT$(K$,1))<>0 THEN K$="CON"+K$:GOTO 1300
510 I=INSTR(M$,LEFT$(K$,1)):IF I=0 THEN 552
520 Q=VAL(MID$(K$,2)):I1=INSTR(K$,","):IF I1<>0 THEN Q1=VAL(MID$(K$,I1+1)):ELSE Q1=0
530 ON I GOSUB 835,104,580,990,1100,560,561,1210,555,1330
550 CLOSE 1:FL=0:GOTO 500
552 PRINT "Unknown statement or command":GOTO 500
555 PRINT:IF K1$="" THEN K1$="*.*"
557 FILES K1$:RETURN
560 PRINT "Program terminated - Returning to DOS":END
561 IF LEN(K$)>1 THEN 565
562 PRINT "X =";S(1),"Y =";S(2),
564 PRINT "Z =";S(3),"T =";S(4):GOTO 500
565 IF Q>1000 THEN PRINT "Out of range":GOTO 500
566 PRINT "R(";RIGHT$(STR$(Q),LEN(STR$(Q))-1);") =";R(Q):GOTO 500
570 REM
580 REM INPUT & ASSEMBLE LINES
590 REM
600 IM=0:MD=0:IF Q=0 THEN N=N+1:GOTO 610
602 Q1=Q:IM=1:IF Q1=1 THEN 604:ELSE IF P(Q-1)>OL OR (P(Q-2)<OL+1 OR P(Q-2)=0) THEN 604
603 Q1=Q1+1
604 B1=1:N2=N:N=Q1
610 REM
620 LOCATE 24,1:PRINT:LOCATE 23,1:PRINT USING "###";N;:PRINT " - ";:LINE INPUT A$
625 IF A$="" THEN 770
640 IF LEN(A$)<3 THEN A$=A$+STRING$(3-LEN(A$),32)
650 R=INSTR(C$,LEFT$(A$,3)):IF R<>0 THEN 670
660 PRINT "Syntax Error":GOTO 769
670 R=(R-1)/3+1:IF R<>INT(R) THEN 660
680 IF R>OL THEN 760
690 IF LEN(A$)>3 THEN 710
700 PRINT "Missing Operand":GOTO 769
710 Q=VAL(MID$(A$,4)):IF R=14 THEN 750:ELSE IF R=10 THEN 742
715 IF R=5 AND Q>50 THEN 740
720 IF R<5 AND Q<501 AND Q>-1 THEN 750
730 IF R<11 AND Q<51 AND Q>-1 THEN 750
732 IF R<OL+1 AND Q<1001 AND Q>-1 THEN 750
740 PRINT "Operand out of range":GOTO 769
742 H$=MKS$(Q):Q3=CVI(LEFT$(H$,2)):Q2=CVI(RIGHT$(H$,2))
744 IF IM=0 THEN P(N)=R:P(N+1)=Q3:P(N+2)=Q2:GOTO 765
746 K(B1)=R:K(B1+1)=Q3:K(B1+2)=Q2:B1=B1+3:GOTO 765
750 IF IM=0 THEN P(N)=R:P(N+1)=Q:GOTO 765
755 K(B1)=R:K(B1+1)=Q:B1=B1+2:GOTO 765
760 IF IM=0 THEN P(N)=R:GOTO 765
762 K(B1)=R:B1=B1+1
765 IF MD=0 THEN LOCATE 23,1:PRINT USING "###: ";N;:PRINT TAB(7);
766 IF MD=0 THEN PRINT MID$(C$,(R-1)*3+1,3);TAB(12);
767 IF MD=0 THEN IF R<OL+1 THEN PRINT Q:ELSE PRINT
768 N=N+1:IF R<OL+1 THEN N=N+1:IF R=10 THEN N=N+1
769 IF MD=0 THEN 620:ELSE RETURN
770 IF IM=0 THEN N=N-1:GOTO 800
771 B1=B1-1:N=N2:FOR I=N TO Q1 STEP -1
772 P(I+B1)=P(I):NEXT I
774 FOR I=0 TO B1-1:P(Q1+I)=K(I+1):NEXT I
775 N=N+B1:GOTO 800
800 GOSUB 920:RETURN
810 REM
820 REM LIST
830 REM
835 IF Q=0 THEN Q=1
840 PRINT:IF Q=1 THEN 845
842 IF P(Q-1)<=OL AND P(Q-2)>OL THEN Q=Q+1
845 IF N=0 THEN RETURN
847 FL=0:IF K1$<>"" THEN OPEN "O",1,K1$:PRINT "Listing to file: ";K1$:FL=1
850 I$=INKEY$:FOR D=Q TO N
860 IF FL THEN PRINT #1,USING "###: ";D;:PRINT #1,TAB(7);
865 IF FL=0 THEN PRINT USING "###: ";D;:PRINT TAB(7);
870 T=P(D):I=0
880 IF T<OL+1 THEN Q=P(D+1):D=D+1:I=1
885 IF T=10 THEN Q1=P(D+1):D=D+1:I=2
890 IF FL THEN PRINT #1,MID$(C$,(T-1)*3+1,3);TAB(12);
895 IF FL=0 THEN PRINT MID$(C$,(T-1)*3+1,3);TAB(12);
897 IF FL=0 THEN 901
900 IF I=1 THEN PRINT #1,Q:GOTO 902: ELSE IF I=2 THEN PRINT #1,CVS(MKI$(Q)+MKI$(Q1)):GOTO 902:ELSE PRINT #1,:GOTO 902
901 IF I=1 THEN PRINT Q: ELSE IF I=2 THEN PRINT CVS(MKI$(Q)+MKI$(Q1)):ELSE PRINT
902 I$=INKEY$:IF I$="" THEN 910
904 IF I$=CHR$(27) THEN D=N:GOTO 910
906 I$=INKEY$:IF I$="" THEN 906
908 IF I$=CHR$(27) THEN D=N
910 NEXT:CLOSE 1:RETURN
920 REM COMPILE LABELS
930 FOR I=1 TO 50:L%(I)=0:NEXT
940 FOR I=1 TO N:A=P(I):IF A=5 THEN 950
945 IF A<=OL THEN I=I+1:IF A=10 THEN I=I+1
948 GOTO 970
950 K=P(I+1):L%(K)=I+2:I=I+1
970 NEXT:RETURN
980 REM
990 REM RECALL ASSEMBLED PROGRAM
1000 REM
1005 IF K1$<>"" THEN F$=K1$:GOTO 1015
1010 LINE INPUT "INPUT FILE: ";F$:IF F$="" THEN RETURN
1015 OPEN "R",1,F$+".RPN",1:N=0
1020 FIELD 1, 1 AS D$:FOR J=1 TO LOF(1)
1030 GET 1:A=ASC(D$):IF A=0 THEN 1070
1040 N=N+1:P(N)=A:IF A>OL THEN 1068
1050 IF A=10 THEN 1062
1052 N=N+1:GET 1:U$=D$:GET 1:U$=U$+D$
1054 P(N)=CVI(U$):J=J+2
1060 GOTO 1068
1062 N=N+1:GET 1:U$=D$:GET 1:U$=U$+D$:P(N)=CVI(U$)
1064 N=N+1:GET 1:U$=D$:GET 1:U$=U$+D$:P(N)=CVI(U$)
1066 J=J+4
1068 NEXT J
1070 CLOSE:PRINT:PRINT USING "#### Lines";N
1080 GOSUB 920:RETURN
1090 REM
1100 REM SAVE CODE
1110 REM
1115 IF K1$<>"" THEN F$=K1$:GOTO 1150
1120 LINE INPUT "OUTPUT FILE: ";F$:IF F$="" THEN RETURN
1150 OPEN "R",1,F$+".RPN",1:FIELD 1, 1 AS D$
1160 FOR I=1 TO N:A=P(I):LSET D$=CHR$(A):PUT 1
1165 IF A=10 THEN 1192
1170 IF A>OL THEN 1200
1180 I=I+1:G=P(I):U$=MKI$(G)
1190 LSET D$=LEFT$(U$,1):PUT 1:LSET D$=RIGHT$(U$,1):PUT 1:GOTO 1200
1192 U$=MKI$(P(I+1))+MKI$(P(I+2)):I=I+2
1194 FOR J=1 TO 4:LSET D$=MID$(U$,J,1):PUT 1
1196 NEXT
1200 NEXT:CLOSE:RETURN
1209 REM
1210 REM DELETE
1211 REM
1220 IF Q1=0 THEN Q1=Q
1222 IF P(Q1)<=OL THEN Q1=Q1+1:IF P(Q1-1)=10 THEN Q1=Q1+1
1224 B=Q1-Q+1
1230 FOR I=Q1+1 TO N
1235 P(I-B)=P(I):NEXT:N=N-B
1240 GOTO 800
1300 A$=K$:MD=1:IM=0:B2=N:N=N+1:P(N)=0:GOSUB 640:N=B2:N1=N+1
1310 IF P(N1)<>0 THEN GOSUB 112
1320 GOTO 500
1330 REM HELP
1340 PRINT:FOR I=0 TO 57
1350 PRINT MID$(C$,3*I+1,3);" ";:IF POS(0)=60 THEN PRINT
1360 NEXT:PRINT:PRINT:PRINT "Insert List Go Delete Save Recall Files Xamine ? Quit":RETURN
1400 REM ERROR TRAP
1410 PRINT:PRINT "Input Error":RESUME 500