C# 计算器程序
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.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 (!"+-*/.".Contains(s)) lbResult.Text = s; //s += "+0.1-0.1"; //执行计算 object result = 999999999999999999999999999999999999999999999999999.99; result = new DataTable().Compute(s, ""); string s2 = result.ToString(); if (s2 == "∞") { MessageBox.Show("除数不能为0"); return; } double db = 0.00; Double.TryParse(s2, out db); lbResult.Text = s2.ToString(); } } catch (Exception E) { MessageBox.Show("我是3年级的小屁孩,数字多了我算不出来!\r\n提示:"+E.Message); return; } } /// <summary> /// 最简单的方式由SQL计算 /// </summary> /// <param name="expression">表达式</param> /// <returns></returns> public static float CalcBySQL(string expression) { string SQL = "SELECT " + expression + " AS RESULT_VALUE"; SqlConnection conn = new SqlConnection("1=1"); SqlCommand cmd = new SqlCommand(SQL, conn); object o = cmd.ExecuteScalar(); //执行SQL. return float.Parse(o.ToString()); } private void btnHelp_Click(object sender, EventArgs e) { MessageBox.Show("谢谢你长得辣么美,还在点击我!", "三克油"); } } }
活到老,学到老。