《最新VB程序设计.doc》由会员分享,可在线阅读,更多相关《最新VB程序设计.doc(22页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-dateVB程序设计VB程序设计1.编写程序,计算学生考试成绩,界面如图所示。要求:(1)程序启动时将窗体标题设置为“成绩计算”。(2)单击“清空”按钮(Command1)将文本框Text1、Text2和Text3清空并将焦点设置为Text1。(3)点击“计算”按钮(Command2)计算学生总成绩并显示在Text3中。其中总成绩=理论成绩*0.7+实验成绩*0.3。(4)点
2、击“退出”按钮(Command3)结束程序运行。参考代码:Private Sub Form_Load()Form1.Caption = 成绩计算End SubPrivate Sub Command1_Click()Text1.Text = Text2.Text = Text3.Text = Text1.SetFocusEnd SubPrivate Sub Command2_Click()Text3.Text = Text1.Text * 0.7 + Text2.Text * 0.3End SubPrivate Sub Command3_Click()EndEnd Sub2.设计一个用户名和密码
3、的检验程序。要求:(1)窗体(Form1)的标题设置为“登录窗口”;密码以 “*”显示。(2)单击“确定”按钮(cd1)验证输入的用户名(Text1)和口令(Text2),如果输入正确在Label3中显示“欢迎使用本系统”;如果不正确在Label3中显示“用户名或密码错误”。设正确的用户名为“admin”,口令为“123456”。(3)单击“退出”按钮(cd2),结束程序。属性窗口:将“确定按钮”的名称设置为cd1,“退出按钮”的名称设置为cd2。参考代码:Private Sub Form_Load()Form1.Caption = 登录窗口Text2.PasswordChar = *End
4、SubPrivate Sub cd1_Click()If Text1.Text = admin And Text2.Text = 123456 ThenLabel3.Caption = 欢迎使用本系统ElseLabel3.Caption = 用户名或密码错误End IfEnd SubPrivate Sub cd2_Click()EndEnd Sub1.88页编程题第一题(1)在窗体中放置一个文本框(Text1)、一个标签(Label1)和一个命令按钮(Command1)。在Command1_Click事件中编写程序,对文本框中输入的成绩进行等级判断,标准是:90分及以上为“优”,80分及以上为
5、“良”,60分及以上为“及格”,其余为“不及格”,并在标签中显示相应等级。要求:分别用IfthenelseIf语句和SelectCase语句编写程序。参考代码:-Private Sub Command1_Click()Dim score As Single,bz As Stringscore = Val(Text1.Text)Select Case scoreCase Is = 90bz = 优Case Is = 80bz = 良Case Is = 60bz = 及格Case Elsebz = 不及格End SelectLabel1.Caption = bzEnd SubPrivate Sub
6、 Command1_Click()Dim score As Single, bz As Stringscore = Val(Text1.Text)If score = 90 Thenbz = 优ElseIf score = 80 Thenbz = 良ElseIf score = 60 Thenbz = 及格Elsebz = 不及格End IfLabel1.Caption = bzEnd Sub2.88页编程题第二题(2)设计一个窗体,计算一元二次方程ax2+bx+c=0的根。参考代码:Private Sub Command1_Click()Dim pbs As Single, a As Sin
7、gle, b As Single, c As SingleDim x1 As Single, x2 As SingleDim flag As Booleanflag = True 标志判断输入的是否为数字,使用多分支结构(考试时该部分可以不写)If Not IsNumeric(Text1.Text) ThenMsgBox a应为数字, 0 + 16 + 0flag = FalseText1.SetFocusElseIf Not IsNumeric(Text2.Text) ThenMsgBox b应为数字, 0 + 16 + 0flag = FalseText2.SetFocusElseIf N
8、ot IsNumeric(Text3.Text) ThenMsgBox c应为数字, 0 + 16 + 0flag = FalseText3.SetFocusEnd IfIf flag = True Thena = Val(Text1.Text)b = Val(Text2.Text)c = Val(Text3.Text)If a = 0 And b = 0 ThenLabel4.Caption = 无意义ElseIf a = 0 And b 0 ThenLabel4.Caption = 方程的根为:x1=x2= + Str(-c / b)Elsepbs = b * b - 4 * a * cI
9、f pbs 0 Thenx1 = (-b + Sqr(pbs) / (2 * a)x2 = (-b - Sqr(pbs) / (2 * a)Label4.Caption = 方程有两不相等的实根,分别为:x1= + Str(x1) + ;x2= + Str(x2)ElseIf pbs = 0 ThenLabel4.Caption = 方程的根为:x1=x2= + Str(-b / 2 * a)ElseLabel4.Caption = 方程无实根End IfEnd IfEnd IfEnd Sub.88页编程题第三题(3)设S=123n,求S不大于4000时最大的n。参考代码:Private Su
10、b Form_Click()Dim n As Integer, s As Integern = 0 这里n=1也对s = 1Do While s 4000n = n + 1s = s * nLoopPrintPrint s不大于4000时最大的n是:; n - 1 注意这里End Sub或写成Private Sub Form_Click()Dim n As Integer, s As Integern = 1 这里n=1对,n=2对,n=0不对s = 1Do While s = 4000s = s * nn = n + 1 注意这两句与上面写法的前后顺序LoopPrintPrint s不大于4
11、000时最大的n是:; n - 2 注意这里End Sub2.88页编程题第四题(4)如果一个数的因子之和等于这个数本身,则称这样的数为“完全数”。例如,整数28的因子为1、2、4、7、14,其和1+2+4+7+14=28,因此28是一个完全数。编写一个程序,从键盘上输入正整数M和N,求出M和N之间的所有完全数。参考代码:方法一Private Sub Command1_Click()m = Val(Text1.Text)n = Val(Text2.Text)For i = m To nFor j = 1 To i - 1If i Mod j = 0 Thens = s + jEnd IfNex
12、t jIf i = s ThenPicture1.Print i;End Ifs = 0Next iEnd Sub参考代码:方法二Private Sub Command2_Click()Dim m As Integer, n As IntegerDim count As Integer, i As Integerm = Val(Text1.Text)n = Val(Text2.Text)For i = m To ns = 0For j = 1 To i 2If i Mod j = 0 Then s = s + jNext jIf s = i ThenPicture1.Print i;count
13、 = count + 1If count Mod 8 = 0 ThenPicture1.PrintEnd IfEnd IfNext iEnd Sub参考代码:方法三详见实验教材实训6-53.编写程序,打印九九乘法表的上三角Private Sub Form_Click()FontSize = 12Print Tab(35); 九九乘法表Print Tab(33); -For i = 1 To 9For j = i To 9Print Tab(j - 1) * 9 + 1); i & * & j & = & i * j;Next jPrintNext iEnd Sub1.113页编程题第一题(1)
14、编写程序,建立一个数组,并通过Rnd函数(该函数返回一个0到1之间的数)为每个数组元素赋一个1到100之间的整数。然后显示所有小于60的元素。参考代码:Private Sub Form_Click()Dim a(1 To 20) As Integer, i As IntegerDim j As Integerj = 0 用来表示小于60的个数建立两个picture,用于显示随机产生的数组和小于60的所以元素Picture1.Print 产生的数组为:Picture1.PrintPicture2.Print 其中小于60的元素有:Picture2.PrintFor i = 1 To 20a(i)
15、 = Int(Rnd * 100 + 1)Picture1.Print a(i);If i Mod 10 = 0 Then Picture1.Print 10个数换行If a(i) max Then max = a(i)If a(i) Max ThenMax = c(i, j)t1 = it2 = jEnd IfNext jNext iPicture6.FontSize = 20Picture6.Print max=; MaxPicture6.Print C(; t1; ,; t2; )End Sub以下三角形式显示A,上三角形式显示B。Private Sub Command4_Click()
16、Picture1.ClsPicture2.ClsPicture1.FontSize = 10Picture2.FontSize = 10For i = 1 To 4For j = 1 To iPicture1.Print a(i, j); ;Next jPicture1.PrintPicture1.PrintNext iFor i = 1 To 4For j = i To 4Picture2.Print Tab(j * 5); b(i, j);Next jPicture2.PrintPicture2.PrintNext iEnd Sub将矩阵B第一行与第三行对应元素交换位置并输出。Privat
17、e Sub Command5_Click()Picture2.ClsFor i = 1 To 4For j = 1 To 4Picture2.Print b(i, j); ;Next jPicture2.PrintPicture2.PrintNext iPicture5.Print B第一行与第三行交换位置:Picture5.PrintFor i = 1 To 4t = b(1, i)b(1, i) = b(3, i)b(3, i) = tNext iFor i = 1 To 4For j = 1 To 4Picture5.Print b(i, j); ;Next jPicture5.Prin
18、tPicture5.PrintNext iEnd Sub4.113页编程题第三题(3)编写程序,随机产生20个不同的数放在数组A中,并按由大到小的顺序排序。从键盘上输入一数X,判断此数是否在该数组A中,若在则输出其所在的位置及X值,否则输出“未找到”。参考代码:Dim a(1 To 20) As IntegerPrivate Sub Form_Load()RandomizeEnd SubPrivate Sub bijiao_Click()Dim i As IntegerFor i = 1 To 20a(i) = Int(Rnd * 100) 题目没要求,随便的一个范围Next iFor i =
19、 1 To 19 比较排序For j = i + 1 To 20If a(j) a(i) Thent = a(i)a(i) = a(j)a(j) = tEnd IfNext jNext iFor i = 1 To 20 打印数组Print a(i);Next iEnd SubPrivate Sub maopao_Click()Dim i As IntegerFor i = 1 To 20 生成数组a(i) = Int(Rnd * 100) 题目没要求,随便的一个范围Next iFor i = 1 To 19 冒泡排序For j = 20 To i + 1 Step -1If a(j) a(j
20、- 1) Thent = a(j)a(j) = a(j - 1)a(j - 1) = tEnd IfNext jNext iFor i = 1 To 20 打印数组Print a(i);Next iEnd SubPrivate Sub xuanze_Click()Dim i As Integer For i = 1 To 20 生成数组 a(i) = Int(Rnd * 100) 题目没要求,随便的一个范围 Next i For i = 1 To 19 选择排序 k = i For j = i + 1 To 20 If a(j) a(k) Then k = j Next j t = a(i)
21、a(i) = a(k) a(k) = t Next i For i = 1 To 20 打印数组 Print a(i); Next iEnd SubPrivate Sub Command1_Click() Dim x As Integer, flag As Integer, i As Integer flag = 0 标志,表示数组中是否存在x x = InputBox(请输入x的值,为整数, 输入, 1) For i = 1 To 20 If x = a(i) Then flag = 1 flag改变,说明存在 Exit For End If Next i If flag = 1 Then
22、Print Print 数组中存在 & x Print 位置: & i Else Print Print 未找到 & x End IfEnd Sub5.选做题-113页编程题第四题(4)编写程序,录入15名学生的成绩,求出其平均分,统计高于平均分、低于60分、大于等于90分的人数,并输出结果。参考代码:Private Sub Form_Click() Dim a(1 To 15) As Single Dim i As Integer, c1 As Integer, c2 As Integer, c3 As Integer, avg As Single For i = 1 To 15 a(i) = InputBox(请输入第 & i & 个学生的成绩, 输入成绩, 60) avg = avg + a(i) Print a(i); Next i avg = avg / 15 For i = 1 To 15 If a(i) avg Then c1 = c1 + 1 If a(i) = 90 Then c3 = c3 + 1 Next i Print Print 15人的平均分为:; avg Print 高于平均分的人数为:; c1 Print 小于60的人数为:; c2 Print 大于等于90的人数为:; c3End Sub