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">
<asp:label id="Label1" runat="server" BackColor="ActiveCaption" Width="264px">WebApplication版本的计算器</asp:label></td></tr><tr>
<td colSpan="5">
<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">
<asp:button id="btn_7" runat="server" Width="40px" Text="7"></asp:button></td>
<TD style="HEIGHT: 37px">
<asp:button id="btn_8" runat="server" Width="40px" Text="8"></asp:button></TD>
<TD style="WIDTH: 56px; HEIGHT: 37px">
<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> </td>
<td style="HEIGHT: 37px">
<asp:button id="btn_sprt" runat="server" Width="36px" Text="sprt"></asp:button></td></tr><tr>
<td style="WIDTH: 23px">
<asp:button id="btn_4" runat="server" Width="40px" Text="4"></asp:button></td>
<td>
<asp:button id="btn_5" runat="server" Width="40px" Text="5"></asp:button></td>
<td style="WIDTH: 56px">
<asp:button id="btn_6" runat="server" Width="48px" Text="6"></asp:button></td>
<td>
<asp:button id="btn_mul" runat="server" Width="39px" Text="*"></asp:button></td>
<td>
<asp:button id="btn_spr" runat="server" Width="32px" Text="spr"></asp:button></td>
</tr>
<tr>
<td style="WIDTH: 23px">
<asp:button id="btn_1" runat="server" Width="40px" Text="1"></asp:button></td>
<td>
<asp:button id="btn_2" runat="server" Width="42px" Text="2"></asp:button></td>
<td style="WIDTH: 56px">
<asp:button id="btn_3" runat="server" Width="44px" Text="3"></asp:button></td>
<td>
<asp:button id="btn_add" runat="server" Width="40px" Text="+"></asp:button></td>
<td>
<asp:button id="btn_rev" runat="server" Width="32px" Text="1/x"></asp:button></td></tr><tr>
<td style="WIDTH: 23px">
<asp:button id="btn_0" runat="server" Width="40px" Text="0"></asp:button></td>
<td>
<asp:button id="btn_sign" runat="server" Width="41px" Text="+/-"></asp:button></td>
<td style="WIDTH: 56px">
<asp:button id="btn_dot" runat="server" Width="46px" Text="."></asp:button></td>
<td>
<asp:button id="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的程序清单: