您的位置:首页 > 编程语言 > ASP

用ASP.NET实现计算器功能

2007-03-14 12:39 399 查看
WebApplication版本的计算器的实现

用户界面的代码如下(WebForm1.aspx):
<%@Pagelanguage="c#"Codebehind="WebForm1.aspx.cs"AutoEventWireup="false"Inherits="WebApplicationcalc.WebForm1"%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<metacontent="MicrosoftVisualStudio.NET7.1"name="GENERATOR">
<metacontent="C#"name="CODE_LANGUAGE">
<metacontent="JavaScript"name="vs_defaultClientScript">
<metacontent="http://schemas.microsoft.com/intellisense/ie5"name="vs_targetSchema">
</HEAD>
<bodybgColor="buttonface"MS_POSITIONING="GridLayout">
<formid="Form1"method="post"runat="server">
<FONTface="宋体"></FONT>
<tablestyle="WIDTH:280px;HEIGHT:168px"borderColor="buttonshadow"width="280"align="center"
bgColor="activeborder"border="1">
<tr><tdcolSpan="5"> 
<asp:labelid="Label1"runat="server"BackColor="ActiveCaption"Width="264px">WebApplication版本的计算器</asp:label></td></tr><tr>
<tdcolSpan="5"> 
<asp:textboxid="txtShow"runat="server"BackColor="Control"Width="264px"BorderColor="Lime"
ReadOnly="True"></asp:textbox></td></tr><tr>
<tdstyle="WIDTH:23px;HEIGHT:37px"align="center"> 
<asp:buttonid="btn_7"runat="server"Width="40px"Text="7"></asp:button></td>
<TDstyle="HEIGHT:37px"> 
<asp:buttonid="btn_8"runat="server"Width="40px"Text="8"></asp:button></TD>
<TDstyle="WIDTH:56px;HEIGHT:37px"> 
<asp:buttonid="btn_9"runat="server"Width="48px"Text="9"></asp:button></TD>
<tdstyle="HEIGHT:37px"><asp:buttonid="btn_div"runat="server"Width="40px"Text="/"></asp:button> </td>
<tdstyle="HEIGHT:37px"> 
<asp:buttonid="btn_sprt"runat="server"Width="36px"Text="sprt"></asp:button></td></tr><tr>
<tdstyle="WIDTH:23px"> 
<asp:buttonid="btn_4"runat="server"Width="40px"Text="4"></asp:button></td>
<td> 
<asp:buttonid="btn_5"runat="server"Width="40px"Text="5"></asp:button></td>
<tdstyle="WIDTH:56px"> 
<asp:buttonid="btn_6"runat="server"Width="48px"Text="6"></asp:button></td>
<td> 
<asp:buttonid="btn_mul"runat="server"Width="39px"Text="*"></asp:button></td>
<td> 
<asp:buttonid="btn_spr"runat="server"Width="32px"Text="spr"></asp:button></td>
</tr>
<tr>
<tdstyle="WIDTH:23px"> 
<asp:buttonid="btn_1"runat="server"Width="40px"Text="1"></asp:button></td>
<td> 
<asp:buttonid="btn_2"runat="server"Width="42px"Text="2"></asp:button></td>
<tdstyle="WIDTH:56px"> 
<asp:buttonid="btn_3"runat="server"Width="44px"Text="3"></asp:button></td>
<td> 
<asp:buttonid="btn_add"runat="server"Width="40px"Text="+"></asp:button></td>
<td> 
<asp:buttonid="btn_rev"runat="server"Width="32px"Text="1/x"></asp:button></td></tr><tr>
<tdstyle="WIDTH:23px"> 
<asp:buttonid="btn_0"runat="server"Width="40px"Text="0"></asp:button></td>
<td> 
<asp:buttonid="btn_sign"runat="server"Width="41px"Text="+/-"></asp:button></td>
<tdstyle="WIDTH:56px"> 
<asp:buttonid="btn_dot"runat="server"Width="46px"Text="."></asp:button></td>
<td> 
<asp:buttonid="btn_sub"runat="server"Width="40px"Text="-"></asp:button></td>
<td> 
<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的程序清单:
usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Web;
usingSystem.Web.SessionState;
namespaceWebApplicationcalc
{
///<summary>
///Global的摘要说明。
///</summary>
publicclassGlobal:System.Web.HttpApplication
{
///<summary>
///必需的设计器变量。
///</summary>
privateSystem.ComponentModel.IContainercomponents=null;
publicGlobal()
{
InitializeComponent();
}
protectedvoidApplication_Start(Objectsender,EventArgse)
{
}
protectedvoidSession_Start(Objectsender,EventArgse)
{
//启动用户会话机制,为计算器保存数据
Session["res"]=0;//记录结果数
Session["tmp"]=0;//当前输入的操作数
Session["opt"]=0;//记录操作码
Session["dot"]=0;//记录是否单击了小数点
Session["num"]=0;//记录操作输入的个数
Session["dotnum"]=0;//记录小数点部分的个数
}
protectedvoidApplication_BeginRequest(Objectsender,EventArgse)
{
}
protectedvoidApplication_EndRequest(Objectsender,EventArgse)
{
}
protectedvoidApplication_AuthenticateRequest(Objectsender,EventArgse)
{
}
protectedvoidApplication_Error(Objectsender,EventArgse)
{
}
protectedvoidSession_End(Objectsender,EventArgse)
{
}
protectedvoidApplication_End(Objectsender,EventArgse)
{
}
#regionWeb窗体设计器生成的代码
///<summary>
///设计器支持所需的方法-不要使用代码编辑器修改
///此方法的内容。
///</summary>
privatevoidInitializeComponent()
{
this.components=newSystem.ComponentModel.Container();
}
#endregion
}
}
3.用程序的设计

