计算器代码,如何在不按等号(=)键的情况下获得答案

时间:2014-08-30 23:36:12

标签: html asp.net

我的计算器代码正常运行。我可以按5 + 9 = 14,但我想按5 = 9 + 5 + 2 = 21而不按=按钮。我试图在输入的所有操作数的末尾得到答案,而不只是5 + 9。到目前为止,这是我的代码。

Public Class Already

    Inherits System.Web.UI.Page

    Dim total2 As Double
    Dim answer As Double
    Dim runningTotal As Double 
    Dim sign As String

    Private Sub Form_Load()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub

     ' Code for the numbers                                               '
    Protected Sub btnZero_Click(sender As Object, e As EventArgs) Handles btnZero.Click
        If txtDisplay.Text = "0" Then txtDisplay.Text = ""
        txtDisplay.Text = txtDisplay.Text + "0"
    End Sub

    Protected Sub btnOne_Click(sender As Object, e As EventArgs) Handles btnOne.Click
        If txtDisplay.Text = "0" Then txtDisplay.Text = ""
        txtDisplay.Text = txtDisplay.Text + "1"
    End Sub

    Protected Sub btnTwo_Click(sender As Object, e As EventArgs) Handles btnTwo.Click
        If txtDisplay.Text = "0" Then txtDisplay.Text = ""
        txtDisplay.Text = txtDisplay.Text + "2"
    End Sub

    Protected Sub btnThree_Click(sender As Object, e As EventArgs) Handles btnThree.Click
        If txtDisplay.Text = "0" Then txtDisplay.Text = ""
        txtDisplay.Text = txtDisplay.Text + "3"
    End Sub

    Protected Sub btnFour_Click(sender As Object, e As EventArgs) Handles btnFour.Click
        If txtDisplay.Text = "0" Then txtDisplay.Text = ""
        txtDisplay.Text = txtDisplay.Text + "4"
    End Sub

    Protected Sub btnFive_Click(sender As Object, e As EventArgs) Handles btnFive.Click
        If txtDisplay.Text = "0" Then txtDisplay.Text = ""
        txtDisplay.Text = txtDisplay.Text + "5"
    End Sub

    Protected Sub btnSix_Click(sender As Object, e As EventArgs) Handles btnSix.Click
        If txtDisplay.Text = "0" Then txtDisplay.Text = ""
        txtDisplay.Text = txtDisplay.Text + "6"
    End Sub

    Protected Sub btnSeven_Click(sender As Object, e As EventArgs) Handles btnSeven.Click
        If txtDisplay.Text = "0" Then txtDisplay.Text = ""
        txtDisplay.Text = txtDisplay.Text + "7"
    End Sub

    Protected Sub btnEight_Click(sender As Object, e As EventArgs) Handles btnEight.Click
        If txtDisplay.Text = "0" Then txtDisplay.Text = ""
        txtDisplay.Text = txtDisplay.Text + "8"
    End Sub

    Protected Sub btnNine_Click(sender As Object, e As EventArgs) Handles btnNine.Click
        If txtDisplay.Text = "0" Then txtDisplay.Text = ""
        txtDisplay.Text = txtDisplay.Text + "9"
    End Sub

    Protected Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        txtDisplay.Text = "0"
        Label1.Text = ""
        Label2.Text = ""
        Label3.Text = ""
        Label4.Text = ""
        txtDisplay2.Text = ""
        lblDisplay.Text = "0"
        lblDisplay.Text = ""
        lblSuperScript.Text = ""
    End Sub

     'Code for the plus button                                    '
    Protected Sub btnPlus_Click(sender As Object, e As EventArgs) Handles btnPlus.Click
        Label1.Text = txtDisplay.Text
       Label2.Text = "+"        
        txtDisplay.Text = ""
    End Sub

    'Code for the equals button                              '
    Protected Sub btnEquals_Click(sender As Object, e As EventArgs) Handles btnEquals.Click

        Label3.Text = txtDisplay.Text
        runningTotal = Label1.Text
        sign = Label2.Text
        total2 = Label3.Text


        Select Case (sign)
            Case Is = "+"
                answer = runningTotal + total2
            Case "-"
                answer = runningTotal - total2
            Case "*"
                answer = runningTotal * total2
            Case "/"
                answer = runningTotal / total2

        End Select 
       txtDisplay.Text = answer.ToString()
        runningTotal = answer
    End Sub

    Protected Sub btnBackSpace_Click(sender As Object, e As EventArgs) Handles btnBackSpace.Click
        If txtDisplay.Text.Length > 1 Then
            txtDisplay.Text = Mid(txtDisplay.Text, 1, Len(txtDisplay.Text) - 1)
        ElseIf txtDisplay.Text.Length = 1 Then
            txtDisplay.Text = 0
        Else
            Exit Sub
        End If
    End Sub

End Class

1 个答案:

答案 0 :(得分:0)

试试这个

