《VB程序设计基础,复习资料,例题分析.doc》由会员分享,可在线阅读,更多相关《VB程序设计基础,复习资料,例题分析.doc(22页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、VB程序设计基础复习资料1.数据类型类型关键字类型符号举例整型Integer%Dim x as IntegerDim x%x=123%长整型Long&Dim x as LongDim x&x=123&单精度型Single!Dim x as SingleDim x!x=12.3!双精度型Double#Dim x as DoubleDim x#x=12.345$逻辑型Boolean无Dim x as Booleanx=True字符型String$Dim x as StringDim x$x=”12345abcdefg”日期型Date无Dim x as Datex=#2012-5-29#2.常用函数
2、函数参数类型返回值说明Sin(x)X:数值型数值型求弧度X的正弦值X:弧度,如果求30度角的正弦值,需要进行角度和弧度的转换,即Sin(30 * 3.1.416 / 180)Cos(x)X:数值型数值型求弧度X的余弦值Tan(x)X:数值型数值型求弧度X的正切值Atan(x)X:数值型数值型求弧度X的反正切值Abs(x)X:数值型数值型求绝对值Exp(x)X:数值型数值型返回e的指定次幂,即exLog(x)X:数值型数值型返回x的自然对数Sgn(x)X:数值型数值型符号函数,正数返回1,负数返回-1,0则返回0Sqr(x)X:数值型数值型返回x的平方根Int(x)X:数值型数值型返回不大于x的
3、最大整数Fix(x)X:数值型数值型返回x的整数部分Fix(-7.8)= -7Int(-7.8)= -8Rnd无数值型取随机数,得到a,b之间的随机数:R=Int(Rnd*(b-a+1)+aLtrim(x)X:字符型字符型去左边的空格Rtrim(x)X:字符型字符型去右边的空格Trim(x)X:字符型字符型去左右两边的空格Left(x,n)X:字符型N:数值型字符型对字符串X截取前N个字符Right(x,n)X:字符型N:数值型字符型对字符串X截取后N个字符Mid(x,m,n)X:字符型M:数值型N:数值型字符型从字符串X的M位开始截取N个字符Len(x)X:字符型数值型求字符串的长度LCas
4、e(x)X:字符型字符型返回小写字母UCase(x)X:字符型字符型返回大写字母Space(n)N:数值型字符型返回N个空格String(n,c)N:数值型C:字符型字符型返回N个字符C,即将C重复N次Instr(x,y)X:字符型Y:字符型数值型返回Y在X中首次出现的位置,0表示Y不在X中Str(x)X:数值型字符型把X转换为字符型,一般用法Trim(Str(x)Val(x)X:字符型数值型把X转换为数值,无法转换则返回0,如Val(“abcd”)的值为0Chr(x)X:数值型字符型X是某个字符的ASCII编码Asc(x)X:字符型数值型返回x的ASCII编码值Date无日期型返回当前日期T
5、ime无日期型返回当前时间Now无日期型返回当前日期和时间Year(d)D:日期型数值型返回日期D的 年 数值Month(d)D:日期型数值型返回日期D的 月 数值Day(d)D:日期型数值型返回日期D的 日 数值Hour(d)D:日期型数值型返回日期D的 时 数值Minute(d)D:日期型数值型返回日期D的 分 数值Second(d)D:日期型数值型返回日期D的 秒 数值IIf(x,y,z)X:逻辑型Y:任意型Z:任意型任意型当x为true时返回y当x为false时返回zInputBox(x)X:字符型字符型弹出输入框,返回输入的值参数x仅仅起到提示作用MsgBox(x)X:字符型弹出一个
6、消息对话框,参数X是消息的内容Print表达式(多个用逗号或分号隔开)屏幕输出函数,可以输出1个或多个变量的值3.程序控制结构结构类型结构体顺序结构从上到下,从左到右分支结构单路分支If then End If如果语句块只有一行,也可以写成:If then 二路分支If thenElse End If多路分支IFIf thenElseIf ElseIf Else End If多路分支SelectSelect Case Case Case Case ElseEnd SelectFor循环For = To Step 步长Next Do循环格式1:先判断条件,后执行循环体Do while/until
7、 Loop格式2:先执行循环体,后判断条件Do Loop while/until While循环While Wend相当于 Do while循环4.数组要求掌握一维数组,看懂二维数组数组必须先申明,然后才能使用Dim x(5) as Integer:申明X是整型的一维数组,最大下标为5,则数量为6,各个数组变量为:X(0)、X(1)、X(2)、X(3)、X(4)、X(5)数组的默认起始下标为0,如果要从1开始,有两种办法:第一, 在“通用对象申明部分”使用语句“Option Base 1”第二, 定义数组时候:Dim Dim x(1 to 5) as Integer二维数组的申明:Dim x(
8、2,2) as Integer:表示申明了一个33的二维数组,各个数组变量为:X(0,0)、X(0,1)、X(0,2)X(1,0)、X(1,1)、X(1,2)X(2,0)、X(2,1)、X(2,2)5.过程和函数过程Sub定义:private sub 过程名字(参数列表) End Sub 例如定义一个求阶层的过程:求X的阶乘,结果赋值给yPrivate sub factorial(x as Integer, y as Long)Y=1For i=x to 1 step -1Y=y * iNext iEnd Sub调用:Dim x As IntegerDim y As Longx = 5Call
9、 factorial(x, y)Print y函数Function,类似于Sub,但是可以有返回值定义:private function 函数名(参数列表) as 返回类型 End Function定义阶乘的函数:Private function factorial(x as Integer) as LongDim y as LongY=1For i=x to 1 step -1Y=y * iNext iFactorial = YEnd Function调用:Dim x As Integerx = 5Print factorial(x)参数传递按值传递ByVal创建新的内存变量,复制值按地址传
10、递ByRef(默认)共用相同的内存变量6.常用控件控件名常用属性常用事件FormCaptionBackcolorFont FontsizeFontBoldFontItalicFontUnderlineLoadClickUnloadLabelAutosizeCaptionWordwrapLeftTopWidthHeight TextText EnabledVisiblechangeOptionbuttonValue clickCheckbox valueclickListBoxListListcountListindexTextSortedSelected Multiselect Additem
11、RemoveitemClearClickDbclickcombobox与list一样styleTimerEnabledintervalTimer 7.练习题1、输入一个数,判断奇数还是偶数?Private Sub Form_Click()w = InputBox(请输入一个数)If w Mod 2 = 0 Then Print 偶数Else Print 奇数End IfEnd Sub2、输入一个数,判断是否是闰年Private Sub Form_Click()w = InputBox(请输入一个年份)If w Mod 4 = 0 And w Mod 100 0 Or w Mod 400 = 0
12、 Then Print 闰年Else Print 不是闰年End IfEnd Sub3、输入一个成绩,判断是否及格,分档次做Private Sub Form_Click()w = InputBox(请输入成绩)If w = 90 Then Print 优秀ElseIf w = 80 Then Print 良好ElseIf w = 70 Then Print 中等ElseIf w = 60 Then Print 及格Else Print 不及格End IfEnd Sub4、三个数比较大小Private Sub Form_Click()a = Val(InputBox(a=?)b = Val(In
13、putBox(b=?)c = Val(InputBox(c=?)If a b Then t = b b = a a = tEnd IfIf a c Then t = c c = a a = tEnd IfIf b c Then t = c c = b b = tEnd IfPrint 从小到大:; a; b; cEnd Sub5、输入一个年份,输出中文大写,即2010-二零一零年Private Sub Form_Click()x = InputBox(请输入一个年份)Y1 = Val(Mid(x, 1, 1)Y2 = Val(Mid(x, 2, 1)y3 = Val(Mid(x, 3, 1)y
14、4 = Val(Mid(x, 4, 1)ss = 零一二三四五六七八九Y1 = Mid(ss, Y1 + 1, 1)Y2 = Mid(ss, Y2 + 1, 1)y3 = Mid(ss, y3 + 1, 1)y4 = Mid(ss, y4 + 1, 1)Print Y1 & Y2 & y3 & y4End Sub6、输入三角形的三个边长,求面积。Private Sub Form_Click()a = Val(InputBox(输入三角形的一个边长)b = Val(InputBox(输入三角形的一个边长)c = Val(InputBox(输入三角形的一个边长)p = (a + b + c) /
15、2m = p * (p - a) * (p - b) * (p - c)s = Sqr(m)If a + b c And a + c b And b + c a And a 0 And b 0 And c 0 Then Print 三角形面积为: & sElse Print 输入的三边不能构成三角形End IfEnd Sub7、任意输入一个数,判断它是否是水仙花数。Private Sub Form_Click()w = InputBox(请输入一个三位数)a = Val(Mid(w, 1, 1)b = Val(Mid(w, 2, 1)c = Val(Mid(w, 3, 1)If Val(w)
16、= a 3 + b 3 + c 3 Then Print w & 是水仙花数Else Print w & 不是水仙花数End IfEnd Sub8、求1+2+3+.+N的和值,N从键盘输入Private Sub Form_Click()w = InputBox(请输入一个数)s = 0For i = 1 To w s = s + iNext iPrint sEnd Sub9、求A!的值,A值从键盘输入。Private Sub Form_Click()a = InputBox(请输入一个数)s = 1For i = a To 1 Step -1 s = s * iNext iPrint sEnd
17、 Sub10、输入10个整数,输入完毕后要做三件事情:A、输出这十个数的和。b、输出这十个数的平均值。c、输出其中奇数的个数,以及这些奇数,放在一个字符串中Private Sub Form_Click()s = 0c = d = 0For i = 1 To 10 a = Val(InputBox(输入第 & i & 个数) s = s + a If a Mod 2 0 Then d = d + 1 c = c & a & End IfNext iPrint 十个数的和; sPrint 平均; (s / 10)Print 奇数个数 & dPrint 奇数为 & cEnd Sub11、定义长度为1
18、0的一个数组,随机产生10个数据分别填入该数组。要求填入的数值不相等,打印出这10个数值。Private Sub Form_Click() Dim x(10) As Integer For i = 1 To 10 x(i) = Int(Rnd * 90) + 10 Next i For i = 1 To 10 Print x(i) Next iEnd Sub12、P86,实例5.1Private Sub Form_Click()Dim f(30) As Longf(1) = 1f(2) = 1For i = 3 To 30 f(i) = f(i - 1) + f(i - 2)Next iFor
19、 i = 1 To 30 Print f(i) If i Mod 5 = 0 Then PrintNext iEnd Sub13、投骰子游戏Private Sub Command1_Click()a = Int(Rnd * (6 - 1 + 1) + 1b = Int(Rnd * (6 - 1 + 1) + 1c = Int(Rnd * (6 - 1 + 1) + 1Text1.Text = aText2.Text = bText3.Text = cIf a = b = c Then Label2.Caption = 哈哈。好手气!End IfIf a = b Or b = c Or a =
20、c Then Label2.Caption = 呵呵。也不错阿!End IfIf a b And b c And a c Then Label2.Caption = 嘿嘿。下次再来!End IfEnd SubPrivate Sub Command2_Click()EndEnd SubPrivate Sub Form_Load()Label1.FontSize = 20Label2.FontSize = 20Label2.Caption = Text1.FontSize = 18Text2.FontSize = 18Text3.FontSize = 18End Sub14、找零钱Private
21、Sub Command1_Click()a = Val(Text1.Text)b = Val(Text2.Text)c = b - ad = c 50e = (c - 50 * d) 10f = (c - 50 * d - 10 * e) 5g = (c - 50 * d - 10 * e - 5 * f) 1Text3.Text = cText4.Text = dText5.Text = eText6.Text = fText7.Text = gEnd SubPrivate Sub Command2_Click()EndEnd Sub15、两个列表框数据迁移Private Sub Comma
22、nd1_Click()Dim i As IntegerFor i = List1.ListCount - 1 To 0 Step -1 If List1.Selected(i) Then List2.AddItem List1.List(i) List1.RemoveItem i End IfNext iEnd SubPrivate Sub Command2_Click()Dim i As IntegerFor i = List2.ListCount - 1 To 0 Step -1 If List2.Selected(i) Then List1.AddItem List2.List(i) L
23、ist2.RemoveItem i End IfNext iEnd SubPrivate Sub Form_Load()For i = 1 To 10 List1.AddItem Item & iNext iEnd Sub16、会移动的文字。文字在表单的范围内左右移动Dim fx As BooleanPrivate Sub Form_Load()Label1.FontSize = 20Label1.Left = 0fx = TrueTimer1.Enabled = TrueTimer1.Interval = 100End SubPrivate Sub Timer1_Timer()If fx T
24、hen Label1.Left = Label1.Left + 100Else Label1.Left = Label1.Left - 100End IfIf Label1.Left = Form4.Width - Label1.Width Then fx = FalseEnd IfEnd Sub17、百分之一秒计时器Dim fen As IntegerDim miao As IntegerDim haom As IntegerPrivate Sub Command1_Click()Timer1.Enabled = TrueEnd SubPrivate Sub Command2_Click()
25、Timer1.Enabled = FalseEnd SubPrivate Sub Command3_Click()Timer1.Enabled = Falsefen = 0miao = 0haom = 0Text1.Text = 00:00:00End SubPrivate Sub Form_Load()Text1.FontSize = 36Text1.Text = 00:00:00Timer1.Enabled = FalseTimer1.Interval = 10fen = 0miao = 0haom = 0End SubPrivate Sub Timer1_Timer()haom = haom + 1If haom = 100 Then haom = 0 miao = miao + 1 If miao = 60 Then miao = 0 fen = fen + 1 End IfEnd Ifa = IIf(haom 10, 0 & haom, & haom)b = IIf(miao 10, 0 & miao, & miao)c = IIf(fen 10, 0 & fen, & fen)Text1.Text = c & : & b & : & aEnd Sub
限制150内