mirror of
https://github.com/Michaelangel007/apple2_russian_peasant_multiplication.git
synced 2024-10-11 08:23:38 +00:00
99 lines
2.0 KiB
QBasic
99 lines
2.0 KiB
QBasic
1 REM 0=OFF,1=PRINT '.' PER PASS,2=PRINT A,B,S
|
|
2 DEBUG=2
|
|
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
|
|
77 IF ZERO=1 THEN PRINT 0;
|
|
79 RETURN:REM <Ctrl-J>
|
|
|
|
80 REM PRINT A
|
|
81 ZERO=1
|
|
82 FOR I=M TO 1 STEP-1
|
|
83 IF S(I)<>0 THEN ZERO = 0
|
|
84 IF ZERO THEN 86
|
|
85 PRINT A(I);
|
|
86 NEXT
|
|
87 IF ZERO=1 THEN PRINT 0;
|
|
89 RETURN:REM <Ctrl-J>
|
|
|
|
90 REM PRINT B
|
|
91 ZERO=1
|
|
92 FOR I=M TO 1 STEP-1
|
|
93 IF S(I)<>0 THEN ZERO=0
|
|
94 IF ZERO THEN 96
|
|
95 PRINT B(I);
|
|
96 NEXT
|
|
97 IF ZERO=1 THEN PRINT 0;
|
|
99 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:IF DEBUG=1 THEN PRINT ".";
|
|
200 IF HALF <> INT(HALF) THEN GOSUB 21: REM S += A
|
|
210 GOSUB 31:REM SHL A
|
|
220 GOSUB 41:REM SHR B
|
|
225 IF DEBUG=2 THEN PRINT "A=";: GOSUB 81: PRINT
|
|
226 IF DEBUG=2 THEN PRINT "B=";: GOSUB 91: PRINT
|
|
227 IF DEBUG=2 THEN PRINT "S=";: GOSUB 71: PRINT
|
|
230 GOTO 180
|
|
240 PRINT A$;" x ";B$;" = ":PRINT " ";:GOSUB 71:PRINT
|
|
|