求VB简易计算器的代码

1-9,+,——,*,/。只有一个空文本框 。
要简单点,不要太复杂了

Dim dflag As Integer

Dim I As Integer

Dim opnre As Integer

Dim prev As Double

Dim oflag As Integer

Dim ind As Integer

Dim soundbz As Boolean

Dim ProgramPath As String

Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal Cx As Long, ByVal Cy As Long, ByVal wFlags As Long) As Long

Public Sub SetOnTop(ByVal IsOnTop As Integer)

   Dim rtn As Long

   If IsOnTop = 1 Then

      '将窗口置于最上

      rtn = SetWindowPos(Me.hwnd, -1, 0, 0, 0, 0, 3)

   Else

      rtn = SetWindowPos(Me.hwnd, -2, 0, 0, 0, 0, 3)

   End If

End Sub

Private Sub Check1_Click()

   If Check1.Value = 1 Then

      SetOnTop 1

   Else

      SetOnTop 0

   End If

End Sub

Private Sub Command1_Click(Index As Integer)

    text1 = Trim(text1)

    If Len(text1) > 20 Then

       Beep

       Exit Sub

    End If

    soundbz = False

    MMControl1.Command = "Close"

     

    MMControl1.FileName = ProgramPath & "/声音文件/" & Index & ".wav"

    MMControl1.Command = "Open"

    MMControl1.Command = "Play"

    

    If ind = 4 Then

        prev = 0

        text1.Caption = " "

        ind = 0

    End If

    opnre = 0

    If oflag = 0 Then

        text1.Caption = " "

    End If

    oflag = 1

    If Command1(Index).Caption <> "." Then

           If text1.Caption <> " 0" Then

                text1.Caption = text1.Caption & Command1(Index).Caption

                Text = Mid(text1, 1, 1)

                If Text = "." Then

                   text1 = "0" & text1

                End If

            Else

                text1.Caption = " " & Command1(Index).Caption

            End If

    Else

            If dflag = 0 Then

                text1.Caption = text1.Caption & "."

                

                dflag = 1

            Else

            Command6.SetFocus

                Exit Sub

            End If

     End If

     Command6.SetFocus

End Sub

Private Sub Command1_KeyPress(Index As Integer, KeyAscii As Integer)

   If KeyAscii = 13 Then KeyAscii = 0

   

End Sub

Private Sub Command2_Click(Index As Integer)

soundbz = False

    MMControl1.Command = "Close"

    If Index = 0 Then MMControl1.FileName = ProgramPath & "/声音文件/加.wav"

    If Index = 1 Then MMControl1.FileName = ProgramPath & "/声音文件/减.wav"

    If Index = 3 Then MMControl1.FileName = ProgramPath & "/声音文件/乘.wav"

    If Index = 2 Then MMControl1.FileName = ProgramPath & "/声音文件/除.wav"

    If Index = 4 Then

       MMControl1.FileName = ProgramPath & "/声音文件/等于.wav"

       soundbz = True

    End If

    MMControl1.Command = "Open"

    MMControl1.Command = "Play"

        If opnre = 0 Or Index = 4 Then

            If ind = 0 Then

                 prev = prev + Val(text1.Caption)

            ElseIf ind = 1 Then

                 prev = prev - Val(text1.Caption)

            ElseIf ind = 2 Then

                If Val(text1.Caption) = 0 Then

                    text1 = "错误"

                    Beep

                    Command6.SetFocus

                    Exit Sub

                Else

                 prev = prev / Val(text1.Caption)

                End If

            ElseIf ind = 3 Then

                 prev = prev * Val(text1.Caption)

            End If

            text1.Caption = Str(prev)

            oflag = 0

        End If

        opnre = 1

        ind = Index

        dflag = 0

        

        Command6.SetFocus

        If Index = 4 Then

           If Option1.Value = True Then text1 = Trim(Round(text1, 2))

           If Option2.Value = True Then text1 = Trim(Round(text1, 3))

           If Option3.Value = True Then text1 = Trim(Round(text1, 4))

           If Option4.Value = True Then text1 = Trim(text1)

           If text1 <> 0 Then

              Text = Mid(text1, 1, 1)

              If Text = "." Then

                text1 = "0" & text1

              End If

           End If

           ls = Len(text1)

           If ls <> 0 Then

              For I = 1 To ls

                 t = Mid(text1, I, 1)

                 DoEvents

                 Do While (MMControl1.Mode <> 525) And soundbz

                   

                    DoEvents

                 Loop

                 MMControl1.Command = "Close"

                 If t <> "." Then

                    If t = "-" Then

                       MMControl1.FileName = ProgramPath & "/声音文件/负.wav"

                    Else

                       MMControl1.FileName = ProgramPath & "/声音文件/" & t & ".wav"

                    End If

                 Else

                    MMControl1.FileName = ProgramPath & "/声音文件/10.wav"

                   

                 End If

                 MMControl1.Command = "Open"

                 MMControl1.Command = "Play"

              Next I

           End If

        End If

End Sub

Private Sub Command2_KeyPress(Index As Integer, KeyAscii As Integer)

   If KeyAscii = 13 Then KeyAscii = 0

End Sub

Private Sub Command3_Click()

soundbz = False

    MMControl1.Command = "Close"

    MMControl1.FileName = ProgramPath & "/声音文件/归零.wav"

    MMControl1.Command = "Open"

    MMControl1.Command = "Play"

    text1.Caption = " 0"

    Command6.SetFocus

End Sub

Private Sub Command3_KeyPress(KeyAscii As Integer)

   If KeyAscii = 13 Then KeyAscii = 0

End Sub

Private Sub Command4_Click()

soundbz = False

    MMControl1.Command = "Close"

    MMControl1.FileName = ProgramPath & "/声音文件/清除.wav"

    MMControl1.Command = "Open"

    MMControl1.Command = "Play"

    

    dflag = 0

    prev = 0

    oflag = 0

    ind = 0

    opnre = 0

    text1.Caption = " 0"

Command6.SetFocus

End Sub

Private Sub Command4_KeyPress(KeyAscii As Integer)

   If KeyAscii = 13 Then KeyAscii = 0

End Sub

Private Sub Command5_Click()

    soundbz = False

    SaveSetting App.EXEName, "保留", "n1", Option1.Value

    SaveSetting App.EXEName, "保留", "n2", Option2.Value

    SaveSetting App.EXEName, "保留", "n3", Option3.Value

    SaveSetting App.EXEName, "保留", "n4", Option4.Value

    SaveSetting App.EXEName, "置顶", "yorn", Check1.Value

    ' MMControl1.Command = "Close"

    Unload Me

End Sub

Private Sub Command5_KeyPress(KeyAscii As Integer)

   If KeyAscii = 13 Then KeyAscii = 0

End Sub

Private Sub Command6_Click()

Command2_Click (4)

End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)

   If text1 = "错误!" Then

     Command4_Click

   End If

   If KeyAscii = Asc(".") Then

        I = 10

         Command1_Click (I)

   ElseIf KeyAscii = Asc("0") Then

        I = 0

         Command1_Click (I)

   ElseIf KeyAscii = Asc("1") Then

        I = 1

          Command1_Click (I)

   ElseIf KeyAscii = Asc("2") Then

        I = 2

          Command1_Click (I)

   ElseIf KeyAscii = Asc("3") Then

        I = 3

          Command1_Click (I)

   ElseIf KeyAscii = Asc("4") Then

        I = 4

          Command1_Click (I)

   ElseIf KeyAscii = Asc("5") Then

        I = 5

          Command1_Click (I)

   ElseIf KeyAscii = Asc("6") Then

        I = 6

          Command1_Click (I)

   ElseIf KeyAscii = Asc("7") Then

        I = 7

          Command1_Click (I)

   ElseIf KeyAscii = Asc("8") Then

        I = 8

          Command1_Click (I)

   ElseIf KeyAscii = Asc("9") Then

        I = 9

          Command1_Click (I)

   ElseIf KeyAscii = Asc("0") Then

        I = 0

          Command1_Click (I)

   ElseIf KeyAscii = Asc("+") Then

        I = 0

          Command2_Click (I)

   ElseIf KeyAscii = Asc("+") Then

        I = 0

          Command2_Click (I)

   ElseIf KeyAscii = Asc("-") Then

        I = 1

          Command2_Click (I)

   ElseIf KeyAscii = Asc("/") Then

        I = 2

          Command2_Click (I)

   ElseIf KeyAscii = Asc("*") Then

        I = 3

          Command2_Click (I)

   ElseIf KeyAscii = Asc("=") Or KeyAscii = 13 Then

          I = 4

          Command2_Click (I)

   ElseIf KeyAscii = Asc("c") Or KeyAscii = Asc("C") Or KeyAscii = 27 Then

          Command4_Click

   ElseIf KeyAscii = Asc("d") Or KeyAscii = Asc("D") Then

        Command3_Click

   End If

