ASP编写⼀个⽹页计算器
本⽂主要介绍使⽤ASP编写⼀个简易的⽹页计算器。前端主要是使⽤⾃带的控件,实现逻辑主要是服务端C#。⼀、实现功能
简单加减乘除四则运算
能防⽌出现多个⼩数点等不符合实际错误
⼆、实现界⾯
三、前端代码
<!DOCTYPE html>
<html xmlns="/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type"content="text/html; charset=utf-8"/>
<title>简易计算器</title>
<style type="text/css">
#form{
height: 400px;
width: 460px;
margin:20px auto;
background-color:#f2f2f2;
}
.title{
width:460px;
font-size:20px;
margin:20px auto;
padding-top: 20px;
text-align:center;
}
.panel{
width:460px;
text-align:center;
}
.panel_txt{
height:45px;
width:335px;
text-align:right;
font-size:20px;
padding: 0px 10px;
flex-direction:row;
margin:20px auto;
}
.
number_area{
width:200px;
}
.btn_normal{
width:50px;
height:40px;
margin:10px 5px;
font-size:20px;
}
.zero{
width:113px;
}
.long{
height:100px;
}
.symbol_area,.equal_area{
width:70px;
}
</style>
</head>
<body>
<form id="form"runat="server">
<div class="title"><Label ID="Label"runat="server"Text="简易计算器"></Label></div>
<div class="panel">
<TextBox ID="Result"runat="server"CssClass="panel_txt"ReadOnly="True"></TextBox>
</div>
<div class="container">
<div class="number_area">
<Button ID="Btn_9"runat="server"Text="9"CssClass="btn_normal"OnClick="Btn_9_Click"/>
<Button ID="Btn_8"runat="server"Text="8"CssClass="btn_normal"OnClick="Btn_8_Click"/>
<Button ID="Btn_7"runat="server"Text="7"CssClass="btn_normal"OnClick="Btn_7_Click"/>
<Button ID="Btn_6"runat="server"Text="6"CssClass="btn_normal"OnClick="Btn_6_Click"/>
<Button ID="Btn_5"runat="server"Text="5"CssClass="btn_normal"OnClick="Btn_5_Click"/>
<Button ID="Btn_4"runat="server"Text="4"CssClass="btn_normal"OnClick="Btn_4_Click"/>
<Button ID="Btn_3"runat="server"Text="3"CssClass="btn_normal"OnClick="Btn_3_Click"/>
<Button ID="Btn_2"runat="server"Text="2"CssClass="btn_normal"OnClick="Btn_2_Click"/>
<Button ID="Btn_1"runat="server"Text="1"CssClass="btn_normal"OnClick="Btn_1_Click"/>
<Button ID="Btn_0"runat="server"Text="0"CssClass="btn_normal zero"OnClick="Btn_0_Click"/>
<Button ID="Btn_dot"runat="server"Text="."CssClass="btn_normal"OnClick="Btn_dot_Click"/>
</div>
<div class="symbol_area">
<Button ID="Btn_divide"runat="server"Text="/"CssClass="btn_normal"OnClick="Btn_divide_Click"/>
<Button ID="Btn_multy"runat="server"Text="×"CssClass="btn_normal"OnClick="Btn_multy_Click"/>
<Button ID="Btn_minus"runat="server"Text="-"CssClass="btn_normal"OnClick="Btn_minus_Click"/>
<Button ID="Btn_add"runat="server"Text="+"CssClass="btn_normal"OnClick="Btn_add_Click"/>
</div>
<div class="equal_area">
<Button ID="Btn_clear"runat="server"Text="←"CssClass="btn_normal long"OnClick="Btn_clear_Click"/> <Button ID="Btn_equal"runat="server"Text="="CssClass="btn_normal long"OnClick="Btn_equal_Click"/> </div>
</div>
</form>
</body>
</html>
四、后台逻辑实现
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Btn_9_Click(object sender, EventArgs e)
protected void Btn_8_Click(object sender, EventArgs e)
{
Result.Text +="8";
}
protected void Btn_7_Click(object sender, EventArgs e)
{
Result.Text +="7";
}
protected void Btn_6_Click(object sender, EventArgs e)
{
Result.Text +="6";
}
protected void Btn_5_Click(object sender, EventArgs e)
{
Result.Text +="5";
}
protected void Btn_4_Click(object sender, EventArgs e)
{
Result.Text +="4";
}
protected void Btn_3_Click(object sender, EventArgs e)
{
Result.Text +="3";
}
protected void Btn_2_Click(object sender, EventArgs e)
{
Result.Text +="2";
}
protected void Btn_1_Click(object sender, EventArgs e)
{
Result.Text +="1";
}
protected void Btn_0_Click(object sender, EventArgs e)
{
Result.Text +="0";
}
protected void Btn_dot_Click(object sender, EventArgs e) {
string str = Result.Text;
if(str.Substring(str.Length -1,1).Equals('.'))
{
Result.Text = str;
}
else
{
char flag ='a';
foreach(char c in str)
{
if(c =='+') flag ='+';
if(c =='-') flag ='-';
if(c =='×') flag ='×';
if(c =='/') flag ='/';
if(str.Contains('.'))
{
Result.Text = str;
}
else
{
Result.Text = str +'.';
}
}
else{
string[] args =new string[2];
string a;
switch(flag)
{
case'+':
args = str.Split('+');
a = args[1];
if(a.Contains('.'))
{
Result.Text = str;
}
else
{说明哪些网站属于asp
Result.Text = str +'.';
}
break;
case'-':
args = str.Split('-');
a = args[1];
if(a.Contains('.'))
{
Result.Text = str;
}
else
{
Result.Text = str +'.';
}
break;
case'×':
args = str.Split('×');
a = args[1];
if(a.Contains('.'))
{
Result.Text = str;
}
else
{
Result.Text = str +'.';
}
break;
case'/':
args = str.Split('/');
a = args[1];
if(a.Contains('.'))
{
Result.Text = str;
}
else
{
Result.Text = str +'.';
}
break;
}
protected void Btn_add_Click(object sender, EventArgs e)
{
string str = Result.Text;
if(str.Contains("+")||(str.Contains("-")||str.Contains("/")||str.Contains("×")))
{
Result.Text = Result.Text;
}
else{
Result.Text +="+";
}
}
protected void Btn_minus_Click(object sender, EventArgs e)
{
string str = Result.Text;
if(str.Contains("+")||(str.Contains("-")|| str.Contains("/")|| str.Contains("×"))) {
Result.Text = Result.Text;
}
else
{
Result.Text +="-";
}
}
protected void Btn_multy_Click(object sender, EventArgs e)
{
string str = Result.Text;
if(str.Contains("+")||(str.Contains("-")|| str.Contains("/")|| str.Contains("×"))) {
Result.Text = Result.Text;
}
else
{
Result.Text +="×";
}
}
protected void Btn_divide_Click(object sender, EventArgs e)
{
string str = Result.Text;
if(str.Contains("+")||(str.Contains("-")|| str.Contains("/")|| str.Contains("×"))) {
Result.Text = Result.Text;
}
else
{
Result.Text +="/";
}
}
protected void Btn_clear_Click(object sender, EventArgs e)
{
String str = Result.Text;
if(!Result.Text.Equals(""))
Result.Text= str.Substring(0, str.Length -1);
}
protected void Btn_equal_Click(object sender, EventArgs e)
{

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。