流程图略
VERSION 5.00
Begin VB.Form mainForm Caption = "教师点名系统"ClientHeight = 5115ClientLeft = 165ClientTop = 810ClientWidth = 9660LinkTopic = "Form1"ScaleHeight = 5115ScaleWidth = 9660StartUpPosition = 3 '窗口缺省Begin VB.ListBox List1 Height = 4380Left = 120TabIndex = 0Top = 480Width = 2295EndBegin VB.Label Label2 Caption = "显示结果处"Height = 255Left = 2640TabIndex = 2Top = 1800Width = 975EndBegin VB.Label Label1 AutoSize = -1 'TrueCaption = "|"BeginProperty Font Name = "宋体"Size = 24Charset = 134Weight = 700Underline = 0 'FalseItalic = 0 'FalseStrikethrough = 0 'FalseEndPropertyHeight = 495Left = 3720TabIndex = 1Top = 1560Width = 255EndBegin VB.Menu BJXSSJ Caption = "编辑学生数据"Begin VB.Menu TJ Caption = "添加一名新同学"EndBegin VB.Menu SC Caption = "删除一名新同学"EndEndBegin VB.Menu KSDM Caption = "开始点名"Begin VB.Menu DM Caption = "点名"EndEndBegin VB.Menu CXXSDMQK Caption = "查询学生点名情况"EndBegin VB.Menu TC Caption = "退出"End
End
Attribute VB_Name = "mainForm"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim XH(100) As String '学号
Attribute XH.VB_VarUserMemId = 1073938432
Dim XM(100) As String '姓名
Attribute XM.VB_VarUserMemId = 1073938433
Dim CS(100) As String '次数
Attribute CS.VB_VarUserMemId = 1073938434
Dim cnt As Integer
Dim FileName As String
Private Sub ReadFile() '导入文件
'On Error GoTo errorHandler ' 设置错误处理Dim strLine As StringDim tmpOpen FileName For Input As #1cnt = 0Do While Not EOF(1) ' 逐行读取文件内容Line Input #1, strLinetmp = Split(strLine, vbTab)XH(cnt) = tmp(0)XM(cnt) = tmp(1)CS(cnt) = tmp(2)cnt = cnt + 1LoopClose #1 '关闭文件Exit Sub ' 退出子过程
errorHandler: ' 错误处理MsgBox FileName & " 打开文件失败,错误代码为:" & Err.Number & ",错误信息为:" & Err.Description
End SubPrivate Sub WriteFile()
'On Error GoTo errorHandler ' 设置错误处理Dim strLine As StringDim tmpDim i As IntegerOpen FileName For Output As #1For i = 0 To cnt - 1strLine = XH(i) + vbTabstrLine = strLine + XM(i) + vbTabstrLine = strLine + CS(i)Print #1, strLine '记录进文件NextClose #1 '关闭文件Exit Sub ' 退出子过程
errorHandler: ' 错误处理MsgBox FileName & " 打开文件失败,错误代码为:" & Err.Number & ",错误信息为:" & Err.Description
End Sub
Private Sub infoToList()Dim i As IntegerDim strLine As StringList1.ClearFor i = 0 To cnt - 1strLine = XH(i) + vbTabstrLine = strLine + XM(i)List1.AddItem (strLine)NextEnd SubPrivate Sub CXXSDMQK_Click()Dim i As IntegerDim strLine As StringList1.ClearFor i = 0 To cnt - 1strLine = XH(i) + vbTabstrLine = strLine + XM(i) + vbTabstrLine = strLine + CS(i)List1.AddItem (strLine)Next
End SubPrivate Sub DM_Click()Dim randomNumber As IntegerrandomNumber = Int(Rnd * cnt)Label1 = XH(randomNumber) + " " + XM(randomNumber)CS(randomNumber) = CS(randomNumber) + 1WriteFile '更新文件ReadFileEnd SubPrivate Sub Form_Load()Randomize ' 初始化随机数种子,一般只需要调用一次FileName = App.Path & "\点名册.txt"ReadFileinfoToList
End SubPrivate Sub Form_Unload(Cancel As Integer)End '一定要加这句,因为原来的窗体只是隐藏了,并没退出
End SubPrivate Sub SC_Click()Dim XXH As String '学号Dim Find As BooleanFind = FalseXXH = InputBox("请输入要删除的学号", "删除")If XXH = "" Then Exit Sub'添加之前查一下学号是否存在,如果存在不能加入Dim i As IntegerFor i = 0 To cnt - 1If XH(i) = XXH ThenFind = TrueExit ForEnd IfNextIf Find = False ThenMsgBox "此学号不存在", , "提示"Exit SubEnd If'用最后一条记录替换当前记录XH(i) = XH(cnt - 1)XM(i) = XM(cnt - 1)CS(i) = CS(cnt - 1)cnt = cnt - 1WriteFileReadFileinfoToList '更新列表End SubPrivate Sub TC_Click()End
End SubPrivate Sub TJ_Click()Dim XXH, XXM As String '学号 ,姓名XXH = InputBox("请输入要加入的学号", "添加")If XXH = "" Then Exit Sub'添加之前查一下学号是否存在,如果存在不能加入Dim i As IntegerFor i = 0 To cnt - 1If XH(i) = XXH ThenMsgBox "此学号已经存在不可以添加", , "提示"Exit SubEnd IfNextXXM = InputBox("请输入要加入的姓名")If XXM = "" Then Exit SubXH(cnt) = XXHXM(cnt) = XXMCS(cnt) = 0cnt = cnt + 1WriteFileReadFileinfoToList '更新列表End Sub
在这个课程设计过程中,我学到了很多关于编程的知识和技巧。通过实际的编程实践,我更深入地理解了程序的结构和逻辑,以及如何使用变量、数组、文件操作等基本概念和功能来实现所需的功能。我也更加熟悉了VB的语法和编程规范。
这个程序的优点是:
简单易懂:程序的逻辑结构清晰,代码注释完善,易于理解和修改。
功能完善:程序实现了学生信息的增加、删除、修改和随机点名等功能,可以方便地管理学生信息。
文件操作:通过文件操作,程序可以将学生信息保存到文件中,并在需要时读取和更新文件,确保数据的持久性和可靠性。
然而,这个程序也存在一些缺憾:
用户界面简陋:程序的用户界面比较简单,没有美化和交互性,用户体验有待提升。
错误处理不完善:程序中使用了错误处理,但只是简单地弹出一个消息框提示错误信息,没有提供更详细的错误处理和恢复机制。
数组大小固定:程序中定义了固定大小的数组来存储学生信息,如果学生数量超过数组大小,会导致数组溢出的问题。应该考虑使用动态数据结构来解决这个问题。
Option Explicit Dim XH(100) As String '学号 Dim XM(100) As String '姓名 Dim CS(100) As String '次数 Dim cnt As Integer Dim FileName As String Private Sub ReadFile() '导入文件 'On Error GoTo errorHandler ' 设置错误处理Dim strLine As StringDim tmpOpen FileName For Input As #1cnt = 0Do While Not EOF(1) ' 逐行读取文件内容Line Input #1, strLinetmp = Split(strLine, vbTab)XH(cnt) = tmp(0)XM(cnt) = tmp(1)CS(cnt) = tmp(2)cnt = cnt + 1LoopClose #1 '关闭文件Exit Sub ' 退出子过程 errorHandler: ' 错误处理MsgBox FileName & " 打开文件失败,错误代码为:" & Err.Number & ",错误信息为:" & Err.Description End SubPrivate Sub WriteFile() 'On Error GoTo errorHandler ' 设置错误处理Dim strLine As StringDim tmpDim i As IntegerOpen FileName For Output As #1For i = 0 To cnt - 1strLine = XH(i) + vbTabstrLine = strLine + XM(i) + vbTabstrLine = strLine + CS(i)Print #1, strLine '记录进文件NextClose #1 '关闭文件Exit Sub ' 退出子过程 errorHandler: ' 错误处理MsgBox FileName & " 打开文件失败,错误代码为:" & Err.Number & ",错误信息为:" & Err.Description End Sub Private Sub infoToList()Dim i As IntegerDim strLine As StringList1.ClearFor i = 0 To cnt - 1strLine = XH(i) + vbTabstrLine = strLine + XM(i)List1.AddItem (strLine)NextEnd SubPrivate Sub CXXSDMQK_Click()Dim i As IntegerDim strLine As StringList1.ClearFor i = 0 To cnt - 1strLine = XH(i) + vbTabstrLine = strLine + XM(i) + vbTabstrLine = strLine + CS(i)List1.AddItem (strLine)Next End SubPrivate Sub DM_Click()Dim randomNumber As IntegerrandomNumber = Int(Rnd * cnt)Label1 = XH(randomNumber) + " " + XM(randomNumber)CS(randomNumber) = CS(randomNumber) + 1WriteFile '更新文件ReadFileEnd SubPrivate Sub Form_Load()Randomize ' 初始化随机数种子,一般只需要调用一次FileName = App.Path & "\点名册.txt"ReadFileinfoToList End SubPrivate Sub Form_Unload(Cancel As Integer)End '一定要加这句,因为原来的窗体只是隐藏了,并没退出 End SubPrivate Sub SC_Click()Dim XXH As String '学号Dim Find As BooleanFind = FalseXXH = InputBox("请输入要删除的学号", "删除")If XXH = "" Then Exit Sub'添加之前查一下学号是否存在,如果存在不能加入Dim i As IntegerFor i = 0 To cnt - 1If XH(i) = XXH ThenFind = TrueExit ForEnd IfNextIf Find = False ThenMsgBox "此学号不存在", , "提示"Exit SubEnd If'用最后一条记录替换当前记录XH(i) = XH(cnt - 1)XM(i) = XM(cnt - 1)CS(i) = CS(cnt - 1)cnt = cnt - 1WriteFileReadFileinfoToList '更新列表End SubPrivate Sub TC_Click()End End SubPrivate Sub TJ_Click()Dim XXH, XXM As String '学号 ,姓名XXH = InputBox("请输入要加入的学号", "添加")If XXH = "" Then Exit Sub'添加之前查一下学号是否存在,如果存在不能加入Dim i As IntegerFor i = 0 To cnt - 1If XH(i) = XXH ThenMsgBox "此学号已经存在不可以添加", , "提示"Exit SubEnd IfNextXXM = InputBox("请输入要加入的姓名")If XXM = "" Then Exit SubXH(cnt) = XXHXM(cnt) = XXMCS(cnt) = 0cnt = cnt + 1WriteFileReadFileinfoToList '更新列表End Sub