主题:[晒] 24点算术计算程序
BAS源程序见附件 (QB4.0,QB7调试通过)
[font=宋体]
DECLARE SUB OP (i%, a$, B$, a#, B#, K1%, K2%, c$, Kf%)
DECLARE SUB Analysis (N%, Kf%)
DEFDBL A-H
DEFINT I-Z
CONST MaxNum = 8, AEL = .00000001#
DIM SHARED e(MaxNum), p$(MaxNum), K(MaxNum)
DIM SHARED Kq, Ans, NNc&, Nans&
'
' 主程序
'
DO
INPUT "N: (0 for Quit)", a$
IF a$ = "" THEN EXIT DO
N = VAL(a$): IF N < 1 THEN EXIT DO
IF N > MaxNum THEN N = MaxNum
FOR i = 1 TO N
DO
PRINT USING "A(#)="; i;
INPUT "", W
p$(i) = LTRIM$(STR$(W))
K(i) = 0
e(i) = W
LOOP UNTIL W > 0 AND W < 100
NEXT i
INPUT "Answer: (def=24)", a$
IF a$ = "" THEN Ans = 24 ELSE Ans = VAL(a$)
Nans& = 0: NNc& = 0: Kq = 0
'
OPEN "24.lst" FOR OUTPUT AS #2
'启动计算
Analysis N, 0
'显示统计信息
IF Nans& = 0 THEN
PRINT " No solution found ";
ELSE
PRINT "Total "; Nans&; "solutions found ";
END IF
PRINT "in "; NNc&; " cases!"
CLOSE #2
PRINT "The output is saved in 24.LST !"
LOOP
END
' 计算程序主体, 可递归调用
SUB Analysis (N, Kf)
IF N = 1 THEN
' 递归结束,判断结果
NNc& = NNc& + 1
Ir = ABS(e(1) - Ans) < AEL
IF Ir THEN
Nans& = Nans& + 1
PRINT USING "#####) &"; Nans&, p$(1);
PRINT #2, USING "#####) &"; Nans&, p$(1);
IF Kf THEN PRINT " ... Fraction" ELSE PRINT
IF Kf THEN PRINT #2, " ... Fraction" ELSE PRINT #2,
END IF
EXIT SUB
ELSE
' 拆解下一层
FOR i = 1 TO N - 1
FOR j = i + 1 TO N
a = e(i): B = e(j): e(j) = e(N)
a$ = p$(i): B$ = p$(j): p$(j) = p$(N)
K1 = K(i): K2 = K(j): K(j) = K(N):
'
IF a >= B THEN
OP i, a$, B$, a, B, K1, K2, "+", Kf1: Analysis N - 1, Kf
OP i, a$, B$, a, B, K1, K2, "-", Kf1: Analysis N - 1, Kf
OP i, a$, B$, a, B, K1, K2, "*", Kf1: Analysis N - 1, Kf
ELSE
OP i, B$, a$, B, a, K2, K1, "+", Kf1: Analysis N - 1, Kf
OP i, B$, a$, B, a, K2, K1, "-", Kf1: Analysis N - 1, Kf
OP i, B$, a$, B, a, K2, K1, "*", Kf1: Analysis N - 1, Kf
END IF
IF B <> 0 THEN
OP i, a$, B$, a, B, K1, K2, "/", Kf1
Analysis N - 1, Kf1 OR Kf
END IF
IF a <> 0 THEN
OP i, B$, a$, B, a, K2, K1, "/", Kf1
Analysis N - 1, Kf1 OR Kf
END IF
'
e(i) = a: e(j) = B
p$(i) = a$: p$(j) = B$
K(i) = K1: K(j) = K2
NEXT j
NEXT i
END IF
END SUB
' 处理算术运算,生成表达式片断
SUB OP (i, a$, B$, a, B, K1, K2, c$, Kf)
Ic = INSTR("+-*/^~", c$)
SELECT CASE Ic
CASE 1: e(i) = a + B
CASE 2: e(i) = a - B
CASE 3: e(i) = a * B
CASE 4:
e(i) = a / B
Kf = ABS(e(i) - INT(e(i) + AEL)) > AEL
END SELECT
'
a1$ = a$
IF K1 > 0 AND Ic > 2 AND K1 <= 2 THEN a1$ = "(" + a$ + ")"
b1$ = B$
IF K2 > 0 THEN
IF Ic > K2 OR (Ic = K2 AND (Ic MOD 2 = 0)) THEN b1$ = "(" + B$ + ")"
END IF
p$(i) = a1$ + c$ + b1$
'
K(i) = Ic
END SUB
[/font]
[font=宋体]
DECLARE SUB OP (i%, a$, B$, a#, B#, K1%, K2%, c$, Kf%)
DECLARE SUB Analysis (N%, Kf%)
DEFDBL A-H
DEFINT I-Z
CONST MaxNum = 8, AEL = .00000001#
DIM SHARED e(MaxNum), p$(MaxNum), K(MaxNum)
DIM SHARED Kq, Ans, NNc&, Nans&
'
' 主程序
'
DO
INPUT "N: (0 for Quit)", a$
IF a$ = "" THEN EXIT DO
N = VAL(a$): IF N < 1 THEN EXIT DO
IF N > MaxNum THEN N = MaxNum
FOR i = 1 TO N
DO
PRINT USING "A(#)="; i;
INPUT "", W
p$(i) = LTRIM$(STR$(W))
K(i) = 0
e(i) = W
LOOP UNTIL W > 0 AND W < 100
NEXT i
INPUT "Answer: (def=24)", a$
IF a$ = "" THEN Ans = 24 ELSE Ans = VAL(a$)
Nans& = 0: NNc& = 0: Kq = 0
'
OPEN "24.lst" FOR OUTPUT AS #2
'启动计算
Analysis N, 0
'显示统计信息
IF Nans& = 0 THEN
PRINT " No solution found ";
ELSE
PRINT "Total "; Nans&; "solutions found ";
END IF
PRINT "in "; NNc&; " cases!"
CLOSE #2
PRINT "The output is saved in 24.LST !"
LOOP
END
' 计算程序主体, 可递归调用
SUB Analysis (N, Kf)
IF N = 1 THEN
' 递归结束,判断结果
NNc& = NNc& + 1
Ir = ABS(e(1) - Ans) < AEL
IF Ir THEN
Nans& = Nans& + 1
PRINT USING "#####) &"; Nans&, p$(1);
PRINT #2, USING "#####) &"; Nans&, p$(1);
IF Kf THEN PRINT " ... Fraction" ELSE PRINT
IF Kf THEN PRINT #2, " ... Fraction" ELSE PRINT #2,
END IF
EXIT SUB
ELSE
' 拆解下一层
FOR i = 1 TO N - 1
FOR j = i + 1 TO N
a = e(i): B = e(j): e(j) = e(N)
a$ = p$(i): B$ = p$(j): p$(j) = p$(N)
K1 = K(i): K2 = K(j): K(j) = K(N):
'
IF a >= B THEN
OP i, a$, B$, a, B, K1, K2, "+", Kf1: Analysis N - 1, Kf
OP i, a$, B$, a, B, K1, K2, "-", Kf1: Analysis N - 1, Kf
OP i, a$, B$, a, B, K1, K2, "*", Kf1: Analysis N - 1, Kf
ELSE
OP i, B$, a$, B, a, K2, K1, "+", Kf1: Analysis N - 1, Kf
OP i, B$, a$, B, a, K2, K1, "-", Kf1: Analysis N - 1, Kf
OP i, B$, a$, B, a, K2, K1, "*", Kf1: Analysis N - 1, Kf
END IF
IF B <> 0 THEN
OP i, a$, B$, a, B, K1, K2, "/", Kf1
Analysis N - 1, Kf1 OR Kf
END IF
IF a <> 0 THEN
OP i, B$, a$, B, a, K2, K1, "/", Kf1
Analysis N - 1, Kf1 OR Kf
END IF
'
e(i) = a: e(j) = B
p$(i) = a$: p$(j) = B$
K(i) = K1: K(j) = K2
NEXT j
NEXT i
END IF
END SUB
' 处理算术运算,生成表达式片断
SUB OP (i, a$, B$, a, B, K1, K2, c$, Kf)
Ic = INSTR("+-*/^~", c$)
SELECT CASE Ic
CASE 1: e(i) = a + B
CASE 2: e(i) = a - B
CASE 3: e(i) = a * B
CASE 4:
e(i) = a / B
Kf = ABS(e(i) - INT(e(i) + AEL)) > AEL
END SELECT
'
a1$ = a$
IF K1 > 0 AND Ic > 2 AND K1 <= 2 THEN a1$ = "(" + a$ + ")"
b1$ = B$
IF K2 > 0 THEN
IF Ic > K2 OR (Ic = K2 AND (Ic MOD 2 = 0)) THEN b1$ = "(" + B$ + ")"
END IF
p$(i) = a1$ + c$ + b1$
'
K(i) = Ic
END SUB
[/font]