asp.net实现一个能够进行加减乘除)简单的网页计算器
aspx.cs文件代码: - using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace WebApplication2
- {
- public partial class Comput : System.Web.UI.Page
- {
- double result;
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
- /*实现数字运算符*/
- protected void Button1_Click(object sender, EventArgs e)
- {
- Button btn = (Button)sender;
- if (TextBox1.Text == "")
- {
- TextBox1.Text = btn.Text;
- }
- else
- {
- TextBox1.Text += btn.Text;
- }
-
- }
- protected void TextBox1_TextChanged(object sender, EventArgs e)
- {
- }
- /*实现结果运算符“=”功能*/
- protected void Button4_Click(object sender, EventArgs e)
- {
- string s_txt = TextBox1.Text;
-
- int space = s_txt.IndexOf(' ');
-
- if (space < 0) //如果只输入一个数值后直接按等于键‘=’
- {
- TextBox1.Text = TextBox1.Text;
- }
- else
- {
- string c1 = s_txt.Substring(0, space);//获取字符串s_txt中的从0开始的n个字符,其中space为想要提取的字符个数
- char operation = Convert.ToChar(s_txt.Substring((space + 1), 1));
- string c2 = s_txt.Substring(space + 3);
- double arg1 = Convert.ToDouble(c1);
- double arg2 = Convert.ToDouble(c2);
- TextBox1.Text = result.ToString();
- switch (operation)
- {
- case '+':
- result = arg1 + arg2;
- break;
- case '-':
- result = arg1 - arg2;
- break;
- case '×':
- result = arg1 * arg2;
- break;
- case '÷':
- result = arg1 / arg2;
- break;
- default:
- throw new ApplicationException();
- }
- TextBox1.Text = result.ToString();
- }
- }
- /*实现小数点*/
- protected void Button2_Click(object sender, EventArgs e)
- {
- Button btn = (Button)sender;
- string sc_txt = TextBox1.Text;
- int space = sc_txt.IndexOf(' ');
- if(TextBox1.Text == "") //屏幕中还没有输入其他数字就直接按小数点
- {
- TextBox1.Text = "0.";
- }
- else
- {
-
- string str_last = sc_txt.Substring(sc_txt.Length - 1, 1);
- if (str_last == " ") //判断小数点是否为该操作数的起始点
- {
- TextBox1.Text += "0.";
- }
- else
- {
- /*判断第二个数是否已有小数点,避免因为第一个数值有小数点而误判*/
- if (space < 0) //若space>0,说明已输入操作符
- {
- if (sc_txt.Contains(".")) // sc_txt.Contains(".")用于判断是否有指定字符
- {
- TextBox1.Text = TextBox1.Text;
- }
- else
- {
- TextBox1.Text += btn.Text;
- }
- }
- else
- {
- //string c1 = s_txt.Substring(0, space);//获取字符串s_txt中的从0开始的n个字符,其中space为个数
- //char operation = Convert.ToChar(s_txt.Substring((space + 1), 1));
- string s2 = sc_txt.Substring(space + 3);
- if (s2.Contains(".")) // sc_txt.Contains(".")用于判断是否有指定字符
- {
- TextBox1.Text = TextBox1.Text;
- }
- else
- {
- TextBox1.Text += btn.Text;
- }
- }
-
- }
- }
-
- }
- /*实现加减乘除运算符*/
- protected void Button3_Click(object sender, EventArgs e)
- {
- Button btn = (Button)sender;
- //要求先输入数字后,才能输入运算符
- if (TextBox1.Text == "")
- {
- TextBox1.Text = "";
- }
- else
- {
- TextBox1.Text = TextBox1.Text + " " + btn.Text + " ";
- }
-
- }
- /*实现退格按钮,点击一次退格,清除最右边的一个字符*/
- protected void Button6_Click(object sender, EventArgs e)
- {
- string s = TextBox1.Text;
- string ss;
- string s_last= s.Substring(s.Length - 1,1);
- if(s_last==" ")
- {
- ss = s.Substring(0, s.Length - 3);
- }
- else
- {
- ss = s.Substring(0, s.Length - 1);
- }
-
- TextBox1.Text = ss;
- }
- //全部清空键
- protected void Button5_Click(object sender, EventArgs e)
- {
-
- TextBox1.Text = "";
- }
- protected void Button16_Click(object sender, EventArgs e)
- {
- string str1=TextBox2.Text;
- }
-
- }
- }
复制代码
该网页计算器的功能比较简单,目前只能两个操作数的加减乘除。 来源:https://blog.csdn.net/Relief_1619/article/details/105870968 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |