码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET 网页计算器的实现

时间:2016-03-22 10:31:49      阅读:535      评论:0      收藏:0      [点我收藏+]

标签:

一、页面设计

 

<%@ 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" 
                        onclick="Button1_Click"/>
                    <asp:Button ID="Button2" runat="server" Text="MR" Width="93px" Height="31px" 
                        onclick="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" 
                        onclick="Button6_Click"/>
                    <asp:Button ID="Button7" runat="server" Text="CE" Width="93px" Height="31px" 
                        onclick="Button7_Click"/>
                    <asp:Button ID="Button8" runat="server" Text="C" Width="93px" Height="31px" 
                        onclick="Button8_Click"/>
                    <asp:Button ID="Button9" runat="server" Text="±" Width="93px" Height="31px"/>
                    <asp:Button ID="Button10" runat="server" Text="√" Width="93px" Height="31px" 
                        onclick="Button10_Click"/>
                    </td>
            </tr>
            <tr>
                <td class="style1">
                <asp:Button ID="Button11" runat="server" Text="7" Width="93px" Height="31px" 
                        onclick="Button11_Click"/>
                    <asp:Button ID="Button12" runat="server" Text="8" Width="93px" Height="31px" 
                        onclick="Button12_Click"/>
                    <asp:Button ID="Button13" runat="server" Text="9" Width="93px" Height="31px" 
                        onclick="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" 
                        onclick="Button16_Click"/>
                    <asp:Button ID="Button17" runat="server" Text="5" Width="93px" Height="31px" 
                        onclick="Button17_Click"/>
                    <asp:Button ID="Button18" runat="server" Text="6" Width="93px" Height="31px" 
                        onclick="Button18_Click"/>
                    <asp:Button ID="Button19" runat="server" Text="*" Width="93px" Height="31px" 
                        onclick="Button19_Click"/>
                    <asp:Button ID="Button20" runat="server" Text="/" Width="93px" Height="31px" 
                        onclick="Button20_Click"/>
                    </td>
            </tr>
            <tr>
                <td class="style1">
                <asp:Button ID="Button21" runat="server" Text="1" Width="93px" Height="31px" 
                        onclick="Button21_Click"/>
                    <asp:Button ID="Button22" runat="server" Text="2" Width="93px" Height="31px" 
                        onclick="Button22_Click"/>
                    <asp:Button ID="Button23" runat="server" Text="3" Width="93px" Height="31px" 
                        onclick="Button23_Click"/>
                    <asp:Button ID="Button24" runat="server" Text="+" Width="93px" Height="31px" 
                        onclick="Button24_Click"/>
                    <asp:Button ID="Button25" runat="server" Text="-" Width="93px" Height="31px" 
                        onclick="Button25_Click"/>
                    </td>
            </tr>
            <tr>
                <td class="style1">
                <asp:Button ID="Button26" runat="server" Text="0" Width="194px" Height="31px" 
                        onclick="Button26_Click"/>
                    <asp:Button ID="Button27" runat="server" Text="." Width="93px" Height="31px" 
                        onclick="Button27_Click"/>
                    <asp:Button ID="Button28" runat="server" Text="=" Width="194px" Height="31px" 
                        onclick="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 + ".";
    }
}

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

ASP.NET 网页计算器的实现

标签:

原文地址:http://www.cnblogs.com/CrazyKing/p/5304829.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!