现在的位置: 首页 > 综合 > 正文

用ASP.NET实现计算器功能

2013年05月09日 ⁄ 综合 ⁄ 共 7642字 ⁄ 字号 评论关闭

WebApplication版本的计算器的实现

 

 

 

 用户界面的代码如下(WebForm1.aspx:

<%@Pagelanguage="c#"Codebehind="WebForm1.aspx.cs"AutoEventWireup="false"Inherits="WebApplicationcalc.WebForm1" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>

         <HEAD>

                   <title>WebForm1</title>

                   <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">

                   <meta content="C#" name="CODE_LANGUAGE">

                   <meta content="JavaScript" name="vs_defaultClientScript">

                   <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">

         </HEAD>

         <body bgColor="buttonface" MS_POSITIONING="GridLayout">

                   <form id="Form1" method="post" runat="server">

                            <FONT face="宋体"></FONT>

                            <table style="WIDTH: 280px; HEIGHT: 168px" borderColor="buttonshadow" width="280" align="center"

                                     bgColor="activeborder" border="1">

                                     <tr><td colSpan="5">&nbsp;

<asp:label id="Label1" runat="server" BackColor="ActiveCaption" Width="264px">WebApplication版本的计算器</asp:label></td></tr><tr>

                                               <td colSpan="5">&nbsp;

                                                        <asp:textbox id="txtShow" runat="server" BackColor="Control" Width="264px" BorderColor="Lime"

                                                                 ReadOnly="True"></asp:textbox></td></tr><tr>

                                               <td style="WIDTH: 23px; HEIGHT: 37px" align="center">&nbsp;

                                                        <asp:button id="btn_7" runat="server" Width="40px" Text="7"></asp:button></td>

                                               <TD style="HEIGHT: 37px">&nbsp;

                                                        <asp:button id="btn_8" runat="server" Width="40px" Text="8"></asp:button></TD>

                                               <TD style="WIDTH: 56px; HEIGHT: 37px">&nbsp;

                                                        <asp:button id="btn_9" runat="server" Width="48px" Text="9"></asp:button></TD>

                                               <td style="HEIGHT: 37px"><asp:button id="btn_div" runat="server" Width="40px" Text="/"></asp:button>&nbsp;</td>

                                               <td style="HEIGHT: 37px">&nbsp;

                                                        <asp:button id="btn_sprt" runat="server" Width="36px" Text="sprt"></asp:button></td></tr><tr>

                                               <td style="WIDTH: 23px">&nbsp;

                                                        <asp:button id="btn_4" runat="server" Width="40px" Text="4"></asp:button></td>

                                               <td>&nbsp;

                                                        <asp:button id="btn_5" runat="server" Width="40px" Text="5"></asp:button></td>

                                               <td style="WIDTH: 56px">&nbsp;

                                                        <asp:button id="btn_6" runat="server" Width="48px" Text="6"></asp:button></td>

                                               <td>&nbsp;

                                                        <asp:button id="btn_mul" runat="server" Width="39px" Text="*"></asp:button></td>

                                               <td>&nbsp;

                                                        <asp:button id="btn_spr" runat="server" Width="32px" Text="spr"></asp:button></td>

                                     </tr>

                                     <tr>

                                               <td style="WIDTH: 23px">&nbsp;

                                                        <asp:button id="btn_1" runat="server" Width="40px" Text="1"></asp:button></td>

                                               <td>&nbsp;

                                                        <asp:button id="btn_2" runat="server" Width="42px" Text="2"></asp:button></td>

                                               <td style="WIDTH: 56px">&nbsp;

                                                        <asp:button id="btn_3" runat="server" Width="44px" Text="3"></asp:button></td>

                                               <td>&nbsp;

                                                        <asp:button id="btn_add" runat="server" Width="40px" Text="+"></asp:button></td>

                                               <td>&nbsp;

                                                        <asp:button id="btn_rev" runat="server" Width="32px" Text="1/x"></asp:button></td></tr><tr>

                                               <td style="WIDTH: 23px">&nbsp;

                                                        <asp:button id="btn_0" runat="server" Width="40px" Text="0"></asp:button></td>

                                               <td>&nbsp;

                                                        <asp:button id="btn_sign" runat="server" Width="41px" Text="+/-"></asp:button></td>

                                               <td style="WIDTH: 56px">&nbsp;

                                                        <asp:button id="btn_dot" runat="server" Width="46px" Text="."></asp:button></td>

                                               <td>&nbsp;

                                                        <asp:button id="btn_sub" runat="server" Width="40px" Text="-"></asp:button></td>

                                               <td>&nbsp;

                                                        <asp:buttonid="btn_equ"runat="server"Width="32px" Text="="></asp:button></td></tr></table></form>

         </body>

</HTML>

2.全局变量的定义

在程序设计中我们需要定义res(记录结果数)tmp(当前输入的操作数)opt(记录操作数的个数)、dot(记录是否单击了小数点)、num(记录输入的操作数的个数)、dotnum(记录小数点部分的个数)这六个全局变量,在程序中运行过程中传递和保存数据。我最初的想到的解决方案是为程序设计一个Calcuater.cs,并在类中添加六个字段引用,然后设置成类的六个属性,以实例化引用类属性的方式在各函数之间进行值传递。从理论上讲这样的事件是可行的,但是正是由于Windows应用程序与Web应用程序存在运行机制上的差异,导致该解决方案在此行不通。这种设计的运行结果是每次单击一次按钮触发一个事件时,在计算器中只能显示该按钮传递的单个值,而不会累计上次触发事件传递的值,更谈不上计算了。

 

 

 

 

Global.asax.cs的程序清单:

using System;

using System.Collections;

using System.ComponentModel;

using System.Web;

using System.Web.SessionState;

namespace WebApplicationcalc

{

/// <summary>

/// Global 的摘要说明。

/// </summary>

public class Global : System.Web.HttpApplication

{

/// <summary>

           /// 必需的设计器变量。

           /// </summary>

           private System.ComponentModel.IContainer components = null;

           public Global()

           {

                    InitializeComponent();

           }      

           protected void Application_Start(Object sender, EventArgs e)

           {

           }

           protected void Session_Start(Object sender, EventArgs e)

           {

        //启动用户会话机制,为计算器保存数据

                    Session["res"]=0; //记录结果数

                    Session["tmp"]=0;//当前输入的操作数

                    Session["opt"]=0; //记录操作码

                    Session["dot"]=0;//记录是否单击了小数点

                    Session["num"]=0;//记录操作输入的个数

                    Session["dotnum"]=0;       //记录小数点部分的个数

           }

           protected void Application_BeginRequest(Object sender, EventArgs e)

           {

           }

           protected void Application_EndRequest(Object sender, EventArgs e)

           {

           }

           protected void Application_AuthenticateRequest(Object sender, EventArgs e)

           {

           }

           protected void Application_Error(Object sender, EventArgs e)

           {

           }

           protected void Session_End(Object sender, EventArgs e)

           {

           }

           protected void Application_End(Object sender, EventArgs e)

           {

           }      

         #region Web 窗体设计器生成的代码

           /// <summary>

           /// 设计器支持所需的方法 - 不要使用代码编辑器修改

           /// 此方法的内容。

           /// </summary>

           private void InitializeComponent()

           {   

                    this.components = new System.ComponentModel.Container();

           }

           #endregion

}

}

抱歉!评论已关闭.