vb2010实验报告-计算器的制作与实现

更新时间:2024-07-01 20:19:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

软件开发环境与工具

实验报告

实验一

实验题目: 计算器的制作与实现 实验类型: 验证 实验地点: 软件实验室一 指导教师: 专业班级: 计算机科学与技术系班 姓 名:

20年10月25日

一、实验目的:

1、熟悉VB.NET程序开发环境,了解VB.NET应用程序设计的基本框架结构。 2、掌握Windows Form的事件处理机制,以及如何在运行期创建控件。 3、初步掌握VB.NET的基本编程方法和技巧。 二、实验内容:

设计一个简单的计算器程序,使之能够实现加、减、乘、除等基本的四则运算。 三、实验设计思路:

先模拟地设计出计算器面板,包含各类按钮和结果输出框,然后写出各个触发事件,包含数字事件、运算符号事件、等号事件等,之后再写各类运算函数,包括加法、减法、乘法和除法,最后于主函数中调用各功能及运算。 四、实验步骤: 1、打开Microsoft Visual Studio 2010,点击“新建项目”,在弹出的对话框中选择“Visual Basic/ Windows”项目类型,“模板”选择“Windows窗体应用程序”,在名称处将默认名WindowsApplication1改为“简易计算器”,然后指定该应用程序的保存路径,点击“确定”,进入窗体设计界面。

2、调整好窗体的大小后,将鼠标移到左侧“工具箱”位置,自动弹出“工具箱”窗口,选择其中的“TextBox”控件对象并将其拖拽到窗体中,调整大小并将其拖动到适当位置,在属性窗口中选定属性名“(Name)”,在右列中将其属性值设为“output”,用于显示数据。 3、用同样的方法向窗体中添加19个Button控件,单击“Button1”控件,同样在属性窗口中选定属性名“(Name)”,在右列中将其属性值设为“zero”,并在属性窗口中选定属性名“Text”,在右列中将其属性值设为“0”。用同样的方法依次将其他Button控件的“(Name)”属性值分别设为“point”、“AllClean”、“equal”、“one”、“two”、“three”、“add”、“subtract”、“four”、“five”、“six”、“multiply”、“divide”、“seven”、“eight”、“nine”、“kai”、“guan”;“Text”属性值分别设为“.”、“AC(归零)”、“=”、“1”、“2”、“3”、“+”、“-”、“4”、“5”、“6”、“*”、“/”、“7”、“8”、“9”、“on”、“off”,最终界面如下:

4、完成窗体和控件的布局及其属性设置后,双击要编写代码的命令按钮,进入代码编译器,开始编写程序代码。 五、实验代码:

Public Class Calculator

Dim strdx() As String = {\, \, \} '声明一个字符串,用以存取数值 Dim calcount1 As String = \ Dim calcount2 As String = \ Dim strvalue As Boolean = False

Private Sub zero_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles zero.Click If strdx(0) = \ Then

output.Text = strdx(0) & \ ElseIf strvalue = False Then strdx(0) = strdx(0) & \ output.Text = strdx(0) & \ Else

strdx(0) = strdx(0) & \ output.Text = strdx(0) End If End Sub

Private Sub point_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles point.Click strvalue = True

strdx(0) = strdx(0) & \ output.Text = strdx(0) End Sub

Private Sub AllClean_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AllClean.Click strdx(0) = \ strdx(1) = \ strdx(2) = \ calcount1 = \ calcount2 = \ strvalue = False output.Text = \ End Sub

Private Sub equal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles equal.Click If strdx(2) = \ Then Select Case calcount1 Case \

output.Text = Str(Val(strdx(1)) + Val(strdx(0))) Case \

output.Text = Str(Val(strdx(1)) - Val(strdx(0))) Case \

output.Text = Str(Val(strdx(1)) * Val(strdx(0))) Case \

If strdx(0) = \ Then output.Text = \ Else

output.Text = Str(Val(strdx(1)) / Val(strdx(0))) End If End Select

ElseIf calcount2 = \ Then

strdx(0) = Str(Val(strdx(0)) * Val(strdx(2))) Select Case calcount1 Case \