End Sub

Private Sub Form_Load()

    If App.PrevInstance = True Then

      Unload Me

      MsgBox "程序正在运行", 64, "提示"

      End

    End If

    soundbz = True

    dflag = 0

    prev = 0

    oflag = 0

    ind = 0

    opnre = 0

    Clipboard.Clear

    MMControl1.DeviceType = "WaveAudio"

    ProgramPath = App.Path

    Option1.Value = GetSetting(App.EXEName, "保留", "n1", True)

    Option2.Value = GetSetting(App.EXEName, "保留", "n2", False)

    Option3.Value = GetSetting(App.EXEName, "保留", "n3", False)

    Option4.Value = GetSetting(App.EXEName, "保留", "n4", False)

    Check1.Value = GetSetting(App.EXEName, "置顶", "yorn", 0)

End Sub

Private Sub Form_Unload(Cancel As Integer)

    MMControl1.Command = "Close"

    End

End Sub

温馨提示:内容为网友见解,仅供参考
第1个回答  2010-11-17

以上的回答这么麻烦,还让人活吗?只需如此:

涉及控件:text,com1,com2三种

属性设置:无

命令如下:

通用命令

Option Explicit

Dim v As Boolean

Dim s As Integer

Dim x As Double

Dim y As Double

按钮一的命令

Private Sub Command1_Click(Index As Integer)

If Form1.Tag = "s" Then

If Index = 10 Then

Text1.Text = "0"

Else

Text1.Text = Command1(Index).Caption

End If

Form1.Tag = ""

Else

Text1.Text = Text1.Text & Command1(Index).Caption

End If

End Sub

按钮二的命令

Private Sub Command2_Click(Index As Integer)

Form1.Tag = "s"

If v Then

x = Val(Text1.Text)

v = Not v

Else

y = Val(Text1.Text)

Select Case s

Case 0

Text1.Text = x + y

Case 1

Text1.Text = x - y

Case 2

Text1.Text = x * y

Case 3

If y <> 0 Then

Text1.Text = x / y

Else

MsgBox ("不能以0为除数")

Text1.Text = x

v = False

End If

Case 4

y = 0

v = False

End Select

x = Val(Text1.Text)

End If

s = Index

End Sub

本回答被提问者和网友采纳
第2个回答  2009-10-09
Public choice As String
Public current As Double
Public prev As Double
Public counter As Integer

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub ac_Click()
Text1.Text = ""
prev = current = 0
'dz.Visible = False
'asterisk.Enabled = False
'equal.Enabled = False
'expo.Enabled = False
'logn.Enabled = False
'logten.Enabled = False
'minus.Enabled = False
'plus.Enabled = False
'sign.Enabled = False
'slash.Enabled = False
'sroot.Enabled = False
'cosine.Enabled = False
'sine.Enabled = False
'tangent.Enabled = False
'invsine.Visible = False
'invcos.Visible = False
'invtan.Visible = False
'expo.Visible = False
End Sub

Private Sub asterisk_Click()
Text1.Text = ""
choice = "*"
prev = current
current = 0
End Sub

Private Sub Command1_Click(Index As Integer)
Text1.Text = Text1.Text & Command1(Index).Caption
current = Text1.Text
asterisk.Enabled = True
equal.Enabled = True
expo.Enabled = True
' logn.Enabled = True
' logten.Enabled = True
minus.Enabled = True
plus.Enabled = True
sign.Enabled = True
slash.Enabled = True
sroot.Enabled = True
' tangent.Enabled = True
'' cosine.Enabled = True
' sine.Enabled = True
End Sub

Private Sub Command3_Click()
Dim MyAngle, MyHSin
MyAngle = 1.3 ' 定义角度(以"弧度"为单位)
MsgBox (Exp(MyAngle) - Exp(-1 * MyAngle)) / 2 ' 计算双曲正弦函数值(sin())

End Sub

Private Sub cosine_Click()
Text1.Text = Cos(current * 3.14159265358979 / 180)
current = Cos(current * 3.14159265358979 / 180)
End Sub

Private Sub equal_Click()
On Error GoTo MyErr
If choice = "+" Then
current = prev + current
Text1.Text = current
ElseIf choice = "-" Then
current = prev - current
Text1.Text = current
ElseIf choice = "*" Then
current = prev * current
Text1.Text = current
ElseIf choice = "/" And current <> 0 Then
current = prev / current
Text1.Text = current
Else
Text1.Text = "Press AC to continue"
dz.Visible = True
dz.Caption = "Division by ZERO"
End If
If current < 0 Then
sroot.Enabled = False
Else
sroot.Enabled = True
End If

Exit Sub
MyErr:
MsgBox Err.Description, vbInformation, "信息提示"
End Sub

Private Sub expo_Click()
On Error GoTo MyErr

Exit Sub
MyErr:
MsgBox Err.Description, vbInformation, "信息提示"
End Sub

Private Sub expo10_Click()
Dim i As Integer
Dim j As Double
j = current
current = 1
For i = 1 To j Step 1
current = current * 10
Next i
Text1.Text = current

End Sub

Private Sub Form_Unload(Cancel As Integer)
Dim value As Integer

End Sub

Private Sub invcos_Click()
Dim temp As Double
temp = current * 3.14159265358979 / 180
If (temp <= 1 And temp >= -1) Then
Text1.Text = 1 / Cos(temp)
current = 1 / Cos(temp)
End If
invsine.Visible = False
invcos.Visible = False
invtan.Visible = False
expo.Visible = False
expo10.Visible = False
End Sub

Private Sub invsine_Click()
Dim temp As Double
temp = current * 3.14159265358979 / 180
If (temp <= 1 And temp >= -1) Then
Text1.Text = 1 / Sin(temp)
current = 1 / Sin(temp)
End If

End Sub

Private Sub invtan_Click()
Text1.Text = 1 / Tan(current * 3.14159265358979 / 180)
current = 1 / Tan(current * 3.14159265358979 / 180)
invsine.Visible = False
invcos.Visible = False
invtan.Visible = False
expo.Visible = False
expo10.Visible = False
End Sub

Private Sub logn_Click()
Text1.Text = Log(current)
current = Log(current)
End Sub

Private Sub logten_Click()
Text1.Text = Log(current) / Log(10)
current = Log(current) / Log(10)
End Sub

Private Sub mode_MouseDown(Button As Integer, shift As Integer, X As Single, Y As Single)
counter = counter + 1
If counter > 2 Then
counter = 1
End If
If counter = 2 Then
displaylb.Caption = "Normal Calculator"
displaylb.ForeColor = &HFFC0C0
Form1.Width = 4830
sine.Visible = False
cosine.Visible = False
tangent.Visible = False
logn.Visible = False
logten.Visible = False
random.Enabled = False
random.Visible = False
shiftb.Visible = False
recall.Visible = False
Else
displaylb.Caption = " Scientific Calculator"
displaylb.ForeColor = &HC0&
Form1.Width = 5670
sine.Visible = True
cosine.Visible = True
tangent.Visible = True
logn.Visible = True
logten.Visible = True
random.Enabled = True
random.Visible = True
shiftb.Visible = True
recall.Visible = True
End If
End Sub

Private Sub point_Click()
If Text1.Text = "" Then
Text1.Text = "0."
Else
Text1.Text = Text1.Text & "."
End If
End Sub

Private Sub random_Click()
Text1.Text = (2.1021020134 * Rnd + 1)
End Sub

Private Sub recall_Click()
Text1.Text = current
End Sub

Private Sub shiftb_Click()
invsine.Visible = True
invcos.Visible = True
invtan.Visible = True
expo.Visible = True
expo10.Visible = True
If Text1.Text <= 1 And Text1.Text >= -1 Then
invsine.Enabled = True
invcos.Enabled = True
End If
invtan.Enabled = True
expo.Enabled = True
expo10.Enabled = True
End Sub

Private Sub sign_Click()
If current < 0 Then
Text1.Text = -current
current = -current
Else
Text1.Text = "-" & Text1.Text
current = -current
End If
If current < 0 Then
sroot.Enabled = False
Else
sroot.Enabled = True
End If
End Sub

Private Sub minus_Click()
Text1.Text = ""
choice = "-"
prev = current
current = 0
End Sub

Private Sub plus_Click()
Text1.Text = ""
choice = "+"
prev = current
current = 0
End Sub

Private Sub sine_Click()
Text1.Text = Sin(current * 3.14159265358979 / 180)
current = Sin(current * 3.14159265358979 / 180)
End Sub

Private Sub slash_Click()
Text1.Text = ""
choice = "/"
prev = current
current = 0
End Sub

Private Sub sroot_Click()
On Error GoTo MyErr
Text1.Text = Sqr(current)
current = Sqr(current)

Exit Sub
MyErr:
MsgBox Err.Description, vbInformation, "信息提示"
End Sub

