计算器vb

合集下载

VB简易计算器代码

VB简易计算器代码

VB简易计算器代码下面是一个简单的VB计算器代码,用于执行基本的加、减、乘、除运算。

```vbOption Strict OnPublic Class CalculatorPrivate Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.ClickDim num1 As Double = CDbl(txtNum1.Text)Dim num2 As Double = CDbl(txtNum2.Text)Dim result As Double = num1 + num2txtResult.Text = result.ToStringEnd SubPrivate Sub btnSubtract_Click(sender As Object, e As EventArgs) Handles btnSubtract.ClickDim num1 As Double = CDbl(txtNum1.Text)Dim num2 As Double = CDbl(txtNum2.Text)Dim result As Double = num1 - num2txtResult.Text = result.ToStringPrivate Sub btnMultiply_Click(sender As Object, e As EventArgs) Handles btnMultiply.ClickDim num1 As Double = CDbl(txtNum1.Text)Dim num2 As Double = CDbl(txtNum2.Text)Dim result As Double = num1 * num2txtResult.Text = result.ToStringEnd SubPrivate Sub btnDivide_Click(sender As Object, e As EventArgs) Handles btnDivide.ClickDim num1 As Double = CDbl(txtNum1.Text)Dim num2 As Double = CDbl(txtNum2.Text)If num2 = 0 ThenMessageBox.Show("除数不能为0!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)ElseDim result As Double = num1 / num2txtResult.Text = result.ToStringEnd IfEnd Sub```此代码创建了一个简单的窗体应用程序,其中包含两个文本框用于输入两个数字,四个按钮用于执行不同的计算操作,以及一个文本框用于显示结果。

VB简单计算器(截图附代码)

VB简单计算器(截图附代码)

VB程序简单计算器最近学完VB,感觉很好,写了个计算器程序,虽然花了不少时间,可也着实高兴。

其中遇到很多问题,最终也在各种资料中得到解决。

现在附上截图和全部代码,希望和大家交流一下,相互学习。

也希望能帮助到准备做计算器的同学。

计算器最终执行文件图标:计算器包括三个窗体(form):主页面form1:其中的“欢迎各位到此一游”是闪烁效果,呵呵,是自己想着无聊,就想出这么个玩意。

与计算器计算功能无关。

具体实现看下来代码。

Form1的实现代码:Public haha As BooleanPrivate Sub Command1_Click()Dim a As Integera = MsgBox("亲爱的你,真的想要退出本系统吗?", _vbYesNo + vbInformation + vbDefaultButton1, "退出系统前的询问撒(⊙o⊙)")If a = 6 Then '表示当选择“是”的时候的返回值EndEnd IfEnd SubPrivate Sub Command2_Click()Me.HideForm2.ShowEnd SubPrivate Sub Command3_Click()Me.HideForm3.ShowEnd SubPrivate Sub Form_Load()haha = FalseEnd SubPrivate Sub Timer1_Timer()haha = Not hahaIf haha ThenLabel2.ForeColor = &HFF00FFElseLabel2.ForeColor = vbWhiteEnd IfEnd Sub有些像图像等一些可见的控件属性就没在代码里写了,直接在属性里设置了。

页面(form2):此页面将鼠标点上去,还有意想不到的效果喲。

