VB子过程与函数过程
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
If y Mod Βιβλιοθήκη Baidu00 = 0 Then leap = 1 Else leap = 0
ElseIf y Mod 4 = 0 Then
leap = 1
End If
If (leap) Then
Print year1; "是闰年"
Else
Print year1; "不是闰年"
End If
End Sub
2.输入数N,给出素数的判断
If y Mod 400 = 0 Then leap = 1 Else leap = 0
ElseIf year1 Mod 4 = 0 Then
leap = 1
End If
If (leap) Then
Print year1; "是闰年"
Else
Print year1; "不是闰年"
End If
请思考:1各题中子过程与函数过程分别怎么写.2返回结果,子过程返回和主过程返回的区别
Print n; "是素数"
Else
Print n; "不是素数"
End If
End Sub
Private Sub Form_Click()
n = Val(InputBox(""))
If Not k(n) Then
Print n; "是素数"
Else
Print n; "不是素数"
End If
End Sub
单过程程序:
子过程程序:主过程输入、主过程输出结果
Private Sub Form_Click()
Dim k As Boolean
n = Val(InputBox(""))
For i = 2 To n - 1
If (n Mod i) = 0 Then k = True
Next i
If Not k Then
For i = 2 To m - 1
If (m Mod i) = 0 Then GoTo NotNextM
Next i
Print m
NotNextM:
Next m
End Sub
Private Sub Form_Click()
Dim s&, n%, i%
s = 1
n = Val(InputBox(""))
c = Val(Text3.Text)
If a > b Then
If a > c Then Max = a Else Max = c
ElseIf b > c Then
Max = b
Else
Max = c
End If
Text4.Text = Max
End Sub
Private Sub Command1_Click()
Do
i = i + 1
s = s * i
LoopWhile i < n
Print n; "的阶乘是"; s
End Sub
注:2.1和3请自行组织子过程(函数)程序
4.输入2数,求最大公约数(最小公倍数=2数乘积/最大公约数)
单过程程序:
函数过程程序:主过程输入主过程输出结果
Private Sub Command1_Click()
End Sub
Private Sub Form_Click()
Dim year1%, leap!
year1 = Val(InputBox("now year=", "input", "2013"))
Call y1(year1)
End Sub
Sub y1(y%)
Dim leap%
If y Mod 100 = 0 Then
m1 = n1
n 1= r
r = m1 Mod n1
Wend
Print m1;n1;”的最大公约数是”;n1
End Function
5.比较3个数的最大值
单过程程序:
函数过程程序:主过程输入主过程输出结果
Private Sub Command1_Click()
a = Val(Text1.Text)
b = Val(Text2.Text)
ElseIf y > z Then
Max = y
Else
Max = z
End If
m1 = Max
End function
1.输入年份,给出闰年的判断
单过程程序:
子过程程序:主过程输入、子过程输出结果
Private Sub Form_Click()
Dim year1%, leap!
year1 = Val(InputBox("now year=", "input", "2013"))
If year1 Mod 100 = 0 Then
Dim m%, n%, r!
m = Val(Text1.Text)
n = Val(Text2.Text)
r = m Mod n
While r <> 0
m = n
n = r
r = m Mod n
Wend
Label1.Caption = Text1.Text & "和" _
& Text2.Text & "的最大公约数是" & n
End Sub
Private Sub Command1_Click()
Dim m%, n%, r!
m = Val(Text1.Text)
n = Val(Text2.Text)
Call GCD(m,n)
End Sub
Function GCD(m1,n1)
r = m1 Mod n1
While r <> 0
Function k(n1) As Boolean
For i = 2 To n1 - 1
If (n1 Mod i) = 0 Then k = True
Next i
End Function
2.1求100以内素数
3.求数N的阶乘
Private Sub Form_Click()
For m = 2 To 100
a = Val(Text1.Text)
b = Val(Text2.Text)
c = Val(Text3.Text)
print “最大值是”; m1(a,b,c)
End Sub
Function m1(x,y,z)
If x > y Then
If x > z Then Max = x Else Max = z
ElseIf y Mod 4 = 0 Then
leap = 1
End If
If (leap) Then
Print year1; "是闰年"
Else
Print year1; "不是闰年"
End If
End Sub
2.输入数N,给出素数的判断
If y Mod 400 = 0 Then leap = 1 Else leap = 0
ElseIf year1 Mod 4 = 0 Then
leap = 1
End If
If (leap) Then
Print year1; "是闰年"
Else
Print year1; "不是闰年"
End If
请思考:1各题中子过程与函数过程分别怎么写.2返回结果,子过程返回和主过程返回的区别
Print n; "是素数"
Else
Print n; "不是素数"
End If
End Sub
Private Sub Form_Click()
n = Val(InputBox(""))
If Not k(n) Then
Print n; "是素数"
Else
Print n; "不是素数"
End If
End Sub
单过程程序:
子过程程序:主过程输入、主过程输出结果
Private Sub Form_Click()
Dim k As Boolean
n = Val(InputBox(""))
For i = 2 To n - 1
If (n Mod i) = 0 Then k = True
Next i
If Not k Then
For i = 2 To m - 1
If (m Mod i) = 0 Then GoTo NotNextM
Next i
Print m
NotNextM:
Next m
End Sub
Private Sub Form_Click()
Dim s&, n%, i%
s = 1
n = Val(InputBox(""))
c = Val(Text3.Text)
If a > b Then
If a > c Then Max = a Else Max = c
ElseIf b > c Then
Max = b
Else
Max = c
End If
Text4.Text = Max
End Sub
Private Sub Command1_Click()
Do
i = i + 1
s = s * i
LoopWhile i < n
Print n; "的阶乘是"; s
End Sub
注:2.1和3请自行组织子过程(函数)程序
4.输入2数,求最大公约数(最小公倍数=2数乘积/最大公约数)
单过程程序:
函数过程程序:主过程输入主过程输出结果
Private Sub Command1_Click()
End Sub
Private Sub Form_Click()
Dim year1%, leap!
year1 = Val(InputBox("now year=", "input", "2013"))
Call y1(year1)
End Sub
Sub y1(y%)
Dim leap%
If y Mod 100 = 0 Then
m1 = n1
n 1= r
r = m1 Mod n1
Wend
Print m1;n1;”的最大公约数是”;n1
End Function
5.比较3个数的最大值
单过程程序:
函数过程程序:主过程输入主过程输出结果
Private Sub Command1_Click()
a = Val(Text1.Text)
b = Val(Text2.Text)
ElseIf y > z Then
Max = y
Else
Max = z
End If
m1 = Max
End function
1.输入年份,给出闰年的判断
单过程程序:
子过程程序:主过程输入、子过程输出结果
Private Sub Form_Click()
Dim year1%, leap!
year1 = Val(InputBox("now year=", "input", "2013"))
If year1 Mod 100 = 0 Then
Dim m%, n%, r!
m = Val(Text1.Text)
n = Val(Text2.Text)
r = m Mod n
While r <> 0
m = n
n = r
r = m Mod n
Wend
Label1.Caption = Text1.Text & "和" _
& Text2.Text & "的最大公约数是" & n
End Sub
Private Sub Command1_Click()
Dim m%, n%, r!
m = Val(Text1.Text)
n = Val(Text2.Text)
Call GCD(m,n)
End Sub
Function GCD(m1,n1)
r = m1 Mod n1
While r <> 0
Function k(n1) As Boolean
For i = 2 To n1 - 1
If (n1 Mod i) = 0 Then k = True
Next i
End Function
2.1求100以内素数
3.求数N的阶乘
Private Sub Form_Click()
For m = 2 To 100
a = Val(Text1.Text)
b = Val(Text2.Text)
c = Val(Text3.Text)
print “最大值是”; m1(a,b,c)
End Sub
Function m1(x,y,z)
If x > y Then
If x > z Then Max = x Else Max = z