《vb编程代码-.pdf》由会员分享,可在线阅读,更多相关《vb编程代码-.pdf(16页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、编写程序,随机生成100个两位整数,并统计出其中小于等于 40、大于40小于等于 70及大于 70的数据个数。代码Private Sub Command1_Click()Dim x As Integer,i As Integer Dim cnt1 As Integer,cnt2 As Integer,cnt3 As Integer cnt1=0:cnt2=0:cnt3=0 Randomize For i=1 To 100 x=Int(Rnd*90+10)If x=40 Then cnt1=cnt1+1 ElseIf x 70 Then cnt2=cnt2+1 Else cnt3=cnt3+1
2、End If Next i Print cnt1,cnt2,cnt3 End Sub 编写程序,单击窗体时在窗体上输出如下所示的图形。代码Private Sub Form_Click()Dim i As Integer,begin As Integer begin=5 For i=1 To 5 Print Tab(begin);For j=1 To 2*i-1 Print*;Next j Print begin=begin-1 Next i begin=2 For i=4 To 1 Step-1 Print Tab(begin);or j=1 To 2*i-1 Print*;Next j Pr
3、int begin=begin+1 Next i End Sub 计算二的n 次幂代码Private Sub Command1_Click()Dim n As Integer,mi As Double Dim i As Integer n=Int(Val(InputBox()mi=1 For i=1 To n mi=mi*2 Next i Print 2 的;n;次幂为:;mi End Sub 编写程序,计算并输出下面级数前n 项(n=80)的部分和。1-1/2+1/3-1/4+1/5+1/n代码Inputbox 中输入 2,出现上述结果Private Sub Command1_Click()
4、Dim n As Integer,i As Integer Dim sum As Double n=Val(InputBox()sum=0 For i=1 To n If i Mod 2 0 Then sum=sum+(1/i)Else sum=sum-(1/i)End If Next i Print sum End Sub 有 100 匹马,驮100 担货:大马驮3 担,中马驮 2 担,两匹小马驮1 担,问有大、中小马各多少?代码显示结果如上所示Private Sub Form_Click()Dim x As Integer,y As Integer,z As Integer Print 大
5、马,中马,小马 For x=0 To 33 For y=0 To 50 z=100-x-y If(x*3+y*2+z*0.5)=100 Then Print x,y,z End If Next y Next x 编写程序,要求从键盘输入0-6 中的任一个,可对应得到星期日、星期一-星期六代码Inputbox 中输入 2 结果如上Dim n As Integer n=Int(Val(InputBox()Select Case n Case 0 Print n;:星期日 Case 1 Print n;:星期一 Case 2 Print n;:星期二 Case 3 Print n;:星期三 Case
6、 4 Print n;:星期四 Case 5 Print n;:星期五 Case 6 Print n;:星期六 Case Else Print n;:错误 End Select End Sub 编写程序,输出下列三角形:(要求用循环做,非循环不给分。)代码运行结果如上图所示Private Sub Command1_Click()Dim i As Integer,j As Integer,begin As Integer begin=7 For i=1 To 4 Print Tab(begin);For j=1 To 2*i-1 Print 1;注意:双引号中 1 后面必须有一个空格Next j
7、 Print begin=begin-2 Next i End Sub 求公式:1/1!+1/2!+1/3!+1/n!直到最后一项小于10(-6)时 e 的值。(注:写在Command1 的 Click 事件中。)代码运行结果如上图所示Private Sub Command1_Click()Dim sum As Double,n As Long,nj As Double Dim i As Long sum=0 n=1 Do nj=1 For i=1 To n nj=nj*i Next i sum=sum+1/nj n=n+1 Loop While(1/nj)=0.000001 Print su
8、m End Sub 编写程序,输出下列三角形:总行数通过键盘输入。代码Inputbox 输入 5 运行结果如上图所示Option Explicit Private Sub Command1_Click()Dim i As Integer,j As Integer,n As Integer,begin As Integer n=Int(Val(InputBox()begin=1 For i=1 To n Print Tab(begin);For j=1 To 9-2*i Print 1;Next j Print begin=begin+2 Next i End Sub 输入一串数字,求其中偶数位
9、数字之和。例如输入 1357986,则求 3+7+8 代码Inputbox 输入 5 运行结果如上图所示Private Sub Command1_Click()Dim str As String,s As String*1,i As Long,sum As Long str=InputBox()Print str sum=0 For i=2 To Len(str)Step 2 s=Mid(str,i,1)sum=sum+Val(s)Print s Next i 输入一串数字,求其中奇数数字构成的数与偶数数字构成的数之差。例如输入 1654238,求 1536428。代码Inputbox 输入
10、5 运行结果如上图所示Private Sub Command1_Click()Dim str As String,s1 As String,s2 As String,i As Long str=InputBox()Print str s1=s2=For i=1 To Len(str)If i Mod 2=1 Then s1=s1&Mid(str,i,1)Else s2=s2&Mid(str,i,1)End If Next i Print s1;-;s2;=;Val(s1)-Val(s2)End Sub 编写程序,单击窗体时在窗体上输出如下所示的图形。代码Private Sub Command1
11、_Click()Dim begin As Integer,n As Integer,i As Long begin=9 For i=1 To 5 Print Tab(begin);For n=1 To 2*i-1 Print*;Next n Print begin=begin-2 Next i begin=3 For i=1 To 4 Print Tab(begin);For n=1 To 9-2*i Print*;Next n Print begin=begin+2 Next i End Sub 有一分数序列:1,1/2,2/3,3/5,5/8,8/13 求出这个数列的前20 项之和。代码O
12、ption Explicit Private Sub Command1_Click()Dim sum As Double Dim a1 As Long,a2 As Long,a3 As Long Dim b1 As Long,b2 As Long,b3 As Long Dim i As Long a1=1:a2=1 b1=1:b2=2 sum=a1/b1+a2/b2 For i=3 To 20 a3=a1+a2 b3=b1+b2 sum=sum+a3/b3 a1=a2 a2=a3 b1=b2 b2=b3 Next i Print sum End Sub 编写程序,计算下面级数前n 项中(n=5
13、0)奇数项的和,并输出该级数最后两项之差的绝对1*2*3+2*3*4+3*4*5+n*(n+1)*(n+2)+代码Option Explicit Private Sub Command1_Click()Dim sum As Long,dif As Long,n As Long sum=0 For n=1 To 49 Step 2 sum=sum+n*(n+1)*(n+2)Next n dif=(49-52)*50*51 Print sum Print Abs(dif)End Sub 编写程序,计算并输出下面级数前n 项(n=30)的部分和。1/(1*2)+1/(2*3)+1/(3*4)+1/(
14、n*(n+1)+代码Option Explicit Private Sub Command1_Click()Dim sum As Double,n As Long sum=0 For n=1 To 30 sum=sum+1/(n*(n+1)Next n Print sum End Sub 编写程序,输出下列三角形:代码编程打印如下图形,总行数通过键盘输入。(要求用循环做,非循环不给分。按原图形直接打印者无效。)代码Inputbox 输入 5 运行结果如上图所示Option Explicit Private Sub Command1_Click()Dim f1 As Long,f2 As Lon
15、g,f3 As Long Dim n As Long,begin As Long Dim i As Long,j As Long n=Int(Val(InputBox()begin=4*n-3 f1=1 f2=1 For i=1 To n f3=f1+f2 Print Tab(begin);For j=1 To 2*i-1 Print Trim(f3);Spc(4-Len(Trim(f3);相邻两数的位置差4 Next j Print begin=begin-4 f1=f2 f2=f3 Next i End Sub Option Explicit Private Sub Command1_Cl
16、ick()Dim code1 As Integer,code2 As Integer For code1=Asc(G)To Asc(A)Step-1 For code2=code1 To Asc(G)Print Chr(code2);Next code2 Print Next code1 End Sub 勾股定理中三个数的关系是:a2+b2=c2 编写程序,输出100 以内满足上述关系的整数组合,例如3,4,5 就是一个整数组合。代码Press enter star this 还有两列结果未显示Option Explicit Private Sub Command1_Click()Dim a
17、As Integer,b As Integer,c As Double Dim cnt cnt=0 For a=1 To 99 For b=1 To 99 c=Sqr(a*a+b*b)If c 100 And Int(c)=c Then Print a;b;c;,cnt=cnt+1 If cnt Mod 4=0 Then Print End If Next b Next a End Sub 通过键盘输入任意长度的字符串,要求将字符 顺 序 倒 置,例 如,将 输 入 的HEJISUANJISHIYANSHI变换成IHSNAYIHSIJNAUSIJEH。代码先后输入5 和 a 后显示结果如上Op
18、tion Explicit Private Sub Command1_Click()Dim s1 As String,s2 As String Dim i As Long s1=InputBox(请输入原始字符串)s2=For i=Len(s1)To 1 Step-1 s2=s2&Mid(s1,i,1)Next i Print s1 Print s2 End Sub 求 1000 以内分别被3、5、7 除,余数都为1的正整数的个数及其和代码Option Explicit Private Sub Command1_Click()Dim n As Integer,sum As Double Dim
19、 cnt As Integer sum=0 cnt=0 For n=1 To 1000 If(n Mod 3=1)And(n Mod 5=1)And(n Mod 7=1)Then Print n cnt=cnt+1 sum=sum+n End If Next n Print Print 共;cnt;个 Print 和为;sum End Sub 代码图形 1_Click()图形 4_Click()Private Sub 图形 1_Click()Cls Print For n=1 To 8 Print Tab(2*n+2);For m=1 To 8 Print Spc(1);*;Next Prin
20、t Next End Sub Private Sub 图形 4_Click()Cls For i=1 To 5 Print Tab(20-3*i);For j=1 To 2*i-1 Print i;Next j Print Next i End Sub Private Sub 图形 6_Click()Cls k=1 For i=1 To 6 图形 6_Click()水仙花数 _Click()图形 2_Click()图形 3_Click()For j=1 To i Print Tab(5*j);k;k=k+1 Next j Print Next i End Sub Private Sub 水仙花
21、数 _Click()一个三位数,如果它等于各位数的立方和,如 153=13+53+33,称为水仙花数。求所有的水仙花数。Cls For i=1 To 9 For j=0 To 9 For k=0 To 9 a=100*i+10*j+k b=i 3+j 3+k 3 If a=b Then Print Tab(4);a Next k Next j Next i End Sub Private Sub 图形 2_Click()Cls Print For n=1 To 5 Print Tab(n+2);For m=n To 10-n Print*;Next Print Spc(4);For m=1 T
22、o 2*n-1 Print*;Next Print Next End Sub Private Sub 图形 3_Click()Cls Print For n=1 To 6 Print Tab(8-n);For m=1 To 2*n-1 Print*;Next m Next n For n=1 To 5 Print Tab(n+2);For m=n To 10-n Print*;Next m Next n End Sub Private Sub 矩阵 _Click()Cls Dim a(10,10)As Integer 图形 5_Click()乘积表 _Click()Dim m As Integ
23、er,n As Integer For n=1 To 10 For m=1 To 10 If n=m Or n=11-m Then a(n,m)=1 Else a(n,m)=0 End If Next Next Print For n=1 To 10 For m=1 To 10 Print Tab(m*3);a(n,m);Next Print Next End Sub Private Sub 图形 5_Click()Cls 编写程序,输出下列三角形:(要求用循环做,非循环不给分)G FG EFG DEFG CDEFG BCDEFG ABCDEFG Dim a As String,i As In
24、teger,k As Integer a=ABCDEFG k=Len(a)For i=1 To k Print Tab(3);Right(a,i)Next i End Sub Private Sub 乘积表 _Click()编写程序,打印如下的乘积表:*3 6 9 12 15 16 17 18 Dim i As Integer,j As Integer Cls Print *3 6 9 12 For i=15 To 18 Print i;For j=3 To 12 Step 3 Print i*j;Next j Print Next i End Sub Private Sub 数列 _Clic
25、k()有一数列:1、1、2、3、5、8、13、21,求出这个数列的前20 项之和数列 _Click()阶梯数 _Click()图形 7_Click()Cls Dim f1 As Single,f2 As Single,f3 As Single Dim i As Integer,sum As Single f1=1 f2=1 Print Tab(3);f1 Print Tab(3);f2 sum=2 Do While i=20-3 f3=f1+f2 f1=f2 f2=f3 sum=sum+f3 i=i+1 Print Tab(3);f3 Loop Print Tab(3);其和是:;sum En
26、d Sub Private Sub 阶梯数 _Click()有一个长阶梯,如果每步跨2 阶最后剩1 阶,如果每步跨3 阶最后剩2 阶,如果每步跨4阶最后剩 3 阶,如果每步跨5 阶最后剩4 阶,如果每步跨6阶最后剩 5 阶,只有当如果每步跨7 阶时恰好走完,问这个阶梯至少有多少阶?Dim a As Integer,i As Integer Cls Do While True i=i+1 If i Mod 2=1 And i Mod 3=2 And i Mod 4=3 And i Mod 5=4 And i Mod 6=5 And i Mod 7=0 Then Print Tab(3);该阶梯至
27、少有;i;阶 Exit Do End If Loop End Sub Private Sub 图形 7_Click()编写程序,输出下列三角形:(要求用循环做,非循环不给分)河 河南 河南农 河南农业 河南农业大河南农业大学Cls Dim a As String,i As Integer,k As Integer a=河南农业大学 k=Len(a)For i=1 To k Print Tab(3);Left(a,i)Next i End Sub 阶乘累加和代码Private Sub Form_Click()Dim sum As Long,jc As Long Dim i As Integer,
28、n As Integer sum=0 For n=1 To 10 jc=1 For i=1 To n jc=jc*i Next i sum=sum+jc Next n Print sum End Sub 求高次方根代码Option Explicit Private Sub Form_Click()说明:此题题目中要求的区间【1,2】有误,忽略之Dim x As Double,fx As Double,fpx As Double,d As Double x=1.5 Do d=(3*x*x*x-4*x*x-5*x+13)/(9*x*x-8*x-5)x=x-d Loop Until Abs(d)=0
29、.0001 Print 方程的根是;x End Sub 数字三角形代码输入 5 后运行结果如图Private Sub Form_Click()Dim i As Long,j As Long Dim n As Long,begin As Long n=Val(InputBox(请输入总行数)begin=2*n-1 For i=1 To n Print Tab(begin);For j=1 To(2*i-1)Print Trim(2*i-1);Next j Print begin=begin-2 Next i End Sub 求阶梯数目;每步跨2阶,剩余1;跨 3剩2;4剩3;5剩4;6剩5,7不
30、剩代码运行结果如图Private Sub Command1_Click()Dim n%n=7 Do While True If n Mod 2=1 And n Mod 3=2 And n Mod 4=3 And n Mod 5=4 And n Mod 6=5 And n Mod 7=0 Then Exit Do End If n=n+1 Loop Print n End Sub 代码Private Sub Form_Click()Dim i As Long,n As Long Dim cnt As Long,sum As Long Dim flag As Boolean cnt=0 sum=0
31、 For n=100 To 1000 flag=True For i=2 To n-1 If n Mod i=0 Then flag=False Exit For End If Next i If flag=True Then cnt=cnt+1 sum=sum+n Print n,If cnt Mod 5=0 Then Print End If Next n Print Print Print 找到;cnt;个质数,;和为;sum End Sub 球从100处自由落,每次反弹回原高度一半,再落,第十次落地后经历?m,最后一次反弹高度代码Private Sub Command1_Click()
32、Dim s!,d!,i!s=0 d=100 For i=1 To 10 s=s+d+d/2 d=d/2 Next i Print 总共距离;s;最后一次的高度;d End Sub 321 米钢料,a 种 17 米,b 种 27 米,求ab各多少段,余料最少为多少代码Private Sub Command1_Click()Dim i%,j%,min%,m%,n%min=321 For i=1 To 321/17 For j=1 To 321/27 If 321-i*17-j*27=0 Then min=321-i*17-j*27 m=i n=j End If Next j Next i Prin
33、t 17 米;m;段,27 米;n;段,最小残料;min End Sub 猴子每天吃掉一半多一个,到第七天只剩一个,求桃子总数代码Private Sub Command1_Click()Dim x1%,x2%,i%x1=1 For i=6 To 1 Step-1 x2=(x1+1)*2 Print 第;i;天桃子个数为:;x2 x1=x2 Next i End Sub 评委打分,去掉最高、最低,输出平均分代码Private Sub Command1_Click()Dim s%,a%,min%,max%,i%min=100 max=0 s=0 For i=1 To 7 a=InputBox(分数
34、:)Print a;s=s+a If a max Then max=a If a j Then Print i,j,k End If Next k Next j Next i End Sub 打印如图所示乘法表代码Private Sub Command1_Click()Dim i As Integer,j As Integer Dim k As Integer k=1 Print*3 6 9 12 For i=15 To 18 k=1 Print Trim(i)For j=3 To 12 Step 3 Print Tab(7*k);Trim(i*j);k=k+1 Next j Print Ne
35、xt i End Sub 有一数列1,1,2,3,5,8,求前 20 项和代码Private Sub Command1_Click()Dim a1%,a2%,a3%,i%a1=1 a2=1 Print a1,a2 For i=3 To 20 a3=a1+a2 Print a3 a1=a2 a2=a3 Next i End Sub 把一个 16 进制数转换成一个10 进制数代码Private Sub Command1_Click()Dim a As Integer,b As Integer Dim s As Integer s=0 a=Text1.Text For i=1 To Len(a)b=
36、Mid(a,Len(a)-i+1,1)If LCase(b)=a Then b=10 End If If LCase(b)=b Then b=11 End If If LCase(b)=c Then b=12 End If If LCase(b)=d Then b=13 End If If LCase(b)=e Then b=14 End If If LCase(b)=f Then b=15 End If s=s+b*16 (i-1)Next i Print s End Sub 编 写 程 序,用 牛 顿 切 线 法 求 方 程f(x)=x+lnx-2.0=0的近似实根r,迭代初值自选,精确到0.0001。(提示:计算公式为x=x-f(x)/f (x))代码Private Sub Command1_Click()Dim x1 As Double,x2 As Double x1=1 x2=x1-(x1+Log(x1)-2)/(1+1/x1)While Abs(x2-x1)0.0001 x1=x2 x2=x1-(x1+Log(x1)-2)/(1+1/x1)Wend Print x2 Print x2+Log(x2)-2 End Sub