全部代码为:Dim isFocus1, isFocus2 As BooleanPrivate Sub Command1_Click()If isFocus1 Then '判断焦点在那个文本框中,便于实现按钮输入Text1.Text = Text1.Text & 0End IfIf isFocus2 ThenText2.Text = Text2.Text & 0End IfEnd SubPrivate Sub Command10_Click() If isFocus1 ThenText1.Text = Text1.Text & 9 End IfIf isFocus2 ThenText2.Text = Text2.Text & 9 End IfEnd SubPrivate Sub Command11_Click() If isFocus1 ThenText1.Text = Text1.Text & "." End IfIf isFocus2 ThenText2.Text = Text2.Text & "." End IfEnd SubPrivate Sub Command12_Click() If isFocus1 ThenText1.Text = -Val(Text1.Text)End IfIf isFocus2 ThenText2.Text = -Val(Text2.Text)End IfEnd SubPrivate Sub Command13_Click()Dim a As Integera = Val(Text1.Text) + Val(Text2.Text)Text3.Text = Val(Text1.Text) & "+" & Val(Text2.Text) & "=" & aIf Text1.Text = "" Or Text2.Text = "" ThenMsgBox "哼哼,双目运算一定要是两个数喔" & vbCrLf & vbCrLf & _ "不用说,补上去是必须的撒", vbCritical, "温馨小提醒(*^__^*)" End IfEnd SubPrivate Sub Command14_Click()Dim a As Integera = Val(Text1.Text) - Val(Text2.Text)Text3.Text = Val(Text1.Text) & "-" & Val(Text2.Text) & "=" & aIf Text1.Text = "" Or Text2.Text = "" ThenMsgBox "哼哼,双目运算一定要是两个数喔" & vbCrLf & vbCrLf & _ "不用说,补上去是必须的撒", vbCritical, "温馨小提醒(*^__^*)" End IfEnd SubPrivate Sub Command15_Click()a = Val(Text1.Text) * Val(Text2.Text)Text3.Text = Val(Text1.Text) & "×" & Val(Text2.Text) & "=" & aIf Text1.Text = "" Or Text2.Text = "" ThenMsgBox "哼哼,双目运算一定要是两个数喔" & vbCrLf & vbCrLf & _ "不用说,补上去是必须的撒", vbCritical, "温馨小提醒(*^__^*)" End IfEnd SubPrivate Sub Command16_Click() '除法的特殊性,除数不能为零If Val(Text2.Text) Thena = Val(Text1.Text) / Val(Text2.Text)Text3.Text = Val(Text1.Text) & "÷" & Val(Text2.Text) & "=" & aIf Text1.Text = "" Or Text2.Text = "" ThenMsgBox "哼哼,双目运算一定要是两个数喔" & vbCrLf & vbCrLf & _ "不用说,补上去是必须的撒", vbCritical, "温馨小提醒(*^__^*)" End IfElseText3.Text = "无穷大∞"MsgBox "亲,除数不可以为零的哟!", vbInformation, "矮油,不得了嘞(*^__^*)"Text2.Text = ""Text2.SetFocusEnd IfEnd SubPrivate Sub Command17_Click() '实现清零,并将焦点给文本框1Text1.Text = ""Text2.Text = ""Text3.Text = ""Text1.SetFocusEnd SubPrivate Sub Command18_Click()Me.Hide '进入计算器1Form1.ShowEnd SubPrivate Sub Command2_Click() If isFocus1 ThenText1.Text = Text1.Text & 1 End IfIf isFocus2 ThenText2.Text = Text2.Text & 1 End IfEnd SubPrivate Sub Command3_Click() If isFocus1 ThenText1.Text = Text1.Text & 2 End IfIf isFocus2 ThenText2.Text = Text2.Text & 2 End IfEnd SubPrivate Sub Command4_Click() If isFocus1 ThenText1.Text = Text1.Text & 3 End IfIf isFocus2 ThenText2.Text = Text2.Text & 3 End IfEnd SubPrivate Sub Command5_Click() If isFocus1 ThenText1.Text = Text1.Text & 4 End IfIf isFocus2 ThenText2.Text = Text2.Text & 4 End IfEnd SubPrivate Sub Command6_Click() If isFocus1 ThenText1.Text = Text1.Text & 5 End IfIf isFocus2 ThenText2.Text = Text2.Text & 5 End IfEnd SubPrivate Sub Command7_Click() If isFocus1 ThenText1.Text = Text1.Text & 6 End IfIf isFocus2 ThenText2.Text = Text2.Text & 6 End IfEnd SubPrivate Sub Command8_Click() If isFocus1 ThenText1.Text = Text1.Text & 7 End IfIf isFocus2 ThenText2.Text = Text2.Text & 7 End IfEnd SubPrivate Sub Command9_Click() If isFocus1 ThenText1.Text = Text1.Text & 8End IfIf isFocus2 ThenText2.Text = Text2.Text & 8End IfEnd SubPrivate Sub Form_Load()isFocus1 = False: isFocus2 = FalseEnd SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)Label4.ForeColor = vbBlackLabel4.FontUnderline = FalseLabel4.FontBold = FalseEnd SubPrivate Sub Label4_Click()Form3.ShowEnd SubPrivate Sub Label4_MouseMove(Button As Integer, Shift As Integer, X AsSingle, Y As Single)Label4.ForeColor = vbGreen Label4.FontUnderline = True Label4.FontBold = True End SubPrivate Sub Text1_GotFocus() isFocus1 = TrueisFocus2 = FalseEnd SubPrivate Sub Text2_GotFocus() isFocus2 = TrueisFocus1 = FalseEnd Sub页面(form3):这里面的亮点自己找哦。

VB简易计算器代码

VB简易计算器代码

VB简易计算器代码以下是一个简单的VB计算器代码:```Public Class Form1Dim firstNum As Double ' 第一个数字Dim secondNum As Double ' 第二个数字Dim operation As Integer ' 1-加法,2-减法,3-乘法,4-除法Private Sub Button0_Click(sender As Object, e As EventArgs) Handles Button0.ClickTextBoxResult.Text = TextBoxResult.Text & "0"End SubPrivate Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.ClickTextBoxResult.Text = TextBoxResult.Text & "1"End SubPrivate Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.ClickTextBoxResult.Text = TextBoxResult.Text & "2"End SubHandles Button3.ClickTextBoxResult.Text = TextBoxResult.Text & "3"End SubPrivate Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.ClickTextBoxResult.Text = TextBoxResult.Text & "4"End SubPrivate Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.ClickTextBoxResult.Text = TextBoxResult.Text & "5"End SubPrivate Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.ClickTextBoxResult.Text = TextBoxResult.Text & "6"End SubPrivate Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.ClickTextBoxResult.Text = TextBoxResult.Text & "7"End SubHandles Button8.ClickTextBoxResult.Text = TextBoxResult.Text & "8"End SubPrivate Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.ClickTextBoxResult.Text = TextBoxResult.Text & "9"End SubPrivate Sub ButtonDot_Click(sender As Object, e As EventArgs) Handles ButtonDot.ClickIf Not TextBoxResult.Text.Contains(".") ThenTextBoxResult.Text = TextBoxResult.Text & "."End IfEnd SubPrivate Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles ButtonAdd.ClickfirstNum = Double.Parse(TextBoxResult.Text)TextBoxResult.Text = ""operation = 1End SubPrivate Sub ButtonSubtract_Click(sender As Object, e As EventArgs) Handles ButtonSubtract.ClickfirstNum = Double.Parse(TextBoxResult.Text)TextBoxResult.Text = ""operation = 2End SubPrivate Sub ButtonMultiply_Click(sender As Object, e As EventArgs) Handles ButtonMultiply.ClickfirstNum = Double.Parse(TextBoxResult.Text)TextBoxResult.Text = ""operation = 3End SubPrivate Sub ButtonDivide_Click(sender As Object, e As EventArgs) Handles ButtonDivide.ClickfirstNum = Double.Parse(TextBoxResult.Text)TextBoxResult.Text = ""operation = 4End SubPrivate Sub ButtonClear_Click(sender As Object, e As EventArgs) Handles ButtonClear.ClickTextBoxResult.Text = ""End SubPrivate Sub ButtonEquals_Click(sender As Object, e As EventArgs) Handles ButtonEquals.ClickDim result As DoublesecondNum = Double.Parse(TextBoxResult.Text)Select Case operationCase 1result = firstNum + secondNumCase 2result = firstNum - secondNumCase 3result = firstNum * secondNumCase 4result = firstNum / secondNumEnd SelectTextBoxResult.Text = result.ToStringEnd SubEnd Class```这个计算器包括数字按钮0-9、小数点按钮、加法、减法、乘法、除法和等于按钮。

