home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Nibble Magazine
/
nib35a.dsk
/
OCTOBER.1988
/
LONG.NUMBR.CALC.bas
< prev
next >
Wrap
BASIC Source File
|
2023-02-26
|
4KB
|
124 lines
10 REM *********************
20 REM * LONG.NUMBER.CALC *
30 REM * BY ALAN H. STEIN *
40 REM * COPYRIGHT(C) 1988 *
50 REM * MICROSPARC, INC. *
60 REM * CONCORD, MA 01742 *
70 REM *********************
100 CLEAR
110 B$ = "":ZERO$ = "0"
120 BS = 10
130 HOME : PRINT "ENTER 1ST # (OR PRESS RETURN TO QUIT)": PRINT "--> ";: GOSUB 1200:T$(1) = IN$: IF IN$ = "" THEN 1190
140 PRINT : PRINT : PRINT "ENTER 2ND # (OR PRESS RETURN TO QUIT)": PRINT "--> ";: GOSUB 1200:T$(2) = IN$: IF IN$ = "" THEN 1190
150 PRINT : PRINT :LMAX = LEN(T$(1))
160 IF LEN(T$(2)) >LMAX THEN LMAX = LEN(T$(2))
170 LMAX = 2 *LMAX +1
180 DIM T(3,LMAX +1)
190 GOSUB 920
200 PRINT : PRINT T$(1);" + (";T$(2);") = ";T$(3)
210 GOSUB 1020
220 PRINT : PRINT T$(1);" - (";T$(2);") = ";T$(3)
230 GOSUB 760
240 PRINT : PRINT T$(1);" * (";T$(2);") = ";T$(3)
250 VTAB 23: HTAB 1: PRINT "PRESS RETURN TO CONTINUE ";: POKE -16368,0: GET A$: GOTO 100
260 REM UNSIGNED ADDITION
270 REM ADD T(1,*)+T(2,*), SUM T(3,*)
280 L(3) = L(1): IF L(2) >L(3) THEN L(3) = L(2)
290 FOR J = 0 TO LMAX:T(3,J) = 0: NEXT J
300 FOR J = 0 TO L(3)
310 T(3,J) = T(1,J) +T(2,J) +T(3,J)
320 IF T(3,J) > = BS THEN T(3,J) = T(3,J) -BS:T(3,J +1) = T(3,J +1) +1
330 NEXT J
340 IF T(3,L(3) +1) < >0 THEN L(3) = L(3) +1
350 RETURN
360 REM UNSIGNED SUBRACTION
370 REM FIND T(3,*)=ABS(T(1,*)-T(2,*))
380 GOSUB 670: REM FIND BIGGER ELEMENT
390 L(3) = L(1): IF L(2) >L(3) THEN L(3) = L(2)
400 FOR J = 0 TO LMAX:T(3,J) = 0: NEXT J
410 FOR J = 0 TO L(3)
420 T(3,J) = T(BIG,J) -T(3 -BIG,J) +T(3,J)
430 IF T(3,J) <0 THEN T(3,J) = T(3,J) +BS:T(3,J +1) = T(3,J +1) -1: GOTO 430
440 NEXT J
450 RETURN
460 REM TAKE STRING T$(I)
470 REM RETURN T(I,*)=INTEGER, L(I)=LENGTH, S$(I)=SIGN
480 T$ = T$(I)
490 S$(I) = B$: IF LEFT$(T$,1) = "-" THEN S$(I) = "-"
500 L(I) = LEN(T$) -1: IF S$(I) = "-" THEN L(I) = L(I) -1
510 FOR J = 0 TO L(I)
520 T(I,J) = VAL( RIGHT$(T$,1))
530 IF J <L(I) THEN T$ = LEFT$(T$, LEN(T$) -1)
540 NEXT J
550 IF L(I) > = LMAX THEN RETURN
560 FOR J = L(I) +1 TO LMAX:T(I,J) = 0: NEXT J
570 RETURN
580 REM TAKE INTEGER T(I,*), SIGN S$(I)
590 REM RETURN STRING T$(I)
600 T$(I) = S$(I)
610 FOR J = L(I) TO 0 STEP -1
620 IF T$(I) < >S$(I) OR T(I,J) < >0 THEN T$(I) = T$(I) + STR$(T(I,J))
630 NEXT J
640 IF T$(I) = B$ THEN T$(I) = ZERO$
650 RETURN
660 REM FIND BIGGER OF T$(1), T$(2)
670 BIG = 0
680 FOR J = LMAX TO 0 STEP -1
690 IF BIG >0 THEN 720
700 IF T(1,J) >T(2,J) THEN BIG = 1
710 IF T(2,J) >T(1,J) THEN BIG = 2
720 NEXT J
730 IF BIG = 0 THEN BIG = 1
740 RETURN
750 REM MULTIPLICATION OF INTEGERS
760 REM T$(3)=T$(1)*T$(2)
770 FOR I = 1 TO 2: GOSUB 470: NEXT I: REM CHANGE TO ARRAYS
780 L(3) = L(1) +L(2)
790 FOR J = 0 TO LMAX:T(3,J) = 0: NEXT J
800 FOR J1 = 0 TO L(1)
810 FOR J2 = 0 TO L(2)
820 J3 = J1 +J2
830 T(3,J3) = T(1,J1) *T(2,J2) +T(3,J3)
840 IF T(3,J3) > = BS THEN T = INT(T(3,J3)/BS):T(3,J3) = T(3,J3) -T *BS:T(3,J3 +1) = T(3,J3 +1) +T
850 NEXT J2
860 NEXT J1
870 S$(3) = B$: IF S$(1) < >S$(2) THEN S$(3) = "-"
880 IF T(3,L(3) +1) < >0 THEN L(3) = L(3) +1
890 I = 3: GOSUB 590: REM CHANGE TO STRING T$(3)
900 RETURN
910 REM ADDITION
920 FOR I = 1 TO 2: GOSUB 470: NEXT I
930 GOSUB 670: REM FIND BIGGER ELEMENT
940 IF S$(1) < >S$(2) THEN 970
950 GOSUB 270: REM ADD
960 GOTO 990
970 REM DIFFERENT SIGNS
980 GOSUB 370: REM SUBTRACT
990 S$(3) = S$(BIG): GOSUB 590
1000 RETURN
1010 REM SUBTRACTION
1020 FOR I = 1 TO 2: GOSUB 470: NEXT I
1030 GOSUB 670: REM FIND BIGGER ELEMENT
1040 IF S$(1) < >S$(2) THEN 1130
1050 REM SAME SIGN
1060 GOSUB 370: REM SUBTRACT
1070 IF BIG = 2 THEN 1100
1080 S$(3) = S$(1)
1090 GOTO 1170
1100 REM REVERSE SIGN OF T(1,*)
1110 IF S$(1) = B$ THEN S$(3) = "-": GOTO 1170
1120 S$(3) = B$: GOTO 1170
1130 REM DIFFERENT SIGNS
1140 REM ADD AND TAKE SIGN OF FIRST ARGUMENT
1150 GOSUB 270: REM ADD
1160 S$(3) = S$(1)
1170 GOSUB 590
1180 RETURN
1190 HOME : VTAB 23: END
1200 IN$ = "":L = 0
1210 GET A$: IF A$ = CHR$(13) THEN RETURN
1220 IF A$ > = "0" AND A$ < = "9" THEN PRINT A$;:IN$ = IN$ +A$:L = L +1: GOTO 1210
1230 IF L >1 AND A$ = CHR$(8) THEN L = L -1:IN$ = LEFT$(IN$,L): CALL -1008: PRINT " ";: CALL -1008: GOTO 1210
1240 IF L = 1 AND A$ = CHR$(8) THEN CALL -1008: PRINT " ";: CALL -1008: GOTO 1200
1250 IF L = 0 AND A$ = "-" THEN IN$ = "-":L = 1: PRINT "-";: GOTO 1210
1260 PRINT CHR$(7);: GOTO 1210