Private Sub tangent_Click()
Text1.Text = Tan(current * 3.14159265358979 / 180)
current = Tan(current * 3.14159265358979 / 180)
End Sub

兄弟你可真行!!要代码也不给分,算了,我就做回好人吧!
第3个回答  2010-11-15
前后台代码....
'请把下面的保存为 form1.frm

VERSION 5.00
Begin VB.Form Calculator
BorderStyle = 1 'Fixed Single
Caption = "计算器"
ClientHeight = 2970
ClientLeft = 2580
ClientTop = 1485
ClientWidth = 3270
ClipControls = 0 'False
BeginProperty Font
Name = "System"
Size = 9.75
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Icon = "CALC.frx":0000
LinkMode = 1 'Source
LinkTopic = "Form1"
MaxButton = 0 'False
PaletteMode = 1 'UseZOrder
ScaleHeight = 2970
ScaleWidth = 3270
WhatsThisHelp = -1 'True
Begin VB.CommandButton Number
Caption = "7"
Height = 480
Index = 7
Left = 120
TabIndex = 7
Top = 600
Width = 480
End
Begin VB.CommandButton Number
Caption = "8"
Height = 480
Index = 8
Left = 720
TabIndex = 8
Top = 600
Width = 480
End
Begin VB.CommandButton Number
Caption = "9"
Height = 480
Index = 9
Left = 1320
TabIndex = 9
Top = 600
Width = 480
End
Begin VB.CommandButton Cancel
Caption = "C"
Height = 480
Left = 2040
TabIndex = 10
Top = 600
Width = 480
End
Begin VB.CommandButton CancelEntry
Caption = "CE"
Height = 480
Left = 2640
TabIndex = 11
Top = 600
Width = 480
End
Begin VB.CommandButton Number
Caption = "4"
Height = 480
Index = 4
Left = 120
TabIndex = 4
Top = 1200
Width = 480
End
Begin VB.CommandButton Number
Caption = "5"
Height = 480
Index = 5
Left = 720
TabIndex = 5
Top = 1200
Width = 480
End
Begin VB.CommandButton Number
Caption = "6"
Height = 480
Index = 6
Left = 1320
TabIndex = 6
Top = 1200
Width = 480
End
Begin VB.CommandButton Operator
Caption = "+"
Height = 480
Index = 1
Left = 2040
TabIndex = 12
Top = 1200
Width = 480
End
Begin VB.CommandButton Operator
Caption = "-"
Height = 480
Index = 3
Left = 2640
TabIndex = 13
Top = 1200
Width = 480
End
Begin VB.CommandButton Number
Caption = "1"
Height = 480
Index = 1
Left = 120
TabIndex = 1
Top = 1800
Width = 480
End
Begin VB.CommandButton Number
Caption = "2"
Height = 480
Index = 2
Left = 720
TabIndex = 2
Top = 1800
Width = 480
End
Begin VB.CommandButton Number
Caption = "3"
Height = 480
Index = 3
Left = 1320
TabIndex = 3
Top = 1800
Width = 480
End
Begin VB.CommandButton Operator
Caption = "X"
Height = 480
Index = 2
Left = 2040
TabIndex = 14
Top = 1800
Width = 480
End
Begin VB.CommandButton Operator
Caption = "/"
Height = 480
Index = 0
Left = 2640
TabIndex = 15
Top = 1800
Width = 480
End
Begin VB.CommandButton Number
Caption = "0"
Height = 480
Index = 0
Left = 120
TabIndex = 0
Top = 2400
Width = 1080
End
Begin VB.CommandButton Decimal
Caption = "."
Height = 480
Left = 1320
TabIndex = 18
Top = 2400
Width = 480
End
Begin VB.CommandButton Operator
Caption = "="
Height = 480
Index = 4
Left = 2040
TabIndex = 16
Top = 2400
Width = 480
End
Begin VB.CommandButton Percent
Caption = "%"
Height = 480
Left = 2640
TabIndex = 17
Top = 2400
Width = 480
End
Begin VB.Label 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
' --------------------------------------------------------------------------
' 版权所有 (C) 1994 Microsoft Corporation
'
' 您可以免费以任何方式使用、修改、复制并分发您认为有用的
' 示例应用程序文件 (或任何修改过的版本)。Microsoft 对任何
' 示例应用程序文件不做任何保证,不负任何责任和义务。
' --------------------------------------------------------------------------
Option Explicit
Dim 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 = 0
Op2 = 0
Form_Load
End Sub