科学计算器vb代码

科学计算器vb代码
Caption = "粘贴"
Shortcut = ^V
End
Begin Menu_Cut
Caption = "剪切"
Shortcut = ^X
End
Begin line1
Caption = "-"
End
Begin Menu_All
Caption = "全选"
Shortcut = ^A
End
Begin line2
Caption = "Exp"
Height = 390
Left = 840
TabIndex = 31
Top = 960
Width = 630
End
Begin Cmd_Sin
Caption = "Sin"
Height = 390
Left = 120
T0
Style = 1 'Graphical
TabIndex = 21
Top = 2400
Width = 510
End
Begin Command1
Caption = "."
Height = 390
Index = 11
Left = 3915
TabIndex = 20
Top = 2400
Width = 510
Left = 825
TabIndex = 33
Top = 1935
Width = 630
End
Begin Cmd_Operator
Caption = "X^Y"
Height = 390

计算器的vb课程设计

计算器的vb课程设计

计算器的vb课程设计一、课程目标知识目标:1. 让学生掌握计算器的基本功能与Visual Basic(VB)编程语言的基础知识。

2. 使学生了解计算器程序的结构与设计流程。

3. 帮助学生理解数学运算符在VB中的运用及其与数学公式的对应关系。

技能目标:1. 培养学生运用VB编程语言编写简单计算器程序的能力。

2. 培养学生分析问题、解决问题的能力,能够针对特定需求设计并优化计算器功能。

3. 提高学生团队协作能力,通过小组讨论和实践,共同完成计算器项目的开发。

情感态度价值观目标:1. 培养学生对计算机编程的兴趣和热情,激发创新意识。

2. 培养学生严谨、细心的学习态度,注重代码规范和程序优化。

3. 培养学生互帮互助、共同进步的团队精神,尊重他人意见,善于沟通与合作。

课程性质:本课程为实践性较强的学科课程,旨在让学生通过动手实践,掌握VB编程的基本技能,提高解决问题的能力。

学生特点:学生具备一定的计算机操作基础,对编程有一定了解,但缺乏实际编程经验。

教学要求:教师应结合学生特点,采用任务驱动、案例教学等方法,引导学生主动探究,注重理论与实践相结合,确保学生能够达到课程目标。

在教学过程中,将目标分解为具体的学习成果,以便于后续的教学设计和评估。

二、教学内容1.VB编程环境介绍与基本操作:使学生熟悉VB开发界面,掌握基本操作方法。

- 熟悉VB集成开发环境- 学会创建、保存和打开VB项目2.计算器程序结构与设计:学习计算器程序的框架设计,理解程序结构。

- 分析计算器功能模块- 设计计算器界面及布局3.数学运算符与表达式:掌握VB中的数学运算符,编写计算器运算逻辑。

- 学习VB中的数学运算符- 编写表达式实现计算器运算功能4.事件处理与函数调用:学习事件处理机制,编写计算器功能函数。

- 理解事件驱动编程原理- 编写按钮点击事件处理程序- 创建自定义函数实现计算器功能5.综合实践:运用所学知识,小组合作开发一个功能完善、界面友好的计算器程序。

VB制作简单计算器的过程

VB制作简单计算器的过程

