VB6.0课后题及常见操作题代码

更新时间:2023-10-01 19:11:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

第四章 1、从键盘上输入4个数,编写程序,计算并输出这4个数的和及平均值。通过inputbox函数输入数据,在窗体上显示和及平均值。

Dim a as single For i=1 to 4 a=inputbox(“请输入一个数:”,“数据输入”) sum=sum+a n=n+1 avg=sum/n next i

print “和是:”,sum print “平均值是:”,avg

2、编写程序,要求用户输入下列信息:姓名、年龄、通信地址、邮政编码、电话,然后将输入的数据用适当的格式在窗体上显示出来。

Dim a as variant Fontsize=20 a=inputbox(“请输入姓名:”,“数据输入”) print “姓名:”;a a=inputbox(“请输入年龄:”,“数据输入”) print “年龄:”;a a=inputbox(“请输入通信地址:”,“数据输入”) print “通信地址:”;a a=inputbox(“请输入邮政编码:”,“数据输入”) print “邮政编码:”;a a=inputbox(“请输入电话:”,“数据输入”) print “电话:”;a

3.a=5,b=2.5,c=7.8,编程序计算: y=(πab)/(a+bc) 程序:

Dim a as single,b as single,c as single a=5 b=2.5 c=7.8 pi=3.14

y=(pi*a*b)/(a+b*c) print “结果是:”;y

4、输入以秒为单位表示的时间,编写程序,将其转换为几日几时几秒。 Dim a as long,d as integer ,h as integer,s as integer a=inputbox(“请输入秒数:”,“数据输入”) d=int(a/86400) h=int((a-d*86400)/3600) s=a-d*86400-h*3600 print d;“日”;h;“时”;s;“秒”

2

5、自由落体位移公式为:s=gt/2+v0t

其中v0为初始速度,g为重力加速度,t为经历的时间,编写程序,求位移量s。设

2

v0=4.8m/s,t=0.5s,g=9.81m/s,在程序中把g定义为符号常量,用inputbox函数输入v0和t两个变量的值。

Dim v as single,t as single,s as single g=9.81

v= inputbox(“请输入初始速度:”,“数据输入”) v=val(v) t=inputbox(“请输入经历的时间:”,“数据输入”) t=val(t) s=g*t*t/2+v*t

print “位移量是:”;s

6.编程计算并输出S的值:s=2+4+6+8+?+100 dim i,s as integer s=0

for i=2 to 100 step 2 s=s+i next print s

7、给定三角形的3条边长,计算三角形的面积。编写程序:首先判断给出的3条边能否构成三角形,如可以构成,则计算并输出三角形的面积,否则要求重新输入。当输入-1时结束程序。

Dim a as single;b as single;c as single Start:

a = inputbox(“请输入第一条边长-1结束:”,”数据输入”) a=val(a) if a=-1 then end end if

b=inputbox(“请输入第二条边长”,“数据输入”) b=val(b) c=inputbox(“请输入第三条边长”,“数据输入”) c=val(c)

if a+b<=c or a+c<=b or b+c<=a then msgbox(“您输入的三条边长不能构成三角形,请重新输入!”) goto start else

l=(a+b+c)/2

s=sqr(l*(l-a)*(l-b)*(l-c)) end if

print “三角形的面积为:”;s

8、税务部门征收所得税,规定如下: (1)收入在200元以内,免征

(2)收入在200-400元内,超过200元的部分纳税3%

(3)收入超过400元的部分,纳税4%

(4)当收入达5000元或超过时,将4%税金改为5% 编写程序实现上述操作。

Dim x as single,y as single x=inputbox(“请输入收入!”,“数据输入”) x=val(x) if x<=200 then y=0

elseif x>200 and x<400 then y=(x-200)*0.03

elseif x>=400 and x<5000 then y=(x-400)*0.04+200*0.03 elseif x>=5000 then

y=(x-400)*0.05+200*0.03 end if

print “需要纳税的金额为:”;y 9、假定有一下每周工作安排: 星期一、三:讲计算机课 星期二、四:讲程序设计课 星期五:进修英语 星期六:政治学习 星期日:休息

