admin 管理员组

文章数量: 887021


2024年1月23日发(作者:体内的常量元素)

33个vb经典代码

1。求三角形的面积代码:

Option Explicit

Dim a!,b!,c!r,!,s!

Private Sub Command1_Click()

a = InputBox(”a=”, "请输入a的数值")

b = InputBox(”b=", "请输入b的数值")

c = InputBox("c=”, ”请输入c的数值")

If a + b 〉 c And a + c 〉 b And b + c > a And a 〉0 Then

r = 1 / 2 * (a + b + c)

s = Sqr(r * (r — a) * (r — b) * (r — c))

Label1。Caption = "三角形的面积为" & s

Else

n = ”输入的数据不能构成三角形"

End If

End Sub

2.三个数排列代码:

Option Explicit

Dim x!, y!, z!, t!

Private Sub Command1_Click()

x = InputBox("输入第一个数 x")

y = InputBox("输入第二个数 y”)

第1页

0 And b 〉0 And c 〉

33个vb经典代码

z = InputBox("输入第三个数 z")

Print "排序前"; x & ” " & y & ” ” & z

If x < y Then t = x: x = y: y = t

If y < z Then

t = y: y = z: z = t

If x 〈 y Then

t = x: x = y: y = t

End If

End If

Print "排序后”; z & ” " & y & ” " & x

End Sub

3。计算一元二次方程 的代码:

Option Explicit

Dim a!,b!,c!,d!,x1!,x2!

Private Sub Command1_Click()

a = InputBox(”a=”, ”请输入一元二次方程的系数a")

b = InputBox("b=”, ”请输入一元二次方程的系数b")

c = InputBox(”c=”, "请输入一元二次方程的系数c”)

If a = 0 Then

Print ”因为a≠0,你输入的a=0,请重新输入系数a"

End If

d = b ^ 2 - 4 * a * c

第2页

33个vb经典代码

If d >= 0 Then

x1 = (—b + Sqr(d)) / (2 * a)

x2 = (—b - Sqr(d)) / (2 * a)

Print ”系数为” & a; b; c & "的一元二次方程的根分别为” & "x1=” &

x1 & ” " & "x2=" & x2

Else

Print ”此方程在实数范围内无解"

End If

End Sub

4。利用Select Case语句输入年份计算属相代码:

Option Explicit

Private Sub Command1_Click()

Dim i As Integer, x As String, Y As Integer

Y = InputBox(”输入你的出生年份Y”)

i = Y Mod 12

Select Case i

Case Is = 0

x = ”猴”

Case Is = 1

x = "鸡”

Case Is = 2

x = "狗"

第3页

33个vb经典代码

Case Is = 3

x = ”猪"

Case Is = 4

x = "鼠”

Case Is = 5

x = ”牛"

Case Is = 6

x = ”虎”

Case Is = 7

x = ”兔”

Case Is = 8

x = ”龙”

Case Is = 9

x = "蛇”

Case Is = 10

x = ”马"

Case Is = 11

x = ”羊”

End Select

Print x

End Sub

5。闰年两种判断方法的代码;

第4页

33个vb经典代码

方法一:Option Explicit

Dim y As Integer

Private Sub Command1_Click()

y = InputBox("y=", "输入年份")

If y Mod 4 = 0 Then

If y Mod 100 = 0 Then

If y Mod 400 = 0 Then

Print "这年为闰年"

Else

Print "这年为平年"

End If

Else

Print ”这年为闰年"

End If

Else

Print "这年为平年”

End If

End Sub

方法二:Private Sub Command2_Click()

y = InputBox("y=”, "输入年份")

If y Mod 4 = 0 And y Mod 100 = 0 Then

Print "这年为闰年"

第5页

33个vb经典代码

ElseIf y Mod 400 = 0 Then

Print "这年为闰年"

Else

Print "这年为平年"

End If

End Sub

6.三种方法计算下式法一:Option Explicit

Dim x!,y!

y1sin2xx21x 的代码:

Private Sub Command1_Click()

x = InputBox(”x=”, ”输入x的值”)

If x <> 0 Then

If x 〉= 1 Or x 〈= —1 Then

y = (1 + Sin(x) * Sin(x) - Sqr(x ^ 2 - 1)) / x

Print ”y="; Format(y, "0.0000”)

Else

Print ”所输入的x不在定义域”

End If

Else

Print "除数不能为零”

End If

End Sub

第6页

33个vb经典代码

法二:Private Sub Command2_Click()

x = InputBox("x=", ”输入x的值")

Select Case x

Case Is = 0

Print ”除数不能为零”

Case Is 〉= 1, Is 〈= —1

y = (1 + Sin(x) * Sin(x) — Sqr(x ^ 2 — 1)) Print "y="; Format(y, "0。0000")

Case Is <〉 0, Is < 1, Is > —1

Print "所输入的x不在定义域"

End Select

End Sub

法三:Private Sub Command3_Click()

x = InputBox(”x=”, "输入x的值”)

If x >= 1 Then

y = (1 + Sin(x) * Sin(x) - Sqr(x ^ 2 — 1)) / x

Print "y="; Format(y, ”0。0000”)

ElseIf x < 1 And x 〉 0 Then

Print "所输入的x不在定义域"

ElseIf x = 0 Then

Print "除数不能为零”

第7页

/ x

33个vb经典代码

ElseIf x >= —1 And x < 0 Then

Print "所输入的x不在定义域"

ElseIf x 〈= -1 Then

y = (1 + Sin(x) * Sin(x) - Sqr(x ^ 2 — 1)) / x

Print ”y=”; Format(y, ”0.0000”)

End If

End Sub

流程图:

输入x的值

X=0?

除数不能为零

∣x∣≥1

y1sin2xx21xX的值不在定义域里

伪代码:

第8页

33个vb经典代码

step1:输入x的值;

step2:判断x是否为零;

step3:若x=0,输出“除数不能为零";若x不为零,则继续判断x的绝对值是否大于1;

step4:若x的绝对值是否大于1,输出y的值;否则x的值不在函数的定义域里,结束。

7。 利用For……Next循环计算1+2+3+4+……+n。

Option Explicit

Dim i%,j%,n%

Private Sub Command1_Click()

j = 0

n = InputBox("输入的n值")

For i = 1 To n

j = j + i

Next i

Print j

End Sub

8。 编写利用辗转相除法及辗转相减法计算公约数的代码

(1)辗转相减法

Private Sub Command10_Click()

Dim m As Long, n As Long, m1 As Long, n1 As Long, r As Long, t As

Long

第9页

33个vb经典代码

m1 = InputBox("输入m1”, "求最大公约数和最小公倍数”)

n1 = InputBox(”输入n1", ”求最大公约数和最小公倍数")

m = m1

n = n1

Do

If n 〉 m Then

t = m

m = n

n = t

End If

r = m — n

If r = 0 Then Exit Do

m = n

n = r

Loop Until r = 0

Print m1 & ",” & n1 & "的最大公约数是" & m

Print m1 & ”,” & n1 & ”的最小公倍数是” & m1

End Sub

(2)辗转相除法

Private Sub Command9_Click()

Dim n%, m%, r%, m1%, n1%

n1 = InputBox("输入n”)

第10页

n1 / m *

33个vb经典代码

m1 = InputBox("输入m")

If m1 〉 n1 Then

m = m1: n = n1

Else

m = n1: n = m1

End If

r = m Mod n

Do While r <〉 0

m = n

n = r

r = m Mod n

Loop

Print m1; ”,"; n1; ”的最大公约数"; n

Print m1; ”,”; n1; ”最小公倍数"; m1 * n1 / n

End Sub

9. 输出一列“*”,一行“*”。

Private Sub Command2_Click()

For i = 1 To 8

Print ”*"

Next i

Print

End Sub

第11页

33个vb经典代码

Private Sub Command3_Click()

For i = 1 To 8

Print ”*”;

Next i

Print

End Sub

10。 一张若干大面积的纸厚0。1mm,将其对折一次厚0.2mm,再对折一次厚0。4mm,如果对折了40次,问厚度是多少?

Private Sub Command4_Click()

Dim s As!,ss!

n = InputBox("输入的n值")

For i = 1 To n

s = 0。1 * 2 ^ n

ss = s * 0.001

Next i

Print "ss=” & Format(ss, "0。0000”) & ”米”

End Sub

11。 一个球自100m处高度自由落下,每次落地后反跳到原高度一半,然后再落下。求它在第10次落地时,共经过多少m?第10次反弹多高?

Private Sub Command5_Click()

Dim i%, s!, h!

第12页

33个vb经典代码

h = 50

s = 100

i = 2

Do Until i > 10

s = s + h * 2

h = h / 2

i = i + 1

Loop

Print "十次落地经过的路程s=" & s; ”米”, ”第十次落地反弹的高度h=" &

h; "米”

End Sub

12. 猴子吃桃问题。猴子第一天摘下若干个桃子,当即吃了一半又多吃一个。第二天双将剩下的吃了一半,又多吃一个,以后每天都吃前一天剩下的一半零一个。到第十天只有一个桃子。问第一天共摘了多少个桃子。

Private Sub Command6_Click()

Dim i%, x1%, x2%

x1 = 1

x2 = 0

i = 1

Do Until i > 9

x2 = (x1 + 1) * 2

第13页

33个vb经典代码

x1 = x2

i = i + 1

Loop

Print ”总桃数为" & x2

End Sub

13。 利用自然对数e的近似值,近似公式为:

e=1+1/1!+1/2!+1/3!+…+1/n!+ …

(1)求其误差小于0。00001时的值;

(2)计算到第10项.

(1)Private Sub Command7_Click()

Dim s!, e!, i%

i = 1

s = 1

e = 1

Do Until i > 9

s = s / i

e = e + s

i = i + 1

Loop

Print ”e=” & e

End Sub

第14页

33个vb经典代码

(2)Private Sub Command8_Click()

Dim i%, t&, e!

i = 0

e = 0

t = 1

Do While 1 / t 〉 0.00001

e = e + 1 / t

i = i + 1

t = t * i

Loop

Print "e="; e

End Sub

14。以单重循环及三重循环找到100到999之间所有的水仙花数;

单重循环:Option Explicit

Private Sub Command1_Click()

Dim a%, b%, c%, i%

For i = 100 To 999

a = i Mod 10: b = (i Mod 100) 10: c = i 100

If i = a ^ 3 + b ^ 3 + c ^ 3 Then

Print i

End If

Next i

End Sub

三重循环:Private Sub Command2_Click()

Dim a%, b%, c%

第15页

33个vb经典代码

For a = 1 To 9

For b = 0 To 9

For c = 0 To 9

If a ^ 3 + b ^ 3 + c ^ 3 = a * 100 + b * 10 + c Then

Print a * 100 + b * 10 + c

End If

Next c

Next b

Next a

End Sub

15.在百钱买百鸡的基础上,练习兑换零钱;

Option Explicit

Dim a%, b%, c%, d%, e%, f%, m!, n%

Private Sub Command1_Click()

n = 0

m = 0

For a = 0 To 2

For b = 0 To (100 - 50 * a) / 20

For c = 0 To (100 — 50 * a — 20 * b) 10

For d = 0 To (100 — 50 * a - 20 * b — 10 * c) 5

For e = 0 To (100 - 50 * a — 20 * b — 10 * c — 5 * d) 2

For f = 0 To 100 — 50 * a — 20 * b — 10 * c - 5 * d — 2

* e

If a * 50 + b * 20 + c * 10 + d * 5 + e * 2 + f = 100 Then

n = n + 1

End If

m = m + 1

Next f

Next e

Next d

Next c

Next b

Next a

n = "总共有" & n & ”种方法。" & ” ” & ”共计算了" & m &

"次。”

End Sub

Private Sub Command2_Click()

n = 0

m = 0

第16页

33个vb经典代码

For a = 0 To 2

For b = 0 To (100 — 50 * a) / 20

For c = 0 To (100 — 50 * a - 20 * b) 10

For d = 0 To (100 — 50 * a — 20 * b - 10 * c) 5

For e = 0 To (100 - 50 * a - 20 * b — 10 * c — 5 * d) 2

n = n + 1

m = m + 1

Next e

Next d

Next c

Next b

Next a

Label1。Caption = "总共有” & n & "种方法." & " ” & "共计算了”次。”

End Sub

16输入一个数,判断该数是否为素数。

Option Explicit

Private Sub Command1_Click()

Dim n!, j!

n = Val(Text1)

j = 2

Do While j 〈= Sqr(n) And n Mod j <〉 0

j = j + 1

Loop

If j > Sqr(n) Then

Print n; ”是素数”

Else

Print n; "是合数”

End If

End Sub

17.求100~200之间所有的素数。

Option Explicit

Dim i%, j%, c%

Private Sub Command1_Click()

For i = 101 To 199 Step 2

For j = 2 To Sqr(i)

If i Mod j = 0 Then Exit For

Next

第17页

& m & "

33个vb经典代码

If j > Sqr(i) Then

Print i;

c = c + 1

If c Mod 5 = 0 Then

Print

End If

End If

Next

End Sub

18.输入一个偶数,验证该数符合哥德巴赫猜想。

Option Explicit

Private Sub Command1_Click()

Dim N%, N1%, N2%, I%, K1%, K2%

N = Val(InputBox("输入大于6的偶数”))

For N1 = 3 To N 2 Step 2

K1 = Int(Sqr(N1))

For I = 2 To K1 '判断N1是否是素数

If N1 Mod I = 0 Then Exit For

Next I

If I 〉 K1 Then ’如果N1为素数,将N分解为N1+N2

N2 = N — N1

K2 = Int(Sqr(N2))

For I = 2 To K2 ’判断N2是否是素数

If N2 Mod I = 0 Then Exit For

Next I

If I > K2 Then ' 如果N2也为素数,则打印输出

Print N & ”=" & N1 & ”+" & N2

End If

End If

第18页

33个vb经典代码

Next N1

End Sub

19。验证100~200之间所有的偶数符合哥德巴赫猜想.

Option Explicit

Private Sub Command1_Click()

Dim i As Integer

Dim j As Integer

Dim x As Integer

Dim y As Integer

Dim z As Integer

k: x = Val(InputBox(”请输入一个数”))

If x < 6 And x Mod 2 <> 0 Then

GoTo k

Else

Print x

For y = 2 To x / 2

z = x — y

For i = 2 To y - 1

If y Mod i = 0 Then Exit For

Next i

If i 〉 y — 1 Then

For j = 2 To z — 1

If z Mod j = 0 Then Exit For

Next j

If j > z - 1 Then

Print y; ”+"; z

End If

End If

Next y

End If

End Sub

20.输出一个5阶的杨辉三角形

Option Explicit

Dim A() As Integer

Dim n As Integer, i, j%

Private Sub Command1_Click()

ReDim A(6, 6)

For i = 1 To 6

For j = 1 To j

If j = 1 Or j = i Then

A(i, j) = 1

Else

第19页

33个vb经典代码

A(i, j) = A(i - 1, j - 1) + A(i — 1, j)

End If

Next j, i

For i = 1 To 6

For j = 1 To i - 1

Print Tab(50 - i * 3 + j * 6); A(i, j); Spc(1);

Next j

Print

Next i

End Sub

21.输出下面的图案

C

C H

C H I

C H I N

C H I N A

Option Explicit

Dim A As String

Dim i%, j%

Private Sub Command1_Click()

For j = 0 To 5

A = Mid("CHINA”, 1, i)

Print A

i = j + 1

Next j

Print

End Sub

22。输出一个n矩阵等腰三角形,其外形如下

提示:倒数第一行一个,倒数第二行3个,……

第20页

33个vb经典代码

Option Explicit

Dim i!,j!,T$,x$

Private Sub Command1_Click()

For i = 0 To 3

x = ””

For j = 1 To (7 - Abs(i) * 2)

x = x & ”*"

Next

T = Space(Abs(i)) & x & Space(Abs(i))

Print T

Next

End Sub

23。先成一个10阶的一维随机数组,数组的每个元素值为[10,50],将这个数组用选择法及冒泡法二种排序方法排序,编写该代码。

Option Explicit

Dim k%,i%,j%,t%,m%,n%

Private Sub Command1_Click()

Dim x%(1 To 5, 1 To 10)

For m = 1 To 5

For n = 1 To 10

Randomize

x(m, n) = Int(Rnd * 100 + 2000) '产生50个随机数

Picture3。Print Tab(6 * n — 4); x(m, n);

Next n

Print

Next m

End Sub

Private Sub Command2_Click()

Dim x%(1 To 10)

For k = 1 To 10

Randomize

第21页

33个vb经典代码

x(k) = Int(Rnd * 41 + 10) ’产生10个随机数

Picture1。Print x(k);

Next k

For i = 1 To 9

For j = i + 1 To 10

If x(i) 〉 x(j) Then

t = x(i): x(i) = x(j): x(j) = t ’大数沉底

End If

Next j

Next i

For m = 1 To 10

Picture2。Print x(m); '打印排列后的顺序

Next m

End Sub

Private Sub Command3_Click()

Dim x%(1 To 10)

For k = 1 To 10

Randomize

x(k) = Int(Rnd * 41 + 10) '产生10个随机数

Picture1。Print x(k);

Next k

For i = 1 To 9

For j = 1 To 10 - i

If x(j) > x(j + 1) Then

t = x(j): x(j) = x(j + 1): x(j + 1) = t ’两两比较,大数换到后面

End If

Next j

Next i

For m = 1 To 10

x(m); '打印排列后的顺序

第22页

33个vb经典代码

Next m

End Sub

24。生成一个50阶的随机数组,其元素值为[100,2000].1、元素值不重复,再人为输入一个数,在50个元素值中找出与之相等的那个元素,如果没有这个数,给出相应的信息.2、元素值可以重复,再人为输入一个数,在50个元素值中找出与之相等的那个元素并统计相等的个数,如果没有这个数,给出相应的信息。

Option Explicit

Dim n%, m%, h%, t%, s%

Dim x%(1 To 5, 1 To 10)

Private Sub Command1_Click()

For m = 1 To 5

For n = 1 To 10

Randomize

x(m, n) = Int(Rnd * 100 + 1) ’产生50个随机数

Picture1。Print Tab(6 * n — 4); x(m, n);

Next n

Next m

End Sub

Private Sub Command2_Click()

h = Val(Text1)

t = False

For m = 1 To 5

For n = 1 To 10

If x(m, n) = h Then

t = True

第23页

33个vb经典代码

End If

If t Then Label2 = ”找到了” Else Label2 = ”没找到"

Next n

Next m

End Sub

Private Sub Command3_Click()

h = Val(Text1)

t = False

For m = 1 To 5

For n = 1 To 10

If x(m, n) = h Then

s = s + 1

t = True

End If

If t Then Label2 = "找到了,这个数出现了" & s & ”次” Else Label2 = ”没找到”

Next n

Next m

End Sub

26.制作一个程序,其功能为输入年份,输出该年份的属相(分别用字符串和数组方式进行数据处理)。

法一:Private Sub Command1_Click()

Dim A%,c%,b$

c = Val((InputBox("请输入你的出生年份”)))

A = c Mod 12

b = Mid("猴鸡狗猪鼠牛虎兔龙蛇马羊”, A + 1, 1)

Print ”你的出生的属相是”; b

End Sub

法二:Private Sub Command2_Click()

Dim tyea As Integer

Dim x(11) As String

第24页

33个vb经典代码

Dim A As Integer

x(0) = ”猴"

x(1) = "鸡"

x(2) = ”狗”

x(3) = "猪"

x(4) = ”鼠"

x(5) = "牛”

x(6) = ”虎"

x(7) = "兔"

x(8) = "龙"

x(9) = ”蛇"

x(10) = "马"

x(11) = "羊"

tyea = Val((InputBox("请输入你的出生年份”)))

A = tyea Mod 12

Print tyea; ”你的出生的属相是"; x(A)

End Sub

M!28。利用自定义过程和函数的方法编程计算下式:N!(MN)!

其中:M、N在程序运行时输入.

Option Explicit

Dim n%, t%, m%

Private Sub Command1_Click()

m = Val(Text1): n = Val(Text2) ’计算M!/(N!(M—N)!)

t = jc(m) / (jc(n) * jc(m — n))

Label1 = "计算结果是” & t

End Sub

Private Function jc&(ByVal m%)

If m = 1 Then

jc = 1

Else

jc = jc(m — 1) * m

End If

End Function

29。利用递归调用方式计算:

第25页

33个vb经典代码

122334(n1)n

其中:n在程序运行时输入。

Option Explicit

Dim n%, t%

Private Sub Command1_Click()

n = Val(Text1) '计算1*2+3*4+^^^(N-1)*N

t = f(n)

Label1 = ”计算结果是” & t

End Sub

Private Function f&(ByVal n%)

If n = 2 Then

f = 2

Else

f = f(n - 1) + n * (n - 1)

End If

End Function

30.利用递归调用方式计算斐波那切数列:

斐波那切数列:1、1、2、3、5、8、13、……、2n-3

Option Explicit

Dim n%, t%

Private Sub Command1_Click()

n = Val(Text1) ' 计算斐波那切数列

t = h(n)

Label1 = "计算结果是" & t

End Sub

Private Function h&(ByVal n%)

If n = 1 Or n = 2 Then

h = 1

第26页

33个vb经典代码

Else

h = h(n - 1) + h(n - 2)

End If

End Function

31。最大程序的利用自定义函数或自定义过程编写验证100~200之间所有的偶数符合哥德巴赫猜想。

Option Explicit

Dim i%, j%, s%

Private Sub Command1_Click()

For i = 100 To 200 Step 2 ’验证100~200之间所有的偶数符合哥德巴赫猜想。

For j = 3 To i / 2

If g(j) And g(i - j) Then

s = s + 1

Picture1。Print Tab(13 — 12 * (s Mod 2)); i & ”=" & j & ”+” & (i — j);

End If

Next j

Next i

End Sub

Private Function g(ByVal m%) As Boolean

g = True

For s = 2 To Sqr(m)

If m Mod s = 0 Then

g = False

End If

Next s

End Function

32。以一个标签为目标,利用一组命令按钮第27页

33个vb经典代码

(控件数组形式)设置其背景色。以一个文本框为目标,利用另一组命令按钮(控件数组形式)设置其字形(加粗、倾斜、下划线).

Private Sub Command1_Click(Index As Integer)

A = Index

Select Case A

Case 0

lor = &HFF&

Command1(4)。Visible = True

Case 1

lor = &HFF0000

Command1(3).Visible = True

Case 2

Label1。BackColor = &HFF00FF

Command1(2)。Visible = True

Case 3

lor = &HFF00&

Command1(1).Visible = True

Case 4

Label1。BackColor = &HFFFF&

End Select

End Sub

第28页

33个vb经典代码

Private Sub Command2_Click(Index As Integer)

Select Case Index

Case 0

ld = True

Command2(1).Visible = True

Case 1

Text1。FontItalic = True

Command2(2)。Visible = True

Case 2

derline = True

Command2(3).Visible = True

Case 3

Text1。FontStrikethru = True

End Select

End Sub

33.制作一个计算器的界面。

。 Option Explicit

Dim a!, b!, c!, e As String

Private Sub Command3_Click()

a = 0

b = 0

c = 0

e = ""

Picture1。Cls

第29页

33个vb经典代码

Text1 = ”"

End Sub

Private Sub Command4_Click()

Call ff

e = "+”

End Sub

Private Sub Command5_Click()

Call ff

e = ”-"

End Sub

Private Sub Command6_Click()

Call ff

e = ”*”

End Sub

Private Sub Command7_Click()

Call ff

e = ”/”

End Sub

Private Sub Command8_Click()

Call ff

End Sub

Private Sub Command9_Click(Index As Integer)

Select Case Index

Case 0

Text1 = Text1 + "0"

Case 1

Text1 = Text1 + "1"

Case 2

Text1 = Text1 + ”2"

Case 3

第30页

33个vb经典代码

Text1 = Text1 + ”3"

Case 4

Text1 = Text1 + ”4"

Case 5

Text1 = Text1 + "5”

Case 6

Text1 = Text1 + "6”

Case 7

Text1 = Text1 + ”7"

Case 8

Text1 = Text1 + "8"

Case 9

Text1 = Text1 + ”9"

End Select

End Sub

Sub ff()

a = Val(Text1)

If e = "" Then

b = a

c = a

Else

Select Case e

Case "+”

c = b + a

Case "-"

c = b — a

Case "*"

c = b * a

Case "/"

If a = 0 Then a = 1

第31页

33个vb经典代码

c = b / a

End Select

b = c

End If

Text1 = "”

us

Picture1。Cls

c

End Sub

第32页


本文标签: 输入 代码 利用 计算 数组