三、还有需要加强改进的地方?
If Left(Text1.Textd1(Index).Caption
Else
Text1.Text = Text1.Text & Command1(Index).Caption
例如:
依次按下“100”、“÷”、“5”,然后再按下“C”,再依次按下“2”、“=”,显示为2。这说明将前面的100÷5都清降掉了.
而依次按下“100”、“÷”、“5”,再按下“CE”,再依次按下“2”、“=”,显示为50,即清除了5,执行了100÷2的操作。
10、前导零不显示
例如:"00123","00.123"
Abs(shu1):取绝对值
len(shu1):测量字符串shu1的长度
left(shu1,n):返回字符shu1中左边n个字符
right(shu1,n):返回字符shu1中右边n个字符
Instr(n,shu1,”.”):找出小数点在shu1中第n次出现的位置
End If
If InStr(1, Text1.Text, ".") = 0 Then
Text1.Text = Text1.Text & "."
End If
If InStr(1, Text1.Text, ".") = 1 Then
Text1.Text = "0" & Text1.Text
9、纠错
说明:C与CE的区别
C键为清除键(CLEAR的缩写) 功能清除所有操作从头再来
CE键为纠错键(CLEAR ERROR的缩写)功能当前操作数归零可以重新输入操作。

科学计算器vb代码

科学计算器vb代码
Style = 1 'Graphical
TabIndex = 21
Top = 2400
Width = 510
End
Begin Command1
Caption = "."
Height = 390
Index = 11
Left = 3915
TabIndex = 20
Top = 2400
Width = 510
Top = 1455
Width = 510
End
Begin Command1
Caption = "4"
Height = 390
Index = 4
Left = 2820
TabIndex = 4
Top = 1455
Width = 510
End
Begin Command1
Caption = "3"
Height = 390
End
End
End
Attribute VB_Name = "Frm_Scientific"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Caption = "*"
Height = 390
IndexLeabharlann = 1Left = 4470
TabIndex = 11
Top = 1455
Width = 510
End

VB计算器

VB计算器

用VB写计算器这是我设计的计算器样式,代码如下:Dim data1 As Double‘定义变量类型’Dim data2 As DoubleDim j As IntegerDim flag As BooleanDim result As StringPrivate Sub Form_Load()flag = TrueText1 = "0"End Sub0~9的代码:Private Sub Command1_Click(Index As Integer)‘0’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "0"End IfElseText1 = ""Text1 = Text1 + "0"End Ifflag = TrueEnd SubPrivate Sub Command11_Click(Index As Integer)‘1’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "1"ElseText1 = ""Text1 = "1"End IfElseText1 = ""Text1 = Text1 + "1"End Ifflag = TrueEnd SubPrivate Sub Command12_Click(Index As Integer)‘2’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "2"ElseText1 = ""Text1 = "2"End IfElseText1 = ""Text1 = Text1 + "2"End Ifflag = TrueEnd SubPrivate Sub Command13_Click(Index As Integer)‘3’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "3"ElseText1 = ""Text1 = "3"End IfElseText1 = ""Text1 = Text1 + "3"End Ifflag = TrueEnd SubPrivate Sub Command14_Click(Index As Integer)‘4’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "4"ElseText1 = ""Text1 = "4"End IfElseText1 = ""Text1 = Text1 + "4"End Ifflag = TrueEnd SubPrivate Sub Command15_Click(Index As Integer)‘5’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "5"ElseText1 = ""Text1 = "5"End IfElseText1 = ""Text1 = Text1 + "5"End Ifflag = TrueEnd SubPrivate Sub Command16_Click(Index As Integer)‘6’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "6"ElseText1 = ""Text1 = "6"End IfElseText1 = ""Text1 = Text1 + "6"End Ifflag = TrueEnd SubPrivate Sub Command17_Click(Index As Integer)‘7’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "7"ElseText1 = ""Text1 = "7"End IfElseText1 = ""Text1 = Text1 + "7"End Ifflag = TrueEnd SubPrivate Sub Command18_Click(Index As Integer)‘8’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "8"ElseText1 = ""Text1 = "8"End IfElseText1 = ""Text1 = Text1 + "8"End Ifflag = TrueEnd SubPrivate Sub Command19_Click(Index As Integer)‘9’If flag = True ThenIf Text1 <> "0" ThenText1 = Text1 + "9"ElseText1 = ""Text1 = "9"End IfElseText1 = ""Text1 = Text1 + "9"End Ifflag = TrueEnd SubPrivate Sub Command2_Click()‘小数点的实现’If Text1 <> "" ThenText1 = Text1 + "."End Ifflag = TrueEnd SubPrivate Sub Command3_Click()‘正负号的转换’If Text1 > 0 And Text1 < 1 ThenText1 = Trim("-") + Trim(Text1)Exit SubEnd IfIf Text1 > -1 And Text1 < 0 ThenText1 = "0" + Trim(-Text1)Exit SubEnd IfText1 = -Text1End Sub‘+-*/加减乘除的实现’Private Sub Command4_Click()‘+’data1 = Text1j = "1"Text1 = "0"Text1.SetFocusEnd SubPrivate Sub Command5_Click()‘-‘data1 = Text1j = "2"Text1 = "0"Text1.SetFocusEnd SubPrivate Sub Command6_Click()‘*’data1 = Text1j = "3"Text1 = "0"Text1.SetFocusEnd SubPrivate Sub Command7_Click()‘/’data1 = Text1j = "4"Text1 = "0"Text1.SetFocusEnd SubPrivate Sub Command10_Click()‘=’data2 = Text1If j = 1 Thenresult = data1 + data2ElseIf j = 2 Thenresult = data1 - data2ElseIf j = 3 Thenresult = data1 * data2ElseIf j = 4 Thenresult = data1 / data2End IfIf result > 0 And result < 1 Thenresult = "0" + Trim(result)End IfIf result > -1 And result < 0 Thenresult = "0" + Trim(-result)result = "-" + resultEnd IfText1 = resultdata1 = "0"flag = FalseEnd SubPrivate Sub Command9_Click()‘C清除按钮’Text1 = "0"Text1.SetFocusEnd Sub。

用VB编写一个简单计算器

用VB编写一个简单计算器

用VB编写一个简单计算器一、功能:实现简单的加减乘除功能,C归零,CE取消输入,%计算并显示第一个操作数的百分比。

二、控件:1个label,20个commandbutton。

三、计算器运行界面:四、详细代码:Option ExplicitDim Op1, Op2 '前面输入的操作数Dim DecimalFlag As Integer '小数点仍然存在吗?Dim NumOps As Integer '操作数个数Dim LastInput '指示上一次按键事件的类型Dim OpFlag '指示未完成的操作Dim TempReadout' C (取消) 按钮的Click 事件过程' 重新设置显示并初始化变量Private Sub Cancel_Click()Readout = Format(0, "0.")Op1 = 0Op2 = 0Form_LoadEnd Sub' CE (取消输入) 按钮的Click 事件过程Private Sub CancelEntry_Click()Readout = Format(0, "0.")DecimalFlag = FalseLastInput = "CE"End Sub' 小数点(.) 按钮的Click 事件过程' 如果上一次按键为运算符,初始化readout 为"0.";' 否则显示时追加一个小数点Private Sub Decimal_Click()If LastInput = "NEG" ThenReadout = Format(0, "-0.")ElseIf LastInput <> "NUMS" ThenReadout = Format(0, "0.")End IfDecimalFlag = TrueLastInput = "NUMS"End Sub' 窗体的初始化过程' 设置所有变量为其初始值Private Sub Form_Load()DecimalFlag = FalseNumOps = 0LastInput = "NONE"OpFlag = " "Readout = Format(0, "0.")'Decimal.Caption = Format(0, ".")End Sub' 数字键(0-9) 的Click 事件过程' 向显示中的数追加新数Private Sub Number_Click(Index As Integer)If LastInput <> "NUMS" ThenReadout = Format(0, ".")DecimalFlag = FalseEnd IfIf DecimalFlag ThenReadout = Readout + Number(Index).CaptionElseReadout = Left(Readout, InStr(Readout, Format(0, ".")) - 1) + Number(Index).Caption + Format(0, ".")End IfIf LastInput = "NEG" Then Readout = "-" & ReadoutLastInput = "NUMS"End Sub' 运算符(+, -, x, /, =) 的Click 事件过程' 如果接下来的按键是数字键,增加NumOps。

用VB制作计算器

用VB制作计算器

用VB制作计算器在VB编程语言中,可以使用Windows Forms应用程序来制作一个简单的计算器。

Windows Forms应用程序是VB的一种可视化编程工具,可以为用户提供一个图形用户界面。

首先,我们需要创建一个新的Windows Forms应用程序项目,并打开默认生成的Form1窗体。

接下来,我们可以使用Windows Forms控件来创建一个计算器的用户界面。

在Form1窗体上,我们可以添加一个TextBox控件用于显示计算结果,并设置其属性为只读。

```vbPrivate ReadOnly resultTextBox As TextBox = New TextBox```然后,我们可以添加一些Button控件来表示计算器的数字和操作符。

我们可以使用按钮的Click事件来处理用户的点击操作。

```vbPrivate ReadOnly buttons As List(Of Button) = New List(Of Button)```接下来,我们需要实现按钮的Click事件处理程序来执行相应的计算逻辑。

我们可以使用Eval函数来计算表达式的值,并将结果显示在TextBox中。

Private Sub Button_Click(sender As Object, e As EventArgs) Dim button As Button = TryCast(sender, Button)If button IsNot Nothing ThenDim buttonText As String = button.TextIf buttonText = "=" Then'计算表达式的值Dim expression As String = resultTextBox.TextDim value As Double = Eval(expression)resultTextBox.Text = value.ToStringElseIf buttonText = "C" Then'清空文本框resultTextBox.Text = ""Else'添加数字或操作符resultTextBox.Text += buttonTextEnd IfEnd IfEnd Sub最后,我们需要在窗体的构造函数或Load事件处理程序中将按钮和TextBox添加到窗体上。

计算器的VB代码

计算器的VB代码

Imports System.MathPublic Class Form1Public opera As String'记录所要执行的运算soper operatorPublic rdp As Boolean'记录用户是否已经输入小数点bdot radix pointPublic equ As Boolean'记录用户是否已经单击等号bequ equalmarkPublic benum As Double'被操作数dblaccPublic actnum As Double'操作数dbldesPublic result As Double'记录运算结果Private Sub subreset()opera = ""benum = 0actnum = 0rdp = Falseequ = FalseOutPut.Text = ""Pkey.Focus()End SubPrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadDim bt(23) As ButtonDim i As Integerbt(1) = B1bt(2) = B2bt(3) = B3bt(4) = B4bt(5) = B5bt(6) = B6bt(7) = B7bt(8) = B8bt(9) = B9bt(10) = B0bt(11) = Babt(12) = Bjbt(13) = Bqbt(14) = Bmbt(15) = Bdbt(16) = Bebt(17) = BFbt(18) = Bsbt(19) = Bxbt(20) = Bcbt(21) = BackSpacebt(22) = CEbt(23) = CFor i = 1 To 23AddHandler bt(i).Click, New System.EventHandler(AddressOf Me.bt_Click) Nextsubreset()End SubPrivate Sub formmain_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.ActivatedPkey.Focus()End SubPrivate Sub bt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim stext As StringDim bclick As Buttonbclick = senderstext = bclick.TextSelect Case stextCase"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"If equ ThenOutPut.Text = ""End Ifequ = FalseOutPut.Text = OutPut.Text + stextCase"."If Not rdp ThenOutPut.Text = OutPut.Text + stextEnd IfCase"+", "-", "*", "/", "%"If Not OutPut.Text.Equals("") Thenbenum = OutPut.TextOutPut.Text = ""opera = stextEnd IfCase"="rdp = FalseIf Not equ And Not OutPut.Text.Equals("") Thenactnum = OutPut.TextEnd Ifequ = TrueSelect Case operaCase"+"result = benum + actnumCase"-"result = benum - actnumCase"*"result = benum * actnumCase"/"If actnum=0 thenMsgBox”除数不能为零,请重新输入”End ifresult = benum / actnumCase"%"If actnum=0 thenMsgBox”求余数不能为零,请重新输入”End ifresult = benum Mod actnumEnd SelectOutPut.Text = resultCase"+/-"If OutPut.Text.Substring(0, 1) = "-"ThenOutPut.Text = OutPut.Text.Substring(1, OutPut.Text.Length - 1) ElseOutPut.Text = "-" + OutPut.TextEnd IfCase"C"subreset()Case"CE"OutPut.Text = ""Case"BackSpace"If OutPut.Text <> ""ThenIf OutPut.Text.Substring(OutPut.Text.Length - 1) = "."Thenrdp = FalseEnd IfOutPut.Text = OutPut.Text.Substring(0, OutPut.Text.Length - 1)End IfCase"Sqr"Dim m As Integer = OutPut.TextIf m >= 0 ThenOutPut.Text = Sqrt(m).ToStringEnd IfCase"1/X"Dim m As Integer = OutPut.TextIf m <> 0 ThenOutPut.Text = (1 / m).ToStringEnd IfEnd SelectEnd SubPrivate Sub Pkey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Pkey.ClickEnd SubEnd Class。

使用VB制作计算器程序

使用VB制作计算器程序

使用VB制作计算器程序计算器程序是一种用于进行数学计算的工具。

最常见的计算器是电子计算器,它可以实现简单的加减乘除等基本计算。

而现在,我们可以使用VB(即Visual Basic)编程语言制作一个计算器程序,使其在计算能力上有所提升。

首先,我们需要创建一个VB Windows Forms应用程序。

在创建项目时,我们可以选择桌面应用程序(Windows Forms)模板。

接下来,我们需要设计计算器的用户界面。

可以使用按钮、文本框等控件来实现基本的数字输入和计算。

假设我们的计算器有一个文本框用于显示输入和结果,并且有数字按钮和运算符按钮来输入表达式,并且还有一个等号按钮用于计算结果。

我们可以使用VB代码来实现各种按钮的功能。

以下是一个简单的计算器程序示例:```vbPublic Class Form1Dim num1 As Double ' 第一个操作数Dim num2 As Double ' 第二个操作数Dim result As Double ' 结果Dim operator As String ' 运算符'数字按钮的点击事件处理函数Private Sub NumberButton_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click, Button0.ClickDim number As Button = CType(sender, Button)TextBox1.Text += number.TextEnd Sub'运算符按钮的点击事件处理函数Private Sub OperatorButton_Click(sender As Object, e As EventArgs) Handles PlusButton.Click, MinusButton.Click, MultiplyButton.Click, DivideButton.ClickDim op As Button = CType(sender, Button)num1 = CDbl(TextBox1.Text)TextBox1.Clear[operator] = op.TextEnd Sub'等号按钮的点击事件处理函数Private Sub EqualsButton_Click(sender As Object, e As EventArgs) Handles EqualsButton.Clicknum2 = CDbl(TextBox1.Text)Select Case [operator]Case "+"result = num1 + num2Case "-"result = num1 - num2Case "*"result = num1 * num2Case "/"result = num1 / num2End SelectTextBox1.Text = result.ToStringEnd Sub'清除按钮的点击事件处理函数Private Sub ClearButton_Click(sender As Object, e As EventArgs) Handles ClearButton.ClickTextBox1.ClearEnd SubEnd Class```以上代码实现了简单的加减乘除功能,并将结果显示在文本框中。

计算器vb课程设计

计算器vb课程设计

计算器vb课程设计一、课程目标知识目标:1. 理解VB编程语言的基本概念,掌握VB计算器程序的基本结构。

2. 学习使用VB计算器中的算术运算符,并能运用到实际编程中。

3. 了解计算机程序解决问题的基本过程,理解程序中变量和数据类型的作用。

技能目标:1. 学会使用VB编程环境,能够编写、调试和运行简单的计算器程序。

2. 培养学生运用VB语言解决实际问题的能力,例如设计一个功能完善、界面友好的计算器程序。

3. 提高学生团队协作能力,通过小组讨论、分享编程技巧,共同优化程序。

情感态度价值观目标:1. 培养学生对计算机编程的兴趣,激发学习主动性和创新意识。

2. 培养学生面对问题时的耐心和毅力,鼓励克服困难,勇于尝试。

3. 增强学生的自信心,通过完成计算器项目,体验编程带来的成就感。

课程性质:本课程为实践性较强的学科,注重培养学生动手能力和编程思维。

学生特点:学生已具备一定的计算机操作基础,对编程有好奇心,但可能缺乏耐心和毅力。

教学要求:结合学生特点,设计具有趣味性和挑战性的实践任务,引导学生主动探究,逐步提高编程能力。

在教学过程中,关注学生的情感态度变化,激发学习兴趣,培养良好的学习习惯。

通过分解课程目标为具体的学习成果,使学生在课程结束后能够独立完成一个简单的VB计算器程序,为后续学习打下坚实基础。

二、教学内容1.VB编程基础:- 数据类型与变量- 运算符与表达式- 程序结构(顺序、分支、循环)2.计算器功能设计:- 界面设计(按钮、文本框)- 事件处理(按钮点击、输入检测)- 运算逻辑(加、减、乘、除、清除、等于)3.编程实践:- 设计一个简单计算器界面- 编写计算器功能代码- 调试与优化程序4.拓展提高:- 学习使用函数和子程序- 计算器界面的美化与功能扩展- 小组合作,分享编程经验教学内容安排与进度:第一周:VB编程基础,介绍数据类型、变量和运算符。

第二周:程序结构,讲解顺序、分支和循环结构。

第三周:计算器功能设计,学习界面设计和事件处理。

用VB6.0编写一个小计算器

用VB6.0编写一个小计算器
储结果的变量。需要注意的是,对于负数进行平方根运算会返回一个复数结果。
04
测试和调试
功能测试
测试所有基本功能
加、减、乘、除、平方、平方根等。
测试运算符优先级
确保遵循数学规则,如先乘除后加减。
测试数字范围
包括整数、小数和科学记数法表示的数字。
错误处理
输入验证
确保输入是数字,防止非法字符输入。
异常处理
在VB6.0中,可以使用"/"运算符进行除法 运算。为了实现除法运算,可以在按钮的 点击事件中编写相应的代码,例如: `result = num1 / num2`,其中`num1`和 `num2`是要相除的两个数,`result`是存储 结果的变量。需要注意的是,如果除数为0 会导致程序出错,因此需要进行除数为0的 判断。
谢谢观看
用VB6.0编写一个小计算器
目录
• 引言 • 设计计算器界面 • 实现计算器功能 • 测试和调试 • 总结和展望
01
引言
目的和背景
提高编程技能和实践能力
练习开发一个简单的计算 器应用程序
掌握VB6.0编程语言的基本 语法和控件
01
03 02
计算器概述
一个基本的计算器应 用程序应具备加、减、 乘、除运算功能
优化用户界面
虽然现在的用户界面已经比较友好,但我们可以 进一步优化它,使其更加美观和易用。例如,可 以调整按钮大小、颜色和布局,使其更加符合用 户审美和使用习惯。
扩展平台兼容性
为了使更多人能够使用这个小计算器,我们可以 考虑将其移植到其他平台,如手机或平板电脑。 这需要我们使用跨平台的开发框架和技术来实现 。
平方和平方根运算
总结词
实现平方和平方根运算的基本功能

VB计算器程序代码

VB计算器程序代码

VB计算器程序代码下面是一个简单的VB计算器程序代码,可以进行基本的四则运算:```VBImports System.MathPublic Class Form1Dim num1, num2, result As DoubleDim operatorType As String = ""Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.LoadEnd SubPrivate Sub Button_Click(sender As Object, e As EventArgs) Handles btn0.Click, btn1.Click, btn2.Click, btn3.Click,btn4.Click, btn5.Click, btn6.Click, btn7.Click, btn8.Click, btn9.Click, btnDot.ClickDim button As Button = CType(sender, Button)If button.Text = "." ThenIf Not txtDisplay.Text.Contains(".") ThentxtDisplay.Text += button.TextEnd IfElsetxtDisplay.Text += button.TextEnd IfEnd SubPrivate Sub Operator_Click(sender As Object, e As EventArgs) Handles btnAdd.Click, btnSubtract.Click, btnMultiply.Click, btnDivide.ClickDim button As Button = CType(sender, Button)num1 = CDbl(txtDisplay.Text)operatorType = button.TexttxtDisplay.ClearEnd SubPrivate Sub btnEquals_Click(sender As Object, e As EventArgs) Handles btnEquals.Clicknum2 = CDbl(txtDisplay.Text)Select Case operatorTypeCase "+"result = num1 + num2Case "-"result = num1 - num2Case "*"result = num1 * num2Case "/"If num2 <> 0 Thenresult = num1 / num2ElseMessageBox.Show("除数不能为零!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)End IfEnd SelecttxtDisplay.Text = result.ToStringEnd SubPrivate Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.ClicktxtDisplay.ClearEnd SubPrivate Sub btnSqrt_Click(sender As Object, e As EventArgs) Handles btnSqrt.ClickIf txtDisplay.Text = "" ThenMessageBox.Show("请输入一个数字!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)Elsenum1 = CDbl(txtDisplay.Text)result = Sqrt(num1)txtDisplay.Text = result.ToStringEnd IfEnd SubPrivate Sub btnPower_Click(sender As Object, e As EventArgs) Handles btnPower.ClickIf txtDisplay.Text = "" ThenMessageBox.Show("请输入一个数字!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)Elsenum1 = CDbl(txtDisplay.Text)result = num1 ^ 2txtDisplay.Text = result.ToStringEnd IfEnd SubPrivate Sub btnBackspace_Click(sender As Object, e As EventArgs) Handles btnBackspace.ClickIf txtDisplay.Text.Length > 0 ThentxtDisplay.Text = txtDisplay.Text.Substring(0, txtDisplay.Text.Length - 1)End IfEnd SubPrivate Sub btnNegative_Click(sender As Object, e As EventArgs) Handles btnNegative.ClickIf txtDisplay.Text.Length > 0 ThenIf txtDisplay.Text.Substring(0, 1) = "-" ThentxtDisplay.Text = txtDisplay.Text.Substring(1, txtDisplay.Text.Length - 1)ElsetxtDisplay.Text = "-" + txtDisplay.TextEnd IfEnd IfEnd SubEnd Class```这个计算器程序具有以下功能:1.支持0-9的数字输入和小数点的输入。

用VB制作计算器

用VB制作计算器

摘要这个程序是我用VB制作的简单计算器,它可以实现简单的四则运算,数字的平方,平方根,正弦,余弦,倒数,以及进制的相互转换。

而且它的功能还有退格,清除当前数据,清除所有数据,复制数据,粘贴数据,退出程序。

在这个计算器的程序中,我用到了单行选择语句If …Then …Else…,If语句的嵌套,多条件选择语句Select Case,而且还用到了组合框,框架,菜单的设计,以及键盘事件,可以通过键盘来用于计算。

在此报告中我重点叙述了计算机的各个功能及用法。

特别是进制间的转换和数字与运算符之间的联系。

还有说明了键盘怎样用于操作计算。

虽然表达能力不太行,但我会尽力使用系统结构图,程序流程图,划分多个模块,让大家明白我设计的运算器的功能的。

目录摘要 (1)目录 (2)第一章任务概述 (3)1.1、目标 (3)1.2、需求概述 (3)1.2.1、鼠标操作 (3)1.2.2、键盘操作 (3)第二章总体设计 (4)2.1、系统结构功能图 (4)第三章功能分配 (5)3.1、打开计算器界面 (5)3.2、平方根,平方,倒数,正弦,余弦的实现 (7)3.3、退格 (8)3.4、清除 (8)3.4.1、清除当前数据 (8)3.4.2清除所有数据 (8)3.5、鼠标的输入 (9)3.6、进制间的转换 (11)小结 (13)第一章任务概述1.1、目标本系统的目标是建立一套可以计算数的平方,平方根,倒数,正弦,余弦,数字的进制转换的计算器系统,系统可以帮助我们更好的学习,更好的节省时间。

还能避免出现一些不必要的计算错误。

1.2、需求概述根据本系统的需求分析,系统的需求可以分为以下的几个方面:1.2.1、鼠标操作你可以利用鼠标点击界面上的各个按钮来实现计算器的运算。

1.2.2、键盘操作当不方便使用鼠标时可以利用键盘来实现运算,数字的复制和粘贴。

第二章总体设计2.1、系统结构功能图图2-1系统结构功能图第三章功能分配3.1、打开计算器界面图3-1 计算器界面Dim dflag As Integer‘小数点仍然存在吗?Dim i As IntegerDim opnre As Integer ‘初始值Dim prev As Double‘前面输入的操作数Dim oflag As Integer ‘等号前后对数字的标记Dim ind As Integer ‘cmdoperator控件数组的数Dim x As DoubleDim y As Double' 运算符 (+, -, x, /, =) 的 Click 事件过程' 如果接下来的按键是数字键,增加 val(text1.Caption)。

VB实例代码-计算器

VB实例代码-计算器

VB实例代码-计算器首先,需建立如下控件:标签:Label1命令按钮:名称 Caption值Cback ?Cclear CECstart CCsin SinCcos CosCsqrt SqrtCsign +/-Cpoint CpointCequal =控件数组:(命令按钮)Calcu(1) +Calcu(2) -Calcu(3) *Calcu(4) /Cnum(0) 0Cnum(1) 1...Cnum(9) 9然后在代码窗口写源码:Option ExplicitDim Num1, Num2 As Double Dim PointIn As Boolean Dim Inputing As Boolean Dim Operation1 As IntegerPrivate Sub Calcu_Click(i As Integer)If Inputing = False Then '在前次运算提交之后尚未输入新的数据Operation1 = i '运算符重置Num1 = Label1 '将显示栏里的数据赋值给第一个操作数Exit SubEnd IfInputing = False '将当前状态置为非输入数据阶段If Num1 <> 0 Then '非首次计算Num2 = Label1 '将显示栏里的数据赋值给第二个操作数Calculate (Operation1) '计算前一次运算并显示结果Else '首次计算Num1 = Label1 '将显示栏里的数据赋值给第一个操作数End IfOperation1 = i '提交运算符End SubPrivate Sub Cback_Click()Dim TheLen As IntegerTheLen = Len(Label1)If TheLen > 2 ThenIf Right(Label1, 1) = "." ThenLabel1 = Left(Label1, TheLen - 2) & "."ElseLabel1 = Left(Label1, TheLen - 1)End IfElseCclear_ClickEnd IfEnd SubPrivate Sub Cclear_Click()Label1 = "0."Num2 = 0PointIn = FalseEnd SubPrivate Sub Ccos_Click()Calculate (6)Inputing = FalseEnd SubPrivate Sub Cequal_Click()If Inputing Then '如果刚输入过数据'Or Operation1 > 0 Then Num2 = Label1 '将显示栏里的数据赋值给第二个操作数End IfInputing = False '将当前状态置为非输入数据阶段Calculate (Operation1) '计算提交的运算并显示结果Num1 = 0Operation1 = 0End SubPrivate Sub Calculate(Oprt As Integer) Select Case OprtCase 1Num1 = Num1 + Num2ShowResult (Num1)Case 2Num1 = Num1 - Num2ShowResult (Num1)Case 3Num1 = Num1 * Num2ShowResult (Num1)Case 4Num1 = Num1 / Num2ShowResult (Num1)Case 5Num2 = Label1Num1 = Sin(Num2)ShowResult (Num1)Case 6Num2 = Label1Num1 = Cos(Num2)ShowResult (Num1)Case 7Num2 = Label1Num1 = Sqr(Num2)ShowResult (Num1)End SelectEnd SubPrivate Sub ShowResult(Num As Double)If Num = Fix(Num) Then '整数Label1 = Num & "."ElseIf Left(Num, 1) = "." Then '第一个字符为小数点Label1 = "0" & NumElseIf Left(Num, 2) = "-." Then '前两个字符为"-." Label1 = "-0." & Right(CStr(Num), Len(CStr(Num)) - 2) ElseLabel1 = NumEnd IfEnd SubPrivate Sub Cnum_Click(Index As Integer)NumInput (Index)End SubPrivate Sub Cpoint_Click()If Inputing = False ThenLabel1 = "0."Inputing = TrueEnd IfPointIn = TrueEnd SubPrivate Sub Csign_Click()If Label1 <> "0." ThenDim StrTemp As StringStrTemp = Label1If Left(StrTemp, 1) = "-" ThenLabel1 = Right(StrTemp, Len(StrTemp) - 1) ElseLabel1 = "-" & StrTempEnd IfEnd IfEnd SubPrivate Sub Csin_Click()Calculate (5)Inputing = FalseEnd SubPrivate Sub Csqrt_Click()Dim x As Longx = Label1If x >= 0 ThenCalculate (7)ElseLabel1 = "Error!"End IfInputing = FalseEnd SubPrivate Sub Cstart_Click()Label1 = "0."Num1 = 0Num2 = 0PointIn = FalseInputing = TrueOperation1 = 0End SubPrivate Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) Cequal.SetFocusIf KeyCode = 46 Then '按Del键Cclear_ClickEnd IfEnd SubPrivate Sub Form_KeyPress(KeyAscii As Integer)Select Case KeyAscii'键入数字:Case 48 To 57NumInput (KeyAscii - 48)Case 46 '小数点Cpoint_Click'键入运算符:Case 43 '加号Calcu_Click (1)Case 45 '减号Calcu_Click (2)Case 42 '乘号Calcu_Click (3)Case 47 '除号Calcu_Click (4)Case 27 '重新开始(退出键)Cstart_ClickCase 8 '退格Cback_ClickCase 13 '等于(回车键)Cequal_ClickEnd SelectEnd SubPrivate Sub Form_Load()Me.Top = (Screen.Height - Me.Height) / 2 Me.Left = (Screen.Width - Me.Width) / 2 Me.KeyPreview = TrueCstart_ClickEnd SubPrivate Sub NumInput(n As Integer) If Len(Label1) > 15 ThenExit SubEnd IfIf Inputing = False ThenCclear_ClickLabel1 = n & "."Inputing = TrueElseIf Label1 <> "0." ThenIf Right(Label1, 1) = "." ThenIf PointIn = False ThenDim TheLen As IntegerTheLen = Len(Label1)Label1 = Left(Label1, TheLen - 1) Label1 = Label1 & n & "."ElseLabel1 = Label1 & nEnd IfElseLabel1 = Label1 & nEnd IfElseIf PointIn ThenLabel1 = Label1 & nElseLabel1 = n & "." End IfEnd IfEnd Sub。

科学计算器vb代码

科学计算器vb代码
Top = 1455
Width = 630
End
Begin mandButton Cmd_Ln
Caption = "Ln"
Height = 390
Left = 1575
TabIndex = 35
Top = 960
Width = 630
End
Begin mandButton Cmd_Square
Caption = "C"
Height = 405
Left = 4785
TabIndex = 15
Top = 405
Width = 750
End
Begin mandButton Cmd_CE
Caption = "CE"
Height = 405
Left = 3975
TabIndex = 14
Caption = "Tan"
Height = 390
Left = 120
TabIndex = 28
Top = 1935
Width = 630
End
Begin mandButton Cmd_Atan
Caption = "Atan"
Height = 390
Left = 120
TabIndex = 27
TabIndex = 17
Top = 1935
Width = 510
End
Begin mandButton Cmd_sqrt
Caption = "Sqrt"
Height = 390
Left = 5025
TabIndex = 16
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
相关文档
最新文档