<强> ASPX

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Calculator.aspx.vb" Inherits="Calculator" %>
<!DOCTYPE html>
<html>
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">

    <asp:ScriptManager runat="server" />

    <asp:UpdatePanel runat="server">
        <ContentTemplate>
            <p>
                <asp:Label ID="EquationLabel" runat="server" Text="&nbsp;" />
            </p>
            <p>
                <asp:Label ID="ResultLabel" runat="server" Text="0" />
            </p>
            <table>
            <tr>
                <td></td>
                <td><asp:Button runat="server" Text="CE" OnClick="back_click" /></td>
                <td><asp:Button runat="server" Text="C" OnClick="clear_click" /></td>
                <td><asp:Button runat="server" Text="=" OnClick="equal_click" /></td>
            </tr>
            <tr>
                <td><asp:Button runat="server" Text="7" OnClick="number_click" /></td>
                <td><asp:Button runat="server" Text="8" OnClick="number_click" /></td>
                <td><asp:Button runat="server" Text="9" OnClick="number_click" /></td>
                <td><asp:Button runat="server" Text="/" OnClick="operand_click" /></td>
            </tr>
            <tr>
                <td><asp:Button runat="server" Text="4" OnClick="number_click" /></td>
                <td><asp:Button runat="server" Text="5" OnClick="number_click" /></td>
                <td><asp:Button runat="server" Text="6" OnClick="number_click" /></td>
                <td><asp:Button runat="server" Text="*" OnClick="operand_click" /></td>
            </tr>
            <tr>
                <td><asp:Button runat="server" Text="1" OnClick="number_click" /></td>
                <td><asp:Button runat="server" Text="2" OnClick="number_click" /></td>
                <td><asp:Button runat="server" Text="3" OnClick="number_click" /></td>
                <td><asp:Button runat="server" Text="-" OnClick="operand_click" /></td>
            </tr>
            <tr>
                <td><asp:Button runat="server" Text="0" OnClick="number_click" /></td>
                <td></td>
                <td></td>
                <td><asp:Button runat="server" Text="+" OnClick="operand_click" /></td>
            </tr>
            </table>

        </ContentTemplate>
    </asp:UpdatePanel>

    </form>
</body>
</html>

VB CodeBehind

Option Explicit On
Option Strict On

Partial Class Calculator
    Inherits System.Web.UI.Page

    ''' <summary>
    ''' Store List of items added in viewstate (numbers and operands)
    ''' </summary>
    Private Property EquationItems As List(Of Object)
        Get
            If ViewState("EquationItems") Is Nothing Then Return New List(Of Object)

            Return CType(ViewState("EquationItems"), List(Of Object))
        End Get
        Set(value As List(Of Object))
            ViewState("EquationItems") = value
        End Set
    End Property

    ''' <summary>
    ''' refersh equation and result
    ''' </summary>
    Private Sub Update()

        Dim Equation = ""
        Dim Result As Double = 0
        For i As Integer = 0 To EquationItems.Count - 1
            Dim item = EquationItems(i)
            Equation += item.ToString & " "

            Dim Num As Integer = If(TypeOf item Is Integer, CInt(item), -1)

            If i = 0 Then
                Result = If(Num > -1, Num, 0)
            Else
                If Num > -1 Then
                    Dim LastOperand = EquationItems(i - 1)
                    Result = Calc(Result, CStr(LastOperand), Num)
                End If
            End If
        Next

        EquationLabel.Text = Equation
        ResultLabel.Text = Result.ToString
    End Sub

    ''' <summary>
    ''' calaculate for each operand
    ''' </summary>
    Private Function Calc(Result As Double, Operand As String, Num As Integer) As Double
        If Operand = "+" Then
            Return Result + Num
        ElseIf Operand = "-" Then
            Return Result - Num
        ElseIf Operand = "*" Then
            Return Result * Num
        ElseIf Operand = "/" Then
            Return If(Num <> 0, Result / Num, 0)
        End If

        Return 0
    End Function

    ''' <summary>
    ''' all numbers click
    ''' </summary>
    Protected Sub number_click(sender As Object, e As System.EventArgs)
        'Get number from button text
        Dim Num = CInt(CType(sender, Button).Text)

        Dim items = EquationItems
        If items.Count > 0 Then
            Dim LastItem = items(items.Count - 1)
            ' Last item was number, so remove and add as digit!
            If TypeOf LastItem Is Integer Then
                '' prevent concat number more than integer
                If LastItem.ToString.Length < 9 Then
                    Num = CInt(LastItem.ToString & Num.ToString)
                Else
                    Num = CInt(LastItem)
                End If
                items.RemoveAt(items.Count - 1)

            End If
        End If

        items.Add(Num)
        EquationItems = items

        Update()
    End Sub

    ''' <summary>
    ''' all operand click
    ''' </summary>
    Protected Sub operand_click(sender As Object, e As System.EventArgs)
        'Get Operand from button text
        Dim Operand = CType(sender, Button).Text

        Dim items = EquationItems

        If items.Count > 0 Then
            Dim LastItem = items(items.Count - 1)
            ' Last item was operand, so remove it
            If TypeOf LastItem Is String Then
                items.RemoveAt(items.Count - 1)
            End If
        End If

        ' No items yet, can't add oeprand
        If items.Count > 0 Then items.Add(Operand)
        EquationItems = items

        Update()
    End Sub

    ''' <summary>
    ''' equal
    ''' </summary>
    Protected Sub equal_click(sender As Object, e As System.EventArgs)
        Update()
    End Sub

    ''' <summary>
    ''' back
    ''' </summary>
    Protected Sub back_click(sender As Object, e As System.EventArgs)
        If EquationItems.Count = 0 Then Exit Sub

        Dim items = EquationItems
        Dim LastItem = items(items.Count - 1)

        If TypeOf LastItem Is Integer Then
            ' Last item was number, so remove it
            items.RemoveAt(items.Count - 1)
        Else
            ' Last item was operand, so remove it and the one before
            items.RemoveRange(items.Count - 2, 2)
        End If

        EquationItems = items

        Update()
    End Sub

    Protected Sub clear_click(sender As Object, e As System.EventArgs)

        EquationItems = New List(Of Object)

        Update()
    End Sub
End Class