ASP.net实现的计算器

Posted on 2006-03-16 11:29  FinalFantasy  阅读(...)  评论(... 编辑 收藏
 

这个计算器是初学的一个作业,在写这个计算器的时候还遇到了些小麻烦。本来是想用自带的web控件来实现手动输入字符进行计算的判断,不然会出现严重的错误。可是自带的验证控件只能够验证通过键盘输入的字符,像我这样设计的如果再通过按钮来更改文本框中的文字就会出现验证的错误判断。这个问题卡了我很久,最后才突然想到用错误处理来自己判断文本框的文字更改,才最后实现这个功能。

此计算器实现功能:基本的加、减、乘、除的运算,还有连续运算,手动输入字符的运算以及判断

代码我觉得还是太长了,不知道还能怎样缩减,还请哪位高人指点。代码如下:

Quote:

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    Class Ent
        Public Shared total, num1, num2 As String
        Public Shared sign As String
        Public Shared Sub click(ByVal a As String)
            total = total + a
        End Sub
    End Class
   
    Sub Count()
        Ent.num2 = TextBox1.Text
        Try
            Select Case Ent.sign
                Case "+"
                    TextBox1.Text = CStr(CDbl(Ent.num1) + CDbl(Ent.num2))
                Case "-"
                    TextBox1.Text = CStr(CDbl(Ent.num1) - CDbl(Ent.num2))
                Case "*"
                    TextBox1.Text = CStr(CDbl(Ent.num1) * CDbl(Ent.num2))
                Case "/"
                    TextBox1.Text = CStr(CDbl(Ent.num1) / CDbl(Ent.num2))
            End Select
           
        Catch ex As Exception
            Label1.Text = "同志请不要那数字以外的字符开计算器的玩笑!"
            TextBox1.Text = "0"
            Ent.total = "0"
            Ent.sign = ""
        End Try
        Ent.total = ""
        Ent.sign = ""
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Ent.click("1")
        TextBox1.Text = Ent.total
        Call CleanLabel()
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Ent.click("2")
        TextBox1.Text = Ent.total
        Call CleanLabel()
    End Sub

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Ent.click("3")
        TextBox1.Text = Ent.total
        Call CleanLabel()
    End Sub

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Ent.click("4")
        TextBox1.Text = Ent.total
        Call CleanLabel()
    End Sub

    Protected Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Ent.click("5")
        TextBox1.Text = Ent.total
        Call CleanLabel()
    End Sub

    Protected Sub Button6_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Ent.click("6")
        TextBox1.Text = Ent.total
        Call CleanLabel()
    End Sub

    Protected Sub Button7_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Ent.click("7")
        TextBox1.Text = Ent.total
        Call CleanLabel()
    End Sub

    Protected Sub Button8_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Ent.click("8")
        TextBox1.Text = Ent.total
        Call CleanLabel()
    End Sub

    Protected Sub Button9_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Ent.click("9")
        TextBox1.Text = Ent.total
        Call CleanLabel()
    End Sub

    Protected Sub Button10_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If Len(Ent.total) > 0 Then
            Call CleanLabel()
            Ent.click("0")
            TextBox1.Text = Ent.total
        End If
    End Sub

    Protected Sub Button18_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Call CleanLabel()
        Ent.total = ""
        Ent.sign = ""
        TextBox1.Text = "0"
    End Sub

    Protected Sub Button11_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If Len(Ent.total) >= 0 Then
            If InStr(Ent.total, ".") = 0 Then
                Ent.click(".")
                TextBox1.Text = Ent.total
            End If
        End If
        Call CleanLabel()
    End Sub

    Protected Sub Button12_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If Len(Ent.sign) = 1 Then
            Call Count()
            Ent.num1 = TextBox1.Text
            Ent.sign = "+"
        Else
            Ent.num1 = TextBox1.Text
            TextBox1.Text = "0"
            Ent.total = ""
            Ent.sign = "+"
        End If
    End Sub

    Protected Sub Button16_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Call Count()
    End Sub

    Protected Sub Button13_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If Len(Ent.sign) = 1 Then
            Call Count()
            Ent.num1 = TextBox1.Text
            Ent.sign = "-"
        Else
            Ent.num1 = TextBox1.Text
            TextBox1.Text = "0"
            Ent.total = ""
            Ent.sign = "-"
        End If
    End Sub

    Protected Sub Button14_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If Len(Ent.sign) = 1 Then
            Call Count()
            Ent.num1 = TextBox1.Text
            Ent.sign = "*"
        Else
            Ent.num1 = TextBox1.Text
            TextBox1.Text = "0"
            Ent.total = ""
            Ent.sign = "*"
        End If
    End Sub

    Protected Sub Button15_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If Len(Ent.sign) = 1 Then
            Call Count()
            Ent.num1 = TextBox1.Text
            Ent.sign = "/"
        Else
            Ent.num1 = TextBox1.Text
            TextBox1.Text = "0"
            Ent.total = ""
            Ent.sign = "/"
        End If
    End Sub

    Protected Sub Button17_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        TextBox1.Text = "0"
        Ent.total = ""
        Call CleanLabel()
    End Sub

    Protected Sub Button19_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If Len(TextBox1.Text) > 1 Then
            TextBox1.Text = Left(TextBox1.Text, (Len(TextBox1.Text) - 1))
            Ent.total = TextBox1.Text
        Else : TextBox1.Text = "0"
        End If
    End Sub

    Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim a As Double
        Try
            a = CDbl(TextBox1.Text)
            Call CleanLabel()
        Catch ex As Exception
            Label1.Text = "同志请不要那数字以外的字符开计算器的玩笑!"
            TextBox1.Text = "0"
            Ent.total = "0"
            Ent.sign = ""
        End Try
    End Sub
   
    Sub CleanLabel()
        Label1.Text = ""
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>计算器</title>
</head>
<body>
    <form id="form1" runat="server">
    <div style=" text-align :center "><h3>计算器</h3></div>
    <div style=" text-align :center ">
        <asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label>&nbsp;
    <table style=" width: 164px; border :ridge 1px #000000">
    <tr>
    <td style="width: 187px">
    <div style=" text-align :center ">
        <asp:TextBox ID="TextBox1" runat="server" BorderStyle="Solid" BorderWidth="1px" Width="180px" CausesValidation="True" OnTextChanged="TextBox1_TextChanged" ForeColor="Transparent">0</asp:TextBox>
        </div>
        </td>
        </tr>
        <tr>
        <td style="width: 187px; height: 23px;">
        <asp:Button ID="Button19" runat="server" OnClick="Button19_Click" Text="BackSpace" Width="72px" UseSubmitBehavior="False" />
        <asp:Button ID="Button17" runat="server" Text="CE" OnClick="Button17_Click" Width="48px" UseSubmitBehavior="False" />
        <asp:Button ID="Button18" runat="server" Text="C" OnClick="Button18_Click" Width="47px" UseSubmitBehavior="False" />
        </td>
        </tr>
        <tr><td style="width: 187px;"><asp:Button ID="Button7" runat="server" Text="7" OnClick="Button7_Click" Width="40px" UseSubmitBehavior="False" />
        <asp:Button ID="Button8" runat="server" Text="8" OnClick="Button8_Click" Width="40px" UseSubmitBehavior="False" />
        <asp:Button ID="Button9" runat="server" Text="9" OnClick="Button9_Click" Width="40px" UseSubmitBehavior="False" />
        <asp:Button ID="Button15" runat="server" Text="/" OnClick="Button15_Click" Width="40px" UseSubmitBehavior="False" /></td></tr>
        <tr><td style="width: 187px;">
        <asp:Button ID="Button4" runat="server" Text="4" OnClick="Button4_Click" Width="40px" UseSubmitBehavior="False" />
        <asp:Button ID="Button5" runat="server" Text="5" OnClick="Button5_Click" Width="40px" UseSubmitBehavior="False" />
        <asp:Button ID="Button6" runat="server" Text="6" OnClick="Button6_Click" Width="40px" UseSubmitBehavior="False" />
        <asp:Button ID="Button14" runat="server" Text="*" OnClick="Button14_Click" Width="40px" UseSubmitBehavior="False" /></td></tr>
        <tr><td style="width: 187px;">
        <asp:Button ID="Button1" runat="server" Text="1" OnClick="Button1_Click" Width="40px" UseSubmitBehavior="False" />
        <asp:Button ID="Button2" runat="server" Text="2" OnClick="Button2_Click" Width="40px" UseSubmitBehavior="False" />
        <asp:Button ID="Button3" runat="server" Text="3" OnClick="Button3_Click" Width="40px" UseSubmitBehavior="False" />
        <asp:Button ID="Button13" runat="server" Text="-" OnClick="Button13_Click" Width="40px" UseSubmitBehavior="False" /></td></tr>
        <tr><td style="width: 187px;">
        <asp:Button ID="Button10" runat="server" Text="0" OnClick="Button10_Click" Width="40px" UseSubmitBehavior="False" />
        <asp:Button ID="Button11" runat="server" Text="." OnClick="Button11_Click" Width="40px" UseSubmitBehavior="False" />
        <asp:Button ID="Button12" runat="server" Text="+" OnClick="Button12_Click" Width="40px" UseSubmitBehavior="False" />
            <asp:Button ID="Button16" runat="server" Text="=" OnClick="Button16_Click" Width="40px" UseSubmitBehavior="False" /></td></tr>
        </table>
    </div>
       
    </form>
</body>
</html>


posts - 1, comments - 1, trackbacks - 0, articles - 0

Copyright © 2019 FinalFantasy
Powered by .NET Core 3.1.0 on Linux