用ASP.NET实现计算器功能

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的程序清单:

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值