VB程序编码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

VB程序编码
1、创建一个窗体,在窗体上添加两个文本框和一个命令按钮,标
题为“交换”,程序运行时用户在两个文本框中本别输入数据,然后点击交换按钮,交换两个文本框中的数据
Private sub command1_click()
C=text1.text
Text1.text=text2.text
Text2.text=c
End sub
2、1、创建一个窗体,输入学号、姓名、年龄、籍贯;然后通过标
签显示用户输入的信息。

通过此程序掌握调试技巧。

如下图所示:Private sub command1_click()
Label5.caption=label1.caption&”:”&text1.text&_
Label2.caption&”:”text2.text&_
Label3,caption&”:”&text3.text&_
End sub
3、输入二次方程的三个系数,然后计算二次方程的根。

平方根函数为sqr()
Dim a as integer
Dim b as integer
Dim c as integer
a = val (text1.text)
b = val (text1.text)
c = val (text1.text)
dim x1 as singel
dim x2 as singel
x1 = (-b + sqr(b*b-4*a*c))/(2*a)
x2 = (-b -sqr(b*b-4*a*c))/(2*a)
label5.caption=”x1=” & x1 & vbcrlf & ”x2=”& x2 4、输入三角形边三个边长,
计算三角形面积。

面积公式:△=,
其中s=(a+b+c)/2
Dim a as integer
Dim b as integer
Dim c as integer
a = val (text1.text)
b = val (text1.text)
c = val (text1.text)
s = (a+b+c)/2
area = sqr(s*(s-a)*(s-b)*(s-c))
label5.caption = “面积:“&area
s(s-a)(s-b)(s-c)End up
方法二
Dim a as integer
Dim b as integer
Dim c as integer
a = val (text1.text)
b = val (text1.text)
c = val (text1.text)
if a +b < c or a+c< b or b+c<="">
s = (a+b+c)/2
area = sqr(s*(s-a)*(s-b)*(s-c))
else print area
s(s-a)(s-b)(s-c)5、输入三个数,由小到大输出
Dim a as Singel
a= val(text1.text)
b= val(text2.text)
c= val(text3.text)
If a
t = a :a = b :b =t
end if
if a < c then
t = a :a = c : c=t
end if
if b <="">
t = b:b=c:c=t
print a ; b;c
end sub
6,输入一个数,判断奇偶性
Private sub command1_click()
Dim a as integer
A = val (text1.text )
If a mod 2= 0 then label 1.caption = “偶数”Else label1.caption =”奇数”
方法二
Private sub command1_click()
Dim a as integer
A = val (text1.text )
If a mod 2= 0 then
S = “偶数”
Else
s= “奇数“
end if
label1.capion=s
end sub
6, 求解一元二次方程Dim a as integer Dim b as integer Dim c as integer
a = val (text1.text)
b = val (text1.text)
c = val (text1.text)
d = b ^2 – 4*a*c
if a <>0 then
if d > 0 then
x1 = (-b)/(2*a)
label1.capyion=”x1”&x1”:x2”&x2
end if
else
label1.capyion = “无实根”
end if
else
end if
end sub
6输入百分制成绩,要求输出成绩等级'A','B','C','D','E'。

90分以上为'A',80~89为'B',70~79为'C',60~69为'D',60分以下为'E' Private sub command 1 _click
a= val (text1.text)
select case a
case is > = 85
label1.caption = “优秀”
case is > = 75
label1.caption = “良好”
case is > = 60
label1.caption = “及格”
case is > = 0
label1.caption = “不及格”
end select
end sub
7、以下每周工作安排
星期一、三:讲计算机课
星期二、四:讲程序设计课
星期五:进修英语
星期六:政治学习
星期日:休息
编写程序,对上述工作日程进行检索,要求输入一周的某一天,程序将输出这一天的安排。

输入0-6分别代表星期日到星期六,输入0-6之外的数则结束程序。

Private sub command 1 _click
a= val (text1.text)
if a = 0 then
label1.caption =”休息”
end if
else
if a = 1 or a =3 then
label1.caption = “计算机课程”elseif a =2 or a =4 then
label1.caption =”程序设计”elseif a = 5 then
label1.caption =”英语”
elseif a =6 then
label1.caption =“政治“
else
end if
end sub
方法二
Private sub command 1 _click
a= val (text1.text)
if a = 0 then
label1.caption =”休息”
else
if a = 1 or a =3 then
label1.caption = “计算机课程”else
if a =2 or a =4 then
label1.caption =”程序设计”else
if a = 5 then
label1.caption =”英语”
else
if a =6 then
label1.caption =“政治“
else
end
endif
endif
endif
endif
endif
endif
方法三
Private sub command 1 _click a= val (text1.text)
select case a
case 0
label1.caption =”休息”
case 1,3
label1.caption = “计算机课程”
case 2,4
label1.caption =”程序设计”
case5
label1.caption =”英语”
case 6
label1.caption =“政治“
case else
end
end select
end sub
7,输入一个年号,判断是否为闰年,(年号能被4整除,但不能被100整除,或者能被四百整除)
Private sub command 1 _click
a= val (text1.text)
if a mod 4 = 0 and a mod 100 <>0 or a mod 400 = 0 then else
end if
end sub
8、(选作)有四个塔,圆心分别为(2,2),(-2,2),(-2,-2),(2,-2),圆半径为1,这四个塔德高度为10m,塔以外无建筑物,进输入任一点的坐标,求该点的建筑高度(塔外的高度为0)
8.求解一元二次方程输入一个x值,输出相应y 值Y = -1(x<0) 0 (x= 0) 1 ( x> 0)
Private sub command 1 _click
x= val (text1.text)
if x < 0 then
label 1.caption = “y= -1”else
if x = 0 then
label 1.caption = “y= 0”else
if x > 0 then
label 1.cap tion = “y= 1”else
end
end if
end if
end if
end sub
循环结构
Private sub command 1 _click s= 0
for I = 1 to 100
s = s + i累加器/s*i累乘器nexti
print s
end sub
求阶乘
Private sub command 1 _click n= val (text1.text)
s =1
for i= 1 to n
s = s *i
next i
print s
end sub
求1到n之间所有奇数的和,步长为2 Private sub command 1 _click
n= val (text1.text)
s = 0
for I = 1 to n
s = s + i
next i
print s
end sub
9、输入整数M、N,计算M、N
之间所有奇数的和。

Private sub command 1 _click
m= val (text1.text)
n= val (text1.text)
if m mod 2 = 0 then m = m+ 1
s = 0
for I = m to n step 2
s = s + i
next i
print s
end sub
10、计算M、N之间能被5或7整
除的所有数的和
Private sub command 1 _click
m= val (text1.text)
n= val (text1.text)
s = 0
for i = m to n
if i mod 5 = 0 or I mod 7 = 0 then s = s + i
end if
next i
print s
end sub
11,判断一个数是否为素数Private sub command 1 _click N= val (text1.text)
For i = 2 to n – 1
If n mod I = 0 then
Print “不是素数”
Exit for
End if
Next
If i = 0 then
Print “是素数”
End if
End sub。

相关文档
最新文档