output.Text = Str(Val(strdx(1)) + Val(strdx(0))) Case \

output.Text = Str(Val(strdx(1)) - Val(strdx(0))) Case \

output.Text = Str(Val(strdx(1)) * Val(strdx(0))) Case \

If strdx(0) = \ Then output.Text = \ Else

output.Text = Str(Val(strdx(1)) / Val(strdx(0))) End If End Select

Else : calcount2 = \

strdx(0) = Str(Val(strdx(2)) / Val(strdx(0))) Select Case calcount1 Case \

output.Text = Str(Val(strdx(1)) + Val(strdx(0))) Case \

output.Text = Str(Val(strdx(1)) - Val(strdx(0))) Case \

output.Text = Str(Val(strdx(1)) * Val(strdx(0))) Case \

If strdx(0) = \ Then output.Text = \ Else

output.Text = Str(Val(strdx(1)) / Val(strdx(0))) End If End Select End If End Sub

Private Sub one_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles one.Click

If strdx(0) = \ Then strdx(0) = \

output.Text = strdx(0) & \ ElseIf strvalue = False Then strdx(0) = strdx(0) & \ output.Text = strdx(0) & \ Else

strdx(0) = strdx(0) & \ output.Text = strdx(0) End If End Sub

Private Sub two_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles two.Click If strdx(0) = \ Then strdx(0) = \

output.Text = strdx(0) & \ ElseIf strvalue = False Then strdx(0) = strdx(0) & \ output.Text = strdx(0) & \ Else

strdx(0) = strdx(0) & \ output.Text = strdx(0) End If End Sub

Private Sub three_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles three.Click If strdx(0) = \ Then strdx(0) = \

output.Text = strdx(0) & \ ElseIf strvalue = False Then strdx(0) = strdx(0) & \ output.Text = strdx(0) & \ Else

strdx(0) = strdx(0) & \ output.Text = strdx(0) End If End Sub

Private Sub add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles add.Click If calcount1 = \ Then calcount1 = \

strdx(1) = strdx(0) strdx(0) = \

Else : Select Case calcount1 Case \

strdx(1) = Str(Val(strdx(0)) + Val(strdx(1))) strdx(0) = \ calcount1 = \ Case \

strdx(1) = Str(Val(strdx(1)) - Val(strdx(0))) strdx(0) = \ calcount1 = \ Case \

strdx(1) = Str(Val(strdx(0)) * Val(strdx(1))) strdx(0) = \ calcount1 = \ Case \

strdx(1) = Str(Val(strdx(1)) / Val(strdx(0))) strdx(0) = \ calcount1 = \ End Select End If End Sub

Private Sub subtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles subtract.Click If calcount1 = \ Then calcount1 = \ strdx(1) = strdx(0) strdx(0) = \

Else : Select Case calcount1 Case \

strdx(1) = Str(Val(strdx(0)) + Val(strdx(1))) strdx(0) = \ calcount1 = \ Case \

strdx(1) = Str(Val(strdx(1)) - Val(strdx(0))) strdx(0) = \ calcount1 = \ Case \

strdx(1) = Str(Val(strdx(0)) * Val(strdx(1))) strdx(0) = \ calcount1 = \ Case \

strdx(1) = Str(Val(strdx(1)) / Val(strdx(0)))

strdx(0) = \ calcount1 = \ End Select End If End Sub

Private Sub four_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles four.Click If strdx(0) = \ Then strdx(0) = \

output.Text = strdx(0) & \ ElseIf strvalue = False Then strdx(0) = strdx(0) & \ output.Text = strdx(0) & \ Else

strdx(0) = strdx(0) & \ output.Text = strdx(0) End If End Sub

Private Sub five_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles five.Click If strdx(0) = \ Then strdx(0) = \

output.Text = strdx(0) & \ ElseIf strvalue = False Then strdx(0) = strdx(0) & \ output.Text = strdx(0) & \ Else

strdx(0) = strdx(0) & \ output.Text = strdx(0) End If End Sub

Private Sub six_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles six.Click If strdx(0) = \ Then strdx(0) = \

output.Text = strdx(0) & \ ElseIf strvalue = False Then strdx(0) = strdx(0) & \ output.Text = strdx(0) & \ Else

strdx(0) = strdx(0) & \

output.Text = strdx(0) End If End Sub

Private Sub multiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles multiply.Click If calcount1 = \ Then calcount1 = \ strdx(1) = strdx(0) strdx(0) = \

Else : Select Case calcount1 Case \

calcount2 = \ strdx(2) = strdx(0) strdx(0) = \ Case \

calcount2 = \ strdx(2) = strdx(0) strdx(0) = \ Case \

strdx(1) = Str(Val(strdx(0)) * Val(strdx(1))) strdx(0) = \ calcount1 = \ Case \

strdx(1) = Str(Val(strdx(1)) / Val(strdx(0))) strdx(0) = \ calcount1 = \ End Select End If End Sub

Private Sub divide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles divide.Click If calcount1 = \ Then calcount1 = \ strdx(1) = strdx(0) strdx(0) = \

Else : Select Case calcount1 Case \

calcount2 = \ strdx(2) = strdx(0) strdx(0) = \ Case \

calcount2 = \

strdx(2) = strdx(0) strdx(0) = \ Case \

strdx(1) = Str(Val(strdx(0)) * Val(strdx(1))) strdx(0) = \ calcount1 = \ Case \

strdx(1) = Str(Val(strdx(1)) / Val(strdx(0))) strdx(0) = \ calcount1 = \ End Select End If End Sub

Private Sub seven_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles seven.Click If strdx(0) = \ Then strdx(0) = \

output.Text = strdx(0) & \ ElseIf strvalue = False Then strdx(0) = strdx(0) & \ output.Text = strdx(0) & \ Else

strdx(0) = strdx(0) & \ output.Text = strdx(0) End If End Sub

Private Sub eight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles eight.Click If strdx(0) = \ Then strdx(0) = \

output.Text = strdx(0) & \ ElseIf strvalue = False Then strdx(0) = strdx(0) & \ output.Text = strdx(0) & \ Else

strdx(0) = strdx(0) & \ output.Text = strdx(0) End If End Sub

Private Sub nine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nine.Click If strdx(0) = \ Then strdx(0) = \

output.Text = strdx(0) & \ ElseIf strvalue = False Then strdx(0) = strdx(0) & \ output.Text = strdx(0) & \ Else

strdx(0) = strdx(0) & \ output.Text = strdx(0) End If End Sub

Private Sub kai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles kai.Click output.Text = \ End Sub

Private Sub guan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles guan.Click Me.Close() End Sub

Private Sub output_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles output.TextChanged End Sub End Class

五、实验结果及截图:

1、单击工具栏中的“启动调试(F5)”按钮,运行程序。 2、单击工具栏中的“on”按钮,文本框显示数据“0.”,如下图所示:

3、实现加法运算(27+31=58):输入数据“27”,点击“+”按钮,再输入数据“31”,按下“=”按钮,文本框显示运算结果“58”,具体过程如下图所示:

4、点击“AC”按钮,实现文本框清零,如下图所示:

5、实现减法运算(47-24=23):依次点击“47”、“-”、“24”、“=”,文本框显示运算结果,如下图所示:

6、点击“AC”,实现乘法运算(27*18=486):依次点击“27”、“*”、“18”、“=”,文本框显示运算结果,如下图所示:

7、点击“AC”,实现除法运算(47/5):依次点击“47”、“/”、“5”、“=”,文本框显示运算结果,如下图所示:

8、点击“off”按钮,程序结束运行,窗口被关闭。 六、实验总结:

通过本次实验,对VB.NET程序开发环境有了进一步的认识,了解了VB.NET应用程序设计的基本框架结构,掌握了Windows Form的事件处理机制以及VB.NET的基本编程方法和技巧,能够对Windows窗体和常用控件进行简单的设计处理,进一步熟悉了VB.NET应用程序的各种运算符、表达式和程序控制结构,基本实现了简单计算器的运算功能。虽然实验还有诸多不完善的地方,如计算结果不能与表达式同时显示,但是我会继续努力,争取掌握更多VB.NET程序设计方面的知识,进一步完善本次实验。

本文来源:https://www.bwwdw.com/article/al9.html

Top