用ASP.NET实现计算器功能

  • Post author:
  • Post category:其他



WebApplication


版本的计算器的实现



















用户界面的代码如下(



WebForm1.aspx







:



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



<!DOCTYPE HTML PUBLIC “-//W

3C
//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





}



}




3.






用程序的设计






using System;
  
  
using System.Collections;
  
  
using System.ComponentModel;
  
  
using System.Data;
  
  
using System.Drawing;
  
  
using System.Web;
  
  
using System.Web.SessionState;
  
  
using System.Web.UI;
  
  
using System.Web.UI.WebControls;
  
  
using System.Web.UI.HtmlControls;
  
  
namespace WebApplicationcalc
  
  
{
  
  
                    /// <summary>
  
  
                    /// WebForm1 的摘要说明。
   
   
                    /// </summary>
  
  
                    public class WebForm1 : System.Web.UI.Page
  
  
                    {
  
  
                                         protected System.Web.UI.WebControls.Label Label1;
  
  
                                         protected System.Web.UI.WebControls.TextBox txtShow;
  
  
                                         protected System.Web.UI.WebControls.Button btn_7;
  
  
                                         protected System.Web.UI.WebControls.Button btn_8;
  
  
                                         protected System.Web.UI.WebControls.Button btn_9;
  
  
                                         protected System.Web.UI.WebControls.Button btn_div;
  
  
                                         protected System.Web.UI.WebControls.Button btn_sprt;
  
  
                                         protected System.Web.UI.WebControls.Button btn_4;
  
  
                                         protected System.Web.UI.WebControls.Button btn_5;
  
  
                                         protected System.Web.UI.WebControls.Button btn_6;
  
  
                                         protected System.Web.UI.WebControls.Button btn_mul;
  
  
                                         protected System.Web.UI.WebControls.Button btn_spr;
  
  
                                         protected System.Web.UI.WebControls.Button btn_1;
  
  
                                         protected System.Web.UI.WebControls.Button btn_2;
  
  
                                         protected System.Web.UI.WebControls.Button btn_3;
  
  
                                         protected System.Web.UI.WebControls.Button btn_add;
  
  
                                         protected System.Web.UI.WebControls.Button btn_rev;
  
  
                                         protected System.Web.UI.WebControls.Button btn_0;
  
  
                                         protected System.Web.UI.WebControls.Button btn_sign;
  
  
                                         protected System.Web.UI.WebControls.Button btn_dot;
  
  
                                         protected System.Web.UI.WebControls.Button btn_sub;
  
  
                                         protected System.Web.UI.WebControls.Button btn_equ;
  
  
                                         public const int NULL = 0;                  // 定义操作码
   
   
                                         public const int ADD = 1;
  
  
                                         public const int SUB = 2;
  
  
                                         public const int MUL = 3;
  
  
                                         public const int DIV = 4;
  
  
                                         public const int SQR = 5;
  
  
                                         public const int SQRT = 6;
  
  
                                         public const int NODOT  = 0;           // 定义是否点击了小数点
   
   
                                         public const int HASDOT = 1;
  
  
                                         private void Page_Load(object sender, System.EventArgs e)
  
  
                                         {
  
  
                                                             // 在此处放置用户代码以初始化页面
   
   
                                         }
  
  

  
  
   
    
  
  
                                         #region Web 窗体设计器生成的代码
   
   
                                         override protected void OnInit(EventArgs e)
  
  
                                         {
  
  
                                                             //
  
  
                                                             // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   
   
                                                             //
  
  
                                                             InitializeComponent();
  
  
                                                             base.OnInit(e);
  
  
                                         }
  
  
                                         /// <summary>
  
  
                                         /// 设计器支持所需的方法 - 不要使用代码编辑器修改
   
   
                                         /// 此方法的内容。
   
   
                                         /// </summary>
  
  
                                         private void InitializeComponent()
  
  
                                         {    
  
  
//利用C#中的事件代理机制对对象进行事件绑定
   
   
//其中09按钮的Click事件都有btn_0_Click()进行处理
   
   
                                                             this.btn_7.Click += new System.EventHandler(this.btn_0_Click);
  
  
                                                             this.btn_8.Click += new System.EventHandler(this.btn_0_Click);
  
  
                                                             this.btn_9.Click += new System.EventHandler(this.btn_0_Click);
  
  
                                                             this.btn_div.Click += new System.EventHandler(this.btn_div_Click);
  
  
                                                             this.btn_sprt.Click += new System.EventHandler(this.btn_sprt_Click);
  
  
                                                             this.btn_4.Click += new System.EventHandler(this.btn_0_Click);
  
  
                                                             this.btn_5.Click += new System.EventHandler(this.btn_0_Click);
  
  
                                                             this.btn_6.Click += new System.EventHandler(this.btn_0_Click);
  
  
                                                             this.btn_mul.Click += new System.EventHandler(this.btn_mul_Click);
  
  
                                                             this.btn_spr.Click += new System.EventHandler(this.btn_spr_Click);
  
  
                                                             this.btn_1.Click += new System.EventHandler(this.btn_0_Click);
  
  
                                                             this.btn_2.Click += new System.EventHandler(this.btn_0_Click);
  
  
                                                             this.btn_3.Click += new System.EventHandler(this.btn_0_Click);
  
  
                                                             this.btn_add.Click += new System.EventHandler(this.btn_add_Click);
  
  
                                                             this.btn_rev.Click += new System.EventHandler(this.btn_rev_Click);
  
  
                                                             this.btn_0.Click += new System.EventHandler(this.btn_0_Click);
  
  
                                                             this.btn_sign.Click += new System.EventHandler(this.btn_sign_Click);
  
  
                                                             this.btn_dot.Click += new System.EventHandler(this.btn_dot_Click);
  
  
                                                             this.btn_sub.Click += new System.EventHandler(this.btn_sub_Click);
  
  
                                                             this.btn_equ.Click += new System.EventHandler(this.btn_equ_Click);
  
  
                                                             this.Load += new System.EventHandler(this.Page_Load);
  
  

  
  
   
    
  
  
                                         }
  
  
                                         #endregion
  
  
                                         private void btn_0_Click(object sender, System.EventArgs e)
  
  
                                         {
  
  
//当点击一个数字按钮的时候,需要进行判断,如果没有点击
    
    
                    //小数点,那么就把原来的数值扩大10倍后再加上当前的数值,
    
    
                    // 如果点击了小数点,那么就将当前的数值除以一个权数,再
    
    
                    // 加上原来的数值,得到新的数值。
   
   
                                                           System.Web.UI.WebControls.Button btnTmp;
  
  
                                                             double i=0;
  
  
                                                             btnTmp=sender as System.Web.UI.WebControls.Button;
  
  
                                                             if(btnTmp!=null)
  
  
                                                             {
  
  
//没有单击小数点
   
   
                                                                   if(Int32.Parse(Session["dot"].ToString())==NODOT) 
  
  
                                                                                 i=double.Parse(btnTmp.Text.ToString());
  
  
                                                                                 Session["tmp"]=double.Parse(Session["tmp"].ToString())*10+i;
  
  
                                                                                 txtShow.Text=Session["tmp"].ToString();
  
  
                                                             }
  
  
                                                             else
  
  
                                                             {
  
  
                                  //单击了小数点  
  
  
                                                                     Session["dotnum"]=Int32.Parse(Session["dotnum"].ToString())+1;
  
  
                                                                   i=double.Parse(btnTmp.Text.ToString())/System.Math.Pow(10,Int32.Parse(Session["dotnum"].ToString()));
  
  
                                                                                 Session["tmp"]=double.Parse(Session["tmp"].ToString())+i;
  
  
                                                                                 txtShow.Text=Session["tmp"].ToString();
  
  
                                                             }
  
  
                                         }
  
  
                                         private void btn_equ_Click(object sender, System.EventArgs e)
  
  
                                         {
  
  
                                                             if(Int32.Parse(Session["num"].ToString())==0)
  
  
                                                             {
  
  
                                                                                 Session["res"]=0;
  
  
                                                                                 Session["tmp"]=0;
  
  
                                         txtShow.Text=Session["tmp"].ToString();
  
  
                                                                                 return;
  
  
                                                             }
  
  
                            //Session对象进行转化减少代码编写量
   
   
                                                             double res=double.Parse(Session["res"].ToString());
  
  
                                                             double tmp=double.Parse(Session["tmp"].ToString());
  
  
                                                             int opt=Int32.Parse(Session["opt"].ToString());
  
  
                                                             switch (opt)
  
  
                                                             {
  
  
                                                                                                      // 加法
   
   
                                                                                 case ADD:
  
  
                                                                                                      res = res + tmp;
  
  
                                                                                                      break;
  
  
                                                                                                      // 减法
   
   
                                                                                 case SUB:
  
  
                                                                                                      res = res - tmp;
  
  
                                                                                                      break;
  
  
                                                                                                      // 乘法
   
   
                                                                                 case MUL:
  
  
                                                                                                      res = res * tmp;
  
  
                                                                                                      break;
  
  
                                                                                                      // 除法
   
   
                                                                                 case DIV:
  
  
                                                                                                      res = res / tmp;
  
  
                                                                                                      break;
  
  
                                                                                                      // 平方
   
   
                                                                                 case SQR:
  
  
                                                                                                      res = tmp * tmp;
  
  
                                                                                                      break;
  
  
                                                                                                      // 平方根
   
   
                                                                                 case SQRT:
  
  
                                                                                                      res = System.Math.Sqrt(tmp);
  
  
                                                                                                      break;
  
  
                                                                                 default:
  
  
                                                                                                      return;
  
  
                                                             }
  
  
                                                             txtShow.Text = res.ToString();
  
  
                              Session["opt"]=NULL;
  
  
                              Session["res"]=0;
  
  
                                                             Session["num"]=0;
  
  
                                         }
  
  
                                         private void btn_div_Click(object sender, System.EventArgs e)
  
  
                                         {
  
  
                              //除法运算
   
   
                                                             Session["opt"]=DIV;
  
  
                                                             if(Int32.Parse(Session["num"].ToString())!=0)
  
  
                                                             {
  
  
                                                                                 if(double.Parse(Session["tmp"].ToString())!=0)
  
  
                Session["res"]=double.Parse(Session["res"].ToString())/double.Parse(Session["tmp"].ToString());
  
  
                                                             }
  
  
                                                             else
  
  
                                  Session["res"]=Session["tmp"];
  
  
                                       Session["num"]=Int32.Parse(Session["num"].ToString())+1;
  
  
                              Session["tmp"]=0;
  
  
                                                             txtShow.Text = Session["res"].ToString();
  
  
                                         }
  
  

  
  
   
    
  
  
                                         private void btn_mul_Click(object sender, System.EventArgs e)
  
  
                                         {
  
  
                             //乘法运算
   
   
                                                             Session["opt"]=MUL;
  
  
                                                             if(Int32.Parse(Session["num"].ToString())!=0)
  
  
                 Session["res"]=double.Parse(Session["res"].ToString())*double.Parse(Session["tmp"].ToString());
  
  
                                         else
  
  
                              Session["res"]=Session["tmp"];
  
  
                              Session["num"]=Int32.Parse(Session["num"].ToString())+1;
  
  
                                                             Session["tmp"]=0;
  
  
                                                             txtShow.Text = Session["res"].ToString();
  
  
                                         }
  
  

  
  
   
    
  
  
                                         private void btn_sub_Click(object sender, System.EventArgs e)
  
  
                                         {
  
  
                               //减法运算
   
   
                                                   Session["opt"]=SUB;
  
  
                                                             if(Int32.Parse(Session["num"].ToString())!=0)
  
  
                                                                                 Session["res"]=double.Parse(Session["res"].ToString())-double.Parse(Session["tmp"].ToString());
  
  
                                                             else
  
  
                                                             Session["res"]=Session["tmp"];
  
  
                                                             Session["num"]=Int32.Parse(Session["num"].ToString())+1;
  
  
                                                             Session["tmp"]=0;
  
  
                                                             txtShow.Text = Session["res"].ToString();                                   
  
  
                                         }
  
  

  
  
   
    
  
  
                                         private void btn_add_Click(object sender, System.EventArgs e)
  
  
                                         {
  
  
                              //加法运算
   
   
                                                             Session["opt"]=ADD;
  
  
                                                             if(Int32.Parse(Session["num"].ToString())!=0)
  
  
                                                                                 Session["res"]=double.Parse(Session["res"].ToString())+double.Parse(Session["tmp"].ToString());
  
  
                                                             else
  
  
                                                                        Session["res"]=Session["tmp"];
  
  
                                                                      Session["num"]=Int32.Parse(Session["num"].ToString())+1;
  
  
                                                                      Session["tmp"]=0;
  
  
                                                                      txtShow.Text = Session["res"].ToString();     
  
  
                                         }
  
  
                                         private void btn_sprt_Click(object sender, System.EventArgs e)
  
  
                                         {
  
  
                              //开方运算
   
   
                                                             if(Int32.Parse(Session["num"].ToString())>0)
  
  
                                                             {
  
  
                                                                  Session["tmp"]=Math.Sqrt(double.Parse(Session["tmp"].ToString()));
  
  
                                 txtShow.Text = Session["tmp"].ToString();
  
  
                                                             }
  
  
                                         }
  
  

  
  
   
    
  
  
                                         private void btn_spr_Click(object sender, System.EventArgs e)
  
  
                                         {
  
  
                          //平方运算
   
   
                    Session["tmp"]=double.Parse(Session["tmp"].ToString())*double.Parse(Session["tmp"].ToString());
  
  
                        txtShow.Text = Session["tmp"].ToString();
  
  
                                         }
  
  
                                         private void btn_rev_Click(object sender, System.EventArgs e)
  
  
                                         {
  
  
//倒数运算
   
   
                                                  Session["tmp"]=1/double.Parse(Session["tmp"].ToString());
  
  
                         txtShow.Text = Session["tmp"].ToString();
  
  
                                         }
  
  
                                         private void btn_dot_Click(object sender, System.EventArgs e)
  
  
                                         {
  
  
                          //单击了小数点
   
   
                                                             Session["dot"]=HASDOT;
  
  
                                                             Session["dotnum"]=0;
  
  
                                         }
  
  

  
  
   
    
  
  
                                         private void btn_sign_Click(object sender, System.EventArgs e)
  
  
                                         {
  
  
                                                  //单击了符号运算 Session["tmp"]=double.Parse(Session["tmp"].ToString())-double.Parse(Session["tmp"].ToString());
  
  
                                                              txtShow.Text = Session["tmp"].ToString();
  
  
                                         }
  
  
                    }
  
  
}
  
  






OK






版权声明:本文为takeie原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。