'=========================================================================== ' Subject: STRING MATH Date: 10-31-95 (16:01) ' Author: Joe Krzton Code: QB, QBasic, PDS ' Origin: FidoNet QUIK_BAS Echo Packet: ALGOR.ABC '=========================================================================== DECLARE FUNCTION Add$ (o1$, o2$) DECLARE SUB AlignDot (x1$, x2$) DECLARE FUNCTION Subtract$ (o1$, o2$) DEFDBL A-Z b = 1.5: c = 0 DO b = b + c * 16 pi = pi + 4 / b PRINT pi; : LOCATE , 1 c = c + 1 LOOP UNTIL INKEY$ = CHR$(27) END DEFINT A-Z FUNCTION Add$ (o1$, o2$) op1$ = o1$: op2$ = o2$ IF LEFT$(op1$, 1) = "-" THEN s1 = -1: ELSE s1 = 1 IF INSTR("+-", LEFT$(op1$, 1)) > 0 THEN op1$ = MID$(op1$, 2) IF LEFT$(op2$, 1) = "-" THEN s2 = -1: ELSE s2 = 1 IF INSTR("+-", LEFT$(op2$, 1)) > 0 THEN op2$ = MID$(op2$, 2) IF s1 = -1 AND s2 = 1 THEN Add$ = Subtract$(op2$, op1$): EXIT FUNCTION IF s1 = 1 AND s2 = -1 THEN Add$ = Subtract$(op1$, op2$): EXIT FUNCTION pfx$ = "": IF s1 = -1 THEN pfx$ = "-" AlignDot op1$, op2$ out$ = "": carry = 0 FOR z = LEN(op1$) TO 1 STEP -1 IF MID$(op1$, z, 1) <> "." THEN ans = VAL(MID$(op1$, z, 1)) + VAL(MID$(op2$, z, 1)) + carry IF out$ <> "" OR (ans MOD 10) > 0 THEN out$ = CHR$(48 + (ans MOD 10)) + out$ carry = INT(ans / 10) ELSE out$ = "." + out$ END IF NEXT z IF RIGHT$(out$, 1) = "." THEN out$ = LEFT$(out$, LEN(out$) - 1) IF carry > 0 THEN out$ = CHR$(48 + carry) + out$ Add$ = pfx$ + out$ END FUNCTION DEFSNG A-Z SUB AlignDot (x1$, x2$) IF INSTR(x1$, ".") = 0 THEN x1$ = x1$ + ".0" IF INSTR(x2$, ".") = 0 THEN x2$ = x2$ + ".0" da1 = INSTR(x1$, ".") da2 = INSTR(x2$, ".") IF da1 < da2 THEN x1$ = STRING$(da2 - da1, "0") + x1$ IF da1 > da2 THEN x2$ = STRING$(da1 - da2, "0") + x2$ IF LEN(x1$) > LEN(x2$) THEN x2$ = x2$ + STRING$(LEN(x1$) - LEN(x2$), "0") IF LEN(x2$) > LEN(x1$) THEN x1$ = x1$ + STRING$(LEN(x2$) - LEN(x1$), "0") END SUB FUNCTION Subtract$ (o1$, o2$) END FUNCTION