1、第 1 页 共 12 页一、实验目的、目标综合运用本课程的相关知识,完成两位数四则运算器的设计、编写与测试,培养学生的实践动手能力,并检测本课程的学习效果。二、实验内容基本要求: 两位数(指 10-99 的自然数)的加减乘除(除法指的是整除)运算,其中减法和除法必须满足“ 被操作数不小于操作数” (即可以不考虑 20-56、420等情况) 两个操作数、运算符均随机产生 完成一题后自动判断对错,并显示出来 可以随时查看已经完成的题目情况 完成一题自动计算题目总数、正确数、错误数和正确率,并实时显示出来扩展要求(不硬性规定): 在答错的题目后面给出正确答案 可增加时钟控件,10 秒(或 15 秒)
2、内必须完成一题 限制题目数目为固定值(如 20 题、50 题) 把程序扩展成为多位数甚至任意位数的运算器三、实验过程与步骤第 2 页 共 12 页1、在 Visual Basic 系统环境下,打开“工程设计” 窗口。2、新建两个窗口 Form1 和 Form2,添加控件并 进行控件的设置, Form1 作用是进行题目数量和答题时间的选择并将数据进行保存以供 Form2 调用,Form2 的作用是进行随机两位数(指 10-99 的自然数)的加减乘除(除法指的是整除)运算,完成一题后自动判断对错,给出错误题目的正确答案,并显示出来,添加时间钟控,显示计算题目总数、正确数、错误数和正确率。第 3 页
3、 共 12 页3、在 Form1 窗口,双 击控件打开代码窗口,设计窗体及控件的事件代码。采用单选的形式选择题目数量和答题时间,当点击确定按钮时,调用 Form2,当点击退出时,退出整个程序。Public tishu As Integer, shijian As IntegerPrivate Sub Form_Load()Text1.Visible = FalseText2.Visible = FalsePicture1.Height = Form1.HeightPicture1.Width = Form1.WidthForm1.ShowMsgBox “请选择 题量 和 答题时间 !“, 48
4、, “提示“Form2.HideUnload Form2End SubPrivate Sub Option1_Click(Index As Integer)tishu = Val(Option1(Index).Caption)Text1.Text = tishu第 4 页 共 12 页End SubPrivate Sub Option2_Click(Index As Integer)shijian = Val(Option2(Index).Caption)Text2.Text = shijianEnd SubPrivate Sub Command1_Click()MsgBox “请直接在文本框
5、中输入数字作答(其中除法 为整除) ,“ & vbLf & “回车完成一题,系统自动生成下一题“, , “ 提示“Form2.ShowForm1.HideEnd SubPrivate Sub Command2_Click()EndEnd Sub4、在 Form2 窗口,双 击控件打开代码窗口,设计窗体及控件的事件代码。(1)定义变量Dim a() As IntegerDim b() As IntegerDim c() As IntegerDim result() As DoubleDim cou As IntegerDim n As StringDim X As StringDim i As
6、IntegerDim p As IntegerDim sum As IntegerDim right As IntegerDim wrong As IntegerDim w As DoubleDim ww As StringDim S As IntegerDim d As Integer第 5 页 共 12 页(2)两位随机数(指 10-99 的自然数)的加减乘除(除法指的是整除)运算Private Sub product()ReDim Preserve a(cou)ReDim Preserve b(cou)ReDim Preserve c(cou)ReDim Preserve result(
7、cou)a(cou) = 10 + Int(Rnd * 90)b(cou) = 1 + Int(Rnd * 4)c(cou) = 10 + Int(Rnd * 90)Select Case b(cou)Case 1Label1.Caption = CStr(a(cou)Label3.Caption = CStr(c(cou)result(cou) = a(cou) + c(cou)Label2.Caption = “+“Case 2If Val(CStr(a(cou) Val(CStr(c(cou) ThenLabel1.Caption = CStr(a(cou)Label3.Caption
8、= CStr(c(cou)result(cou) = a(cou) - c(cou)ElseLabel1.Caption = CStr(c(cou)Label3.Caption = CStr(a(cou)result(cou) = c(cou) - a(cou)End IfLabel2.Caption = “-“Case 3Label1.Caption = CStr(a(cou)Label3.Caption = CStr(c(cou)result(cou) = a(cou) * c(cou)Label2.Caption = “*“Case 4If Val(CStr(a(cou) Val(CSt
9、r(c(cou) ThenLabel1.Caption = CStr(a(cou)Label3.Caption = CStr(c(cou)result(cou) = Int(a(cou) / c(cou)ElseLabel1.Caption = CStr(c(cou)第 6 页 共 12 页Label3.Caption = CStr(a(cou)result(cou) = Int(c(cou) / a(cou)End IfLabel2.Caption = “/“End SelectEnd Sub(3)设定 Form2 加载时的相关数据Private Sub Form_Load()Picture
10、1.Height = Form2.HeightPicture1.Width = Form2.WidthRandomizeCall productS = Val(Form1.Text2.Text)Label9.Caption = “剩余时间“ & Space(2) & S & Space(1) & “S“Label10.Caption = “总题数:“ & Space(2) & Val(Form1.Text1.Text)sum = 0right = 0wrong = 0w = 0End Sub(4)通过一个钟控和一个文本框的回车键方法实现两位随机数(指 10-99 的自然数)的加减乘除(除法指的
11、是整除)运算的判断,给出错误答案并将式子显示出来,统计正确题数和错误题数Private Sub Timer1_Timer()If p = 0 Theni = i + 1p = S - iLabel9.Caption = “剩余时间“ & Space(2) & p & Space(1) & “S“End IfIf p = 0 Thenwrong = wrong + 1Label7.Caption = “错误题数:“ & Space(2) & wrongn = “X = result(cou)List1.AddItem (Label1.Caption & Label2.Caption & Labe
12、l3.Caption & 第 7 页 共 12 页Label4.Caption & Text1.Text & Chr(32) & CStr(n) & Space(3) & “答案是“ & CStr(X)i = -1Call productEnd IfIf sum = Val(Form1.Text1.Text) ThenMsgBox “共答“ & Val(Form1.Text1.Text) & “题,“ & “正确“ & right & “题,“ & “错误“ & wrong & “题,“ & “正确率为“ & ww, , “成绩“Unload Form2Form1.ShowLoad Form1
13、MsgBox “请 重新选择 题量和答题时间 或者 退出系统“, 48, “提示“End IfEnd SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)If KeyAscii = 13 Theni = -1If Text1.Text = “ Or IsNumeric(Text1.Text) = False ThenMsgBox “请输入答案!“, 48, “提示“ElseIf Val(Text1.Text) 0 Thenw = right / sumIf w = 0 Thenww = wLabel8.Caption = “正确率:“ & Spa
14、ce(2) & wwElseww = Format(w, “#.#“) * 100 & “%“Label8.Caption = “正确率:“ & Space(2) & wwEnd IfEnd IfEnd Sub(6)利用两个按键控件和一个图形显示控件分别实现重新开始、退出系统、识别等功能Private Sub Command3_Click()Unload Form2Form1.ShowLoad Form1MsgBox “请 重新选择 题量和答题时间 或者 退出系统“, 48, “提示“End SubPrivate Sub Command1_Click()EndEnd SubPrivate Su
15、b Picture2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)MsgBox “版 权所有,请勿模仿“, 48, “提示“End Sub第 9 页 共 12 页四、测试数据及运行结果【输入的测试数据,输出的结果】第 10 页 共 12 页第 11 页 共 12 页第 12 页 共 12 页五、实验总结、 经验分享【简单叙述心得,指出本次上机实验的关键环节或关键点;若经多次失败最终成功,请阐述失败之处和导致失败的原因,并简单给出你是如何一步步成功(或经验分享);若本次上机实验最终没有成功,则阐述成败之处及其原因分析。无论何种情况, 请进行自我评价】