usingSystem;

usingSystem.Collections;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Web;

usingSystem.Web.SessionState;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.HtmlControls;

namespaceWebApplicationcalc

{

///<summary>

///WebForm1的摘要说明。

///</summary>

publicclassWebForm1:System.Web.UI.Page

{

protectedSystem.Web.UI.WebControls.LabelLabel1;

protectedSystem.Web.UI.WebControls.TextBoxtxtShow;

protectedSystem.Web.UI.WebControls.Buttonbtn_7;

protectedSystem.Web.UI.WebControls.Buttonbtn_8;

protectedSystem.Web.UI.WebControls.Buttonbtn_9;

protectedSystem.Web.UI.WebControls.Buttonbtn_div;

protectedSystem.Web.UI.WebControls.Buttonbtn_sprt;

protectedSystem.Web.UI.WebControls.Buttonbtn_4;

protectedSystem.Web.UI.WebControls.Buttonbtn_5;

protectedSystem.Web.UI.WebControls.Buttonbtn_6;

protectedSystem.Web.UI.WebControls.Buttonbtn_mul;

protectedSystem.Web.UI.WebControls.Buttonbtn_spr;

protectedSystem.Web.UI.WebControls.Buttonbtn_1;

protectedSystem.Web.UI.WebControls.Buttonbtn_2;

protectedSystem.Web.UI.WebControls.Buttonbtn_3;

protectedSystem.Web.UI.WebControls.Buttonbtn_add;

protectedSystem.Web.UI.WebControls.Buttonbtn_rev;

protectedSystem.Web.UI.WebControls.Buttonbtn_0;

protectedSystem.Web.UI.WebControls.Buttonbtn_sign;

protectedSystem.Web.UI.WebControls.Buttonbtn_dot;

protectedSystem.Web.UI.WebControls.Buttonbtn_sub;

protectedSystem.Web.UI.WebControls.Buttonbtn_equ;

publicconstintNULL=0;//定义操作码

publicconstintADD=1;

publicconstintSUB=2;

publicconstintMUL=3;

publicconstintDIV=4;

publicconstintSQR=5;

publicconstintSQRT=6;

publicconstintNODOT=0;//定义是否点击了小数点

publicconstintHASDOT=1;

privatevoidPage_Load(objectsender,System.EventArgse)

{

//在此处放置用户代码以初始化页面

}


#regionWeb窗体设计器生成的代码

overrideprotectedvoidOnInit(EventArgse)

{

//

//CODEGEN:该调用是ASP.NETWeb窗体设计器所必需的。

//

InitializeComponent();

base.OnInit(e);

}

///<summary>

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

///此方法的内容。

///</summary>

privatevoidInitializeComponent()

{

//利用C#中的事件代理机制对对象进行事件绑定

//其中0~9按钮的Click事件都有btn_0_Click()进行处理

this.btn_7.Click+=newSystem.EventHandler(this.btn_0_Click);

this.btn_8.Click+=newSystem.EventHandler(this.btn_0_Click);

this.btn_9.Click+=newSystem.EventHandler(this.btn_0_Click);

this.btn_div.Click+=newSystem.EventHandler(this.btn_div_Click);

this.btn_sprt.Click+=newSystem.EventHandler(this.btn_sprt_Click);

this.btn_4.Click+=newSystem.EventHandler(this.btn_0_Click);

this.btn_5.Click+=newSystem.EventHandler(this.btn_0_Click);

this.btn_6.Click+=newSystem.EventHandler(this.btn_0_Click);

this.btn_mul.Click+=newSystem.EventHandler(this.btn_mul_Click);

this.btn_spr.Click+=newSystem.EventHandler(this.btn_spr_Click);

this.btn_1.Click+=newSystem.EventHandler(this.btn_0_Click);

this.btn_2.Click+=newSystem.EventHandler(this.btn_0_Click);

this.btn_3.Click+=newSystem.EventHandler(this.btn_0_Click);

this.btn_add.Click+=newSystem.EventHandler(this.btn_add_Click);

this.btn_rev.Click+=newSystem.EventHandler(this.btn_rev_Click);

this.btn_0.Click+=newSystem.EventHandler(this.btn_0_Click);

this.btn_sign.Click+=newSystem.EventHandler(this.btn_sign_Click);

this.btn_dot.Click+=newSystem.EventHandler(this.btn_dot_Click);

this.btn_sub.Click+=newSystem.EventHandler(this.btn_sub_Click);

this.btn_equ.Click+=newSystem.EventHandler(this.btn_equ_Click);

this.Load+=newSystem.EventHandler(this.Page_Load);


}

#endregion

privatevoidbtn_0_Click(objectsender,System.EventArgse)

{

//当点击一个数字按钮的时候,需要进行判断,如果没有点击

//小数点,那么就把原来的数值扩大10倍后再加上当前的数值,

//如果点击了小数点,那么就将当前的数值除以一个权数,再

//加上原来的数值,得到新的数值。

System.Web.UI.WebControls.ButtonbtnTmp;

doublei=0;

btnTmp=senderasSystem.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();

}

}

privatevoidbtn_equ_Click(objectsender,System.EventArgse)

{

if(Int32.Parse(Session["num"].ToString())==0)

{

Session["res"]=0;

Session["tmp"]=0;

txtShow.Text=Session["tmp"].ToString();

return;

}

//对Session对象进行转化减少代码编写量

doubleres=double.Parse(Session["res"].ToString());

doubletmp=double.Parse(Session["tmp"].ToString());

intopt=Int32.Parse(Session["opt"].ToString());

switch(opt)

{

//加法

caseADD:

res=res+tmp;

break;

//减法

caseSUB:

res=res-tmp;

break;

//乘法

caseMUL:

res=res*tmp;

break;

//除法

caseDIV:

res=res/tmp;

break;

//平方

caseSQR:

res=tmp*tmp;

break;

//平方根

caseSQRT:

res=System.Math.Sqrt(tmp);

break;

default:

return;

}

txtShow.Text=res.ToString();

Session["opt"]=NULL;

Session["res"]=0;

Session["num"]=0;

}

privatevoidbtn_div_Click(objectsender,System.EventArgse)

{

//除法运算

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();

}


privatevoidbtn_mul_Click(objectsender,System.EventArgse)

{

//乘法运算

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();

}


privatevoidbtn_sub_Click(objectsender,System.EventArgse)

{

//减法运算

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();

}


privatevoidbtn_add_Click(objectsender,System.EventArgse)

{

//加法运算

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();

}

privatevoidbtn_sprt_Click(objectsender,System.EventArgse)

{

//开方运算

if(Int32.Parse(Session["num"].ToString())>0)

{

Session["tmp"]=Math.Sqrt(double.Parse(Session["tmp"].ToString()));

txtShow.Text=Session["tmp"].ToString();

}

}


privatevoidbtn_spr_Click(objectsender,System.EventArgse)

{

//平方运算

Session["tmp"]=double.Parse(Session["tmp"].ToString())*double.Parse(Session["tmp"].ToString());

txtShow.Text=Session["tmp"].ToString();

}

privatevoidbtn_rev_Click(objectsender,System.EventArgse)

{

//倒数运算

Session["tmp"]=1/double.Parse(Session["tmp"].ToString());

txtShow.Text=Session["tmp"].ToString();

}

privatevoidbtn_dot_Click(objectsender,System.EventArgse)

{

//单击了小数点

Session["dot"]=HASDOT;

Session["dotnum"]=0;

}


privatevoidbtn_sign_Click(objectsender,System.EventArgse)

{

//单击了符号运算Session["tmp"]=double.Parse(Session["tmp"].ToString())-double.Parse(Session["tmp"].ToString());

txtShow.Text=Session["tmp"].ToString();

}

}

}


OK!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: