主题:超长数运算
而我觉得QB里的字符串特别是不定长字符串,
使用起来很方便,所以我很喜欢用QB的字符串函数
mid$(),instr()等等,方便快捷
不定长字符串最大可以去到32767个字符,
大概已经够用的了,还不够用的话,再定义一个字符数组
转换成十六进制来计算,每一个字符代替两位十六进制数字,够恐怖的了
我觉得我这辈子都不怎么会有这个机会用得上了,否则我该进天文学研究所去了.
DECLARE FUNCTION longx$ (a$, b$) '乘法
DECLARE FUNCTION longqu$ (a$, b$) '除法
DECLARE FUNCTION longadd$ (a$, b$)'加法
DECLARE FUNCTION longsub$ (a$, b$)'减法
DECLARE FUNCTION cut0$ (a$)
DECLARE FUNCTION dao$ (a$)
FUNCTION cut0$ (a$)
c$ = a$
l% = LEN(c$)
DO UNTIL l% <= 1
IF LEFT$(c$, 1) < "1" THEN
l% = l% - 1
c$ = RIGHT$(c$, l%)
ELSE
EXIT DO
END IF
LOOP
cut0$ = c$
END FUNCTION
FUNCTION dao$ (a$)
b$ = a$
l% = LEN(b$)
FOR i% = 1 TO l% \ 2
k$ = MID$(b$, i%, 1)
MID$(b$, i%, 1) = MID$(b$, l% - i% + 1, 1)
MID$(b$, l% - i% + 1, 1) = k$
NEXT
dao$ = b$
END FUNCTION
FUNCTION longadd$ (a$, b$)
a1$ = dao$(LTRIM$(RTRIM$(a$)))
b1$ = dao$(LTRIM$(RTRIM$(b$)))
la% = LEN(a1$)
lb% = LEN(b1$)
IF la% > lb% THEN l% = la% ELSE l% = lb%
FOR i% = 1 TO l%
a% = VAL(MID$(a1$, i%, 1))
b% = VAL(MID$(b1$, i%, 1))
c% = a% + b% + k%
k% = c% \ 10
c$ = c$ + CHR$(48 + c%)
NEXT
IF k% > 0 THEN c$ = c$ + CHR$(48 + k%)
longadd$ = dao$(c$)
END FUNCTION
FUNCTION longqu$ (a$, b$)
a1$ = LTRIM$(RTRIM$(a$))
b1$ = LTRIM$(RTRIM$(b$))
la% = LEN(a1$)
lb% = LEN(b1$)
FOR i% = lb% TO la%
c1$ = LEFT$(a1$, i%)
k% = -1
DO
k% = k% + 1
c2$ = c1$
c1$ = longsub$(c1$, b1$)
LOOP UNTIL LEFT$(c1$, 1) = "-"
c$ = c$ + CHR$(48 + k%)
c2$ = RTRIM$(LTRIM$(c2$))
l2% = LEN(c2$)
MID$(a1$, 1, i%) = SPACE$(i% - l2%) + c2$
NEXT
'a1$为余数
longqu$ = cut0$(c$)
END FUNCTION
FUNCTION longsub$ (a$, b$)
a1$ = cut0$(LTRIM$(RTRIM$(a$)))
b1$ = cut0$(LTRIM$(RTRIM$(b$)))
la% = LEN(a1$)
lb% = LEN(b1$)
IF la% > lb% THEN l% = la% ELSE l% = lb%
IF la% < lb% OR (la% = lb% AND b1$ > a1$) THEN
cc$ = "-"
SWAP a1$, b1$
END IF
a1$ = dao$(a1$)
b1$ = dao$(b1$)
FOR i% = 1 TO l%
a% = VAL(MID$(a1$, i%, 1))
b% = VAL(MID$(b1$, i%, 1))
c% = k% + a% - b%
IF c% < 0 THEN
c% = c% + 10
k% = -1
END IF
c$ = c$ + CHR$(48 + c%)
NEXT
longsub$ = cc$ + cut0$(dao$(c$))
END FUNCTION
FUNCTION longx$ (a$, b$)
a1$ = dao$(LTRIM$(RTRIM$(a$)))
b1$ = dao$(LTRIM$(RTRIM$(b$)))
la% = LEN(a1$)
lb% = LEN(b1$)
c$ = SPACE$(la% + lb%)
FOR i% = 1 TO la%
ia% = VAL(MID$(a1$, i%, 1))
FOR j% = 1 TO lb%+1
ib% = VAL(MID$(b1$, j%, 1))
lc% = i% + j% - 1
ic% = ia% * ib% + VAL(MID$(c$, lc%, 1)) + k%
IF ic% > 9 THEN
k% = ic% \ 10
ic% = ic% MOD 10
else
k%=0
END IF
MID$(c$, lc%, 1) = CHR$(48 + ic%)
NEXT j%, i%
longx$ = dao$(RTRIM$(c$))
END FUNCTION
完全是按照日常口算方法去计算的,
暂时只做到整数的运算,小数点和符号的运算还没有加进来,
欢迎大家有时间帮忙提提意见如何加上小数点和符号的运算处理,
弄好后还可以加进括号的多项式计算.