vb制作简易计算器保存(用vb制作简单的计算器)

今天给各位分享vb制作简易计算器保存的知识,其中也会对用vb制作简单的计算器进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

用VB做一个简单的计算器,该怎么做?

我是抢劫的,把分给我吧

首先建立一个command1 然后剪切复制 系统提示说已经有一个COMMAND1 是否要建立控件数组?

你点击是 这样重复9次 分别把caption该成0到9 然后建立command2 方法同上,caption为 +-*/ 然后是 command3 caption是=

然后输入代码,首先在通用里生命dim shu1,shu2

dim suanfu as string

private sub command1(index)_click

text1.text=text1.text commmand1(index).capton

end sub

command2里 输入

shu1=text1.text

suanfu=text1.text command(index).capiton

text1.text=""

end sub

command3里输入

shu2=text1.text

selece case suanfu

case "+"

text1.text=shu1+shu2

case "-"

text1.text=shu1-shu2

case "*"

text1.text=shu*shu2

case "/"

if shu2=0 then

msgbox "分母不能为0"

else text1.text=shu1/shu2

end if

end select

end sub

这是最简单的计算器的 如果还需要什么功能 我也可以告诉你的 我学VB3个月了 略有小成 所以想来照顾新手的 我以前也是新手啊 新手见新手两眼泪旺旺 顺便把分给我吧

急急急,求大神用vb教做一个简易计算器,加减乘除就可以。能用加分

按你的要求做的代码如下:

在1个新窗体上编排以下控件:

添加1个Label1控件用作显示数据,name=lblScreen

添加1个Command控件数组(10个数字)用作输入数据的按钮0~9,name=cmdNumber,index=0~9

添加1个Command控件用作小数点按钮“.”,name=cmdPoint

添加1个Command控件等号按钮“=”,name=cmdEqual

添加1个Command控件数组(4个符号)用作加减乘除按钮,name=cmdArithmetic,index=0~3

好了,然后复制以下代码运行即可成功啦:

'***************************************************************************************

Private strCalculateType As String '存放运算类型

Private blnCalFlag As Boolean '标记是否已经输入加数、或减数、或乘数、或除数等

Private dblNum1 As Double '存放被加数、或被减数、或被乘数、或被除数等

Private dblNum2 As Double '存放加数、或减数、或乘数、或除数等

Private dblMemo As Double '内存中的数

Private Sub Form_Load()

Dim i As Integer

lblScreen = "0"  '初始化显示屏和关闭按钮

lblScreen.BackColor = vbBlack

strCalculateType = ""  '初始化运算符类型和标记变量

blnCalFlag = False

dblMemo = 0   '初始化内存变量

End Sub

'四则运算

Private Sub cmdArithmetic_Click(Index As Integer)

Select Case Index

Case 0 '输入加法

cmdEqual_Click

strCalculateType = "+"

dblNum1 = Val(lblScreen)

Case 1 '输入减法

cmdEqual_Click

strCalculateType = "-"

dblNum1 = Val(lblScreen)

Case 2 '输入乘法

cmdEqual_Click

strCalculateType = "*"

dblNum1 = Val(lblScreen)

Case 3 '输入初法

cmdEqual_Click

strCalculateType = "/"

dblNum1 = Val(lblScreen)

End Select

End Sub

'求结果(即等于)

Private Sub cmdEqual_Click()

dblNum2 = Val(lblScreen)

If strCalculateType = "/" Then

lblScreen = dblNum1 / dblNum2

ElseIf strCalculateType = "*" Then

lblScreen = dblNum1 * dblNum2

ElseIf strCalculateType = "-" Then

lblScreen = dblNum1 - dblNum2

ElseIf strCalculateType = "+" Then

lblScreen = dblNum1 + dblNum2

ElseIf strCalculateType = "x^y" Then

lblScreen = dblNum1 ^ dblNum2

End If

'    If Left(lblScreen.Caption, 1) = "." Then lblScreen.Caption = "0"  lblScreen.Caption

'    If Len(lblScreen.Caption)  15 Then lblScreen.Caption = Left(lblScreen.Caption, 15)

strCalculateType = ""

blnCalFlag = False

End Sub

'使用控件数组输入数据

Private Sub cmdNumber_Click(Index As Integer)

If lblScreen = "0" Then

lblScreen = ""

End If

If blnCalFlag = False Then

lblScreen = CStr(Index)

blnCalFlag = True

Else

lblScreen = lblScreen + CStr(Index)

End If

End Sub

'输入小数点

Private Sub cmdPoint_Click()

If blnCalFlag = True Then

If InStr(1, lblScreen, ".") = 0 Then

If lblScreen  "" Then

