http://210.27.228.219/ComputerManage/PhyItem/phytwo.htm
大学计算机实验 (VB语言) |
写复杂点的程序,就会觉得单个变量不好使用,用保存的数据太多了。还好VB.NET给我们提供了几种高级一点的用来存储数据的结构:数组,枚举与结构体
1.数组,多个数据类型相同的数据的集合,顺序排放。
代码'5-1.vbClass SimpleCnl
Public Shared Sub Main()
Dim i As Integer
Dim allowedExtensions() As String = {".jpg", "bmp", "gif"}
Dim fileExtension As String
Dim isAllowed As Boolean = False
fileExtension = System.Console.ReadLine()
For i = 0 To allowedExtensions.Length-1
If allowedExtensions(i) = fileExtension.ToLower Then
isAllowed = True
Exit For
End If
Next
If isAllowed Then
System.Console.WriteLine("允许上传")
Else
System.Console.WriteLine("文件类型不允许")
End If
End Sub
End Class
二维数组要复杂,但我们要搞明白的还是怎样定义,怎样初始化,怎样访问。有个问题要记住的Dim a(9),数组a的长度是10
2.枚举
枚举规定了数据的取值范围,用更有意义的名称来代表一个值
代码'5-2.vbClass SimpleCnl
Public Enum CRUD
Create=100
Retrive
Update
Delete
End Enum
Public Shared Sub Main()
Dim commandName As CRUD
System.Console.WriteLine("选择要执行的操作CRUB:Create,Retrive,Update,Delete")
commandName = System.Enum.Parse(GetType(CRUD), System.Console.ReadLine())
Select Case commandName
Case CRUD.Create
System.Console.WriteLine("增加记录")
Case CRUD.Retrive
System.Console.WriteLine("检索记录")
Case CRUD.Update
System.Console.WriteLine("更新记录")
Case CRUD.Delete
System.Console.WriteLine("删除记录")
End Select
End Sub
End Class
3.结构体,多个变量的组合体,用来共同描述某一事物整体
代码
'5-3.vb 多项式相加Class SimpleCnl
Public Structure Term
Dim coef As Integer '系数
Dim exp As Integer '指数
Sub New(ByVal c As Integer, ByVal e As Integer)
coef = c
exp = e
End Sub
End Structure
Public Shared Sub Main()
Dim polynomialA() As Term = {New Term(1, 0), New Term(1, 1), New Term(1, 2)}
Dim polynomialB() As Term = {New Term(1, 1),New Term(1,4)}
Dim polynomialC(polynomialA.Length + polynomialB.Length) As Term
Dim i, j, k As Integer
i = 0 : j = 0 : k = 0
Do While i < polynomialA.Length And j < polynomialB.Length
If polynomialA(i).exp < polynomialB(j).exp Then
polynomialC(k).exp = polynomialA(i).exp
polynomialC(k).coef = polynomialA(i).coef
i += 1
ElseIf polynomialA(i).exp > polynomialB(j).exp Then
polynomialC(k).exp = polynomialB(j).exp
polynomialC(k).coef = polynomialB(j).coef
j += 1
Else
polynomialC(k).exp = polynomialA(i).exp
polynomialC(k).coef = polynomialA(i).coef + polynomialB(j).coef
i += 1
j += 1
End If
k += 1
Loop
If i = polynomialA.Length Then
Do While j < polynomialB.Length
polynomialC(k).exp = polynomialB(j).exp
polynomialC(k).coef = polynomialB(j).coef
j += 1
k += 1
Loop
Else
Do While i < polynomialA.Length
polynomialC(k).exp = polynomialA(i).exp
polynomialC(k).coef = polynomialA(i).coef
i += 1
k += 1
Loop
End If
ReDim Preserve polynomialC(k - 1) '输出相加后得到的多项式
k = 0
Do
System.Console.Write("{0}x^{1} + ", polynomialC(k).coef, polynomialC(k).exp)
k += 1
Loop Until k = polynomialC.Length - 1
System.Console.Write("{0}x^{1} ", polynomialC(k).coef, polynomialC(k).exp)
End Sub
End Class
VB6结构体
Option Explicit
Private Type lk '定义结构体 这个要写到外面
x As Long
y As Long
z As Long
End Type
Sub aldkjf()
Dim p() As lk '定义动态数组
Dim a As Long, b As Long, c As Long, d As Long
For a = 2 To 98 'a循环从2到98
For b = 2 To 98 'b循环从2到98
c = a * b
If c > 40 Then '当c大于40时,给动态数组赋值
ReDim Preserve p(d)
p(d).x = a
p(d).y = b
p(d).z = c
MsgBox p(d).x & p(d).y & p(d).z '显示动态数组中的3个元素
d = d + 1
End If '记得加 END IF
Next
Next
End Sub
Private Sub Command1_Click()'需要一个事件来触发一下
Call aldkjf
End Sub
这三种结构,实际使用的都很多,要想用的好,就得多练。
数组与结构体的区别
他们都是数据结构,不过数组只能存放同样类型的数据,而结构体则能够存放不同类型的数据,结构体里面也可以有结构体。
数组是一组同类型的数据集合,形象简单点你可以认为是数学的数列;
而结构体是一类特殊的数据类型,由自己定义,比如说我们做一个钟的类,我们定义钟的数据结构,就可以用结构体定义三个整型变量(分、时、秒)作为钟的数据结构,三个整型变量作为一个结构体。
数组是线性的 只能储存一种类型的数据
如 int a[10]
结构体能储存多种类型的数据 如
struct tree{
int i;
char a[10];
double t;
}a;