250 likes | 372 Views
阅读程序专题复习. 1 .写出下列程序的运行结果 DIM x AS INTEGER, y AS INTEGER CLS DATA 3,6,5 FOR i = 1 TO 2 RESTORE READ a, b% x = a + b% RESTORE a1 READ c, d y = c + d RESTORE READ a, b, c, d z = (a + b%) * i / (c + d) PRINT "x+y+z="; x + y + z NEXT i a1: DATA -2,-3,5,7,9 END. 3 6 9 -2 -3 -5
E N D
1.写出下列程序的运行结果 DIM x AS INTEGER, y AS INTEGER CLS DATA 3,6,5 FOR i = 1 TO 2 RESTORE READ a, b% x = a + b% RESTORE a1 READ c, d y = c + d RESTORE READ a, b, c, d z = (a + b%) * i / (c + d) PRINT "x+y+z="; x + y + z NEXT i a1: DATA -2,-3,5,7,9 END 3 6 9 -2 -3 -5 3,6,5,-2 3 7 3 6 9 -2 -3 -5 3,6,5,-2 6 10 x+y+z=7 x+y+z=10
2.写出下列程序的运行结果 DIM x AS INTEGER, y AS INTEGER x = 4: y = 2: n = 0 DO WHILE x < 12 AND y < 12 SELECT CASE x + y - 1 CASE 2 x = x + x + y + 1 y = x + y CASE 3, 5 y = y + x CASE 7 TO 18 x = x + 3 * y CASE ELSE x = 2: y = 2 END SELECT n = n + 1 LOOP PRINT x, y, n END 4 2 5 6 1 4 6 9 22 2 22 6 结果: 22 6 2
3、阅读下面程序并写出输出结果。 DIM a, b AS INTEGER DIM s AS STRING * 5 READ a, b WHILE b <> -1 SELECT CASE SGN(a + b) CASE IS < 0 m = m% + ABS(a + b) CASE 0 IF a > 0 THEN n = n + b ELSE s = STR$(a) + s END IF CASE ELSE l = l + 1 END SELECT READ a, b WEND PRINT m, n, s, l DATA 2.5,5.2,4,-5.6,-8,8.3,-20,20.4,3,8.5,-10.5,3,-16,15.7,5.8,-1 END 即a+b<0 结果: 7.5 0-16-22 即a+b=0 即a+b>0 >0 <0 =0 =0 >0 <0 =0
4、阅读下面程序并写出输出结果 Y=1 FOR A=1 TO 5 Y=Y+1 FOR B=1 TO A FOR C=1 TO B Y=Y+B NEXT C NEXT B NEXT A PRINT “Y=”;Y,A,B,C END 当A=1时 Y=Y+1*1 当A=2时 Y=Y+1*1+2*2 当A=3时 Y=Y+1*1+2*2+3*3 当A=4时 Y=Y+1*1+2*2+3*3+4*4 当A=5时 Y=Y+1*1+2*2+3*3+4*4+5*5 另Y初值为1,共5次Y+1 Y=Y+B*B 结果: Y=111 6 6 6
5.写出下列程序的运行结果 r = 0: s = 0: t = 0 FOR a = 1 TO 5 r = r + 1 FOR b = 1 TO 4 s = s + 1 FOR c = 1 TO b t = t + 1 NEXT c r = r + 2 s = -s NEXT b s = s + 1 r = r - 2 t = t + a a = a + 1 NEXT a PRINT "r="; r, "s="; s, "t="; t END 循环3次,1,3,5 t=t+10+a r=r+7 s=s+1 t=t+1+2+3+4 r=r+8 s=s+0 t=t+b 结果: r=21 s=3 t=39
6、阅读下面程序并写出输出结果 DIM A(3,3) FOR K=1 TO 3 FOR M=1 TO 3 READ A(K,M) NEXT M NEXT K S=0 FOR K=1 TO 3 FOR M=1 TO 3 IF K<>M THEN IF K<>4-M THEN S=S+A(K,M) END IF END IF NEXT M NEXT K PRINT “S=”;S DATA 1,2,3,4,5,6,7,8,9 END 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 K=4-M K=M 结果: S=20
7、阅读下面程序并写出输出结果。 n = 4 DIM a(n, n) AS INTEGER, max AS INTEGER FOR i = 1 TO n FOR j = 1 TO n READ a(i, j) NEXT j,i FOR i = 1 TO n max = a(i, 1) FOR j = 2 TO n IF max < a(i, j) THEN max = a(i, j) NEXT j WHILE max <> a(i, i) t = a(i, n) FOR k = n - 1 TO 1 STEP -1 a(i, k + 1) = a(i, k) NEXT k a(i, 1) = t WEND NEXT i FOR i = 1 TO n FOR j = 1 TO n PRINT a(i, j); NEXT j PRINT NEXT i DATA 31,8,25,9,4,13,1,78,2,35,6,20,45,3,33,7 31 8 25 9 4 13 1 78 2 35 6 20 45 3 33 7 求每行的最大值,并移动到对角线所在列 31 8 25 9 1 78 4 13 20 2 35 6 3 33 7 45
8、阅读下面程序并写出输出结果。 DIM a AS STRING a = "AXHIDYBVZJC" FOR i = 1 TO LEN(a) C$ = STRING$(1, MID$(a, i)) + C$ NEXT i PRINT "A="; C$ i = 1 DO UNTIL i > LEN(a) b$ = MID$(a, i, 1) IF ASC(b$) > 75 THEN MID$(a, i) = MID$(STR$(ASC(b$)), 2) i = i + 1 END IF i = i + 1 LOOP PRINT "A="; a PRINT "I="; i - 1 END 即C$=“CJZVBYDIHXA” 打印:A= CJZVBYDIHXA AXHIDYBVZJC 即b$>”K” A88ID8986JC 结果: A= CJZVBYDIHXA A= A88ID8986JC I=11
9. 阅读下面程序并写出输出结果: A$ = "My Home Town" TEMP$ = "" N = LEN(A$) DIM B$(N) FOR I = 1 TO N C$ = MID$(A$, I, 1) IF C$ > "a" AND C$ < "z" THEN B$(I) = CHR$(ASC(C$) + 1) ELSE B$(I) = C$ END IF NEXT I FOR I = 1 TO N TEMP$ = B$(I) + TEMP$ NEXT I PRINT TEMP$ END 小写字母下一个大写不变 反序 结果: oxpT fnpH zM
10.写出下列程序的运行结果 DECLARE SUB f (x!, y!, z!) DIM SHARED c a = 10: b = 6: c = 3 CALL f((a), b + 3, c) PRINT "a="; a, "b="; b, "c="; c CALL f(a, b - 4, c) PRINT "a="; a, "b="; b, "c="; c END SUB f (x, y, z) IF x > 5 OR y > x THEN z = z + c y = y + 1 x = x - 1 ELSE x = x - 2 y = y + z END IF PRINT "x="; x, "y="; y, "z="; z END SUB x=10,y=9,z=3 z=6,y=10,x=9 打印x=9 y=10 z=6① c=6 打印a=10 b=6 c=6② x=10,y=2,z=6 z=12,y=3,x=9 打印x=9 y=3 z=12③ 打印a=9 b=6 c=12④ 结果: x=9 y=10 z=6 a=10 b=6 c=6 x=9 y=3 z=12 a=9 b=6 c=12
11、阅读下面程序并写出运行结果。 DECLARE SUB EXAM(M AS INTEGER,N AS INTEGER) 运行结果: DIM A AS INTEGER DIM B AS INTEGER A=10:B=10 CALL EXAM(A,B) PRINT “A=”;A,”B=”;B A=20:B=20 CALL EXAM((A),B) PRINT “A=”;A,”B=”;B END SUB EXAM(M AS INTEGER,N AS INTEGER) STATIC C AS INTEGER M=A*M N=N+C+M C=M END SUB m=10,n=10 m=0,n=10 c=0 打印A=0 B=10① m=20, n =20 m=0,n=20 c=0 打印A=20 B=20②
12、阅读下面程序并写出输出结果。 DECLARE SUB sub1 (b) DECLARE SUB sub2 (b, c!) CLS DIM SHARED a a = 1: b = 2: c = 3 FOR i = 1 TO 2 CALL sub1((i)) PRINT a, b, c NEXT i END SUB sub1 (b) SHARED c a = a * c b = a / b c = c + b PRINT a, b, c CALL sub2(b, c) END SUB SUB sub2 (b, c) STATIC a a = a + b * 2 b = a MOD c / b c = a + b END SUB 当i=1时 b=1,c=3 a=3,b=3,c=6 打印3,3,6① Call sub2(b,c) b= ,c= b=3,c=6 a=6,b=0,c=6 0 6 打印3,2,6② 当i=2时 b=2,c=6 a=18,b=9,c=15 打印18,9,15③ Call sub2(b,c) b= ,c= b=9,c=15 a=18,b=0,c=24 0 24 打印18,2,24④
1、下面程序的功能是打印一个5行4列的矩阵, 并将每列中的最小数所在的行位置放入顺序文件MINROW.DAT中,矩阵中的元素值是[1,100]之间且正负相间的随机整数。 OPTION BASE 1 ______________ DIM B(4) T=1 FOR I=1 TO 5 FOR J=1 TO 4 A(I,J)= ___________ T = -T PRINT A(I,J); NEXT J PRINT NEXT I FOR J=1 TO 4 MIN=1 FOR I=2 TO 5 IF A(I,J)<A(MIN,J) THEN MIN=I _______________ END IF NEXT I NEXT J ______________ FOR I=1 TO 4 WRITE #1,B(I) NEXT I CLOSE #1 END 最小数所在行放入一维数组 B(J)=I 定义方阵 DIM A(5,4) OPEN “MINROW.DAT” FOR OUTPUT AS #1 打开顺序文件 (INT(RND*100)+1)*T 产生随机数
2、下列程序的功能是寻找并输出11?~999之间的数M,它满足M,M2和M3均为回文数。所谓回文数是指其各位数字左右对称的整数。例如:121,676,94249等。满足上述条件的数如M=11,M2=121,M3=1331皆为回文数。确定回文数的重要一步是确定一个整数的逆序数,即将该整数的各个数字从左至右逆序排列。2、下列程序的功能是寻找并输出11?~999之间的数M,它满足M,M2和M3均为回文数。所谓回文数是指其各位数字左右对称的整数。例如:121,676,94249等。满足上述条件的数如M=11,M2=121,M3=1331皆为回文数。确定回文数的重要一步是确定一个整数的逆序数,即将该整数的各个数字从左至右逆序排列。 DECLARE FUNCTION SYM( N AS LONG) FOR M=11 TO 999 IF THEN PRINT M;M*M;M*M*M END IF NEXT M END FUNCTION SYM( N AS LONG) M=0 . DO UNTIL I=0 M= . I=I\10 LOOP IF M=N THEN SYM=1 ELSE SYM=0 . 函数调用 SYM(M)=1 AND SYM(M*M)=1 AND SYSM(M*M*M)=1 确定循环变量I的初值 I=N M*10+I MOD 10 求倒序数 自定义函数的结束标记 END FUNCTION
3、下面程序用于学生成绩处理。先从学生成绩文件flscore.dat中读取所有学生的语文、数学、英语、专业综合的成绩并存放到数组score中,求出每位考生成绩总分total,并按考生总分降序输出学生准考证号student以及成绩总分total到顺序文件fldkdz.dat中。请在相应横线上填空完成程序。3、下面程序用于学生成绩处理。先从学生成绩文件flscore.dat中读取所有学生的语文、数学、英语、专业综合的成绩并存放到数组score中,求出每位考生成绩总分total,并按考生总分降序输出学生准考证号student以及成绩总分total到顺序文件fldkdz.dat中。请在相应横线上填空完成程序。 OPTION BASE 1 DECLARE SUB getorder( ) DIM SHARED n AS INTEGER DIM SHARED student(1000) AS STRING DIM SHARED score(1000,4),total(1000) n=1000 OPEN ________________________ FOR i=1 TO n INPUT #1,student(i) FOR j=1 TO 4 INPUT #1,score(i,j) NEXT j NEXT i CLOSE #1 CALL getorder OPEN “fldkdz.dat” FOR OUTPUT AS #2 FOR i=1 TO n WRITE ______________________ NEXT i CLOSE #2 END SUB getorder FOR i=1 TO n total(i)=0 FOR j=1 TO 4 total(i)= __________________ NEXT j NEXT i FOR j=1 TO n-1 p=j FOR i=j+1 TO n ________________________ NEXT i IF p<>j THEN SWAP total(j),total(p):SWAP student(j),student(p) END IF NEXT j END SUB #2,student(i),total(i) total(i)+score(i,j) “flscore.dat” FOR input AS #1 if total(p)<total(i) then p=i
FOR I=1 TO X-1 FOR J=I+1 TO X d(i)<d(j) 是 否 d(i),d(j)交换 4、现有若干个字符串(不超过100个)存放在顺序文件ABC.DAT中,每个字符串为一个记录,下列程序用于读出该文件中的所有字符串,并将每个字符串中的数字字符依次取出构成一个相应的整数,然后将所有整数按从大到小排序,最后输出这些整数中互不相同的整数。函数CZ$(A$) 是将一个字符串中的数字字符依次取出构成一个数字字符串;子程序SORT1(D(),X)是用选择法对D数组降序排序。要求画出子程序SORT1的流程图。 DECLARE FUNCTION CZ$(A$) DECLARE SUB SORT(D() AS INTEGER,X) DIM N AS INTEGER OPEN ________________________ DIM A(100) AS INTEGER N=0 DO UNTIL _______________________ N=N+1 INPUT #1 , B$ A(N)= ______________________ LOOP . FOR I=1 TO N IF A(I)<>A(I-1) THEN PRINT A(I); NEXT I CLOSE : END FUNCTION CZ$(A$) END FUNCTION SUB SORT1(D( ) AS INTEGER,X) END SUB 子程序流程图 FOR I=1 TO LEN(A$) C$=MID$(A$,I,1) IF C$>=“0” AND C$<=“9” THEN B$=B$+C$ NEXT CZ$=B$ FOR i=1 TO X-1 FOR j=1 TO X-i if d(j)<d(j+1) then swap d(j),d(j+1) Next j,i “ABC.DAT” FOR INPUT AS #1 EOF(1) OR N=100 VAL(CZ$(B$)) CALL SORT1(A(),N)
5、猜价格:中央电视台的“幸运52”栏目深受观众喜爱,其中“猜商品价格”的节目更是脍炙人口,请你编一个程序模拟这一游戏:由计算机随机产生200至5000之间的一个整数,作为某件商品的价格,然后由你去猜这件商品的价格是多少,若你猜的数大了,则输出提示“高了”,若你猜的数小了,则输出提示“低了”,然后根据提示继续猜,直到计算机提示“对了”为止,并统计你猜的总次数。画N-S流程图。5、猜价格:中央电视台的“幸运52”栏目深受观众喜爱,其中“猜商品价格”的节目更是脍炙人口,请你编一个程序模拟这一游戏:由计算机随机产生200至5000之间的一个整数,作为某件商品的价格,然后由你去猜这件商品的价格是多少,若你猜的数大了,则输出提示“高了”,若你猜的数小了,则输出提示“低了”,然后根据提示继续猜,直到计算机提示“对了”为止,并统计你猜的总次数。画N-S流程图。 x = INT(RND * (5000 - 200 + 1) + 200) INPUT jg _____________________ DO WHILE ___________ IF jg > x THEN PRINT "高了" DO INPUT a LOOP UNTIL a < jg jg = a s = s + 1 ELSE END IF LOOP PRINT "对了" PRINT "你猜的总次数为:"; s END S=0 Jg<>x PRINT “低了" DO INPUT a LOOP UNTIL a > jg jg = a s = s + 1
1. DO INPUT N LOOP UNTIL N>=1 AND N=INT(N) FOR I=1 TO N S=S+____ NEXT PRINT S K=1 K=-K A=A+I T=T+K/A T*K
2、有两个DATA子句,在一个DATA子句中分别为10名学生的编号、姓名;在另外一个DATA2、有两个DATA子句,在一个DATA子句中分别为10名学生的编号、姓名;在另外一个DATA 子句中也提供了这10名学生的编号(但与前面的编号不一致)和成绩,如下所示。请编写程序按分数高低顺序(冒泡法)输出其成绩、编号及姓名,如果及格则在其后输出“S”,如果不及格则在其后输出“F”。 要求每行只能输出一名学生的情况。 DATA 1,Charles,2,Mark,3,Bill,4,Vincent,5,William,6,Joseph,7, James,8,Henry,9,Gary,10, Martin DATA 5,100,4,80,2,86,3,90,1,85,6,77,7, 63,8,36,10,80,9, 55 Dim bh(10),xm$(10),cj(10) FOR I=1 TO 10 read bh(i),xm$(i) NEXT FOR I=1 TO 10 read bh1,cj1 for j=1 to 10 if bh(j)=bh1 then cj(j)=cj1:exit for NEXT j,i For i=1 to 9 For j=1 to 10-i if cj(j)<cj(j+1) then swap cj(j),cj(j+1)…… endif Next j,i FOR I=1 TO 10 print bh(i),xm$(i),cj(i), if cj(i>>=60 then ?“S” else ?”F” NEXT
3、编程计算如下式子S=1*1+(1*2-2*2)+(1*3-2*3+3*3)+……+(1*9-2*9+3*9-4*9+5*9-6*9+7*9-8*9+9*9)并写出相应的流程图或N-S图。3、编程计算如下式子S=1*1+(1*2-2*2)+(1*3-2*3+3*3)+……+(1*9-2*9+3*9-4*9+5*9-6*9+7*9-8*9+9*9)并写出相应的流程图或N-S图。 FOR I=1 TO 9 S=S+____ NEXT PRINT S K=1 1*i-2*i+3*i-4*i+…+i*i即(1-2+3-4+…+i)*i T=T+I*K K=-K K=1:T=0:S=0 FOR I=1 TO 9 T=T+I*K K=-K S=S+T*I T*I 打印S
4.编程让计算机产生20个[20,90]之间的随机整数,将它们按从大到小的顺序排列后转换成二进制数。4.编程让计算机产生20个[20,90]之间的随机整数,将它们按从大到小的顺序排列后转换成二进制数。 Dim a(20) FOR I=1 TO 20 A(i)=int(rnd*71)+20 NEXT 定义数组 产生随机数 FOR I=1 TO 19 FOR J=I+1 TO 20 IF A(I)<A(J) THEN SWAP A(I),A(J) NEXT J,I 排序 FOR I=1 TO 20 X=A(I):B$=“” WHILE X>0 B$=LTRIM$(STR$(X MOD 2))+B$ X=X\2 WEND PRINT B$; NEXT 十进制转二进制字符串
5、某班有30个学生,已将他们的信息(包括:学号 姓名 年龄 性别 入学分数)存入在顺序文件sx.dat中,现要求将学生的信息按成绩从高到底排序,再放回原来的文件中去。 OPEN “sx.dat” For input as #1 Dim xh$(20),xm$(30),xb$(30),cj(30) While not eof(1) i=i+1 input #1, xh$(i),xm$(i),xb$(i),cj(i) Wend 打开文件 定义数组 读文件 For i=1 to 29 For j=1 to 30-i if cj(j)<cj(j+1) then swap xh$(j),xh$(j+1)…… endif Next j,i 排序 close OPEN “sx.dat” For output as #2 For i=1 to 30 write #2, xh$(i),xm$(i),xb$(i),cj(i) Next Close 打开文件 写文件