当前位置:编程学习 > VB >>

VB制作简单计算器问题,关于加法

这是数字0的代码:
Private Sub Command0_Click(Index As Integer)
If Text1.Text = "0" Then
Text1.Text = "0"
Else
Text1.Text = Text1.Text + "0"
End If
End Sub
其余数字与它一样
这是加号的代码
Private Sub Command17_Click(Index As Integer)
Text1.Text = ""
x = "+"
pre = now
End Sub
其中pre , now定义为DOUBLE
下面是等号的代码,只列出了加法的部分
Private Sub Command16_Click(Index As Integer)
If x = "+" Then
now = pre + now
Text1.Text = now
End If
End Sub
由于加法怎么也算不对,所以拿上来请各位高手看看错误在哪里
谢谢各位!!!
答案:初学者的VB计算器

窗体代码如下

Option Explicit

Dim blnStratrOperationFlag As Boolean   '是否开始运算
Dim lngOperationFlag As Long            '运算标志
Dim dblFirstOperationValue As Double    '先前的操作数

Private Sub cmdValuedDecimal_Click(Index As Integer) '数字及小数点

    Select Case Index

        Case 0

        AddTextOperationValue "0"

        Case 1

        AddTextOperationValue "1"

        Case 2

        AddTextOperationValue "2"

        Case 3

        AddTextOperationValue "3"

        Case 4

        AddTextOperationValue "4"

        Case 5

        AddTextOperationValue "5"

        Case 6

        AddTextOperationValue "6"

        Case 7

        AddTextOperationValue "7"

        Case 8

        AddTextOperationValue "8"

        Case 9

        AddTextOperationValue "9"

        Case 10

        AddTextOperationValue "."

    End Select

End Sub

Private Sub AddTextOperationValue(strOperationValue As String)

    If Len(txtOperationValue) > 8 And blnStratrOperationFlag = False Then Exit Sub '判断是否输入数字超过9个

    If txtOperationValue = "0" And strOperationValue = "0" Then Exit Sub           '是否什么也没输入或为0时候输入0则退出

    If lngOperationFlag <> 0 And blnStratrOperationFlag = True Then                '如果有操作符且开始运算为真则

        txtOperationValue = ""
        blnStratrOperationFlag = False

    End If

    If txtOperationValue = "0" And strOperationValue <> "." Then txtOperationValue = "" '头次输入数字

    If Right$(txtOperationValue, 1) = "." And strOperationValue = "." Then Exit Sub     '避免多次输入小数点

    txtOperationValue = txtOperationValue & strOperationValue        '累加字符

End Sub

Private Sub cmdOperation_Click(Index As Integer)  '+ - * / =

    Select Case Index

        Case 0

        lngOperationFlag = 1

        Evaluate

        Case 1

        lngOperationFlag = 2

        Evaluate

        Case 2

        lngOperationFlag = 3

        Evaluate

        Case 3

        lngOperationFlag = 4

        Evaluate

        Case 4

        DisposeResult

    End Select

End Sub

Private Sub Evaluate()      '四则运算赋值

    dblFirstOperationValue = Val(txtOperationValue)

    blnStratrOperationFlag = True
    txtOperationValue = ""

End Sub

Private Sub DisposeResult()  '处理计算结果

    On Error GoTo ToExit     '打开错误陷阱

    Select Case lngOperationFlag  '操作标志

        Case 1

        txtOperationValue = dblFirstOperationValue + Val(txtOperationValue)

        Case 2

        txtOperationValue = dblFirstOperationValue - Val(txtOperationValue)

        Case 3

        txtOperationValue = dblFirstOperationValue * Val(txtOperationValue)

        Case 4

        txtOperationValue = dblFirstOperationValue / Val(txtOperationValue)

    End Select

    lngOperationFlag = 0     '操作标志清0

    Exit Sub

ToExit:

    MsgBox "除数不能为0!", vbOKOnly, "错误"

    Resume Next

End Sub

Private Sub cmdClearAll_Click()        'C按钮清除所有的运算结果

    lngOperationFlag = 0
    txtOperationValue = "0"

End Sub

Private Sub cmdClearLastInput_Click()  'CE按钮清除最后一次输入的数字

    txtOperationValue = "0"

End Sub

Private Sub Form_Unload(Cancel As Integer)

    Set frmCalculator = Nothing

 
now已经是给vb使用的时间函数了,你是不能使用的
首先不能用NOW来定义,它是内置函数名。改为NOW1。
我的代码如下:
Dim X As String
Dim pre As Double
Dim now1 As Double

'这是加号的代码
Private Sub Command17_Click(Index As Integer)
pre = Val(Text1.Text)
Text1.Text = ""
X = "+"
'pre = Now'这一句不要
End Sub
'其中pre , now1定义为DOUBLE
'下面是等号的代码,只列出了加法的部分
Private Sub Command16_Click(Index As Integer)
If X = "+" Then
now1 = Val(Text1.Text)

now1 = pre + now1
Text1.Text = now1
End If
End Sub


数字输入处理你的方法也可以,但有更简单的处理办法,设置控件数组command0(0)、command0(1)、....、command0(9)一共10个,分别将它们的caption属性值设为0、1、...、9,这样就可以用下面一句代码来处理0-9所有数字输入问题。效果一样,但代码简洁的多。

Private Sub Command0_Click(Index As Integer)

Text1.Text = Text1.Text & Command0(Index).Caption
End Sub

上一个:如何用VB给文件添加属性备注?
下一个:vb中 如何自动使用webbrowser登录代理服务器

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,