calculator.aspx.cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class Default2 : System.Web.UI.Page
{
static double[] num = new double[10]; //操作数数组
static char[] oprt = new char[10]; //操作运算符
static string no="";//数字字符串转换为double型数据
static int i = 0, j = 0, st = 0, q = 0;//i数组下标,j运算符下标,st显示屏清零标识符,q 括号下标
static int[] kh = new int[10];//括号数量和坐标 protected void Page_Load(object sender, EventArgs e)
{ if (st == 1) { Label1.Text = "";st = 0; }//清零后操作重新显示 }
protected void Button_Click(object sender, EventArgs e)//数字键组合按钮事件,利用中间no过度得到double型数据
{
Button b=sender as Button;
no += b.Text.ToString() ;
Label1.Text += b.Text;
num[i] = Convert.ToDouble(no);
}
protected void ButtonOprt_Click(object sender, EventArgs e)
{
Button b = sender as Button;
Label1.Text += b.Text;
oprt[j] = Convert.ToChar(b.Text);//操作运算符-+*/
++i; ++j; no = ""; if (j - kh[q] == 3)//保持数字数组中的个数为三个,大于三个时就可以判断计算更新数字数组;
//kh[q]是对于括号中的数字进行统计
{
switch (oprt[j-2].ToString())
{
case "/"://判断第二个运算符是不是*/,假如是还需判断第一个是不是,从而决定计算顺序
{
if (oprt[j - 3].ToString().Equals("/") || oprt[j - 3].ToString().Equals("*")) break;
else { num[i-2] /= num[i-1]; goto update2; }//
} case "*":
{
if (oprt[j - 3].ToString().Equals("/") || oprt[j - 3].ToString().Equals("*")) break;
else { num[i - 2] *= num[i - 1]; goto update2; }
}
default: break; }
switch (oprt[j - 3].ToString())//计算第一个运算符
{
case "/": num[i - 3] /= num[i - 2]; goto update1;
case "*": num[i - 3] *= num[i - 2]; goto update1;
case "+": num[i - 3] += num[i - 2]; goto update1;
case "-": num[i - 3] -= num[i - 2]; goto update1;
default: break; } update1://根据计算结果更新数组和运算符
num[i - 2] = num[i - 1];
oprt[j - 3] = oprt[j - 2];
update2:
i--; j--;
oprt[j - 1] = oprt[j]; } } protected void ButtonDot_Click(object sender, EventArgs e)
{
Label1.Text += ".";
no += ".";
}
protected void ButtonClear_Click(object sender, EventArgs e)//数据清零按钮
{
i = j = 0;no = "";//数据清零
Label1.Text ="0";
st = 1;
}
protected void ButtonTime_Click(object sender, EventArgs e)//显示时间按钮
{
i = j = 0; no = "";//数据清零
Label1.Text = DateTime.Now.ToString();
st = 1;
}
protected void ButtonResult_Click(object sender, EventArgs e)
{
Label1.Text += RstBt.Text;
if (j == 2)
{
switch (oprt[1].ToString())//同ButtonOprt_Click事件中
{
case "/":
{
if (oprt[0].ToString().Equals("/") || oprt[0].ToString().Equals("*")) break;
else { num[1] /= num[2]; i--; j--; break; }
}
case "*":
{
if (oprt[0].ToString().Equals("/") || oprt[0].ToString().Equals("*")) break;
else { num[1] *= num[2]; i--; j--; break; }
}
default: break; }
}
for (int k = 0; k <i; k++)//计算结果
{
switch (oprt[k].ToString())
{
case "/": num[k] /= num[k + 1]; break;
case "*": num[k] *= num[k + 1]; break;
case "+": num[k] += num[k + 1]; break;
case "-": num[k] -= num[k + 1]; break;
default: break;
}
num[k+1] = num[k];
}
num[0] = num[i];
Label1.Text += num[0];
i = j = 0; no = "";//数据清零
st = 1;
}
protected void ButtonOprt2_Click(object sender, EventArgs e)
{
/*对应于计算器中最左边一排运算符
* 使用举例如:计算5!需先点击3数字按钮
* 然后点击!阶乘按钮,显示5_n!
* 点击=按钮即显示结果 5_n!=120,其他同理*/
Button b = sender as Button;
Label1.Text += ("_" + b.Text);
switch (b.ID)
{
case ("t"): num[i] = Math.Pow(10, num[i]); break;
case ("s"): num[i] = Math.Pow(num[i], 2); break;
case ("f"):
for (int m = (int)num[i] - 1; m >= 1; m--)
{ num[i] *= m; }
break;
case ("r"): num[i] = 1 / num[i]; break;
case ("p"): num[i] = num[i] / 100; break;
default: break;
} }
protected void ButtonLkh_Click(object sender, EventArgs e)
{
Button b = sender as Button;
Label1.Text += b.Text;
kh[++q] = i;//记录括号位置,以方便括号中计算结果后存储到num相应索引中
//ButtonOprt_Click中j->j-kh[q],num[0]->num[kh[q]], }
protected void ButtonRkh_Click(object sender, EventArgs e)
{
Button b = sender as Button;
Label1.Text += b.Text;
//当点击一次右括号时把括号中的结果计算出来
//同于=中的算法,num[0]->num[kh[q]],i=kh[q--];j=kh[q--];
if (j - kh[q] == 2)
{
switch (oprt[j-1].ToString())//同ButtonOprt_Click事件中
{
case "/":
{
if (oprt[j-2].ToString().Equals("/") || oprt[j-2].ToString().Equals("*")) break;
else { num[i-2] /= num[i-1]; i--; j--; break; }
}
case "*":
{
if (oprt[j - 2].ToString().Equals("/") || oprt[j - 2].ToString().Equals("*")) break;
else { num[i - 2] *= num[i - 1]; i--; j--; break; }
}
default: break; }
}
for (int k = kh[q]; k < i; k++)//计算结果
{
switch (oprt[k].ToString())
{
case "/": num[k] /= num[k + 1]; break;
case "*": num[k] *= num[k + 1]; break;
case "+": num[k] += num[k + 1]; break;
case "-": num[k] -= num[k + 1]; break;
default: break;
}
num[k + 1] = num[k];
}
num[kh[q]] = num[i];
i = kh[q];
j = kh[q--];
}
}
calculator.aspx代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="calculator.aspx.cs" Inherits="Default2" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css"> .bib
{width:50px;
height:35px;
line-height:30px;
background-position: 0 -132px;} </style>
</head>
<body>
<form id="form1" runat="server">
<div> <asp:Panel ID="Panel1" runat="server" BorderStyle=" inset" Width="240px"
Height="35px">
<asp:Label ID="Label1" runat="server" Text="" Height="34px" Width="260px" ></asp:Label>
</asp:Panel> <asp:Panel ID="Panel2" runat="server" BorderStyle="none" Width="283px" >
<asp:Button ID="t" runat="server" Text="10^" class="bib"
onclick="ButtonOprt2_Click" />
<asp:Button ID="Button2" runat="server" Text="(" class="bib"
onclick="ButtonLkh_Click" />
<asp:Button ID="Button3" runat="server" Text=")" class="bib"
onclick="ButtonRkh_Click" />
<asp:Button ID="Button4" runat="server" Text="C" class="bib"
onclick="ButtonClear_Click" />
<asp:Button ID="Button5" runat="server" Text="T" class="bib"
onclick="ButtonTime_Click" /> <br /> <asp:Button ID="s" runat="server" Text="x^2" class="bib"
onclick="ButtonOprt2_Click" />
<asp:Button ID="Button7" runat="server" Text="7" class="bib"
onclick="Button_Click" />
<asp:Button ID="Button8" runat="server" Text="8" class="bib"
onclick="Button_Click" />
<asp:Button ID="Button9" runat="server" Text="9" class="bib"
onclick="Button_Click"/>
<asp:Button ID="Button10" runat="server" Text="/" class="bib"
onclick="ButtonOprt_Click" /> <br /> <asp:Button ID="f" runat="server" Text="n!" class="bib"
onclick="ButtonOprt2_Click"/>
<asp:Button ID="Button12" runat="server" Text="4" class="bib"
onclick="Button_Click" />
<asp:Button ID="Button13" runat="server" Text="5" class="bib"
onclick="Button_Click"/>
<asp:Button ID="Button14" runat="server" Text="6" class="bib"
onclick="Button_Click"/>
<asp:Button ID="Button15" runat="server" Text="*" class="bib"
onclick="ButtonOprt_Click" />
<br /> <asp:Button ID="r" runat="server" Text="1/x" class="bib"
onclick="ButtonOprt2_Click"/>
<asp:Button ID="Button17" runat="server" Text="1" class="bib"
onclick="Button_Click" />
<asp:Button ID="Button18" runat="server" Text="2" class="bib"
onclick="Button_Click" />
<asp:Button ID="Button19" runat="server" Text="3" class="bib"
onclick="Button_Click" />
<asp:Button ID="Button20" runat="server" Text="-" class="bib"
onclick="ButtonOprt_Click" />
<br /> <asp:Button ID="p" runat="server" Text="%" class="bib" onclick="ButtonOprt2_Click" />
<asp:Button ID="Button22" runat="server" Text="0" class="bib"
onclick="Button_Click" />
<asp:Button ID="Button23" runat="server" Text="." class="bib"
onclick="ButtonDot_Click"/>
<asp:Button ID="RstBt" runat="server" Text="=" class="bib"
onclick="ButtonResult_Click" />
<asp:Button ID="Button25" runat="server" Text="+" class="bib"
onclick="ButtonOprt_Click" />
</asp:Panel>
</div>
</form>
</body>
</html>