主题:小数化简程序(自创!)
程序如下:
CLS
INPUT n$
DIM a, b AS LONG: DIM m, h AS LONG: DIM i, j AS LONG
l = LEN(n$): k = INSTR(n$, "/")
a = VAL(MID$(n$, 1, k - 1)): b = VAL(MID$(n$, k + 1, l))
m = a: h = b: i = a: j = b
DO
r = m MOD h
m = h
h = r
LOOP UNTIL r = 0
a = a / m: b = b / m
PRINT LTRIM$(STR$(i)); "/"; LTRIM$(STR$(j)); "=";
IF a > b THEN PRINT LTRIM$(STR$(a \ b)); "+"; LTRIM$(STR$(a - b * (a \ b))); "/"; LTRIM$(STR$(b)): END
IF a = b THEN PRINT "1": END
IF b = 1 THEN PRINT LTRIM$(STR$(a)) ELSE PRINT LTRIM$(STR$(a)); "/"; LTRIM$(STR$(b))
END
很简单的程序
输出效果如:
把一个分数化成最简分数:
输入:2/4
输出:2/4=1/2
输入:8/6
输出:8/6=1+1/3
输入:4/2
输出:4/2=2
8/6=1+1/3 也就是所谓的带分数
以上是我的方法,以下是唐铭康的方法:
CLS
DIM a, b AS LONG
DO
INPUT a$
p = INSTR(1, a$, "/")
IF p = 0 THEN PRINT "DATA ERROR!": END
a = VAL(LEFT$(a$, p - 1))
b = VAL(MID$(a$, p + 1, 1000))
LOOP UNTIL a > 0 AND b > 0 AND a < 100000 AND b < 100000
PRINT a$; "=";
IF a > b THEN m = b ELSE m = a
FOR i = m TO 1 STEP -1
IF a MOD i = 0 AND b MOD i = 0 THEN EXIT FOR
NEXT i
a = a / i: b = b / i
IF a / b = a \ b THEN PRINT LTRIM$(STR$(a / b)): END
x$ = LTRIM$(STR$(a)): y$ = LTRIM$(STR$(b))
IF a > b THEN
x$ = LTRIM$(STR$(a - b * (a \ b)))
PRINT LTRIM$(STR$(a \ b)); "+";
END IF
PRINT x$; "/"; y$
END
来回帖的顺便把这题做一下,我看你们有什么不一样的方法。
CLS
INPUT n$
DIM a, b AS LONG: DIM m, h AS LONG: DIM i, j AS LONG
l = LEN(n$): k = INSTR(n$, "/")
a = VAL(MID$(n$, 1, k - 1)): b = VAL(MID$(n$, k + 1, l))
m = a: h = b: i = a: j = b
DO
r = m MOD h
m = h
h = r
LOOP UNTIL r = 0
a = a / m: b = b / m
PRINT LTRIM$(STR$(i)); "/"; LTRIM$(STR$(j)); "=";
IF a > b THEN PRINT LTRIM$(STR$(a \ b)); "+"; LTRIM$(STR$(a - b * (a \ b))); "/"; LTRIM$(STR$(b)): END
IF a = b THEN PRINT "1": END
IF b = 1 THEN PRINT LTRIM$(STR$(a)) ELSE PRINT LTRIM$(STR$(a)); "/"; LTRIM$(STR$(b))
END
很简单的程序
输出效果如:
把一个分数化成最简分数:
输入:2/4
输出:2/4=1/2
输入:8/6
输出:8/6=1+1/3
输入:4/2
输出:4/2=2
8/6=1+1/3 也就是所谓的带分数
以上是我的方法,以下是唐铭康的方法:
CLS
DIM a, b AS LONG
DO
INPUT a$
p = INSTR(1, a$, "/")
IF p = 0 THEN PRINT "DATA ERROR!": END
a = VAL(LEFT$(a$, p - 1))
b = VAL(MID$(a$, p + 1, 1000))
LOOP UNTIL a > 0 AND b > 0 AND a < 100000 AND b < 100000
PRINT a$; "=";
IF a > b THEN m = b ELSE m = a
FOR i = m TO 1 STEP -1
IF a MOD i = 0 AND b MOD i = 0 THEN EXIT FOR
NEXT i
a = a / i: b = b / i
IF a / b = a \ b THEN PRINT LTRIM$(STR$(a / b)): END
x$ = LTRIM$(STR$(a)): y$ = LTRIM$(STR$(b))
IF a > b THEN
x$ = LTRIM$(STR$(a - b * (a \ b)))
PRINT LTRIM$(STR$(a \ b)); "+";
END IF
PRINT x$; "/"; y$
END
来回帖的顺便把这题做一下,我看你们有什么不一样的方法。