主题:是英雄就编这道题
QB爱好者
[专家分:370] 发布于 2007-10-06 21:11:00
输入一个带括号的表达式,允许用:
数字(含负数)
+,-,*,/,\,MOD
要求算出其结果(保留5位小数)
注:
1/7*7=1
5/3*12=20
回复列表 (共17个回复)
沙发
Matodied [专家分:7560] 发布于 2007-10-07 21:33:00
我的想法是:
设立一个函数,用来计算一个表达式(不含括号)。
然后把主表达式的括号全部分解出来(不过你要说明,这里的括号可以用什么,是只能用(),还是能用()、[]、{},我这里按前者来),每一个括号里的内容就是一个子表达式,用刚才设立的函数把所有子表达式(括号里的内容)的结果算出来,再用这些结果来代替原来主表达式的括号处,最后计算主表达式。
比如主表达式=2 * (6 + 7 MOD 4) + (2 * 6 + 7) MOD 4。
计算该表达式过程如下:
(1)这个表达式有两个子表达式,分别是(6 + 7 MOD 4)和(2 * 6 + 7)。
(2)调用calc("6+7MOD4")(注意,在计算子表达式时所有的数字和算符之间全部紧挨着,不空格),得结果9;调用calc("2*6+7"),得结果19。
(3)将主表达式变成2 * 9 + 19 MOD 4。
(4)调用calc("2*9+19MOD4"),的最终结果21。
在calc函数里,在计算时需要注意优先级问题,即先算*、/,再算\,再算MOD,最后算+、-。
板凳
staa [专家分:3690] 发布于 2007-10-08 15:50:00
以前发过类似的贴,是一个简单计算函数,不带括号,仅有+-*/四则运算
把那个函数扩展一下,加上对\和MOD的处理就能满足二楼的设想
另外,对多层括号的处理也有点小麻烦,要注意一下, 还有就是对E的处理,E是科学计数的规定符号
因为一个算式开始的时候就是个字符串, 所以要大量使用字符串函数
3 楼
staa [专家分:3690] 发布于 2007-10-08 15:51:00
去括号时有个技巧,就是先找),再向回找(
呵呵
4 楼
moz [专家分:37620] 发布于 2007-10-08 21:44:00
我非英雄
5 楼
hxy_anhui [专家分:320] 发布于 2009-01-31 15:33:00
CLS
INPUT S$
L = LEN(S$): DIM F$(L), N(L): J = 1
FOR I = 1 TO L
A$ = MID$(S$, I, 1)
IF A$ < "0" OR A$ > "9" THEN
F$(J) = A$: N(J) = VAL(B$): J = J + 1: B$ = ""
ELSE B$ = B$ + A$
END IF
NEXT I: N(J) = VAL(B$)
FOR I = 1 TO J
IF F$(I) = "^" THEN N(I) = N(I) ^ N(I + 1): GOSUB 10
NEXT I
FOR I = 1 TO J
IF F$(I) = "*" THEN N(I) = N(I) * N(I + 1): GOSUB 10
IF F$(I) = "/" THEN N(I) = N(I) / N(I + 1): GOSUB 10
NEXT I
FOR I = 1 TO J
IF F$(I) = "+" THEN N(I) = N(I) + N(I + 1): GOSUB 10
IF F$(I) = "-" THEN N(I) = N(I) - N(I + 1): GOSUB 10
NEXT I
PRINT S$; "="; N(1)
END
10
FOR Q = I TO L - 2: F$(Q) = F$(Q + 1): N(Q + 1) = N(Q + 2): NEXT Q
I = I - 1: J = J - 1
RETURN
6 楼
冰冻小企鹅 [专家分:190] 发布于 2009-02-17 19:42:00
我不是英雄,不懂编程!那是有怪的!
[em9][em9][em9][em9][em9][em9][em9][em9][em9][em9][em9][em9][em9][em9][em9][em9][em9][em9][em9][em9][em9][em9]
7 楼
phile [专家分:2310] 发布于 2009-07-21 09:54:00
简单简单!!!
CLS
INPUT A$
L = LEN(A$)
DIM A$(L), B(L)
PRINT A$; "=";
DO
Q = INSTR(A$, ")")
IF Q <> 0 THEN
K = 0
DO
K = INSTR(K + 1, A$, "(")
IF K < Q AND K <> 0 THEN P = K
LOOP UNTIL K = 0 OR K > Q
AA$ = MID$(A$, 1, P - 1)
AB$ = MID$(A$, P + 1, Q - (P + 1))
AC$ = MID$(A$, Q + 1)
GOSUB 100
AB$ = LTRIM$(STR$(B(1)))
A$ = AA$ + AB$ + AC$
END IF
LOOP UNTIL Q = 0
AB$ = A$
GOSUB 100
PRINT USING"########.#####";B(1)
END
100
T$ = ""
J = 1
LA = LEN(AB$)
FOR I = 1 TO LA
X$ = MID$(AB$, I, 1)
IF X$ = "-" AND MID$(AB$ , I + 1,1)>="0" AND MID$(AB$ ,I + 1, 1)<="9" THEN
T$=T$+X$
ELSEIF X$ = "+" OR X$ = "-" OR X$ = "*" OR X$ = "/" OR X$ = "\" X$ = "^" OR X$="M" THEN
IF X$ = "M" THEN
X$=X$+MID$(AB$ ,I + 1,2)
I=I+2
END IF
A$(J) = X$
B(J) = VAL(T$)
T$ = ""
J = J + 1
ELSE
T$ = T$ + X$
END IF
NEXT I
B(J) = VAL(T$)
FOR T = 1 TO J
IF A$(T) = "^" THEN B(T) = B(T) ^ B(T + 1): GOSUB 200
NEXT T
FOR T = 1 TO J
IF A$(T) = "*" THEN B(T) = B(T) * B(T + 1): GOSUB 200
IF A$(T) = "/" THEN B(T) = B(T) / B(T + 1): GOSUB 200
NEXT T
FOR T = 1 TO J
IF A$(T) ="\" TTHEN B(T) = B(T) \ B(T + 1): GOSUB 200
NEXT T
FOR T=1 TO J
IF A$(T) = "MOD" THEN B(T) = B(T) MOD B(T + 1):GOSUB 200
FOR T = 1 TO J
IF A$(T) = "+" THEN B(T) = B(T) + B(T + 1): GOSUB 200
IF A$(T) = "-" THEN B(T) = B(T) - B(T + 1): GOSUB 200
NEXT T
RETURN
200
FOR P = T TO LA - 2
A$(P) = A$(P + 1)
B(P + 1) = B(P + 2)
NEXT P
J = J - 1
T = T - 1
RETURN
[em2]
8 楼
phile [专家分:2310] 发布于 2009-07-21 15:12:00
还行吧!
9 楼
phile [专家分:2310] 发布于 2009-07-21 15:26:00
我又加了"^"
10 楼
FFKK [专家分:1610] 发布于 2009-07-21 20:20:00
CLS
INPUT A$
L = LEN(A$)
DIM A$(L), B(L)
PRINT A$; "=";
DO
Q = INSTR(A$, ")")
IF Q <> 0 THEN
K = 0
DO
K = INSTR(K + 1, A$, "(")
IF K < Q AND K <> 0 THEN P = K
LOOP UNTIL K = 0 OR K > Q
AA$ = MID$(A$, 1, P - 1)
AB$ = MID$(A$, P + 1, Q - (P + 1))
AC$ = MID$(A$, Q + 1)
GOSUB 100
AB$ = LTRIM$(STR$(B(1)))
A$ = AA$ + AB$ + AC$
END IF
LOOP UNTIL Q = 0
AB$ = A$
GOSUB 100
PRINT USING"########.#####";B(1)
END
100
T$ = ""
J = 1
LA = LEN(AB$)
FOR I = 1 TO LA
X$ = MID$(AB$, I, 1)
IF X$ = "-" AND MID$(AB$ , I + 1,1)>="0" AND MID$(AB$ ,I + 1, 1)<="9" THEN
T$=T$+X$
ELSEIF X$ = "+" OR X$ = "-" OR X$ = "*" OR X$ = "/" OR X$ = "\" X$ = "^" OR X$="M" THEN
IF X$ = "M" THEN
X$=X$+MID$(AB$ ,I + 1,2)
I=I+2
END IF
A$(J) = X$
B(J) = VAL(T$)
T$ = ""
J = J + 1
ELSE
T$ = T$ + X$
END IF
NEXT I
B(J) = VAL(T$)
FOR T = 1 TO J
IF A$(T) = "^" THEN B(T) = B(T) ^ B(T + 1): GOSUB 200
NEXT T
FOR T = 1 TO J
IF A$(T) = "*" THEN B(T) = B(T) * B(T + 1): GOSUB 200
IF A$(T) = "/" THEN B(T) = B(T) / B(T + 1): GOSUB 200
NEXT T
FOR T = 1 TO J
IF A$(T) ="\" TTHEN B(T) = B(T) \ B(T + 1): GOSUB 200
NEXT T
FOR T=1 TO J
IF A$(T) = "MOD" THEN B(T) = B(T) MOD B(T + 1):GOSUB 200
FOR T = 1 TO J
IF A$(T) = "+" THEN B(T) = B(T) + B(T + 1): GOSUB 200
IF A$(T) = "-" THEN B(T) = B(T) - B(T + 1): GOSUB 200
NEXT T
RETURN
200
FOR P = T TO LA - 2
A$(P) = A$(P + 1)
B(P + 1) = B(P + 2)
NEXT P
J = J - 1
T = T - 1
RETURN
______________________________________________________
[em1]
我来回复