ASP.NET 网页计算器的实现

一、页面设计

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>网页计算器</title>
    <style type="text/css">
        .style1
        {
            width: 292px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <table style="border: 1px ridge #3300ff; background-color:#40E0D0; width: 497px;" 
            align="center">
            <caption style="border-right: #0000ff 1px ridge; border-top: #0000ff 1px ridge; border-left: #0000ff 1px ridge;
                border-bottom: #0000ff 1px ridge">
                <asp:Label ID="Label1" runat="server" Text="计算器"></asp:Label>
            </caption>
            <tr>
                <td class="style1">
                    <asp:TextBox ID="txtShow" runat="server" Width="500px" Height="50px"></asp:TextBox></td>
            </tr>
            <tr>
                <td class="style1">
                    <asp:Button ID="Button1" runat="server" Text="MC" Width="93px" Height="31px" 
                        οnclick="Button1_Click"/>
                    <asp:Button ID="Button2" runat="server" Text="MR" Width="93px" Height="31px" 
                        οnclick="Button2_Click"/>
                    <asp:Button ID="Button3" runat="server" Text="MS" Width="93px" Height="31px"/>
                    <asp:Button ID="Button4" runat="server" Text="M+" Width="93px" Height="31px"/>
                    <asp:Button ID="Button5" runat="server" Text="M-" Width="93px" Height="31px"/>
                    </td>
            </tr>
            <tr>
                <td class="style1">
                <asp:Button ID="Button6" runat="server" Text="←" Width="93px" Height="31px" 
                        οnclick="Button6_Click"/>
                    <asp:Button ID="Button7" runat="server" Text="CE" Width="93px" Height="31px" 
                        οnclick="Button7_Click"/>
                    <asp:Button ID="Button8" runat="server" Text="C" Width="93px" Height="31px" 
                        οnclick="Button8_Click"/>
                    <asp:Button ID="Button9" runat="server" Text="±" Width="93px" Height="31px"/>
                    <asp:Button ID="Button10" runat="server" Text="√" Width="93px" Height="31px" 
                        οnclick="Button10_Click"/>
                    </td>
            </tr>
            <tr>
                <td class="style1">
                <asp:Button ID="Button11" runat="server" Text="7" Width="93px" Height="31px" 
                        οnclick="Button11_Click"/>
                    <asp:Button ID="Button12" runat="server" Text="8" Width="93px" Height="31px" 
                        οnclick="Button12_Click"/>
                    <asp:Button ID="Button13" runat="server" Text="9" Width="93px" Height="31px" 
                        οnclick="Button13_Click"/>
                    <asp:Button ID="Button14" runat="server" Text="%" Width="93px" Height="31px"/>
                    <asp:Button ID="Button15" runat="server" Text="PI" Width="93px" Height="31px"/>
                    </td>
            </tr>
            <tr>
                <td class="style1">
                <asp:Button ID="Button16" runat="server" Text="4" Width="93px" Height="31px" 
                        οnclick="Button16_Click"/>
                    <asp:Button ID="Button17" runat="server" Text="5" Width="93px" Height="31px" 
                        οnclick="Button17_Click"/>
                    <asp:Button ID="Button18" runat="server" Text="6" Width="93px" Height="31px" 
                        οnclick="Button18_Click"/>
                    <asp:Button ID="Button19" runat="server" Text="*" Width="93px" Height="31px" 
                        οnclick="Button19_Click"/>
                    <asp:Button ID="Button20" runat="server" Text="/" Width="93px" Height="31px" 
                        οnclick="Button20_Click"/>
                    </td>
            </tr>
            <tr>
                <td class="style1">
                <asp:Button ID="Button21" runat="server" Text="1" Width="93px" Height="31px" 
                        οnclick="Button21_Click"/>
                    <asp:Button ID="Button22" runat="server" Text="2" Width="93px" Height="31px" 
                        οnclick="Button22_Click"/>
                    <asp:Button ID="Button23" runat="server" Text="3" Width="93px" Height="31px" 
                        οnclick="Button23_Click"/>
                    <asp:Button ID="Button24" runat="server" Text="+" Width="93px" Height="31px" 
                        οnclick="Button24_Click"/>
                    <asp:Button ID="Button25" runat="server" Text="-" Width="93px" Height="31px" 
                        οnclick="Button25_Click"/>
                    </td>
            </tr>
            <tr>
                <td class="style1">
                <asp:Button ID="Button26" runat="server" Text="0" Width="194px" Height="31px" 
                        οnclick="Button26_Click"/>
                    <asp:Button ID="Button27" runat="server" Text="." Width="93px" Height="31px" 
                        οnclick="Button27_Click"/>
                    <asp:Button ID="Button28" runat="server" Text="=" Width="194px" Height="31px" 
                        οnclick="Button28_Click"/>
                    </td>
            </tr>
        </table>
    </form>
</body>
</html>

  效果图:

二、按钮函数:

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    double d_result;

    protected void Button26_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        txtShow.Text += btn.Text;
    }
    protected void Button21_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        txtShow.Text += btn.Text;
    }
    protected void Button22_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        txtShow.Text += btn.Text;
    }
    protected void Button23_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        txtShow.Text += btn.Text;
    }
    protected void Button16_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        txtShow.Text += btn.Text;
    }
    protected void Button17_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        txtShow.Text += btn.Text;
    }
    protected void Button18_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        txtShow.Text += btn.Text;
    }
    protected void Button11_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        txtShow.Text += btn.Text;
    }
    protected void Button12_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        txtShow.Text += btn.Text;
    }
    protected void Button13_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        txtShow.Text += btn.Text;
    }
    protected void Button24_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        txtShow.Text = txtShow.Text + " " + btn.Text + " ";
    }
    protected void Button25_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        txtShow.Text = txtShow.Text + " " + btn.Text + " ";
    }
    protected void Button19_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        txtShow.Text = txtShow.Text + " " + btn.Text + " ";
    }
    protected void Button20_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        txtShow.Text = txtShow.Text + " " + btn.Text + " ";
    }
    protected void Button28_Click(object sender, EventArgs e)
    {
        
        string s_txt = txtShow.Text;
        int space = s_txt.IndexOf(' ');
        string c1 = s_txt.Substring(0, space);
        char operation = Convert.ToChar(s_txt.Substring((space + 1), 1));
        string c2 = s_txt.Substring(space + 3);
        double arg1 = Convert.ToDouble(c1);
        double arg2 = Convert.ToDouble(c2);
        switch (operation)
        {
            case '+':
                d_result = arg1 + arg2;
                break;
            case '-':
                d_result = arg1 - arg2;
                break;
            case '*':
                d_result = arg1 * arg2;
                break;
            case '/':
                d_result = arg1 / arg2;
                break;
            case '':
                d_result = Math.Sqrt(arg1);
                break;
            default:
                throw new ApplicationException();
        }
        txtShow.Text = d_result.ToString();
    }
    protected void Button8_Click(object sender, EventArgs e)
    {
        txtShow.Text = "";
    }
    protected void Button6_Click(object sender, EventArgs e)
    {
        string s = txtShow.Text;
        string ss = s.Substring(0, s.Length - 1);
        txtShow.Text = ss;
    }
    protected void Button10_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        txtShow.Text = txtShow.Text + " " + btn.Text + " ";
    }
    protected void Button7_Click(object sender, EventArgs e)
    {
        txtShow.Text = "0";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        d_result=0;
        double arg1=0;
        double arg2 = 0;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        txtShow.Text = d_result.ToString();
    }
    protected void Button27_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        txtShow.Text = txtShow.Text + ".";
    }
}

目前我只写了几个按钮的功能,其他的我还不是很清楚,以后改进。

转载于:https://www.cnblogs.com/CrazyKing/p/5304829.html

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值