' CE (取消输入) 按钮的 Click 事件过程
Private Sub CancelEntry_Click()
Readout = Format(0, "0.")
DecimalFlag = False
LastInput = "CE"
End Sub

' 小数点 (.) 按钮的 Click 事件过程
' 如果上一次按键为运算符,初始化 readout 为 "0.";
' 否则显示时追加一个小数点
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.")
'Decimal.Caption = Format(0, ".")
End Sub

' 数字键 (0-9) 的 Click 事件过程
' 向显示中的数追加新数
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

' 运算符 (+, -, x, /, =) 的 Click 事件过程
' 如果接下来的按键是数字键,增加 NumOps。
' 如果有一个操作数,则设置 Op1。
' 如果有两个操作数,则将 Op1 设置为 Op1 与
' 当前输入字符串的运算结果,并显示结果
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

' 百分比键 (%) 的 Click 事件过程
' 计算并显示第一个操作数的百分数
Private Sub Percent_Click()
Readout = Readout / 100
LastInput = "Ops"
OpFlag = "%"
NumOps = NumOps + 1
DecimalFlag = True
End Sub
第4个回答  2010-11-16
'============== 你要做的准备工作:==========================
' 1、在窗体上放一个textbox文本框。和一个command按钮。
' 2、把按钮的index属性设置为0。(或者复制一个command1按钮,
' 在弹出的窗口中选择是,然后删除掉那个复制的按钮.)
' 3、把以下代码粘贴到代码区。
'由于时间关系,没有对输入错误、除数为0等问题进行处理
'=======个人水平有限,难免出现错误,望请见谅。Loumou=========

Private Sub Command1_Click(Index As Integer)
Dim aa As String, bb As String
Dim cc(2) As Double
Dim i As Integer, n As Integer, m As Integer
n = 0: m = 0
Select Case Index
Case 0 To 9: Text1.Text = Text1.Text & Index
Case 10 To 13: Text1.Text = Text1.Text & Command1(Index).Caption
Case 14:
aa = Text1.Text
For i = 1 To Len(aa)
bb = Mid(aa, i, 1)
n = InStr(1, "+-×÷", bb)
If n <> 0 Then
If m = 0 Then
cc(0) = Val(Mid(aa, 1, i - 1))
cc(1) = n
m = i
Else
cc(2) = Val(Mid(aa, m + 1, i - 1 - m))
Select Case cc(1)
Case 1: cc(0) = Val(Val(cc(0)) + Val(cc(2)))
Case 2: cc(0) = Val(Val(cc(0)) - Val(cc(2)))
Case 3: cc(0) = Val(Val(cc(0)) * Val(cc(2)))
Case 4: cc(0) = Val(Val(cc(0)) / Val(cc(2)))
End Select
cc(1) = n: m = i
End If
End If
Next i
If m <> 0 Then
cc(2) = Val(Mid(aa, m + 1, i - 1 - m))
Select Case cc(1)
Case 1: cc(0) = Val(Val(cc(0)) + Val(cc(2)))
Case 2: cc(0) = Val(Val(cc(0)) - Val(cc(2)))
Case 3: cc(0) = Val(Val(cc(0)) * Val(cc(2)))
Case 4: cc(0) = Val(Val(cc(0)) / Val(cc(2)))
End Select
End If
Text1.Text = Text1.Text & "=" & cc(0)
Case 15: Text1.Text = ""
End Select
End Sub

Private Sub Form_Load()
'------- 窗体属性设置 --------
Me.Width = 3450
Me.Height = 3600
'------- 文本框属性设置 --------
Text1.Alignment = 1
Text1.Left = 360
Text1.Top = 240
Text1.Width = 2535
Text1.Height = 375
Text1.Appearance = 0
Text1.Text = ""
Text1.FontSize = 11
'------- 按钮属性设置 --------
With Command1(0)
.Caption = "0"
.Left = 480
.Top = 960
.Height = 495
.Width = 495
End With
Dim i As Integer
For i = 1 To 15
Load Command1(i)
Command1(i).Top = (i \ 4) * 500 + Command1(0).Top
Command1(i).Left = (i Mod 4) * 500 + Command1(0).Left
Command1(i).Caption = IIf(i < 10, i, IIf(i = 10, "+", IIf(i = 11, "-", IIf(i = 12, "×", IIf(i = 13, "÷", IIf(i = 14, "=", "C"))))))
Command1(i).Visible = True
Next i

'------- 窗体属性设置 --------

End Sub
相似回答