WindowsForm 计算器
计算器
可以分一下类
数字键
+ - * / %
=和撤销
归零C最简单
下面看一下计算器具体的代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication4 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } //记录是否刚点过运算符 private bool isok = true; //记录结果 private decimal Sum; //记录上一次的运算符 private string PreYunsuanfu; //记录表达式 private string Biaodashi; private void btnling_Click(object sender, EventArgs e) { if (isok) { txtshuru.Text = "0"; // isok = false; } else { txtshuru.Text += "0"; } } private void btnyi_Click(object sender, EventArgs e) { if (isok) { txtshuru.Text = "1"; isok = false; } else { txtshuru.Text += "1"; } } private void btner_Click(object sender, EventArgs e) { if (isok) { txtshuru.Text = "2"; isok = false; } else { txtshuru.Text += "2"; } } private void btnsan_Click(object sender, EventArgs e) { if (isok) { txtshuru.Text = "3"; isok = false; } else { txtshuru.Text += "3"; } } private void btnsi_Click(object sender, EventArgs e) { if (isok) { txtshuru.Text = "4"; isok = false; } else { txtshuru.Text += "4"; } } private void btnwu_Click(object sender, EventArgs e) { if (isok) { txtshuru.Text = "5"; isok = false; } else { txtshuru.Text += "5"; } } private void btnliu_Click(object sender, EventArgs e) { if (isok) { txtshuru.Text = "6"; isok = false; } else { txtshuru.Text += "6"; } } private void btnqi_Click(object sender, EventArgs e) { if (isok) { txtshuru.Text = "7"; isok = false; } else { txtshuru.Text += "7"; } } private void btnba_Click(object sender, EventArgs e) { if (isok) { txtshuru.Text = "8"; isok = false; } else { txtshuru.Text += "8"; } } private void btnjiu_Click(object sender, EventArgs e) { if (isok) { txtshuru.Text = "9"; isok = false; } else { txtshuru.Text += "9"; } } private void btnjia_Click(object sender, EventArgs e) { if (PreYunsuanfu == null) { Sum = decimal.Parse(txtshuru.Text); Biaodashi = txtshuru.Text; } else { if (!isok) { if (PreYunsuanfu == "+") { Sum = Sum + decimal.Parse(txtshuru.Text); } if (PreYunsuanfu == "-") { Sum = Sum - decimal.Parse(txtshuru.Text); } if (PreYunsuanfu == "*") { Sum = Sum * decimal.Parse(txtshuru.Text); } if (PreYunsuanfu == "/") { Sum = Sum / decimal.Parse(txtshuru.Text); } if (PreYunsuanfu == "%") { Sum = Sum % decimal.Parse(txtshuru.Text); } Biaodashi = Biaodashi + PreYunsuanfu + txtshuru.Text; } } txtjisuanshi.Text = Biaodashi + "+"; PreYunsuanfu = "+"; isok = true; txtshuru.Text = Sum.ToString(); } private void btnjian_Click(object sender, EventArgs e) { if (PreYunsuanfu == null) { Sum = decimal.Parse(txtshuru.Text); Biaodashi = txtshuru.Text; } else { if (!isok) { if (PreYunsuanfu == "+") { Sum = Sum + decimal.Parse(txtshuru.Text); } if (PreYunsuanfu == "-") { Sum = Sum - decimal.Parse(txtshuru.Text); } if (PreYunsuanfu == "*") { Sum = Sum * decimal.Parse(txtshuru.Text); } if (PreYunsuanfu == "/") { Sum = Sum / decimal.Parse(txtshuru.Text); } if (PreYunsuanfu == "%") { Sum = Sum % decimal.Parse(txtshuru.Text); } Biaodashi = Biaodashi + PreYunsuanfu + txtshuru.Text; } } txtjisuanshi.Text = Biaodashi + "-"; PreYunsuanfu = "-"; isok = true; txtshuru.Text = Sum.ToString(); } private void btncheng_Click(object sender, EventArgs e) { if (PreYunsuanfu == null) { Sum = decimal.Parse(txtshuru.Text); Biaodashi = txtshuru.Text; } else { if (!isok) { if (PreYunsuanfu == "+") { Sum = Sum + decimal.Parse(txtshuru.Text); } if (PreYunsuanfu == "-") { Sum = Sum - decimal.Parse(txtshuru.Text); } if (PreYunsuanfu == "*") { Sum = Sum * decimal.Parse(txtshuru.Text); } if (PreYunsuanfu == "/") { Sum = Sum / decimal.Parse(txtshuru.Text); } if (PreYunsuanfu == "%") { Sum = Sum % decimal.Parse(txtshuru.Text); } Biaodashi = Biaodashi + PreYunsuanfu + txtshuru.Text; } } txtjisuanshi.Text = Biaodashi + "*"; PreYunsuanfu = "*"; isok = true; txtshuru.Text = Sum.ToString(); } private void btnchu_Click(object sender, EventArgs e) { if (PreYunsuanfu == null) { Sum = decimal.Parse(txtshuru.Text); Biaodashi = txtshuru.Text; } else { if (!isok) { if (PreYunsuanfu == "+") { Sum = Sum + decimal.Parse(txtshuru.Text); } if (PreYunsuanfu == "-") { Sum = Sum - decimal.Parse(txtshuru.Text); } if (PreYunsuanfu == "*") { Sum = Sum * decimal.Parse(txtshuru.Text); } if (PreYunsuanfu == "/") { Sum = Sum / decimal.Parse(txtshuru.Text); } if (PreYunsuanfu == "%") { Sum = Sum % decimal.Parse(txtshuru.Text); } Biaodashi = Biaodashi + PreYunsuanfu + txtshuru.Text; } } txtjisuanshi.Text = Biaodashi + "/"; PreYunsuanfu = "/"; isok = true; txtshuru.Text = Sum.ToString(); } private void btnmo_Click(object sender, EventArgs e) { if (PreYunsuanfu == null) { Sum = decimal.Parse(txtshuru.Text); Biaodashi = txtshuru.Text; } else { if (!isok) { if (PreYunsuanfu == "+") { Sum = Sum + decimal.Parse(txtshuru.Text); } if (PreYunsuanfu == "-") { Sum = Sum - decimal.Parse(txtshuru.Text); } if (PreYunsuanfu == "*") { Sum = Sum * decimal.Parse(txtshuru.Text); } if (PreYunsuanfu == "/") { Sum = Sum / decimal.Parse(txtshuru.Text); } if (PreYunsuanfu == "%") { Sum = Sum % decimal.Parse(txtshuru.Text); } Biaodashi = Biaodashi + PreYunsuanfu + txtshuru.Text; } } txtjisuanshi.Text = Biaodashi + "%"; PreYunsuanfu = "%"; isok = true; txtshuru.Text = Sum.ToString(); } private void btnClear_Click(object sender, EventArgs e) { txtshuru.Text = "0"; txtjisuanshi.Text = ""; isok = true; PreYunsuanfu = null; Sum = 0; Biaodashi = ""; } private void btnchexiao_Click(object sender, EventArgs e) { if (!isok) { if (txtshuru.Text.Length == 1) { txtshuru.Text = "0"; isok = true; } else { txtshuru.Text = txtshuru.Text.Substring(0, txtshuru.Text.Length - 1); } } } private void btndian_Click(object sender, EventArgs e) { if (txtshuru.Text == "0") { txtshuru.Text = "0."; } if (!isok && !txtshuru.Text.Contains(".")) { txtshuru.Text += "."; } isok = false; } private void btndeng_Click(object sender, EventArgs e) { txtjisuanshi.Text = ""; if (PreYunsuanfu == "+") { Sum = Sum + decimal.Parse(txtshuru.Text); } if (PreYunsuanfu == "-") { Sum = Sum - decimal.Parse(txtshuru.Text); } if (PreYunsuanfu == "*") { Sum = Sum * decimal.Parse(txtshuru.Text); } if (PreYunsuanfu == "/") { Sum = Sum / decimal.Parse(txtshuru.Text); } if (PreYunsuanfu == "%") { Sum = Sum % decimal.Parse(txtshuru.Text); } txtshuru.Text = Sum.ToString(); isok = true; Biaodashi = ""; Sum = 0; PreYunsuanfu = null; } private void btnzhengfu_Click(object sender, EventArgs e) { if (!isok) { if (decimal.Parse(txtshuru.Text) > 0) { txtshuru.Text = "-" + txtshuru.Text; } else { txtshuru.Text = txtshuru.Text.Substring(1); } } } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace FirstForm { public partial class JiSuanQi : Form { public JiSuanQi() { InitializeComponent(); } private double sum=0; private string biaodashi; private string Preyunsuanfu;//存放之前的运算符 private bool isok = true;//用来判断是否刚刚点过运算符 private void btnling_Click(object sender, EventArgs e) { Button btn = (Button)sender; if (isok) { txtshuru.Text = btn.Text; isok = false; } else { txtshuru.Text += btn.Text; } } private void btnjia_Click(object sender, EventArgs e) { Button btn = (Button)sender; string yunsuanfu = btn.Text; if (Preyunsuanfu == null) { biaodashi += txtshuru.Text; sum = double.Parse(txtshuru.Text); } else { if (!isok) { if (Preyunsuanfu == "+") { sum += double.Parse(txtshuru.Text); } if (Preyunsuanfu == "-") { sum -= double.Parse(txtshuru.Text); } if (Preyunsuanfu == "*") { sum *= double.Parse(txtshuru.Text); } if (Preyunsuanfu == "/") { sum /= double.Parse(txtshuru.Text); } if (Preyunsuanfu == "%") { sum %= double.Parse(txtshuru.Text); } biaodashi = biaodashi+ Preyunsuanfu + txtshuru.Text; txtshuru.Text = sum.ToString(); } } txtjisuanshi.Text = biaodashi + yunsuanfu; Preyunsuanfu = yunsuanfu; isok = true; } private void btndeng_Click(object sender, EventArgs e) { } private void btnchexiao_Click(object sender, EventArgs e) { } } }