lblScreen = lblScreen + "."

Else

lblScreen = "0."

End If

End If

Else

If blnCalFlag = False Then

lblScreen = "0."

blnCalFlag = True

End If

End If

End Sub

'***************************************************************************************

怎么样?给你的代码非常详细了吧,包括给你的解说哦!

使用VB编写一个简单的计算器,在线等

VERSION 5.00

Begin VB.Form Form1

BorderStyle = 1 'Fixed Single

Caption = "简易计算器 V1.0"

ClientHeight = 3825

ClientLeft = 45

ClientTop = 375

ClientWidth = 4440

LinkTopic = "Form1"

MaxButton = 0 'False

MinButton = 0 'False

ScaleHeight = 3825

ScaleWidth = 4440

StartUpPosition = 3 '窗口缺省

Begin VB.Frame Frame1

Height = 3735

Left = 120

TabIndex = 0

Top = 0

Width = 4215

Begin VB.CommandButton cmdOperator

Caption = "X"

Height = 495

Index = 3

Left = 1680

TabIndex = 16

Top = 2880

Width = 615

End

Begin VB.CommandButton cmdClearEntry

Caption = "C"

Height = 495

Left = 3120

TabIndex = 15

Top = 2880

Width = 615

End

Begin VB.CommandButton cmdNumber

Caption = "4"

Height = 495

Index = 4

Left = 2400

TabIndex = 14

Top = 1200

Width = 615

End

Begin VB.CommandButton cmdNumber

Caption = "5"

Height = 495

Index = 5

Left = 3120

TabIndex = 13

Top = 1200

Width = 615

End

Begin VB.CommandButton cmdNumber

Caption = "6"

Height = 495

Index = 6

Left = 240

TabIndex = 12

Top = 2040

Width = 615

End

Begin VB.CommandButton cmdNumber

Caption = "7"

Height = 495

Index = 7

Left = 960

TabIndex = 11

Top = 2040

Width = 615

End

Begin VB.CommandButton cmdNumber

Caption = "8"

Height = 495

Index = 8

Left = 1680

TabIndex = 10

Top = 2040

Width = 615

End

Begin VB.CommandButton cmdNumber

Caption = "9"

Height = 495

Index = 9

Left = 2400

TabIndex = 9

Top = 2040

Width = 615

End

Begin VB.CommandButton cmdNumber

Caption = "0"

Height = 495

Index = 0

Left = 3120

TabIndex = 8

Top = 2040

Width = 615

End

Begin VB.CommandButton cmdOperator

Caption = "-"

Height = 495

Index = 2

Left = 960

TabIndex = 7

Top = 2880

Width = 615

End

Begin VB.CommandButton cmdEquals

Caption = "="

Height = 495

Left = 2400

TabIndex = 6

Top = 2880

Width = 615

End

Begin VB.CommandButton cmdNumber

Caption = "1"

Height = 495

Index = 1

Left = 240

TabIndex = 5

Top = 1200

Width = 615

End

Begin VB.CommandButton cmdNumber

Caption = "2"

Height = 495

Index = 2

Left = 960

TabIndex = 4

Top = 1200

Width = 615

End

Begin VB.CommandButton cmdNumber

Caption = "3"

Height = 495

Index = 3

Left = 1680

TabIndex = 3

Top = 1200

Width = 615

End

Begin VB.CommandButton cmdOperator

Caption = "+"

Height = 495

Index = 1

Left = 240

TabIndex = 2

Top = 2880

Width = 615

End

Begin VB.TextBox txtDisplay

Alignment = 1 'Right Justify

BackColor = H00C0E0FF

BeginProperty Font

Name = "宋体"

Size = 14.25

Charset = 0

Weight = 700

Underline = 0 'False

Italic = 0 'False

Strikethrough = 0 'False

EndProperty

Height = 495

Left = 360

TabIndex = 1

Top = 480

Width = 3375

End

End

End

Attribute VB_Name = "Form1"

Attribute VB_GlobalNameSpace = False

Attribute VB_Creatable = False

Attribute VB_PredeclaredId = True

Attribute VB_Exposed = False

Option Explicit

Private StoredValue As Double

Private Const opNone = 0

Private Const opAdd = 1

Private Const opSubtract = 2

Private Const opMultiply = 3

Private Const opDivide = 4

Private Operator As Integer

Private NewEntry As Boolean

'清除显示内容,保存运算符

Private Sub cmdClearEntry_Click()

txtDisplay.Text = ""

StoredValue = 0

Operator = opNone

txtDisplay.SetFocus

End Sub

'计算上一操作符的运算结果

Private Sub cmdEquals_Click()

Dim new_value As Double

If txtDisplay.Text = "" Then

