四则运算(官方程序)

阅读更多
四则运算(官方程序)
****四则运算****
]10 CLS
]20 DIM OPERAND(6),OPERATOR(6),EPS(12)
]30 DIM PRIORITY(6,6),STACK1(6),STACK2(6)
]40 RANGE=10:AMOUNT=10:DIFFICULTY=1
]50 FOR I=0 TO 6
]60 FOR J=0 TO 6
]70 READ PRIORITY(I,J)
]80 NEXT J
]90 NEXT I
]200 CLS
]210 LOCATE 1,4:PRINT "小学四则运算":PRINT "1,开始测试" /*主菜单*/
]220 PRINT "2,选择难度"
]230 PRINT "3,题目数量"
]240 PRINT "请选择(1-3):";
]250 LOCATE 5,13:CHOICE$=INDEX$ /*定位光标*/
]252 IF ASC(CHOICE$)=27 THEN GOTO 9999
]260 IF NOT (CHOICE$="b" OR CHOICE$="n" OR CHOICE$="m") THEN GOTO 200
]272 IF CHOICE$="b" GOTO 400
]273 IF CHOICE$="n" GOTO 500
]274 IF CHOICE$="m" GOTO 600
]400 IF DIFFICULTY<6 THEN IF DIFFICULTY=5 THEN GOTO 1000 ELSE GOTO 700
]500 CLS
]510 PRINT "1,加法 2,减法": PRINT "3,乘法 4,除法" /*二级菜单*/
]520 PRINT "5,四则混合 6,小于10": PRINT "7,小于100"
]540 PRINT "请选择(1-7):";
]550 LOCATE 5,13:CHOICE$=INDEX$
]562 IF CHOICE$="b" THEN DIFFICULTY=1: GOTO 200
]563 IF CHOICE$="n" THEN DIFFICULTY=2: GOTO 200
]564 IF CHOICE$="m" THEN DIFFICULTY=3: GOTO 200
]565 IF CHOICE$="g" THEN DIFFICULTY=4: GOTO 200
]566 IF CHOICE$="h" THEN DIFFICULTY=5: GOTO 200
]567 IF CHOICE$<>"j" AND CHOICE$<>"t" THEN GOTO 500
]570 IF CHOICE$="j" THEN RANGE=10 ELSE IF CHOICE$="t" THEN RANGE=100
]580 GOTO 200
]600 CLS
]610 PRINT RINT "请输入题目数量:";
]620 INPUT " ";A$
]630 AMOUNT=INT(VAL(A$)) /*将字符转化为数值*/
]640 IF AMOUNT <=0 THEN GOTO 600
]660 GOTO 200
]700 REM 简单运算
]701 RIGHT=0
]710 FOR TIMES=1 TO AMOUNT
]730 NUM1=INT(RANGE*RND(1)) /*使所取的随机数小于10,由RANGE值决定*/
]740 NUM2=INT(RANGE*RND(1))
]750 IF DIFFICULTY=4 AND NUM2<=1 THEN GOTO 740
]760 ON DIFFICULTY GOSUB 3500,3600,3700,3800
]770 IF ANSWER <0 THEN GOTO 730
]775 IF ANSWER <>INT(ANSWER) AND DIFFICULTY=4 THEN GOTO 730 /*答题错误转730重做此题*/
]780 CLS: PRINT NUM1;
]790 ON DIFFICULTY GOSUB 2510,2520,2530,2540
]800 PRINT NUM2;"=";
]810 INPUT REPLY$
]815 IF LEN(REPLY$)>0 AND VAL(REPLY$)=0 AND REPLY$<>"0" THEN GOTO 780
]820 IF ABS(VAL(REPLY$)-ANSWER)<.01 THEN PRINT "太棒了!^_^":RIGHT=RIGHT+1:GOTO 830
]825 PRINT "加油喔!":PRINT "正确答案:";INT(ANSWER+0.5)
]830 IF TIMES<>AMOUNT THEN PRINT "按任意键继续...";:GOSUB 3900
]880 NEXT TIMES
]890 PRINT "总共答对:";RIGHT;" 答错:",AMOUNT-RIGHT
]900 PRINT "是否继续(Y/N)";
]910 CON$=INKEY$
]920 IF CON$<>"n" THEN GOTO 700
]930 GOTO 200
]1000 REM 四则混合运算
]1001 RIGHT =0
]1010 FOR TIMES=1 TO AMOUNT
]1019 REM 生成表达式
]1020 FOR I=0 TO 6
]1024 OPERAND(I)=0:OPERATOR(I)=0
]1050 EPS(I)=0:EPS(12-I)=0
]1060 NEXT I
]1069 REM NUM:操作数的个数(2-4)
]1070 NUM=INT(3*RND(1))+2
]1080 FOR I=0 TO NUM-1
]1090 OPERAND(I)=INT(RANGE*RND(1))
]1110 NEXT I
]1120 IF INT(4*RND(1))=1 AND NUM<>2 THEN GOTO 1170
]1130 FOR I=0 TO NUM-2
]1140 OPERATOR(I)=INT(4*RND(1))+1
]1150 NEXT I
]1160 GOTO 1300
]1169 REM 生成带括号的表达式
]1170 BRACKET=INT((NUM-1)*RND(1))
]1180 FOR I=0 TO NUM
]1190 OPERATOR(I)=INT(4*RND(1))+1
]1200 NEXT I
]1210 OPERATOR(BRACKET)=5
]1220 OPERATOR(BAACKET+2)=6
]1299 REM 为计算表达式的值做准备
]1300 J=0: I=0 :EXPROI=0
]1310 WHILE INT(I+0.5)]1320 IF OPERATOR(J)=5 THEN J=J+1:EPS(EXPROI)=-5:EXPROI=EXPPOI+1
]1330 EPS(EXPPOI)=OPERAND(I):EXPROI=EXPPOI+1
]1340 ON OPERATOR(J) GOSUB 2000,2100,2200,2300
]1350 IF OPERATOR(J)=6 THEN J=J+1:EPS(EXPPOI)=-6:EXPPOI=EXPPOI+1:GOTO 1340
]1360 I=I+1:j=J+1
]1370 WEND
]1390 EPS(EXPPOI)=-7
]1499 REM 计算运算式的值
]1500 ST1=0: ST2=0: EXPPOI=0
]1510 STACK2(ST2)=-7:ST2=ST2+1
]1520 CH=EPS(EXPPOI):EXPPOI=EXPPOI+1
]1530 WHILE NOT (CH-7 AND STACK2(ST2-1)=-7)
]1540 IF CH>0 THEN GOSUB 1570 ELSE GOSUB 1580
]1550 WEND
]1560 GOTO 1600
]1570 STACK1(ST1)=CH:ST1=ST1+1:CH=EPS(EXPPOI):EXPPOI=EXPPOI+1
]1575 RETURN
]1580 ON PRIORITY(-STACK2(ST2-1)-1,-CH-1) GOSUB 3100,3200,3300 '注意ON语句的用法
]1585 RETURN
]1599 REM 判断用户答案是否正确
]1600 IF STACK1(ST1-1)<0 THEN GOTO 1020
]1610 ANSWER=STACK1(ST1-1)
]1615 CLS
]1620 FOR K=0 TO EXPPOI-1
]1630 IF EPS(K)>=0 THEN PRINT EPS(K);ELSE ON -EPS(K) GOSUB 2510,2520,2540,2550,2560
]1650 NEXT K
]1660 PRINT "=";
]1670 INPUT REPLY$
]1675 IF LEN(REPLY$)>0 AND VAL(REPLY$)>0 AND VAL(REPLY$=0 AND REPLY$<>"0" THEN GOTO 1615
]1680 IF ABS(VAL(REPLY$)-ANSWER)<.01 THEN GOSUB 1910 ELSE GOSUB 1930
]1690 IF TIMES <> AMOUNT THEN PRINT "按任意键继续...";:GOSUB 3900
]1700 NEXT TIMES
]1710 PRINT "总共答对:";RIGHT;" 答错:";AMOUNT-RIGHT
]1720 PRINT "是否继续(Y/N)";
]1730 CON$=INKEY$
]1740 IF CON$<>"n" THEN GOTO 1000
]1800 GOTO 200
]1910 PRINT "太棒了!^_^":PRINT=RIGHT+1
]1920 RETURN
]1930 PRINT "加油喔!":PRINT "正确答案:";:IF ANSWER<1 THEN PRINT "0";
]1940 PRINT INT (ANSWER*1000+0.5)/1000
]1950 RETURN
]2000 EPS(EXPROI)=-1:EXPPOI=EXPPOI+1
]2010 RETURN
]2100 EPS(EXPPOI)=-2:EXPPOI=EXPPOI+1
]2110 RETURN
]2200 EPS(EXPPOI)=-3:EXPPOI=EXPPOI+1
]2210 RETURN
]2300 EPS(EXPPOI)=-4:EXPPOI=EXPPOI+1
]2310 RETURN
]2510 PRINT "+";:RETURN
]2520 PRINT "-";:RETURN
]2530 PRINT "X";:RETURN
]2540 PRINT "/";:RETURN
]2550 PRINT "(";:RETURN
]2560 PRINT ")";:RETURN
]3100 STACK2(ST2)=CH:ST2=ST2+1
]3310 CH=EPS(EXPPOI):EXPPOI=EXPPOI+1
]3120 RETURN
]3200 ST2=ST2-1:STACK2(ST2)=0
]3210 CH=EPS(EXPPOI):EXPPOI=EXPPOI+1
]3220 RETURN
]3300 ST2=ST2-1:THETA=STACK(ST2):STACK2(ST2)=0
]3310 ST1=ST1-1:NUM2=STACK1(ST1):STACK1(ST1)=0
]3320 ST1=ST1-1:NUM1=STACK1(ST1):STACK1(ST1)=0
]3330 ON-THETA GOSUB 3500,3600,3700,3810
]3340 STACK1(ST1)=ANSWER:ST1=ST1+1
]3350 IF THETA=-4 AND ANSWER=9999 THEN GOTO 1020
]3360 RETURN
]3500 ANSWER=NUM1+NUM2: RETURN
]3600 ANSWER=NUM1-NUM2:RETURN
]3700 ANSWER=NUM1*NUM2:RETURN
]3800 ANSWER=NUM1/NUM2:RETURN
]3810 IF NUM2=0 THEN ANSWER=9999 ELSE ANSWER=NUM1/NUM2
]3820 RETURN
]3900 KEYVAL$=CHR$(255)
]3910 WHILE 1
]3920 IF KEYVAL$<>INKEY$ THEN GOTO 3940
]3930 WEND
]3940 RETURN
]4001 DATA 3,3,1,1,1,3,3
]4002 DATA 3,3,1,1,1,3,3
]4003 DATA 3,3,3,3,1,3,3
]4004 DATA 3,3,3,3,1,3,3
]4005 DATA 1,1,1,1,1,2,0
]4006 DATA 3,3,3,3,0,3,3
]4007 DATA 1,1,1,1,1,0,2
]9999 END

 

你可能感兴趣的:(J#)