
Option Explicit
Dim Op1, Op2 Dim DecimalFlag As Integer Dim NumOps As Integer Dim LastInput
Dim OpFlag Dim TempReadout
Private Sub Cancel_Click()
Readout = Format(0, "0")
Op1 = 0
Op2 = 0
Form_Load
End Sub
Private Sub CancelEntry_Click()
Readout = Format(0, "0")
DecimalFlag = False
LastInput = "CE"
End Sub
Private Sub Decimal_Click()
If LastInput = "NEG" Then
Readout = Format(0, "-0")
ElseIf LastInput <> "NUMS" Then
Readout = Format(0, "0")
End If
DecimalFlag = True
LastInput = "NUMS"
End Sub
Private Sub Form_Load()
DecimalFlag = False
NumOps = 0
LastInput = "NONE"
OpFlag = " "
Readout = Format(0, "0")
'DecimalCaption = Format(0, "")
End Sub
Private Sub Number_Click(Index As Integer)
If LastInput <> "NUMS" Then
Readout = Format(0, "")
DecimalFlag = False
End If
If DecimalFlag Then
Readout = Readout + Number(Index)Caption
Else
Readout = Left(Readout, InStr(Readout, Format(0, "")) - 1) + Number(Index)Caption + Format(0, "")
End If
If LastInput = "NEG" Then Readout = "-" & Readout
LastInput = "NUMS"
End Sub
Private Sub Operator_Click(Index As Integer)
TempReadout = Readout
If LastInput = "NUMS" Then
NumOps = NumOps + 1
End If
Select Case NumOps
Case 0
If Operator(Index)Caption = "-" And LastInput <> "NEG" Then
Readout = "-" & Readout
LastInput = "NEG"
End If
Case 1
Op1 = Readout
If Operator(Index)Caption = "-" And LastInput <> "NUMS" And OpFlag <> "=" Then
Readout = "-"
LastInput = "NEG"
End If
Case 2
Op2 = TempReadout
Select Case OpFlag
Case "+"
Op1 = CDbl(Op1) + CDbl(Op2)
Case "-"
Op1 = CDbl(Op1) - CDbl(Op2)
Case "X"
Op1 = CDbl(Op1) CDbl(Op2)
Case "/"
If Op2 = 0 Then
MsgBox "除数不能为 0", 48, "计算器"
Else
Op1 = CDbl(Op1) / CDbl(Op2)
End If
Case "="
Op1 = CDbl(Op2)
Case "%"
Op1 = CDbl(Op1) CDbl(Op2)
End Select
Readout = Op1
NumOps = 1
End Select
If LastInput <> "NEG" Then
LastInput = "OPS"
OpFlag = Operator(Index)Caption
End If
End Sub
Private Sub Percent_Click()
Readout = Readout / 100
LastInput = "Ops"
OpFlag = "%"
NumOps = NumOps + 1
DecimalFlag = True
End Sub 窗体代码 VERSION 500
Begin VBForm Calculator
BorderStyle = 1 'Fixed Single
Caption = "计算器"
ClientHeight = 2970
ClientLeft = 2580
ClientTop = 1485
ClientWidth = 3270
ClipControls = 0 'False
BeginProperty Font
Name = "System"
Size = 975
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Icon = "CALCfrx":0000
LinkMode = 1 'Source
LinkTopic = "Form1"
MaxButton = 0 'False
PaletteMode = 1 'UseZOrder
ScaleHeight = 2970
ScaleWidth = 3270
WhatsThisHelp = -1 'True
Begin VBCommandButton Number
Caption = "7"
Height = 480
Index = 7
Left = 120
TabIndex = 7
Top = 600
Width = 480
End
Begin VBCommandButton Number
Caption = "8"
Height = 480
Index = 8
Left = 720
TabIndex = 8
Top = 600
Width = 480
End
Begin VBCommandButton Number
Caption = "9"
Height = 480
Index = 9
Left = 1320
TabIndex = 9
Top = 600
Width = 480
End
Begin VBCommandButton Cancel
Caption = "C"
Height = 480
Left = 2040
TabIndex = 10
Top = 600
Width = 480
End
Begin VBCommandButton CancelEntry
Caption = "CE"
Height = 480
Left = 2640
TabIndex = 11
Top = 600
Width = 480
End
Begin VBCommandButton Number
Caption = "4"
Height = 480
Index = 4
Left = 120
TabIndex = 4
Top = 1200
Width = 480
End
Begin VBCommandButton Number
Caption = "5"
Height = 480
Index = 5
Left = 720
TabIndex = 5
Top = 1200
Width = 480
End
Begin VBCommandButton Number
Caption = "6"
Height = 480
Index = 6
Left = 1320
TabIndex = 6
Top = 1200
Width = 480
End
Begin VBCommandButton Operator
Caption = "+"
Height = 480
Index = 1
Left = 2040
TabIndex = 12
Top = 1200
Width = 480
End
Begin VBCommandButton Operator
Caption = "-"
Height = 480
Index = 3
Left = 2640
TabIndex = 13
Top = 1200
Width = 480
End
Begin VBCommandButton Number
Caption = "1"
Height = 480
Index = 1
Left = 120
TabIndex = 1
Top = 1800
Width = 480
End
Begin VBCommandButton Number
Caption = "2"
Height = 480
Index = 2
Left = 720
TabIndex = 2
Top = 1800
Width = 480
End
Begin VBCommandButton Number
Caption = "3"
Height = 480
Index = 3
Left = 1320
TabIndex = 3
Top = 1800
Width = 480
End
Begin VBCommandButton Operator
Caption = "X"
Height = 480
Index = 2
Left = 2040
TabIndex = 14
Top = 1800
Width = 480
End
Begin VBCommandButton Operator
Caption = "/"
Height = 480
Index = 0
Left = 2640
TabIndex = 15
Top = 1800
Width = 480
End
Begin VBCommandButton Number
Caption = "0"
Height = 480
Index = 0
Left = 120
TabIndex = 0
Top = 2400
Width = 1080
End
Begin VBCommandButton Decimal
Caption = ""
Height = 480
Left = 1320
TabIndex = 18
Top = 2400
Width = 480
End
Begin VBCommandButton Operator
Caption = "="
Height = 480
Index = 4
Left = 2040
TabIndex = 16
Top = 2400
Width = 480
End
Begin VBCommandButton Percent
Caption = "%"
Height = 480
Left = 2640
TabIndex = 17
Top = 2400
Width = 480
End
Begin VBLabel Readout
Alignment = 1 'Right Justify
BackColor = &H0000FFFF&
BorderStyle = 1 'Fixed Single
Caption = "0"
BeginProperty Font
Name = "MS Sans Serif"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00000000&
Height = 375
Left = 120
TabIndex = 19
Top = 105
Width = 3000
End
End
Attribute VB_Name = "Calculator"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
简单的加减乘除。
界面:
窗体包含三个文本框,Text1与Text2用于输入计算数字,Text3用于显示计算结果。四个按钮(加减乘除)。一个标签Label4,用于显示计算方法。
代码:
Private Sub Command1_Click()
a = Text1Text
b = Text2Text
result = a + b
Text3Text = result
Label4Caption = "加法"
End Sub
Private Sub Command2_Click()
Dim a As Double
Dim b As Double
Dim result As Double
a = Text1Text
b = Text2Text
result = a - b
Text3Text = result
Label4Caption = "减法"
End Sub
Private Sub Command3_Click()
Dim a As Double
Dim b As Double
Dim result As Double
a = Text1Text
b = Text2Text
result = a + b
Text3Text = result
Label4Caption = "乘法"
End Sub
Private Sub Command4_Click()
Dim a As Double
Dim b As Double
Dim result As Double
a = Text1Text
b = Text2Text
If b = 0 Then
MsgBox "除数不能为0", 0, "错误"
Else
result = a / b
Text3Text = result
Label4Caption = "除法"
End If
End Sub
不知道你想要哪种,偶是新手,只会最简单的
首先拖出三个text,再拖出4个Command按钮 caption分别为+ - / 这样就准备就绪了
先讲一下简单用法,第一个text是第一个加数/被减数/因数/被除数,而第二个text是第二个加数/减数/因数/除数,第三个text是和/差/积/商,在text1输入一个数,text2输入一个,然后分别点击command“+ - /”text3就会出现结果
然后随便单击一个窗口,把里面的文字删去,再写上:
Private Sub Command1_Click()
Text3Text = Str$(Val(Text1Text) + Val(Text2Text))
End Sub
Private Sub Command2_Click()
Text3Text = Str$(Val(Text1Text) - Val(Text2Text))
End Sub
Private Sub Command3_Click()
Text3Text = Str$(Val(Text1Text) Val(Text2Text))
End Sub
Private Sub Command4_Click()
Text3Text = Str$(Val(Text1Text) / Val(Text2Text))
End Sub
然后运行,应该就可以了
简单点的:
dim 上一个数 as double
dim 下一个数 as double
dim 结果 as double
dim *** 作 as string
private sub 加减乘除(byval sender as object, byval e as eventargs) handles 加click, 减click, 乘click, 除click
上一个数 = cdbl(输入框text) ' 存储当前输入的数值
输入框text = "" ' 等待下一个数值
select case sendertext ' 根据按下的按钮的文本属性确定你要做什么,这里可以用加减乘除号代替
case "加"
*** 作 = "加"
case "减"
*** 作 = "减"
case "乘"
*** 作 = "乘"
case "除"
*** 作 = "除"
end select
end sub
private sub 计算(byval sender as object, byval e as eventargs) handles 等号click
下一个数 = 输入框text
输入框text = ”“
select case *** 作
case "加"
结果 = 上一个数 + 下一个数
case "减"
结果 = 上一个数 - 下一个数
case "乘"
结果 = 上一个数 下一个数
case "除"
结果 = 上一个数 / 下一个数
end select
输入框text = cstr(结果)
上一个数 = 结果
end sub
NET 的哈,我没有 NET 现在,不知道行不行
我是抢劫的,把分给我吧
首先建立一个command1 然后剪切复制 系统提示说已经有一个COMMAND1 是否要建立控件数组?
你点击是 这样重复9次 分别把caption该成0到9 然后建立command2 方法同上,caption为 +-/ 然后是 command3 caption是=
然后输入代码,首先在通用里生命dim shu1,shu2
dim suanfu as string
private sub command1(index)_click
text1text=text1text & commmand1(index)capton
end sub
command2里 输入
shu1=text1text
suanfu=text1text & command(index)capiton
text1text=""
end sub
command3里输入
shu2=text1text
selece case suanfu
case "+"
text1text=shu1+shu2
case "-"
text1text=shu1-shu2
case ""
text1text=shushu2
case "/"
if shu2=0 then
msgbox "分母不能为0"
else text1text=shu1/shu2
end if
end select
end sub
这是最简单的计算器的 如果还需要什么功能 我也可以告诉你的 我学VB3个月了 略有小成 所以想来照顾新手的 我以前也是新手啊 新手见新手两眼泪旺旺 顺便把分给我吧
//等于号处理,将结果保存在m_preStr中
void
CJSQDlg::OnBtnequal()
{
if(
!m_equal
)
{
double
x
=
atof(
m_preStr
);
double
y
=
atof(
m_postStr
);
switch(
m_Fuhao
)
{
case
1:
x
=
x
+
y;
break;
case
2:
x
=
x
-
y;
break;
case
3:
x
=
x
y;
break;
case
4:
x
=
x
/
y;
break;
case
5:
x
=
(int)x
%
(int)y;
break;
}
CString
LSstr;
LSstrFormat(
"%g",
x
);//将x转化成字符串,保存在LSstr中
SetDlgItemText(
IDC_EDIT1,
LSstr
);//显示最后结果
终于编写出来了,代码如下: Option Explicit Dim strNumber As String Dim strPoint As String Dim dblNum1 As Double Dim intOperator As Integer '清除结果 Private Sub cmdGT_Click() txtDisplayText = "0" strNumber = "" strPoint = "" intOperator = 7 End Sub '输入数字 Private Sub cmdNumber_Click(Index As Integer) strNumber = strNumber & cmdNumber(Index)Caption txtDisplayText = strNumber & strPoint End Sub Private Sub cmdOnOff_Click() End End Sub '运算过程 Private Sub cmdOperator_Click(Index As Integer) Dim dblnum2 As Double '是第一次单击运算符时,将输入的值先赋给第一个数,否则赋值给第二个数进行运算 If intOperator = 7 Then dblNum1 = CDbl(txtDisplayText) Else dblnum2 = CDbl(Val(txtDisplayText)) '根据输入的符号进行运算 '求普通运算 Select Case intOperator Case 0 dblNum1 = dblNum1 + dblnum2 Case 1 dblNum1 = dblNum1 - dblnum2 Case 2 dblNum1 = dblNum1 dblnum2 Case 3 If dblnum2 <> 0 Then dblNum1 = dblNum1 / dblnum2 Else MsgBox "除数不能为“0”!请重新输入除数。", vbOKOnly + vbInformation, "除零错误" Index = intOperator End If Case 6 dblNum1 = dblNum1 dblnum2 / 100 End Select End If '取得当前输入的运算符,以做下次运算 intOperator = Index strNumber = "" txtDisplay = CStr(dblNum1) '判断是否为文本框中的数字加点 If Not txtDisplay Like "" Then txtDisplayText = txtDisplayText & "" End If End Sub Private Sub cmdOtherOper_Click(Index As Integer) Dim dblNum As Double '求平方根,平方, dblNum = CDbl(Val(txtDisplayText)) Select Case Index Case 0 '验证数据是否有效 If dblNum >= 0 Then txtDisplayText = CStr(Sqr(dblNum)) Else MsgBox "负数不能开平方根!", _ vbOKOnly + vbCritical, "开平方根错误" End If Case 1 txtDisplayText = CStr(dblNum ^ 2) End Select '判断是否为文本框中的数字加点 If Not txtDisplay Like "" Then txtDisplayText = txtDisplayText & "" End If End Sub Private Sub cmdPoint_Click() strNumber = strNumber & strPoint strPoint = "" End Sub Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) '使被按下的数字键的对应按钮取得焦点 Select Case KeyCode Case 48 To 57 cmdNumber(KeyCode - 48)SetFocus Case 96 To 105 cmdNumber(KeyCode - 96)SetFocus Case Else '使按下的符号键对应的按钮取得焦点 If KeyCode = 107 Or (Shift = vbShiftMask And KeyCode = 187) Then cmdOperator(0)SetFocus cmdOperator_Click (0) ElseIf KeyCode = 109 Or KeyCode = 189 Then cmdOperator(1)SetFocus cmdOperator_Click (1) ElseIf KeyCode = 106 Or (Shift = vbShiftMask And KeyCode = 56) Then cmdOperator(2)SetFocus cmdOperator_Click (2) ElseIf KeyCode = 111 Or KeyCode = 191 Then cmdOperator(3)SetFocus cmdOperator_Click (3) ElseIf KeyCode = 13 Then cmdOperator(7)SetFocus cmdOperator_Click (7) ElseIf KeyCode = 8 Then cmdGTSetFocus Call cmdGT_Click End If End Select End Sub Private Sub Form_KeyPress(KeyAscii As Integer) '将合法的数据输入到文本框 Select Case KeyAscii Case 48 To 58 '调用数字键点击处理程序 cmdNumber_Click KeyAscii - 48 KeyAscii = 0 Case 46 '调用小数点输入 cmdPoint_Click KeyAscii = 0 Case 13 '当敲击回车时,不能触发Form的 KeyUp 事件,因此在这里设置文本框的焦点 txtDisplaySetFocus Case Else KeyAscii = 0 End Select End Sub Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer) txtDisplaySetFocus End Sub Private Sub Form_Load() strNumber = "" strPoint = "" intOperator = 7 End Sub
我是抢劫的,把分给我吧
首先建立一个command1 然后剪切复制 系统提示说已经有一个COMMAND1 是否要建立控件数组?
你点击是 这样重复9次 分别把caption该成0到9 然后建立command2 方法同上,caption为 +-/ 然后是 command3 caption是=
然后输入代码,首先在通用里生命dim shu1,shu2
dim suanfu as string
private sub command1(index)_click
text1text=text1text & commmand1(index)capton
end sub
command2里 输入
shu1=text1text
suanfu=text1text & command(index)capiton
text1text=""
end sub
command3里输入
shu2=text1text
selece case suanfu
case "+"
text1text=shu1+shu2
case "-"
text1text=shu1-shu2
case ""
text1text=shushu2
case "/"
if shu2=0 then
msgbox "分母不能为0"
else text1text=shu1/shu2
end if
end select
end sub
这是最简单的计算器的 如果还需要什么功能 我也可以告诉你的 我学VB3个月了 略有小成 所以想来照顾新手的 我以前也是新手啊 新手见新手两眼泪旺旺 顺便把分给我吧
以上就是关于如何用VB编写一个加减乘除计算器只要代码!全部的内容,包括:如何用VB编写一个加减乘除计算器只要代码!、用VB设计一个计算器程序,急、如何用vb编写简单计算器等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)