主题:“高精度四则运算”的程序怎么编?
剑圣风暴
[专家分:450] 发布于 2007-07-22 07:48:00
请教各位高人,“高精度四则运算”的程序怎么编?
回复列表 (共21个回复)
11 楼
wzc1996 [专家分:1680] 发布于 2007-07-24 19:25:00
给分
12 楼
wzc1996 [专家分:1680] 发布于 2007-07-24 19:25:00
加法
CLS:INPUT A$,B$:LA=LEN(A$):LB=LEN(B$)
IF LB>LA THEN SWAP A$,B$:SWAP LA,LB
N=LA+1:DIM A(N),B(N)
FOR I=1 TO LA
A(I)=VAL(MID$(A$,LA+1-I,1)
NEXT I
FOR I=1 TO LB
B(I)=VAL(MID$(B$,LB+1-I,1)
NEXT I
FOR I=1 TO LA
X=A(I)+B(I):A(I+1)=A(I+1)+X\10:A(I)=X MOD 10
NEXT I
IF A(N)=0 THEN N=N-1
FOR I=N TO 1 STEP -1:PRINT USING "#";A(I);NEXT I
13 楼
wzc1996 [专家分:1680] 发布于 2007-07-24 19:26:00
减法
INPUT A$,B$
LA=LEN(A$):LB=LEN(B$)
A1$=A$:B1$=B$
IF LA<LB THEN SWAP LA,LB:SWAP A$,B$:C$="-"
N=LA:DIM A(N),B(N)
FOR I=1TO LA:A(I)=VAL(MID$(A$,N-I,1)):NEXT I
FOR I=1TO LB:B(I0=VAL(MID$(B$,LB-I,1)):NEXT I
FOR I=1TO N
A(I)=A(I)-B(I)
A(I+1)=A(I+1)+(A(I)<0)
A(I)=A(I)-(A(I)<0)*10
NEXT I
PRINT A1$;"-";B1$;"=";
DO WHILE A(N)=0AND N>1:N=N-1:NEXT I
IF C$="-" THEN PRINT"-";
FOR I=N TO 1 STEP-1:?USING"#";A(I);:NEXT I
乘法
INOUT A4,B$
LA=LEN(A$):L=LEN(B$):LC=LA+LB
DIM A(LA),B(LB),C(LC)
FOR I=1TO LA:A(I)=VAL(MID$(A$,LA-I,1)):NEXT I
FOR I=1TO LB:B(I)=VAL(MID$(B$,LB-I,1)):NEXT I
FOR I=1 TO LA:FOR J=1TO LB
X=A(I)*B(I):C(W)=C(W)+X MOD 10
C(W+1)=C(W+1)+C(W)\10+X\10
C(W)=C(W) MOD 10
NEXT J,I
PRINT A$;"*";B$;"=";
WHILE C(LC)=0:LC=LC-1:WEND
FOR I=LC TO 1 STEP -1:PRINT USING"#";C(I):NEXT I
14 楼
wzc1996 [专家分:1680] 发布于 2007-07-24 19:28:00
CLS
INPUT a,b,c
s$ = "0."
FOR i = 1 TO c
x = a * 10 \ b
a = a * 10 MOD b
s$ = s$ + LTRIM$(STR$(x))
NEXT i
PRINT s$
END
A是被除数,B是除数,C是你要除到商的哪一位
15 楼
麒麟23 [专家分:80] 发布于 2007-07-26 09:34:00
见红书11章.
16 楼
冷石_jasv [专家分:1570] 发布于 2007-07-28 12:21:00
哎...世风日下...
看来教人授鱼不如授之以渔....
还是直接给鱼更能使人有好感啊........
17 楼
moz [专家分:37620] 发布于 2007-07-28 16:56:00
楼上典型的见鱼眼开型.
18 楼
QB爱好者 [专家分:370] 发布于 2007-08-13 15:24:00
最简单的就是
CLS
INPUT "A,B=",A#,B#
PRINT A# + B# , A# - B#; A# * B#,A# / B#
END
19 楼
天尝地酒 [专家分:870] 发布于 2007-08-14 10:21:00
高精度减法:
CLS
INPUT a$
INPUT b$
IF a$ = b$ THEN PRINT "0": END
PRINT a$; "-"; b$; "=";
da = INSTR(a$, "."): db = INSTR(b$, ".")
IF da = 0 THEN a1 = LEN(a$) ELSE a1 = da - 1: a2 = LEN(a$) - da
IF db = 0 THEN b1 = LEN(b$) ELSE b1 = db - 1: b2 = LEN(b$) - db
IF a1 < b1 OR b1 = a1 AND a$ < b$ THEN
PRINT "-"; : SWAP a$, b$: SWAP a1, b1: SWAP a2, b2
END IF
IF a1 > b1 THEN m1 = a1 ELSE m1 = b1
IF a2 > b2 THEN m2 = a2 ELSE m2 = b2
DIM AX$(-m1 TO m2), BX$(-m1 TO m2), s$(-m1 TO m2)
FOR I = -a1 TO a2: AX$(I) = MID$(a$, a1 + I + 1, 1): NEXT I
FOR I = -b1 TO b2: BX$(I) = MID$(b$, b1 + I + 1, 1): NEXT I
FOR I = m2 TO -m1 STEP -1
IF I <> 0 THEN
X = VAL(AX$(I)) - VAL(BX$(I)) - c
IF X < 0 THEN X = X + 10: c = 1 ELSE c = 0
END IF: s$(I) = LTRIM$(STR$(X))
NEXT I
WHILE s$(-m1) = "0" AND m1 > 1: m1 = m1 - 1: WEND
FOR I = -m1 TO m2
IF I = 0 THEN
IF (a2 <> 0 OR b2 <> 0) THEN PRINT ".";
ELSE
PRINT s$(I);
END IF
NEXT I: END
加分!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
20 楼
hxy_anhui [专家分:320] 发布于 2009-01-29 22:11:00
加法:
CLS
INPUT A$, B$
LA = LEN(A$): LB = LEN(B$)
IF LB > LA THEN SWAP A$, B$: SWAP LA, LB
N = LA + 1: DIM A(N), B(N)
FOR I = 1 TO LA: A(I) = VAL(MID$(A$, N - I, 1)): NEXT I
FOR I = 1 TO LB: B(I) = VAL(MID$(B$, LB + 1 - I, 1)): NEXT I
FOR I = 1 TO LA
X = A(I) + B(I)
A(I + 1) = A(I + 1) + X \ 10
A(I) = X MOD 10
NEXT I
WHILE A(N) = 0: N = N - 1: WEND
FOR I = N TO 1 STEP -1: PRINT USING "#"; A(I); : NEXT I
END
减法:
CLS
INPUT A$, B$
LA = LEN(A$): LB = LEN(B$)
PRINT A$; "-"; B$; "=";
IF LB > LA OR LB = LA AND B$ > A$ THEN SWAP A$, B$: SWAP LA, LB: PRINT "-";
N = LA: DIM A(N + 1), B(N)
FOR I = 1 TO LA: A(I) = VAL(MID$(A$, LA + 1 - I, 1)): NEXT I
FOR I = 1 TO LB: B(I) = VAL(MID$(B$, LB + 1 - I, 1)): NEXT I
FOR I = 1 TO LA
A(I) = A(I) - B(I)
IF A(I) < 0 THEN A(I + 1) = A(I + 1) - 1: A(I) = A(I) + 10
NEXT I
WHILE A(N) = 0 AND N > 1: N = N - 1: WEND
FOR I = N TO 1 STEP -1: PRINT USING "#"; A(I); : NEXT I
END
乘法:
CLS
INPUT A$, B$
LA = LEN(A$): LB = LEN(B$): LC = LA + LB
DIM A(LA), B(LB), C(LC)
FOR I = 1 TO LA: A(I) = VAL(MID$(A$, LA + 1 - I, 1)): NEXT I
FOR I = 1 TO LB: B(I) = VAL(MID$(B$, LB + 1 - I, 1)): NEXT I
FOR I = 1 TO LA: FOR J = 1 TO LB
X = A(I) * B(J): W = I + J - 1
C(W) = C(W) + X MOD 10
C(W + 1) = C(W + 1) + C(W) \ 10 + X \ 10
C(W) = C(W) MOD 10
NEXT J, I
PRINT A$; "*"; B$; "=";
WHILE C(LC) = 0 AND LC > 1: LC = LC - 1: WEND
FOR I = LC TO 1 STEP -1: PRINT USING "#"; C(I); : NEXT I
END
除法:
CLS
INPUT A, B, E
PRINT A; "/"; B; "="; STR$(A \ B);
A = A MOD B
IF A <> 0 THEN PRINT "."; ELSE END
FOR I = 1 TO E
IF A = 0 THEN END
A = A * 10: PRINT USING "#"; A \ B;
A = A MOD B
NEXT I
END
我来回复