new_value = 0

Else

new_value = CDbl(txtDisplay.Text)

End If

Select Case Operator

Case opNone

StoredValue = new_value

Case opAdd

StoredValue = StoredValue + new_value

Case opSubtract

StoredValue = StoredValue - new_value

Case opMultiply

StoredValue = StoredValue * new_value

Case opDivide

StoredValue = StoredValue / new_value

End Select

Operator = opNone

NewEntry = True

txtDisplay.Text = Format$(StoredValue)

txtDisplay.SetFocus

End Sub

' 显示数字

Private Sub cmdNumber_Click(Index As Integer)

If NewEntry Then

txtDisplay.Text = Format$(Index)

NewEntry = False

Else

txtDisplay.Text = txtDisplay.Text Format$(Index)

End If

txtDisplay.SetFocus

End Sub

Private Sub cmdOperator_Click(Index As Integer)

cmdEquals_Click

Operator = Index

NewEntry = True

txtDisplay.SetFocus

End Sub

Private Sub txtDisplay_Change()

txtDisplay.SelStart = Len(txtDisplay.Text)

End Sub

Private Sub txtDisplay_GotFocus()

txtDisplay_Change

End Sub

如何用vb编写简单计算器

不知道你想要哪种,偶是新手,只会最简单的

首先拖出三个text,再拖出4个Command按钮 caption分别为+ - * / 这样就准备就绪了

先讲一下简单用法,第一个text是第一个加数/被减数/因数/被除数,而第二个text是第二个加数/减数/因数/除数,第三个text是和/差/积/商,在text1输入一个数,text2输入一个,然后分别点击command“+ - * /”text3就会出现结果

然后随便单击一个窗口,把里面的文字删去,再写上:

Private Sub Command1_Click()

Text3.Text = Str$(Val(Text1.Text) + Val(Text2.Text))

End Sub

Private Sub Command2_Click()

Text3.Text = Str$(Val(Text1.Text) - Val(Text2.Text))

End Sub

Private Sub Command3_Click()

Text3.Text = Str$(Val(Text1.Text) * Val(Text2.Text))

End Sub

Private Sub Command4_Click()

Text3.Text = Str$(Val(Text1.Text) / Val(Text2.Text))

End Sub

然后运行,应该就可以了

用VB怎样做一个100位的计算器

你说的功能

我想只能用高精度算法来实现

我给你思路

代码其实很简单

楼上那么多

看都不想看

首先定义一个100个元素的数组

每个元素存放相应位数的数字

然后加法的话把另外一个要加的数同样的存在另外一个数组里面

然后把2个数组对应位数的数字加起来

如果超过10了就把这个元素的前驱+1

即进位

即可

这个很简单了

你自己试试吧

做好VB计算器,如何保存?

参考下:

计算器的程序,很简单,只弄了+-法,乘除没弄,只要把代码看懂照着+-法改一下就行了,不过涉及到多级运算只用一个LastClick是不行的

程序的控件就两种

CommandBotton和TextBox都是最基本的

先创建十一个CommandBotton,Caption改为0-9以及一个"."(小数点)

创建三个CommandBotton,Caption改为“-”,“+”“=”

创建五个CommandBotton,Caption改为“CM”(clearmemory) “MR”(memoryrecall) “MS”(memorysave) “C”(clear) “AC”(allclear)

再创建个TextBox

下面的是对应代码,很清楚

