'=========================================================================== ' Subject: UNLIMITED DIGIT INTEGERS Date: 10-27-96 (00:00) ' Author: Tom Nelson (Chien-Lung Su) Code: QB, QBasic, PDS ' Origin: cs85107@cc.chpi.edu.tw Packet: ALGOR.ABC '=========================================================================== ' Program name : "Unlimited-digit integers addition & multiplication" ' Author : Tom Nelson (Chien-Lung Su) ' Date : 1996/10/27 ' Copyright : Feel free to use it in your programs. ' Or you can send a copy of your product to me ! :) ' Notice : No error-checking on the given strings for operations ! ' i.e. You should develop error-preventing code yourself ! ' E-mail address : tomnelson.bbs@tnbbs.tnmgeb.edu.tw ' cs85107@cc.chpi.edu.tw ' cs85107@cssun1.cs.chpi.edu.tw ' Home page : http://web.chpi.edu.tw/~cs85107/ \ ' http://web.cs.chpi.edu.tw/~cs85107/ |-> Coming soon ! ' http://Lung.dorm.chpi.edu.tw/ / ' P.S. : This program has Turbo Pascal version. If you would ' like to take a view of it, please contact me ! DECLARE FUNCTION UnlimitedMul$ (NumString1$, NumString2$) ' The great function of the program ! :) DECLARE FUNCTION StrMul$ (NumString1$, NumString2$) ' A function performs a 1-digit integer to multi-digit integer multiplication. DECLARE FUNCTION StrAdd$ (NumString1$, NumString2$) ' A function performs integer addition. ' Called by the two functions above. CLS PRINT "Unlimited-digit integers addition & multiplication." PRINT "Written By Tom Nelson 1996." PRINT INPUT "Enter the 1st number : ", Num1$ INPUT "Enter the 2nd number : ", Num2$ Num1$ = LTRIM$(RTRIM$(Num1$)) Num2$ = LTRIM$(RTRIM$(Num2$)) PRINT IF LEN(Num1$) > LEN(Num2$) THEN Max1% = LEN(Num1$) ELSE Max1% = LEN(Num2$) END IF Max2% = LEN(Num1$) + LEN(Num2$) - 1 PRINT SPACE$(Max2% - LEN(Num1$) + 3) + Num1$ PRINT "x) " + SPACE$(Max2% - LEN(Num2$)) + Num2$ PRINT STRING$(Max2% + 3, "-") PRINT SPACE$(Max2% - LEN(UnlimitedMul$(Num1$, Num2$)) + 3) + UnlimitedMul$(Num1$, Num2$) PRINT PRINT SPACE$(Max1% - LEN(Num1$) + 3) + Num1$ PRINT "+) " + SPACE$(Max1% - LEN(Num2$)) + Num2$ PRINT STRING$(Max1% + 3, "-") PRINT SPACE$(Max1% - LEN(StrAdd$(Num1$, Num2$)) + 3) + StrAdd$(Num1$, Num2$) END FUNCTION StrAdd$ (NumString1$, NumString2$) IF NumString1$ = "" OR NumString2$ = "" THEN EXIT FUNCTION DIM Array1%(1024) DIM Array2%(1024) DIM Array3%(1024) Add1$ = LTRIM$(RTRIM$(NumString1$)) Add2$ = LTRIM$(RTRIM$(NumString2$)) Len1% = LEN(Add1$) Len2% = LEN(Add2$) FOR Temp% = 1 TO Len1% Array1%(Temp%) = VAL(MID$(Add1$, Len1% + 1 - Temp%, 1)) NEXT Temp% FOR Temp% = 1 TO Len2% Array2%(Temp%) = VAL(MID$(Add2$, Len2% + 1 - Temp%, 1)) NEXT Temp% IF Len1% > Len2% THEN Max% = Len1% ELSE Max% = Len2% FOR Temp% = 1 TO Max% + 1 Sum% = Array1%(Temp%) + Array2%(Temp%) + CarryFlag% IF Sum% >= 10 THEN CarryFlag% = 1 Array3%(Temp%) = Sum% - 10 ELSE CarryFlag% = 0 Array3%(Temp%) = Sum% END IF NEXT Temp% FOR Temp% = Max% + 1 TO 1 STEP -1 Result$ = Result$ + LTRIM$(STR$(Array3%(Temp%))) NEXT Temp% FOR Temp% = 1 TO LEN(Result$) IF MID$(Result$, Temp%, 1) <> "0" THEN Okay$ = MID$(Result$, Temp%, LEN(Result$) + 1 - Temp%) EXIT FOR END IF NEXT Temp% ERASE Array1% ERASE Array2% ERASE Array3% StrAdd$ = Okay$ END FUNCTION FUNCTION StrMul$ (NumString1$, NumString2$) IF NumString1$ = "" OR NumString2$ = "" THEN EXIT FUNCTION Len1% = LEN(LTRIM$(RTRIM$(NumString1$))) Len2% = LEN(LTRIM$(RTRIM$(NumString2$))) DIM Array1$(1024) IF Len1% >= 1 AND Len2% = 1 THEN Begin% = Len1% Mul1$ = LTRIM$(RTRIM$(NumString1$)) Mul2$ = LTRIM$(RTRIM$(NumString2$)) ELSEIF Len1% = 1 AND Len2% >= 1 THEN Begin% = Len2% Mul1$ = LTRIM$(RTRIM$(NumString2$)) Mul2$ = LTRIM$(RTRIM$(NumString1$)) ELSE EXIT FUNCTION END IF FOR Times% = Begin% TO 1 STEP -1 Temp% = VAL(MID$(Mul1$, Times%, 1)) * VAL(Mul2$) Array1$(Begin% - Times% + 1) = LTRIM$(STR$(Temp%)) + STRING$(Begin% - Times%, "0") NEXT Times% FOR Times% = 2 TO Begin% Array1$(1) = StrAdd$(Array1$(1), Array1$(Times%)) NEXT Times% StrMul$ = Array1$(1) ERASE Array1$ END FUNCTION FUNCTION UnlimitedMul$ (NumString1$, NumString2$) Length2% = LEN(LTRIM$(RTRIM$(NumString2$))) DIM Sum$(Length2%) FOR I% = Length2% TO 1 STEP -1 Sum$(Length2% - I% + 1) = StrMul$(NumString1$, MID$(NumString2$, I%, 1)) + STRING$(Length2% - I%, "0") NEXT I% Temp$ = Sum$(1) FOR Times% = 2 TO Length2% Temp$ = StrAdd$(Temp$, Sum$(Times%)) NEXT Times% UnlimitedMul$ = Temp$ ERASE Sum$ Temp$ = "" END FUNCTION