主题:求助,怎样设计除法的竖式程序
我这个程序利用命令行参数计算加减乘除(单步),并以竖式形式显示结果.其中除法的竖式有错误(加粗的那一行).请大家帮助.
' $DYNAMIC
OPTION BASE 1
DIM a AS STRING, b AS STRING, result AS LONG, resMod AS LONG
DIM i AS LONG
DIM c AS STRING
c = COMMAND$
CLS
i = INSTR(c, "+")
IF i > 1 THEN
a = LEFT$(c, i - 1): b = MID$(c, i + 1)
result = VAL(a) + VAL(b)
PRINT STRING$(LEN(STR$(result)) + 1 - LEN(a), " ") + a
PRINT "+" + STRING$(LEN(STR$(result)) - LEN(b), " ") + b
PRINT STRING$(LEN(STR$(result)) + 1, "-")
PRINT " " + STR$(result)
END IF
i = INSTR(c, "-")
IF i > 1 THEN
a = LEFT$(c, i - 1): b = MID$(c, i + 1)
result = VAL(a) - VAL(b)
PRINT " " + a
PRINT "-" + STRING$(LEN(a) - LEN(b), " ") + b
PRINT STRING$(LEN(a) + 1, "-")
PRINT STRING$(LEN(a) - LEN(STR$(result)) + 1, " ") + STR$(result)
END IF
i = INSTR(c, "*")
IF i > 1 THEN
a = LEFT$(c, i - 1): b = MID$(c, i + 1)
result = VAL(a) * VAL(b)
DIM r(LEN(b)) AS LONG
PRINT STRING$(LEN(STR$(result)) + 1 - LEN(a), " ") + a
PRINT "x" + STRING$(LEN(STR$(result)) - LEN(b), " ") + b
PRINT STRING$(LEN(STR$(result)) + 1, "-")
IF LEN(b) >= 2 THEN
FOR i = 1 TO LEN(b)
r(i) = VAL(a) * VAL(MID$(b, LEN(b) - i + 1, 1))
IF (r(i)) <> 0 THEN
PRINT STRING$(LEN(STR$(result)) + 2 - LEN(STR$(r(i))) - i, " ") + STR$(r(i))
END IF
NEXT
PRINT STRING$(LEN(STR$(result)) + 1, "-")
END IF
PRINT " " + STR$(result)
END IF
i = INSTR(c, "/")
IF i > 1 THEN
a = LEFT$(c, i - 1): b = MID$(c, i + 1)
result = VAL(a) / VAL(b)
resMod = VAL(a) MOD VAL(b)
PRINT STRING$(LEN(a) + LEN(b) + 1 - LEN(STR$(result)), " ") + STR$(result)
PRINT STRING$(LEN(b) + 1, " ") + STRING$(LEN(a), "_")
PRINT b; ")"; a
FOR i = 2 TO LEN(STR$(result))
j = VAL(MID$(STR$(result), i, 1))
j = j * VAL(b)
PRINT STRING$(LEN(b) + i - 2, " ") + STR$(j)
PRINT STRING$(LEN(b) + 1, " ") + STRING$(LEN(a), "-")
[b]PRINT STRING$(LEN(b) + 2, " ") + STR$(VAL(a) - j)[/b]
NEXT
END IF
' $DYNAMIC
OPTION BASE 1
DIM a AS STRING, b AS STRING, result AS LONG, resMod AS LONG
DIM i AS LONG
DIM c AS STRING
c = COMMAND$
CLS
i = INSTR(c, "+")
IF i > 1 THEN
a = LEFT$(c, i - 1): b = MID$(c, i + 1)
result = VAL(a) + VAL(b)
PRINT STRING$(LEN(STR$(result)) + 1 - LEN(a), " ") + a
PRINT "+" + STRING$(LEN(STR$(result)) - LEN(b), " ") + b
PRINT STRING$(LEN(STR$(result)) + 1, "-")
PRINT " " + STR$(result)
END IF
i = INSTR(c, "-")
IF i > 1 THEN
a = LEFT$(c, i - 1): b = MID$(c, i + 1)
result = VAL(a) - VAL(b)
PRINT " " + a
PRINT "-" + STRING$(LEN(a) - LEN(b), " ") + b
PRINT STRING$(LEN(a) + 1, "-")
PRINT STRING$(LEN(a) - LEN(STR$(result)) + 1, " ") + STR$(result)
END IF
i = INSTR(c, "*")
IF i > 1 THEN
a = LEFT$(c, i - 1): b = MID$(c, i + 1)
result = VAL(a) * VAL(b)
DIM r(LEN(b)) AS LONG
PRINT STRING$(LEN(STR$(result)) + 1 - LEN(a), " ") + a
PRINT "x" + STRING$(LEN(STR$(result)) - LEN(b), " ") + b
PRINT STRING$(LEN(STR$(result)) + 1, "-")
IF LEN(b) >= 2 THEN
FOR i = 1 TO LEN(b)
r(i) = VAL(a) * VAL(MID$(b, LEN(b) - i + 1, 1))
IF (r(i)) <> 0 THEN
PRINT STRING$(LEN(STR$(result)) + 2 - LEN(STR$(r(i))) - i, " ") + STR$(r(i))
END IF
NEXT
PRINT STRING$(LEN(STR$(result)) + 1, "-")
END IF
PRINT " " + STR$(result)
END IF
i = INSTR(c, "/")
IF i > 1 THEN
a = LEFT$(c, i - 1): b = MID$(c, i + 1)
result = VAL(a) / VAL(b)
resMod = VAL(a) MOD VAL(b)
PRINT STRING$(LEN(a) + LEN(b) + 1 - LEN(STR$(result)), " ") + STR$(result)
PRINT STRING$(LEN(b) + 1, " ") + STRING$(LEN(a), "_")
PRINT b; ")"; a
FOR i = 2 TO LEN(STR$(result))
j = VAL(MID$(STR$(result), i, 1))
j = j * VAL(b)
PRINT STRING$(LEN(b) + i - 2, " ") + STR$(j)
PRINT STRING$(LEN(b) + 1, " ") + STRING$(LEN(a), "-")
[b]PRINT STRING$(LEN(b) + 2, " ") + STR$(VAL(a) - j)[/b]
NEXT
END IF