不懂来问我`~

Dim Counter As Double ‘这个用来记录结果

Dim Memory As Double ‘用来记录你保存的数,这个我们用的科学计算器里也有

Dim Reg As Double ‘这个也是用来记录结果

Dim CalcNumber As Long ‘这个是判断:1当之前点过一次+或-时再点一次运算结果,2如果前面没点过+或-则不进行运算

Dim DisplayFromNew As Boolean ‘这个用来判断输入框状态:是接着输入还是从新输入新的数

Dim AlreadyDecimalThere As Boolean ‘这个用来判断是否能用小数点

Dim LastClick As String ‘这个用来记录最后一次选择的运算法则

Private Sub Form_Load()

Display.Text = "0"

Reg = 0

CalcNumber = 0

DisplayFromNew = True

AlreadyDecimalThere = False

End Sub

Private Sub AllClear_Click() ‘这个不用说了,是全部清空

DisplayFromNew = True ‘恢复到初始状态

Counter = 0

Display.Text = "0" ‘清空输入框

CalcNumber = 0

Reg = 0

AlreadyDecimalThere = False ‘恢复到初始状态

End Sub

Private Sub Clear_Click() ‘清空,但不清空纪录

DisplayFromNew = True

Display.Text = "0"

End Sub

Private Sub ClearMemory_Click() ‘清空纪录

Memory = 0

End Sub

Private Sub Decimal_Click() ‘关于小数点的

If AlreadyDecimalThere = False Then

AddDisplayDigit "."

AlreadyDecimalThere = True ‘每次输入只能输一次小数点

End If

End Sub

Private Sub Equals_Click() ‘等号时的

If LastClick = "Minus" Then

If CalcNumber = 0 Then

Reg = CDbl(Display.Text) ‘记录

DisplayFromNew = True

CalcNumber = 1

Else ‘这个的意思是,如果只点了法则(+或-)之后没点数,就加上前面输的数,但我觉得这样就等于前面输得数的2倍(+时)或0(-时),所以不用这么麻烦

Counter = Reg - CDbl(Display.Text)

Display.Text = CStr(Counter)

DisplayFromNew = True

AlreadyDecimalThere = False

Reg = CDbl(Display.Text)

End If

ElseIf LastClick = "Addition" Then ‘这是加法,和减法一样

If CalcNumber = 0 Then

Reg = CDbl(Display.Text)

DisplayFromNew = True

CalcNumber = 1

Else

Counter = Reg + CDbl(Display.Text)

Display.Text = CStr(Counter)

DisplayFromNew = True

AlreadyDecimalThere = False

Reg = CDbl(Display.Text)

End If

End If

CalcNumber = 0

End Sub

Sub AddDisplayDigit(Addition As String) ‘这一段十分重要,也是大家要学习的,就是自己创建一个函数,这样应用起来会十分方便

If DisplayFromNew = True Then

Display.Text = Addition ‘当DisplayFromNew = True时(大家注意杂那几种情况下= True)输入栏从新显示刚输入的数

DisplayFromNew = False ‘之后立即变为False,因为要接着这个数继续输入

Else

Display.Text = Display.Text Addition ‘当DisplayFromNew = False 就是接着输入栏里已有的数字继续输入

End If

End Sub

注:为何要弄两种情况呢,因为当点击-或+时,要求输入栏从新记录

Private Sub MemorySave_Click() ‘这是保存记录

If DisplayFromNew = False Then

Memory = CDbl(Display.Text)

End If

End Sub

Private Sub MemoryRecall_Click() ‘打开纪录

Display = CStr(Memory)

End Sub

Private Sub Minus_Click() ‘点击

If CalcNumber = 0 Then

Reg = CDbl(Display.Text)

DisplayFromNew = True

CalcNumber = 1

Else ‘这个就是如果前面输过一次-号,再点-号就运算结果,这一点是很重要的,如果不加这个就无法进行连续运算 Counter = Reg - CDbl(Display.Text)

Display.Text = CStr(Counter)

DisplayFromNew = True

AlreadyDecimalThere = False

Reg = CDbl(Display.Text)

End If

LastClick = "Minus"

End Sub

Private Sub Plus_Click() ‘和减法一样

If CalcNumber = 0 Then

Reg = CDbl(Display.Text)

DisplayFromNew = True

CalcNumber = 1

Else

Counter = Reg + CDbl(Display.Text)

Display.Text = CStr(Counter)

DisplayFromNew = True

AlreadyDecimalThere = False

Reg = CDbl(Display.Text)

End If

LastClick = "Addition"

End Sub

‘下面的就简单了0-9,不过用到了前面定义的函数AddDisplayDigit,大家看到这个自定义函数的作用了吧

Private Sub Nine_Click()

AddDisplayDigit "9"

End Sub

Private Sub Eight_Click()

AddDisplayDigit "8"

End Sub

Private Sub Seven_Click()

AddDisplayDigit "7"

End Sub

Private Sub Six_Click()

AddDisplayDigit "6"

End Sub

Private Sub Five_Click()

AddDisplayDigit "5"

End Sub

Private Sub Four_Click()

AddDisplayDigit "4"

End Sub

Private Sub Three_Click()

AddDisplayDigit "3"

End Sub

Private Sub Two_Click()

AddDisplayDigit "2"

End Sub

Private Sub One_Click()

AddDisplayDigit "1"

End Sub

Private Sub Zero_Click()

AddDisplayDigit "0"

End Sub

关于vb制作简易计算器保存和用vb制作简单的计算器的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

1、本网站名称:源码村资源网
2、本站永久网址:https://www.yuanmacun.com
3、本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行删除处理。
4、本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
5、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
6、本站资源大多存储在云盘,如发现链接失效,请联系我们我们会第一时间更新。
源码村资源网 » vb制作简易计算器保存(用vb制作简单的计算器)

1 评论

您需要 登录账户 后才能发表评论

发表评论

欢迎 访客 发表评论