试编写一个程序,对上述工作日程进行检索。程序运行后,要求输入一周里的某一天,程序将输出这一天的工作安排。在输入时用0-6分别代表星期日到星期六,如果输入0-6之外的数,则程序结束运行。

Dim x as integer x=inputbox(“请输入一个整数进行检索:”,“数据输入”) select case x case 0 y=“休息” case 1

y=“讲计算机课” case 2

y=“讲程序设计课” case 3

y=“将计算机课” case 4

y=“将程序设计课” case 5 y=“进修英语” case 6 y=“政治学习” case else end

end select print y

10。vb数字金字塔1 121 12321一直到9 Private Sub Form_Click() Dim a(1 To 9) As Integer For j = 1 To 9 a(j) = j Next j

For i = 1 To 9

Print Tab(30 - i * 3); For j = 1 To 2 * i - 1 If j > i Then

Print a(2 * i - j); Else

Print a(j); End If Next j Next i End Sub

11.勾股定理中三个数的关系是a*a+b*b=c*c。编写程序,输出30以内满足上述关系的整数组合。例如3,4,5就是一个满足要求的整数组合

Private Sub Command1_Click()

Dim Max As Integer, I As Integer, J As Integer, K Max = 30

For I = 3 To Max - 2

For J = I + 1 To Max - 1 K = Sqr(I ^ 2 + J ^ 2)

If K = Int(K) And K <= Max Then Print I, J, K Next Next End Sub

12.如果一个数的因子之和等于这个数的本身,则称这样的数为“完全数”。例如,整数28的因子为1,2,4,7,14,其和1+2+4+7+14=28,因此28是一个完全数,编写一个程序,从键盘上输入整数M和N,求出M和N之间的所有的完全数

form load()

'int i,j,m,n,sum

