主要时间浪费在
//执行计算
object result = new DataTable().Compute(s, "");
这条语句上。如果不加处理,特别大的整数相乘,会提示值 太大。只好将整数加个0变为小数;
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Windows.Forms; namespace JcCalc { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btn0_Click(object sender, EventArgs e) { rtbResult.Text += "0"; } private void btn1_Click(object sender, EventArgs e) { rtbResult.Text += "1"; } private void btn2_Click(object sender, EventArgs e) { rtbResult.Text += "2"; } private void btn3_Click(object sender, EventArgs e) { rtbResult.Text += "3"; } private void btn4_Click(object sender, EventArgs e) { rtbResult.Text += "4"; } private void btn5_Click(object sender, EventArgs e) { rtbResult.Text += "5"; } private void btn6_Click(object sender, EventArgs e) { rtbResult.Text += "6"; } private void btn7_Click(object sender, EventArgs e) { rtbResult.Text += "7"; } private void btn8_Click(object sender, EventArgs e) { rtbResult.Text += "8"; } private void btn9_Click(object sender, EventArgs e) { rtbResult.Text += "9"; } //加法 private void btnAdd_Click(object sender, EventArgs e) { lbResult.Text = ""; string s = rtbResult.Text; //判断输入框是否为空 if (s.Length > 0) { s = s.Substring(s.Length - 1); //如果最后一个是运算符,则返回不执行操作; if ("+-*/.".Contains(s)) return; rtbResult.Text += "+"; } } //减法 private void btnSub_Click(object sender, EventArgs e) { lbResult.Text = ""; string s = rtbResult.Text; //判断输入框是否为空 if (s.Length > 0) { s = s.Substring(s.Length - 1); //如果最后一个是运算符,则返回不执行操作; if ("+-*/.".Contains(s)) return; rtbResult.Text += "-"; } } //乘法 private void btnMul_Click(object sender, EventArgs e) { lbResult.Text = ""; string s = rtbResult.Text; //判断输入框是否为空 if (s.Length > 0) { s = s.Substring(s.Length - 1); //如果最后一个是运算符,则返回不执行操作; if ("+-*/.".Contains(s)) return; rtbResult.Text += "*"; } } //除法 private void btnDiv_Click(object sender, EventArgs e) { lbResult.Text = ""; string s = rtbResult.Text; //判断输入框是否为空 if (s.Length > 0) { s = s.Substring(s.Length - 1); //如果最后一个是运算符,则返回不执行操作; if ("+-*/.".Contains(s)) return; rtbResult.Text += "/"; } } private void btnClear_Click(object sender, EventArgs e) { rtbResult.Text = ""; lbResult.Text = ""; } private void btnDel_Click(object sender, EventArgs e) { string s = rtbResult.Text; //如果S长度大于0,则删除最后一位; if (s.Length > 0) s = s.Substring(0, s.Length - 1); rtbResult.Text = s; } private void btnPoint_Click(object sender, EventArgs e) { string s = rtbResult.Text; //判断输入框是否为空 if (s.Length > 0) { s = s.Substring(s.Length - 1); //如果最后一个是运算符,则返回不执行操作; if ("+-*/.".Contains(s)) return; rtbResult.Text += "."; } } private void button19_Click(object sender, EventArgs e) { float dResult = 0; string s = rtbResult.Text; //判断输入框是否为空 try { if (s.Length > 0) { //如果没有运算符 if (s.IndexOf("+") < 0 && s.IndexOf("-") < 0 && s.IndexOf("*") < 0 && s.IndexOf("/") < 0) { return; } //这是执行“object result = new DataTable().Compute(s, "");” //遇到的一个问题 //如果有特别大的整数相乘,会出现错误,需要将其转换为带小数位的数 if (s.IndexOf("*") > 0) { string tmpOperate = ""; string sTemp = ""; string sMul = s.Substring(0, s.IndexOf("*")); //如果没有小数,直接处理后就结束 int tt = sMul.IndexOf("."); if (sMul.IndexOf(".") <= 0) { sTemp = s.Substring(0, s.IndexOf("*")); sTemp += ".0"; sTemp += s.Substring(s.IndexOf("*")); s = sTemp; } else { for (int i = sMul.Length; i > 0; i--) { tmpOperate = sMul.Substring(i-1, 1); //如果已经是小数了就结束 if (tmpOperate == ".") return; //如果不是小数,就在整数后面加上 .0使其变为小数,防止计算整数出错; if (tmpOperate == "+" || tmpOperate == "-" || tmpOperate == "*" || tmpOperate == "/") { sTemp = s.Substring(0, s.IndexOf("*")); sTemp += ".0"; sTemp += s.Substring(s.IndexOf("*")); s = sTemp; break; } } } } //执行计算 object result = new DataTable().Compute(s, ""); string s2 = result.ToString(); if (s2 == "∞") { MessageBox.Show("除数不能为0"); return; } lbResult.Text = s2.ToString(); } } catch (Exception E) { MessageBox.Show("我是3年级的小屁孩,数字多了我算不出来!\r\n提示:" + E.Message, "有点小尴尬"); return; } } /// <summary> /// 最简单的方式由SQL计算 /// </summary> /// <param name="expression">表达式</param> /// <returns></returns> private void btnHelp_Click(object sender, EventArgs e) { MessageBox.Show("谢谢你长得辣么美,还在点击我!", "三克油"); } } }
标签:sender,c#,Text,void,object,rtbResult,源码,EventArgs,2.0 From: https://www.cnblogs.com/lrzy/p/17318291.html