aspx计算器,计算部分是用javascript实现
代码:
Code
<%@ Page Language="C#" %>
<!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>
<script runat="server">
public void Num_Click(object sender, EventArgs e)
{
Button bt = (Button)sender;
if (DispClear.Text == "true")
{
Disp.Text = "";
DispClear.Text = "false";
}
Disp.Text = Disp.Text + bt.Text;
}
public void DotBttn_Click(object sender, EventArgs e)
{
bool you = true;
string youdian = Disp.Text;
for (int k = 0; k < youdian.Length;k++)
{
if (youdian[k] == '.')
{
you = true; break;
}
else
{
you = false;
}
}
if (you)
{
Disp.Text = Disp.Text;
}
else
{
Disp.Text = Disp.Text + DotBttn.Text;
}
}
public void Oper_Click(object sender, EventArgs e)
{
Button bt = (Button)sender;
Operand1.Text = Disp.Text;
Operator1.Text = bt.Text;
Disp.Text = "0";
DispClear.Text = "true";
}
public void Equals_Click(object sender, EventArgs e)
{
double result = 0;
Operand2.Text = Disp.Text;
switch (Operator1.Text)
{
case "+":
result = Convert.ToDouble(Operand1.Text) + Convert.ToDouble(Operand2.Text); break;
case "-":
result = Convert.ToDouble(Operand1.Text) - Convert.ToDouble(Operand2.Text); break;
case "×":
result = Convert.ToDouble(Operand1.Text) * Convert.ToDouble(Operand2.Text); break;
case "÷":
result = Convert.ToDouble(Operand1.Text) / Convert.ToDouble(Operand2.Text); break;
}
Disp.Text = result.ToString();
DispClear.Text = "true";
}
public void xfsh_Click(object sender, EventArgs e)
{
Disp.Text = (Convert.ToDouble(Disp.Text) * (-1)).ToString();
DispClear.Text = "true";
}
public void Dsh_Click(object sender, EventArgs e)
{
Disp.Text = (1 / Convert.ToDouble(Disp.Text)).ToString();
DispClear.Text = "true";
}
public void Clear_Click(object sender, EventArgs e)
{
Disp.Text = "0";
DispClear.Text = "true";
}
public void AllClear_Click(object sender, EventArgs e)
{
Disp.Text = "0";
DispClear.Text = "true";
Operand1.Text = "";
Operand2.Text = "";
}
public void SqltNum_Click(object sender, EventArgs e)
{
Disp.Text = (System.Math.Sqrt(Convert.ToDouble(Disp.Text))).ToString();
DispClear.Text = "true";
}
</script>
</head>
<body style="text-align: center">
<form id="Form1" runat="server">
<asp:Label id="Disp" runat="server" Font-Bold="True" Width="160px">0</asp:Label> <br />
<table>
<tbody>
<tr>
<td width="34">
<asp:Button id="xfsh" OnClick="xfsh_Click" runat="server" Width="30px" Text="+/-"></asp:Button>
</td>
<td width="0">
</td>
<td width="10">
</td>
<td width="10">
</td>
<td style="width: 28px">
<asp:Button id="Div" OnClick="Oper_Click" runat="server" Width="25px" Text="÷"></asp:Button>
</td>
</tr>
<tr>
<td>
<asp:Button id="Dsh" OnClick="Dsh_Click" runat="server" Width="30px" Text="1/x"></asp:Button>
</td>
<td>
<asp:Button id="Num7" OnClick="Num_Click" runat="server" Width="25px" Text="7"></asp:Button>
</td>
<td>
<asp:Button id="Num8" OnClick="Num_Click" runat="server" Width="25px" Text="8"></asp:Button>
</td>
<td>
<asp:Button id="Num9" OnClick="Num_Click" runat="server" Width="25px" Text="9"></asp:Button>
</td>
<td style="width: 28px">
<asp:Button id="Times" OnClick="Oper_Click" runat="server" Width="25px" Text="×"></asp:Button>
</td>
</tr>
<tr>
<td>
<asp:Button id="SqltNum" OnClick="SqltNum_Click" runat="server" Width="30px" Text="√"></asp:Button>
</td>
<td>
<asp:Button id="Num4" OnClick="Num_Click" runat="server" Width="25px" Text="4"></asp:Button>
</td>
<td>
<asp:Button id="Num5" OnClick="Num_Click" runat="server" Width="25px" Text="5"></asp:Button>
</td>
<td>
<asp:Button id="Num6" OnClick="Num_Click" runat="server" Width="25px" Text="6"></asp:Button>
</td>
<td style="width: 28px">
<asp:Button id="Minus" OnClick="Oper_Click" runat="server" Width="25px" Text="-"></asp:Button>
</td>
</tr>
<tr>
<td>
<asp:Button id="Clear" OnClick="Clear_Click" runat="server" Width="30px" Text="C" BorderColor="Red" BackColor="Red"></asp:Button>
</td>
<td>
<asp:Button id="Num1" OnClick="Num_Click" runat="server" Width="25px" Text="1"></asp:Button>
</td>
<td>
<asp:Button id="Num2" OnClick="Num_Click" runat="server" Width="25px" Text="2"></asp:Button>
</td>
<td>
<asp:Button id="Num3" OnClick="Num_Click" runat="server" Width="25px" Text="3"></asp:Button>
</td>
<td rowspan="2" style="width: 28px">
<asp:Button id="Plus" OnClick="Oper_Click" runat="server" Width="25px" Text="+" Height="52px"></asp:Button>
</td>
</tr>
<tr>
<td>
<asp:Button id="AllClear" OnClick="AllClear_Click" runat="server" Width="30px" Text="AC" BorderColor="Red" BackColor="Red"></asp:Button>
</td>
<td>
<asp:Button id="Num0" OnClick="Num_Click" runat="server" Width="25px" Text="0"></asp:Button>
</td>
<td>
<asp:Button id="DotBttn" OnClick="DotBttn_Click" runat="server" Width="25px" Text="."></asp:Button>
</td>
<td>
<asp:Button id="Equals" OnClick="Equals_Click" runat="server" Width="25px" Text="="></asp:Button>
</td>
</tr>
</tbody>
</table>
<br />
<asp:Label id="Operand1" runat="server" Width="160px"></asp:Label><br />
<br />
<asp:Label id="Operand2" runat="server" Width="160px" Visible="true"></asp:Label>
<br />
<br />
<asp:Label id="Operator1" runat="server" Visible="true" Width="160px"></asp:Label>
<br />
<br />
<asp:Label id="DispClear" runat="server" Visible="true" Width="160px">true</asp:Label>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<!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>
<script runat="server">
public void Num_Click(object sender, EventArgs e)
{
Button bt = (Button)sender;
if (DispClear.Text == "true")
{
Disp.Text = "";
DispClear.Text = "false";
}
Disp.Text = Disp.Text + bt.Text;
}
public void DotBttn_Click(object sender, EventArgs e)
{
bool you = true;
string youdian = Disp.Text;
for (int k = 0; k < youdian.Length;k++)
{
if (youdian[k] == '.')
{
you = true; break;
}
else
{
you = false;
}
}
if (you)
{
Disp.Text = Disp.Text;
}
else
{
Disp.Text = Disp.Text + DotBttn.Text;
}
}
public void Oper_Click(object sender, EventArgs e)
{
Button bt = (Button)sender;
Operand1.Text = Disp.Text;
Operator1.Text = bt.Text;
Disp.Text = "0";
DispClear.Text = "true";
}
public void Equals_Click(object sender, EventArgs e)
{
double result = 0;
Operand2.Text = Disp.Text;
switch (Operator1.Text)
{
case "+":
result = Convert.ToDouble(Operand1.Text) + Convert.ToDouble(Operand2.Text); break;
case "-":
result = Convert.ToDouble(Operand1.Text) - Convert.ToDouble(Operand2.Text); break;
case "×":
result = Convert.ToDouble(Operand1.Text) * Convert.ToDouble(Operand2.Text); break;
case "÷":
result = Convert.ToDouble(Operand1.Text) / Convert.ToDouble(Operand2.Text); break;
}
Disp.Text = result.ToString();
DispClear.Text = "true";
}
public void xfsh_Click(object sender, EventArgs e)
{
Disp.Text = (Convert.ToDouble(Disp.Text) * (-1)).ToString();
DispClear.Text = "true";
}
public void Dsh_Click(object sender, EventArgs e)
{
Disp.Text = (1 / Convert.ToDouble(Disp.Text)).ToString();
DispClear.Text = "true";
}
public void Clear_Click(object sender, EventArgs e)
{
Disp.Text = "0";
DispClear.Text = "true";
}
public void AllClear_Click(object sender, EventArgs e)
{
Disp.Text = "0";
DispClear.Text = "true";
Operand1.Text = "";
Operand2.Text = "";
}
public void SqltNum_Click(object sender, EventArgs e)
{
Disp.Text = (System.Math.Sqrt(Convert.ToDouble(Disp.Text))).ToString();
DispClear.Text = "true";
}
</script>
</head>
<body style="text-align: center">
<form id="Form1" runat="server">
<asp:Label id="Disp" runat="server" Font-Bold="True" Width="160px">0</asp:Label> <br />
<table>
<tbody>
<tr>
<td width="34">
<asp:Button id="xfsh" OnClick="xfsh_Click" runat="server" Width="30px" Text="+/-"></asp:Button>
</td>
<td width="0">
</td>
<td width="10">
</td>
<td width="10">
</td>
<td style="width: 28px">
<asp:Button id="Div" OnClick="Oper_Click" runat="server" Width="25px" Text="÷"></asp:Button>
</td>
</tr>
<tr>
<td>
<asp:Button id="Dsh" OnClick="Dsh_Click" runat="server" Width="30px" Text="1/x"></asp:Button>
</td>
<td>
<asp:Button id="Num7" OnClick="Num_Click" runat="server" Width="25px" Text="7"></asp:Button>
</td>
<td>
<asp:Button id="Num8" OnClick="Num_Click" runat="server" Width="25px" Text="8"></asp:Button>
</td>
<td>
<asp:Button id="Num9" OnClick="Num_Click" runat="server" Width="25px" Text="9"></asp:Button>
</td>
<td style="width: 28px">
<asp:Button id="Times" OnClick="Oper_Click" runat="server" Width="25px" Text="×"></asp:Button>
</td>
</tr>
<tr>
<td>
<asp:Button id="SqltNum" OnClick="SqltNum_Click" runat="server" Width="30px" Text="√"></asp:Button>
</td>
<td>
<asp:Button id="Num4" OnClick="Num_Click" runat="server" Width="25px" Text="4"></asp:Button>
</td>
<td>
<asp:Button id="Num5" OnClick="Num_Click" runat="server" Width="25px" Text="5"></asp:Button>
</td>
<td>
<asp:Button id="Num6" OnClick="Num_Click" runat="server" Width="25px" Text="6"></asp:Button>
</td>
<td style="width: 28px">
<asp:Button id="Minus" OnClick="Oper_Click" runat="server" Width="25px" Text="-"></asp:Button>
</td>
</tr>
<tr>
<td>
<asp:Button id="Clear" OnClick="Clear_Click" runat="server" Width="30px" Text="C" BorderColor="Red" BackColor="Red"></asp:Button>
</td>
<td>
<asp:Button id="Num1" OnClick="Num_Click" runat="server" Width="25px" Text="1"></asp:Button>
</td>
<td>
<asp:Button id="Num2" OnClick="Num_Click" runat="server" Width="25px" Text="2"></asp:Button>
</td>
<td>
<asp:Button id="Num3" OnClick="Num_Click" runat="server" Width="25px" Text="3"></asp:Button>
</td>
<td rowspan="2" style="width: 28px">
<asp:Button id="Plus" OnClick="Oper_Click" runat="server" Width="25px" Text="+" Height="52px"></asp:Button>
</td>
</tr>
<tr>
<td>
<asp:Button id="AllClear" OnClick="AllClear_Click" runat="server" Width="30px" Text="AC" BorderColor="Red" BackColor="Red"></asp:Button>
</td>
<td>
<asp:Button id="Num0" OnClick="Num_Click" runat="server" Width="25px" Text="0"></asp:Button>
</td>
<td>
<asp:Button id="DotBttn" OnClick="DotBttn_Click" runat="server" Width="25px" Text="."></asp:Button>
</td>
<td>
<asp:Button id="Equals" OnClick="Equals_Click" runat="server" Width="25px" Text="="></asp:Button>
</td>
</tr>
</tbody>
</table>
<br />
<asp:Label id="Operand1" runat="server" Width="160px"></asp:Label><br />
<br />
<asp:Label id="Operand2" runat="server" Width="160px" Visible="true"></asp:Label>
<br />
<br />
<asp:Label id="Operator1" runat="server" Visible="true" Width="160px"></asp:Label>
<br />
<br />
<asp:Label id="DispClear" runat="server" Visible="true" Width="160px">true</asp:Label>
</form>
</body>
</html>