文章目录
- 一、简单计算器
- 二、复杂计算器
一、简单计算器
新建Web应用项目,窗体页面
窗体设计代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AdoDemo.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>简单计算器</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtNum1" runat="server" Width="100px"></asp:TextBox>
<asp:TextBox ID="txtNum2" runat="server" Width="100px"></asp:TextBox>
<br />
<asp:DropDownList ID="ddlOperation" runat="server">
<asp:ListItem Text="+" Value="Addition"></asp:ListItem>
<asp:ListItem Text="-" Value="Subtraction"></asp:ListItem>
<asp:ListItem Text="*" Value="Multiplication"></asp:ListItem>
<asp:ListItem Text="/" Value="Division"></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btnCalculate" runat="server" Text="计算" OnClick="btnCalculate_Click"/>
<br />
<asp:Label ID="lbResult" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace AdoDemo
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnCalculate_Click(object sender, EventArgs e)
{
double num1, num2;
if (double.TryParse(txtNum1.Text, out num1) && double.TryParse(txtNum2.Text, out num2))
{
switch (ddlOperation.SelectedValue)
{
case "Addition":
lbResult.Text = (num1 + num2).ToString();
break;
case "Subtraction":
lbResult.Text = (num1 - num2).ToString();
break;
case "Multiplication":
lbResult.Text = (num1 * num2).ToString();
break;
case "Division":
lbResult.Text = (num1 / num2).ToString();
break;
}
}
else
{
lbResult.Text = "请输入有效数字!";
}
}
}
}
运行结果:
二、复杂计算器
创建项目:
窗体设计代码(查看设计器
/查看标记
进行设计模式和设计代码模式切换):
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AdoDemo.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>复杂计算器</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.calculator {
background-color: #fff;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
padding: 20px;
width: 300px;
}
.display {
background-color: #222;
color: #fff;
font-size: 2em;
padding: 10px;
text-align: right;
border-radius: 5px;
margin-bottom: 10px;
}
.buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
}
.button {
background-color: #f0f0f0;
border: none;
border-radius: 5px;
font-size: 1.5em;
padding: 20px;
cursor: pointer;
transition: background-color 0.3s;
}
.button:hover {
background-color: #ddd;
}
.button.operator {
background-color: #ff9500;
color: #fff;
}
.button.operator:hover {
background-color: #e08900;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="calculator">
<asp:TextBox ID="txtDisplay" runat="server" CssClass="display" ReadOnly="true" Width="292px"></asp:TextBox>
<div class="buttons">
<asp:Button ID="btnClear" runat="server" Text="C" CssClass="button" OnClick="btnClear_Click" />
<asp:Button ID="btnBackspace" runat="server" Text="←" CssClass="button" OnClick="btnBackspace_Click" />
<asp:Button ID="btnPercent" runat="server" Text="%" CssClass="button" OnClick="btnPercent_Click" />
<asp:Button ID="btnDivide" runat="server" Text="÷" CssClass="button operator" OnClick="btnOperator_Click" />
<asp:Button ID="btn7" runat="server" Text="7" CssClass="button" OnClick="btnNumber_Click" />
<asp:Button ID="btn8" runat="server" Text="8" CssClass="button" OnClick="btnNumber_Click" />
<asp:Button ID="btn9" runat="server" Text="9" CssClass="button" OnClick="btnNumber_Click" />
<asp:Button ID="btnMultiply" runat="server" Text="×" CssClass="button operator" OnClick="btnOperator_Click" />
<asp:Button ID="btn4" runat="server" Text="4" CssClass="button" OnClick="btnNumber_Click" />
<asp:Button ID="btn5" runat="server" Text="5" CssClass="button" OnClick="btnNumber_Click" />
<asp:Button ID="btn6" runat="server" Text="6" CssClass="button" OnClick="btnNumber_Click" />
<asp:Button ID="btnSubtract" runat="server" Text="-" CssClass="button operator" OnClick="btnOperator_Click" />
<asp:Button ID="btn1" runat="server" Text="1" CssClass="button" OnClick="btnNumber_Click" />
<asp:Button ID="btn2" runat="server" Text="2" CssClass="button" OnClick="btnNumber_Click" />
<asp:Button ID="btn3" runat="server" Text="3" CssClass="button" OnClick="btnNumber_Click" />
<asp:Button ID="btnAdd" runat="server" Text="+" CssClass="button operator" OnClick="btnOperator_Click" />
<asp:Button ID="btn0" runat="server" Text="0" CssClass="button" OnClick="btnNumber_Click" />
<asp:Button ID="btnDecimal" runat="server" Text="." CssClass="button" OnClick="btnDecimal_Click" />
<asp:Button ID="btnEquals" runat="server" Text="=" CssClass="button operator" OnClick="btnEquals_Click" />
</div>
</div>
</form>
</body>
</html>
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace AdoDemo
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["CurrentValue"] = "0";
ViewState["Operator"] = string.Empty;
ViewState["FirstOperand"] = string.Empty;
}
}
protected void btnNumber_Click(object sender, EventArgs e)
{
string number = ((Button)sender).Text;
if (ViewState["CurrentValue"].ToString() == "0")
{
ViewState["CurrentValue"] = number;
}
else
{
ViewState["CurrentValue"] += number;
}
txtDisplay.Text = ViewState["CurrentValue"].ToString();
}
protected void btnOperator_Click(object sender, EventArgs e)
{
ViewState["FirstOperand"] = ViewState["CurrentValue"];
ViewState["Operator"] = ((Button)sender).Text;
ViewState["CurrentValue"] = "0";
}
protected void btnEquals_Click(object sender, EventArgs e)
{
double firstOperand = Convert.ToDouble(ViewState["FirstOperand"]);
double secondOperand = Convert.ToDouble(ViewState["CurrentValue"]);
string operatorSymbol = ViewState["Operator"].ToString();
double result = 0;
switch (operatorSymbol)
{
case "+":
result = firstOperand + secondOperand;
break;
case "-":
result = firstOperand - secondOperand;
break;
case "×":
result = firstOperand * secondOperand;
break;
case "÷":
result = firstOperand / secondOperand;
break;
}
ViewState["CurrentValue"] = result.ToString();
txtDisplay.Text = result.ToString();
}
protected void btnClear_Click(object sender, EventArgs e)
{
ViewState["CurrentValue"] = "0";
ViewState["Operator"] = string.Empty;
ViewState["FirstOperand"] = string.Empty;
txtDisplay.Text = "0";
}
protected void btnBackspace_Click(object sender, EventArgs e)
{
string currentValue = ViewState["CurrentValue"].ToString();
if (currentValue.Length > 1)
{
ViewState["CurrentValue"] = currentValue.Substring(0, currentValue.Length - 1);
}
else
{
ViewState["CurrentValue"] = "0";
}
txtDisplay.Text = ViewState["CurrentValue"].ToString();
}
protected void btnPercent_Click(object sender, EventArgs e)
{
double currentValue = Convert.ToDouble(ViewState["CurrentValue"]);
ViewState["CurrentValue"] = (currentValue / 100).ToString();
txtDisplay.Text = ViewState["CurrentValue"].ToString();
}
protected void btnDecimal_Click(object sender, EventArgs e)
{
if (!ViewState["CurrentValue"].ToString().Contains("."))
{
ViewState["CurrentValue"] += ".";
}
txtDisplay.Text = ViewState["CurrentValue"].ToString();
}
}
}
运行结果: