《课后习题及补充习题(程序设计).pdf》由会员分享,可在线阅读,更多相关《课后习题及补充习题(程序设计).pdf(16页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、第九章1.编写程序,接受用户输入的20个数,输出20个数的平均值。解1Su b p r o 41()Di m a(l To 20)A s Si n g l e,i A s I n t e g e r,su m A s Si n g l esu m =0F o r i =1 To 20a(i)=V a l Gn p u t Bo x(请输入数据:)su m =su m +a(i)Ne xt iMsg Bo x 平均值为:&su m /20En d Su b解2Su b p r o 411()Di m a(l To 20)A s Si n g l e,i A s I n t e g e r,su
2、m A s Si n g l e,n A s I n t e g e rn =0su m 二 0F o r i =1 To 20a(i)=V a l(I n p u t Bo x(请输第&i&个 数 )su m =su m +a(i)n =n +1Ne xt iMsg Bo x 平均值为:&su m /nEn d Su b2.编写程序进行计算1!+3!+5!+7!。解1Su b p r o 421()Di m i A s I n t e g e r,t A s I n t e g e r,su m A s I n t e g e rt =1:su m =0F o r i =1 To 7t =t
3、 *iI f i Mo d 2 0 0 Th e nsu m =su m +tEn d I fNe xt iMsg Bo x l!+3!+5!+7!=&su mEn d Su b解2Su b p r o 422()Di m i A s I n t e g e r,t A s I n t e g e r,su m A s I n t e g e rt =1:su m =0F o r i =1 To 7t =1F o r j =1 To it =t *jNe xt jI f i Mo d 2 0 0 Th e nsu m =su m +tEn d I fNe xt iMsg Bo x l!+3!+
4、5!+7!=&su mEn d Su b解3Su b p r o 423()Di m i A s I n t e g e r,t A s I n t e g e r,su m A s I n t e g e rt =1:su m =0F o r i =1 To 7 st e p 2t =1F o r j =1 To it =t *jNe xt jSu m=su m+tNe xt iMsg Bo x l!+3!+5!+7!=&su mEn d Su b3.输入两个整数,求两个整数之间能被3整除的数之和。Su b p r o 43()Di m x A s I n t e g e r,y A s I
5、 n t e g e r,t A s I n t e g e r,su m A s I n t e g e r,i A s I n t e g e rx=V a l(I n p u t Bo x(请输入一个整数)y=V a l(I n p u t Bo x(请输入一个整数)I f x y Th e nt =xx=yy=tEn d I fF o r i =x To yI f i Mo d 3=0 Th e nsu m =su m +iEn d I fNe xt iMsg Bo x x&和&y&之间可以被3整除的数之和为:&su mEn d Su b4.编写程序将教学管理数据库中学生成绩表中的百分
6、制成绩转成用5分制表示,每20分 为个等级。,解1Su b p r o 441()Di m c n A s Ne w A DODB.Co n n e c t i o nDi m r s A s Ne w A DODB.Re c o r d se tDi m st r sq l A s St r i n gDi m sc o r e A s A DODB.F i e l dSe t c n =Cu r r e n t Pr o j e c t.Co n n e c t i o nst r sq l =zzse l e c t *f r o m 学生成绩r s.Op e n st r sq l,c
7、 n,a d Op e n Dyn a m i c,a d Lo c k Op t i m i st i c,a d Cm d Te xtSe t sc o r e =r s.F i e l d s(成绩”)r s.Mo ve F i r stDo W h i l e No t r s.EOFSe l e c t Ca se sc o r eCa se 80 To 100sc o r e =5Ca se 60 To 80sc o r e =4Ca se 40 To 60sc o r e =3Ca se 20 To 40sc o r e =2Ca se El sesc o r e =1En d
8、Se l e c tr s.Mo ve Ne xtLo o pr s.Cl o sec n.Cl o seEn d Su b,解2Su b p r o 442()Di m c n A s Ne w A DODB.Co n n e c t i o nDi m r s A s Ne w A DODB.Re c o r d se tDi m st r sq l A s St r i n gDi m sc o r e A s A DODB.F i e l dSe t c n =Cu r r e n t Pr o j e c t.Co n n e c t i o nst r sq l =se l e c
9、 t *f r o m 学生成绩r s.Op e n st r sq l,c n,a d Op e n Dyn a m i c,a d Lo c k Op t i m i st i c,a d Cm d Te xtSe t sc o r e =r s.F i e l d s(成绩)r s.Mo ve F i r stDo W h i l e No t r s.EOFI f I n t (sc o r e 20)=4 Th e nsc o r e -5El se l f I n t(sc o r e 20)=3 Th e nsc o r e =4El se l f I n t(sc o r e
10、20)=2 Th e nsc o r e =3El se l f I n t(sc o r e 20)=1 Th e nsc o r e =2El sesc o r e =1En d I fr s.Mo ve Ne xtLo o pr s.Cl o sec n.Cl o seEn d Su b第10章1.设计一个实现两个整数相加并将结果返回的函数。F u n c t i o n a d d 41(a A s I n t e g e r,b A s I n t e g e r)A s I n t e g e ra d d 41=a +bEn d F u n c t i o n主调过程示例:Su
11、b m a i n 41()Di m x A s I n t e g e r,y A s I n t e g e rx=V a K l n p u t Bo xCx)y=V a i (I n p u t Bo x(z,y=,z)Msg Bo x a d d 41(x,y)En d Su b2.假如利用第1题中的函数实现两个浮点型数值相加,那么应该如何修改函数?单精度浮点型F u n c t i o n a d d 421(a A s Si n g l e,b A s Si n g l e)A s Si n g l eDi m m A s Si n g l e,n A s Si n g l em
12、 =CSn g (a)n =CSn g(b)a d d 421=a +bEn d F u n c t i o n主调过程示例:Su b m a i n 421()Di m x A s Si n g l e,y A s Si n g l ex=V a i(I n p u t Bo x(x=)y=V a i (I n p u t Bo x(z,y=,z)Msg Bo x a d d 421(x,y)En d Su b双精度浮点型F u n c t i o n a d d 422(a A s Do u b l e,b A s Do u b l e)A s Do u b l eDi m m A s D
13、o u b l e,n A s Do u b l em =CDb l(a)n =CDb l(b)a d d 422=a +bEn d F u n c t i o n主调过程示例:Su b m a i n 422()Di m x A s Do u b l e,y A s Do u b l ex=V a i(I n p u t Bo x(x=)y=V a i (I n p u t Bo x(z,y=,z)Msg Bo x a d d 422(x,y)En d Su b3.编写一个函数,实现从给定的表中删除指定的数据。F u n c t i o n d e l d a t a(st r t a b
14、l e A s St r i n g,st r f i e l d A s St r i n g,d a t a)Di m c n A s Ne w A DODB.Co n n e c t i o nDi m r st A s Ne w A DODB.Re c o r d se tDi m st r sq l A s St r i n gSe t c n 二 Cu r r e n t Pr o j c c t.Co n n e c t i o nst r sq l =se l e c t *f r o m&st r t a b l er st.Op e n st r sq l,c n,a d
15、Op e n Dyn a m i c,a d Lo c k Op t i m i st i c,a d Cm d Te xtr st.Mo ve F i r stDo W h i l e No t r st.EOFI f r st.F i e l d s(st r f i e l d)=d a t a Th e nr st.De l e t eEn d I fr st.Mo ve Ne xtLo o pr st.Cl o sec n.Cl o seEn d F u n c t i o n主调过程示例:Pr i va t e Su b m a i n 43()Di m x A s St r i
16、n g,y A s St r i n g,z A s Si n g l ex=I n p u t Bo x(x=)y=I n p u t Bo x(,zy=z/)z=V a i(I n p u t Bo x(z=)d e l d a t a x,y,zEn d Su b4.编 写 个过程swa p,要求该过程能够接收2个字符,并将这2个字符进行交换。Su b Swa p(x A s St r i n g,y A s St r i n g)Di m z A s St r i n gz=xx=yy=ZDe b u g.Pr i n t x=&xDe b u g.Pr i n t y=&yEn d
17、Su b主调过程示例:Pr i va t e Su b m a i n 44()Di m n A s St r i n gDi m m A s St r i n gn =l n p u t Bo x(n=)m =I n p u t Bo x(m=)Swa p n,mEn d Su b第11章三、操作题1.设计一个窗体,在窗体上放置一个文本框(t xt i n p u t)和 按 钮(b t n OK),编写按钮的单击事件(b t n OK _ Cl i c k),实现这样的功能:在文本框中输入内容,单击按钮,可以将文本框中的内容显示在窗体的标题上。窗体名称:窗体标题示例命令按钮的单击事件代码如
18、下:Pr i va t e Su b b t n OK _ Cl i c k()Me.Ca p t i o n =t xt i n p u t 或者:f o r m s!窗体标题示例.c a p t i o n=t xt i n p u tEn d Su b 又或者:f o r m _ 窗体标题示例.c a p t i o n=t xt i n p u t2.创建一个窗体,并在窗体上放上命令按钮CMD,要求,在单击CMD按钮时,图书表中的单价降低10船数量增加10,然后将总金额字段填充完整。原始“图书”表如下:叵图 书:表书号书名 1 作者出版社出版日期数量单价ISBN 7-302-0236I
19、SBN 7-302-0380数据结构(C语言严前敏,吴伟民C程序设计觉二片谭浩强清华大学出版社清华大学出版社2004-11-012002-07-011419 19.80 29.70ISBN 7-81110-09 Access数据库程F 陈桂林等安徽大学出版社2006-01-0119 40.50米00.00I 记录:,11 -旧 1*1共有记录数:3设计窗体如下:-命令按钮的单击事件代码如下:Pr i va t e Su b CMD Cl i c k ODi m c n A s Ne w A DODB.Co n n e c t i o nDi m r st A s Ne w A DODB.Re
20、c o r d se tDi m st r sq l A s St r i n gSe t c n =Cu r r e n t Pr o j e c t.Co n n e c t i o nst r sq l =se l e c t *f r o m 图书r st.Op e n st r sq l,c n,a d Op e n Dyn a m i c,a d Lo c k Op t i m i st i c,a d Cm d Te xtr st.Mo ve F i r stDo W h i l e No t r st.EOFr st.F i e l d s(单价)=r st.F i e l d
21、 s(单价)*0.9r st.F i e l d s(数 量 )=r st.F i e l d s(数量)+10r st.F i e l d s(总金额)=r st.F i e l d s(单价)*r st.F i e l d s(数量)r st.Mo ve Ne xtLo o pr st.Cl o sec n.Cl o seEn d Su b单击命令按钮后,“图书”表如下:图 书:表书号书名作者出版社出版日期数量单价 1fcSBN 7-302-023耳数据结构(C语言片严前敏,吴伟民清华大学出版社2004-11-0124 17.82ISBN 7-302-0380 C程序设计能二K谭浩强清华大
22、学出版社2002-07-0129 26.73ISBN 7-81110-09 Access数据库程f 陈桂林等安徽大学出版社2006-01-0129 36.45米00.00记录:,1 11 1 1共有记录数:33.设计个电话计费程序,窗体如下图所示,假定每分钟通过费用为0.1元,不足1分钟按1分钟计。标题为“开始”的命令按钮的单击事件代码如下:Pri v at e S u b Co mman d 8 _Cl i ck()T ex t O=T ex t 2 二 T ex t 4=T ex t 6=T ex t O=T i meOEn d S u b标题为“计费”的命令按钮的单击事件代码如下:Pri
23、 v at e S u b Co mman d 9_Cl i ck()Di m x As Dat eDi m y As S i n g l eT ex t 2=T i meOx =T ex t 2-T ex t OT ex t 4=xIf S eco n d(x)0 T h eny =Ho u r(x)*60 *0.1 +(Mi n u t e(x)+1)*0.1T ex t 6=y&元 El sey =Ho u r(x)*60 *0.1 +Mi n u t e(x)*0.1T ex t 6=y&元”En d IfEn d S u b补充习题部分:1.一元二次方程求解Pri v at e S
24、u b sh l ()Di m a As S i n g l e,b As S i n g l e,c As S i n g l eDi m d As S i n g l e,x 2 As S i n g l e,x l As S i n g l ea=V ai (In pu t Bo x (z/a=,/)b=V ai (In pu t Bo x (z/b=,z)c=V ai (In pu t Bo x (,zc=,/)d =b 2-4*a*cIf a 0 An d d =0 T h enx l =(b+S qr(d)/(2*a)x 2=(-b-S qr(d)/(2*a)Msg Bo x 方程
25、的根为:x l=&x l&,x 2=&x 2El seMsg Bo x 方程无实根En d IfEn d S u b2.分段函数:(x=0,y=0;x 0,y=l)i f语句:Pri v at e S u b sh 21 ()Di m x As S i n g l e,y As In t eg erx =V ai (In pu t Bo x Cx=O)If x =0 T h eny =0El sel f x 0y =1Case 0y =0Case El sey 二 TEn d S el ectMsg Bo x y=&yEn d S u b3.求s=l*2+2*3+3*4+.21*22Pri v
26、 at e S u b sh 3()Di m s As In t eg erDi m a As In t eg er,b As In t eg er,c As In t eg ers=0a=0b=1Do W h i l e a 21 An d b b T h enc=aa=bb 二 cEn d Ifi =aDo W h i l e i =a An d i c An d a+c b An d b+c a T h end =(a+b+c)/2s=S qr(d *(d -a)*(d -b)*(d -c)T ex t 4=sEl seT ex t 4=不构成三角形En d IfEn d S u b6.
27、在两个文本框中输入两个数,单击“最小公倍数”按钮求出两个数的最小公倍数并显示在第一个文本框中。窗体如下:命令按钮单击事件如下:Pri v at e S u b Co mman d 4 Cl i ck()Di m x As In t eg er,y As In t eg er,r As In t eg er,t As In t eg erx =T x t 1y =t x t 2If x y T h ent =xx =yy 二 tEn d Ifr=x Mo d yDo W h i l e r 0 x =yy =rr=x Mo d yLo o pT x t l =T x t l *t x t 2/y
28、En d S u b7.在第一个文本框中输入一个三位数,单击“判断”按钮,判断其是否是水仙花数。若是水仙花数则在第二个文本框中显示“是水仙花数”,若不是水仙花数在第二个文本框中显示“不是水仙花数”。水仙花数特点:原数二百位数的立方+十位数的立方十 个位数的立方。例如:1 53=13+53+33窗体如下:命令按钮单击事件如下:Pri v at e S u b Co mman d 4_Cl i ck()Di m s As S t ri n g,a As S t ri n g,b As S t ri n g,c As S t ri n gDi m x As In t eg er,y As In t
29、eg er,z As In t eg ers=T ex t la=Mi d (s,1,1)b=Mi d(s,2,1)c=Mi d(s,3,1)x =V ai(a)y =V ai(b)z =V ai (c)If V ai (s)=x*x*x+y*y*y+z*z*z T h enT ex t 2=是水仙花数El seT ex t 2二不是水仙花数En d IfEn d S u b8.编写程序实现英文字符串的大小写转换解1Pri v at e S u b ch an g el()Di m st rl As S t ri n g,st r2 As S t ri n g,s As S t ri n gD
30、i m i As In t eg er,n As In t eg er,x As In t eg erst rl 二In pu t Bo x(请输入一个英文字符串,大小写转换)n =Len(st rl)st r2=Fo r i =1 T o ns=Mi d(st rl,i,1)x =Asc(s)If x 97 T h enx =x +32El sex =x -32En d Ifst r2=st r2+Ch r(x)Nex t iDebu g.Pri n t st r2En d S u b解2Pri v at e S u b ch an g e2()Di m st rl As S t ri n
31、g,st r2 As S t ri n g,s As S t ri n gDi m i As In t eg er,n As In t eg er,x As In t eg erst rl =In pu t Bo x(请输入一个英文字符串,大小写转换)n =Len(st rl)st r2=Fo r i =1 T o ns=Mi d(st rl,i,1)x =Asc(s)If x a(j)T h ent =a(i)a(i)=a(j)a(j)=tEn d IfNex t jNex t iFo r i =1 T o 1 0Debu g.Pri n t a(i)Nex t iEn d S u b1 0
32、 .输入字符串,颠倒顺序显示Pri v at e S u b d d cx ()Di m st rl,st r2,s As S t ri n gDi m i,n As In t eg erst rl 二In pu t Bo x(请输入一个字符串,颠倒次序”)st r2=n =Len(st rl)Fo r i =1 T o ns=Mi d (st rl,n -i +1,1)st r2=st r2+sNex t iDebu g.Pri n t st r2En d S u b第十章补充例题 补充例题1 若窗体中已有一个名为Commandl命令按钮,编写下面的代码,窗体运行时,单击命令按钮,将显示什么
33、结果?Private Sub commandl_click()s=,ABBACDDCBAMFor i=6 To 2 Step-2x=Mid(s,i9 i)y=Left(s,i)z=Right(s9 i)z=x&y&zNextiMsgBox zEnd Sub 补充例题 2 求 sum=l+(l+3)+(l+3+5)+.+(l+3+5+7+.+39)?Private Sub commandl_click()m=l:sum=0:t=0Do while m4 then i=iA2Msgbox iEnd Sub 补充例题7写出运行结果Sub sfun(x As Single,y As Single)t=
34、xx=t/yy=t Mod yEnd SubPrivate Sub command4_click()Dim a As SingleDim b As Singlea=5:b=4sfun a,bMsgBox a&Chr(13)&bEnd Sub一、打开窗体或报表1)命令格式DoCmd.OpenForm formname-,view,filternam e,wherecondition,datamode,windowmodeDoCmd.OpenReport reportname.,view,filternam e,wherecondition注意:当后面若干参数全部省略时,各“,”可以省略;但当中间的若干参数被省略,最后还有其他参数时,中间的“,”必须写全例如:DoCmd.OpenForm“学生管理”,acFormEditDoCmd.OpenReport 学生信息表”二、关闭窗体或报表DoCmd.Close objecttype,objectname,saveObjecttype acForm,acReport,acTable,acDataAccessPage,acQuery,acMacro例:DoCmd.C lo se关闭当前窗体DoCmd.Close acFor叫“学生管理”DoCmd.Close acReport,“学生信息表”