apple2_russian_peasant_mult.../rpm.bas
2016-08-16 14:17:30 -07:00

73 lines
1.4 KiB
QBasic

10 HOME:GOTO 100:REM <Ctrl-J>
20 REM S += A
21 C=0
22 FOR I=1 TO M
23 D=S(I) + A(I) + C
24 C=0:IF D > 9 THEN C=1:D=D-10
25 S(I)=D
26 NEXT
29 RETURN:REM <Ctrl-J>
30 REM SHL A
31 C=0
32 FOR I=1 TO M
33 SUM=A(I)*2 + C
34 C=0:IF SUM > 9 THEN C=1:SUM=SUM-10
35 A(I)=SUM
36 NEXT
39 RETURN:REM <Ctrl-J>
40 REM SHR B
41 C=0
42 FOR I=M TO 1 STEP-1
43 V=B(I) + C
44 Q=INT(V/2)
45 B(I)=Q
46 C=(V-Q*2)*10
47 NEXT
49 RETURN:REM <Ctrl-J>
50 REM IS B[] ZERO?
51 ZERO=1
52 FOR I=1 TO M
53 IF B(I)<>0 THEN ZERO=0
54 NEXT
59 RETURN:REM <Ctrl-J>
60 REM FILL BCD ARRAY: T$ -> S[]
61 T=LEN(T$)
62 FOR I=1 TO T
63 S(T-I+1)=VAL(MID$(T$,I,1))
64 NEXT
65 FOR I=T+1 TO M
66 S(I) = 0
67 NEXT
69 RETURN:REM <Ctrl-J>
70 REM PRINT S[]
71 ZERO=1
72 FOR I=M TO 1 STEP -1
73 IF S(I)<>0 THEN ZERO=0
74 IF ZERO THEN 76:REM SKIP LEADING ZEROES
75 PRINT S(I);
76 NEXT
79 RETURN:REM <Ctrl-J>
100 DIM A(32),B(32),S(64):M=32
110 PRINT "RUSSIAN PEASANT MULTIPLICATION":PRINT
120 PRINT "ENTER A":INPUT A$
130 PRINT "ENTER B":INPUT B$
140 T$=A$:GOSUB 61:N=T :FOR I=1 TO T:A(I)=S(I):NEXT
150 T$=B$:GOSUB 61:M=T+N:FOR I=1 TO T:B(I)=S(I):NEXT
160 T=0:GOSUB 65:REM S = 0
170 REM WHILE B != 0
180 GOSUB 51:IF ZERO THEN 240
190 HALF=B(1)*.5
200 IF HALF <> INT(HALF) THEN GOSUB 21: REM S += A
210 GOSUB 31:REM SHL A
220 GOSUB 41:REM SHR B
230 GOTO 180
240 PRINT A$;" x ";B$;" = ":PRINT " ";:GOSUB 71:PRINT