m =inputbox(\输入最小正整数\n =inputbox (\输入最大正整数\for j=m to n sum=0

for i=1 to j

if (j mod i=0) and (i <>j ) then sum=sum i next

if j=sum then print sum & \是完全数\

next

print \查找完毕\beep end sub

13.从键盘上输入一个学生的学号和考试成绩,然后输出该学生的学号和考试成绩,并根据成绩按下面的规定输出对该学生的评语:

成绩 80~100 60~79 50~59 40~49 0~39

评语 very good good fair poor fail (所需控件:三个标签,两个文本框,一个命令按扭) Private Sub Command1_Click() Dim s$, a$, mark! s = Text1.Text

mark = Val(Text2.Text) Select Case mark Case Is < 40 a = \ Case Is < 50 a = \ Case Is < 60 a = \ Case Is < 80 a = \ Case 80 To 100 a = \ End Select

Print \学号:\成绩评定为\ End Sub

14.一个两位的正整数,如将它的个位数字与十位数字对调,则产生另一个正整数,我们把后者叫做前者的对调数。如给定一个两位的正整数,请找到另一个两位的正整数,值得这两个整数之和等于他们各自的对调数之和,例如,12+32=21+23.编写程序,输入一个两位的正整数,把具有这种特征的每一对两位正整数都找出来

Private Sub Command1_Click() Dim a As String Dim i As Long

a = InputBox(\输入一个两位的正整数:\ If Val(a) <= 0 Or Val(a) >= 100 Then MsgBox \输入不合法\ Exit Sub End If

For i = 1 To 99

If Val(a) + i = Val(getDuiTiao(a)) + getDuiTiao(CStr(i)) Then Debug.Print i End If

Dim b As Single Dim c As Single Dim x1 As Single Dim x2 As Single Dim f As Double a = Val(Text1.Text) b = Val(Text2.Text) c = Val(Text3.Text) d = b ^ 2 - 4 * a * c If a = 0 Then

MsgBox (\二次相系数不得为零\ElseIf d < 0 Then MsgBox (\无解\Else

f = Sqr(d)

x1 = (f - b) / (2 * a) x2 = (-b - f) / (2 * a) Text4.Text = x1 Text5.Text = x2 End If End Sub

6.编写任意数的立方的函数,然后求1-20的立方 Private Sub Command1_Click() Dim I As Integer For I = 1 To 20

Print I & \的3次方=\End Sub

Function xxx(I As Integer) As Double xxx = I * I * I End Function

7.用VB编写一个输入0--6数字,显示汉字对照输出星期的Function过程 建立一个form1 Option Explicit

Dim A As Byte, B As String Private Sub Form_Load()

Form1.AutoRedraw = True Do

B = InputBox(\输入数字(0-6):\输入\

If Val(B) > -1 And Val(B) < 7 Or Len(B) = 1 Then Exit Do Loop

A = Val(B) Select Case A

Case 0

B = \星期日\ Case 1

B = \星期一\ Case 2

B = \星期二\ Case 3

B = \星期三\ Case 4

B = \星期四\ Case 5

B = \星期五\ Case 6

B = \星期六\ End Select Print B End Sub

输入一个正整数编程判断奇数还是偶数

Private Sub Command1_Click() Dim a As String Dim n As Long

a = InputBox(\输入一个正整数\输入\n = CLng(a)

If n Mod 2 = 0 Then Print n; \是偶数\Else

Print n; \是奇数\End If End Sub

VB输入一个正整数编程判断奇数还是偶数

Private Sub Command1_Click() Dim a As String Dim n As Long

a = InputBox(\输入一个正整数\输入\n = CLng(a)

If n Mod 2 = 0 Then Print n; \是偶数\Else

Print n; \是奇数\End If End Sub

计算1+2.。。。+100

Private Sub Command1_Click() Sum = 0

For i = 1 To 100 Sum = Sum + i Next i

Print \2+3+??+100=\ End Sub

编写每周工作安排。

Private Sub Command4_Click() Dim x%

x = InputBox(\请输入星期(0-6)\Select Case x Case 1, 3

Print \今天是星期\讲计算机课\ Case 2, 4

Print \今天是星期\讲程序设计课\ Case 5

Print \今天是星期\进修英语\ Case 6

Print \今天是星期\政治学习\ Case 0

Print \今天是星期日\休息\ Case Else End

End Select End Sub

输入10个学生的成绩,找出最高分,并求最高分是第几位同学。 Dim b(10) As Integer

Private Sub Command1_Click() Const n = 10 Max = 0: k = 0 For i = 1 To n

b(i) = InputBox(\请输入第\个同学的成绩\求最高分) If b(i) > Max Then Max = b(i) End If Next i

Print \最高分是第\个同学,其成绩是:\End Sub

7二维数组的输出。

Dim s(3, 2) As Integer Private Sub Form_Click()

For i = 0 To 3 Print Tab(5); For j = 0 To 2

s(i, j) = i * 2 + j + 1 Print s(i, j); Next j Print Next i End Sub

比较5个数的大小。 Dim S(5) As Integer Private Sub Form_Click() Const n = 5 For i = 1 To n

S(i) = InputBox(\请输入第\个数\数据排序) Next i

For i = 1 To n - 1 Min = i

For j = i + 1 To n

If S(j) < S(Min) Then Min = j Next j t = S(i)

S(i) = S(Min) Next i

For i = 1 To 5 Print S(i) Next i End Sub

抽数排大小

Private Sub Command1_Click()

Dim Data() As String, MaxNum As Integer, Temp As Integer, StepVal As Integer, i As Integer, i2 As Integer Dim TempStr As String

Data() = Split(Text1.Text, \StepVal = UBound(Data()) - 1 For i = 0 To StepVal For t = 0 To i

If Val(Data(t)) < Val(Data(i + 1)) Then MaxNum = Data(i + 1) Data(i + 1) = Data(t) ' Data(t) = MaxNum

End If Next Next

For i2 = 0 To UBound(Data())

TempStr = TempStr & IIf(TempStr = \Next

Text2.Text = TempStr MsgBox \End Sub

Private Sub Command2_Click()

Dim Data() As String, MaxNum As Integer, Temp As Integer, StepVal As Integer, i As Integer, i2 As Integer

Data() = Split(Text1.Text, \StepVal = UBound(Data()) - 1 For i = 0 To StepVal For t = 0 To i

If Val(Data(t)) < Val(Data(i + 1)) Then MaxNum = Data(t)

ElseIf Val(Data(t)) > Val(Data(i + 1)) Then MaxNum = Data(i + 1) Data(i + 1) = Data(t) Data(t) = MaxNum

ElseIf Data(t) = Data(i + 1) Then End If Next Next

Text2.Text = \

For i2 = 0 To UBound(Data())

Text2.Text = Text2.Text & \Next

MsG.EndTime End Sub

Private Sub Command3_Click() Dim i As Integer, s As Integer s = Val(Text3.Text) Text1.Text = \For i = 0 To s

Text1.Text = Text1.Text & IIf(Text1.Text = \Next End Sub

1、求100以内的素数。 Private Sub Form_Click()

tag = True

For i = 3 To n / 2

If (n Mod i) = 0 Then tag = False Next i

If tag Then ss = n End Function

Private Sub Picture1_Click() Dim i%, avera avera = 0

For i = 1000 To 1100

If ss(i) <> 0 Then Print ss(i) avera = avera + ss(i) Next i

Picture1.Print avera End Sub

40、编一子过程ProcMin,求一维数组a中的最小值,子过程的形参自己确定。 Private Sub Form_Click() Dim a(1 To 10), amin, i% For i = 1 To 10

a(i) = Int(Rnd * 101) Print a(i); Next i

Call ProcMin(a(), amin) Print

Print \End Sub

Sub ProcMin(b(), min) Dim i%

min = b(LBound(b))

For i = LBound(b) + 1 To UBound(b) If b(i) < min Then min = b(i) Next i End Sub

41、编一个冒泡排序法子过程,对已知的若干整数按递减次序排列。提示:子过程的形式为sub sort1(a%())。提示:子过程的形式为sub sort1(a%())。 Private Sub Command1_Click() Dim b%(10), i% Print \排序前\For i = 0 To 9

b(i) = Int(Rnd * 100) Print b(i); Next i Print

Print \排序后\Call sort(b()) End Sub

Sub sort(a%())

Dim i%, m%, n%, min%, j% n = UBound(a)

For i = 0 To n - 1

For j = 0 To n - 1 - i

If a(j) < a(j + 1) Then m = a(j): a(j) = a(j + 1): a(j + 1) = m Next j Next i

For i = LBound(a) To UBound(a) - 1 Print a(i); Next i End Sub

42、编一个选择排序法子过程,对已知的若干整数按递增次序排列。提示:子过程的形式为sub sort1(a%())。

Private Sub Command1_Click() Dim b%(10), i% Print \排序前\For i = 1 To 10

b(i) = Int(Rnd * 100) Print b(i); Next i Print

Print \排序后\Call sort(b()) End Sub

Sub sort(a%())

Dim i%, m%, n%, min%, t% n = UBound(a)

For i = 0 To n - 1 min = i

For j = i + 1 To n

If a(j) < a(min) Then min = j Next j

m = a(i): a(i) = a(min): a(min) = m Next i

For i = LBound(a) To UBound(a) Print a(i); Next i End Sub

43、编一个冒泡排序法子过程,对已知的若干整数按递增次序排列。提示:子过程的形式

为sub sort1(a%())。

Private Sub Command1_Click() Dim b%(10), i% Print \排序前\For i = 0 To 9

b(i) = Int(Rnd * 100) Print b(i); Next i Print

Print \排序后\Call sort(b()) End Sub

Sub sort(a%())

Dim i%, m%, n%, min%, j% n = UBound(a)

For i = 0 To n - 1

For j = 0 To n - 1 - i

If a(j) > a(j + 1) Then m = a(j): a(j) = a(j + 1): a(j + 1) = m Next j Next i

For i = LBound(a) + 1 To UBound(a) Print a(i); Next i End Sub

本文来源:https://www.bwwdw.com/article/setd.html

Top