主题:[原创]N进制转M进制(含小数部分和正负号,测试版)
'2=<n,m<36,n进制转m进制,保留x位小数(没考虑四舍五入)。程序存在两个问题,请为怕麻烦的读者自行解决。另,回复得急,未经严格测试,可能有问题,仅起到抛砖引玉的作用。
CLS : flag$ = "+": INPUT a$, n, m, x: a = INSTR(a$, ".")
FOR i = 1 TO LEN(a$)
c$ = MID$(a$, i, 1)
IF (c$ < "0" AND c$ <> "+" AND c$ <> "-" AND c$ <> ".") OR (c$ > "9" AND c$ < "A") OR c$ > "Z" THEN PRINT "Reinput": END
IF i <> 1 AND (c$ = "-" OR c$ = "+") THEN PRINT "Reinput": END
IF c$ = "." THEN idat = idat + 1: IF idat > 1 OR i = 1 OR i = i THEN PRINT "Reinput": END
'以上判断输入格式部分仍存在着问题,就是当源数据是N进制时,输入的a$中的任意字符不能超过N。这部分只要参照后面的语句,定个界即可,故这里就不麻烦了。
NEXT i
IF a <> 0 THEN
a1$ = MID$(a$, 1, a - 1): a2$ = MID$(a$, a + 1, LEN(a$) - a + 1)
ELSE
a1$ = a$: a2$ = "0"
END IF
'以上是分解整数和小数部分
IF MID$(a1$, 1, 1) = "-" THEN a1$ = MID$(a1$, 2, LEN(a1$) - 1): flag$ = "-"
'该语句判断正负号
ba1 = 0: k = 0
FOR i = LEN(a1$) TO 1 STEP -1
c$ = MID$(a1$, i, 1)
IF c$ >= "A" AND c$ <= "Z" THEN c = ASC(c$) - 55 ELSE c = ASC(c$) - 48
ba1 = ba1 + c * n ^ k: k = k + 1
NEXT i: btemp = ba1: btemp$ = ""
'以上N进制转10进制(不好意思,我不会N一下子转成M,只能曲线救国)
DO
bmd = btemp MOD m
IF bmd < 10 THEN m$ = CHR$(bmd + 48) ELSE m$ = CHR$(bmd + 55)
btemp = btemp \ m
btemp$ = m$ + btemp$
LOOP UNTIL btemp = 0
'以上10转M。整数部分处理完毕。下面开始处理小数部分
ba2 = 0: k = 0
FOR i = 1 TO LEN(a2$)
c$ = MID$(a2$, i, 1)
IF c$ >= "A" AND c$ <= "Z" THEN c = ASC(c$) - 55 ELSE c = ASC(c$) - 48
k = k + 1: ba2 = ba2 + c * n ^ (-k)
NEXT i: btemp1$ = "": bbb = ba2
'以上是小数部分自N进制转成10进制
FOR i = 1 TO x
c = ba2 * m: cin = INT(c): ba2 = c - cin
IF cin < 10 THEN cin$ = CHR$(cin + 48) ELSE cin$ = CHR$(cin + 55)
btemp1$ = btemp1$ + cin$
NEXT i
'以上是小数部分自10进转成M进制(怕麻烦所以没考虑四舍五入,仅硬生生地保留了X位小数)
PRINT a$; "=";
IF btemp1$ <> "" THEN PRINT btemp$ + "." + btemp1$ ELSE PRINT btemp$
END
CLS : flag$ = "+": INPUT a$, n, m, x: a = INSTR(a$, ".")
FOR i = 1 TO LEN(a$)
c$ = MID$(a$, i, 1)
IF (c$ < "0" AND c$ <> "+" AND c$ <> "-" AND c$ <> ".") OR (c$ > "9" AND c$ < "A") OR c$ > "Z" THEN PRINT "Reinput": END
IF i <> 1 AND (c$ = "-" OR c$ = "+") THEN PRINT "Reinput": END
IF c$ = "." THEN idat = idat + 1: IF idat > 1 OR i = 1 OR i = i THEN PRINT "Reinput": END
'以上判断输入格式部分仍存在着问题,就是当源数据是N进制时,输入的a$中的任意字符不能超过N。这部分只要参照后面的语句,定个界即可,故这里就不麻烦了。
NEXT i
IF a <> 0 THEN
a1$ = MID$(a$, 1, a - 1): a2$ = MID$(a$, a + 1, LEN(a$) - a + 1)
ELSE
a1$ = a$: a2$ = "0"
END IF
'以上是分解整数和小数部分
IF MID$(a1$, 1, 1) = "-" THEN a1$ = MID$(a1$, 2, LEN(a1$) - 1): flag$ = "-"
'该语句判断正负号
ba1 = 0: k = 0
FOR i = LEN(a1$) TO 1 STEP -1
c$ = MID$(a1$, i, 1)
IF c$ >= "A" AND c$ <= "Z" THEN c = ASC(c$) - 55 ELSE c = ASC(c$) - 48
ba1 = ba1 + c * n ^ k: k = k + 1
NEXT i: btemp = ba1: btemp$ = ""
'以上N进制转10进制(不好意思,我不会N一下子转成M,只能曲线救国)
DO
bmd = btemp MOD m
IF bmd < 10 THEN m$ = CHR$(bmd + 48) ELSE m$ = CHR$(bmd + 55)
btemp = btemp \ m
btemp$ = m$ + btemp$
LOOP UNTIL btemp = 0
'以上10转M。整数部分处理完毕。下面开始处理小数部分
ba2 = 0: k = 0
FOR i = 1 TO LEN(a2$)
c$ = MID$(a2$, i, 1)
IF c$ >= "A" AND c$ <= "Z" THEN c = ASC(c$) - 55 ELSE c = ASC(c$) - 48
k = k + 1: ba2 = ba2 + c * n ^ (-k)
NEXT i: btemp1$ = "": bbb = ba2
'以上是小数部分自N进制转成10进制
FOR i = 1 TO x
c = ba2 * m: cin = INT(c): ba2 = c - cin
IF cin < 10 THEN cin$ = CHR$(cin + 48) ELSE cin$ = CHR$(cin + 55)
btemp1$ = btemp1$ + cin$
NEXT i
'以上是小数部分自10进转成M进制(怕麻烦所以没考虑四舍五入,仅硬生生地保留了X位小数)
PRINT a$; "=";
IF btemp1$ <> "" THEN PRINT btemp$ + "." + btemp1$ ELSE PRINT btemp$
END