这个计算器我已经搞了一个多星期了,虽然基本功能已经实现,但各种各样的bug层出不穷,之前跟学长说已经完成了60%,真是太年轻,原来bug的修复才是最难搞的,完善了这一个,另一个又随之出现,无奈只能不断地打补丁,现在代码已经800多行了.................说实话看着挺有成就感的,下一步要开始精简代码了,简练才是王道
附上我的界面学长说他丑,真没审美眼光,多好看。
附上又难看又冗长的代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { double a, b; bool Clear = false; bool Add = false; bool Continue =false; bool Block = false; string i = ""; bool Judge1= true; bool Judge2 = true; bool BlockBackspace = false; bool ClearRecPrecent = false; bool ClearResult = false; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { BtnNum1.Font = new Font("楷体_GB2312", 18, FontStyle.Bold); BtnNum2.Font = new Font("楷体_GB2312", 18, FontStyle.Bold); BtnNum3.Font = new Font("楷体_GB2312", 18, FontStyle.Bold); BtnNum4.Font = new Font("楷体_GB2312", 18, FontStyle.Bold); BtnNum5.Font = new Font("楷体_GB2312", 18, FontStyle.Bold); BtnNum6.Font = new Font("楷体_GB2312", 18, FontStyle.Bold); BtnNum7.Font = new Font("楷体_GB2312", 18, FontStyle.Bold); BtnNum8.Font = new Font("楷体_GB2312", 18, FontStyle.Bold); BtnNum9.Font = new Font("楷体_GB2312", 18, FontStyle.Bold); BtnZero.Font = new Font("楷体_GB2312", 18, FontStyle.Bold); BtnAdd.Font = new Font("楷体_GB2312", 18, FontStyle.Bold); BtnSub.Font = new Font("楷体_GB2312", 18, FontStyle.Bold); BtnDiv.Font = new Font("楷体_GB2312", 18, FontStyle.Bold); BtnMul.Font = new Font("楷体_GB2312", 18, FontStyle.Bold); BtnResult.Font = new Font("楷体_GB2312", 18, FontStyle.Bold); BtnPoint.Font = new Font("楷体_GB2312", 18, FontStyle.Bold); BtnPrecent.Font = new Font("楷体_GB2312", 18, FontStyle.Bold); BtnBackspace.Font = new Font("楷体_GB2312", 18, FontStyle.Bold); BtnRec.Font = new Font("楷体_GB2312", 18, FontStyle.Bold); BtnSign.Font = new Font("楷体_GB2312", 18, FontStyle.Bold); BtnSqrt.Font = new Font("楷体_GB2312", 18, FontStyle.Bold); BtnBackspace.Font = new Font("楷体_GB2312", 18, FontStyle.Bold); textBox1.Font = new Font("仿宋", 20, FontStyle.Bold); BtnClearEnter.Font = new Font("楷体_GB2312", 18, FontStyle.Bold); BtnClear.Font = new Font("楷体_GB2312", 18, FontStyle.Bold); } private void textBox1_TextChanged(object sender, EventArgs e) { } private void BtnNum7_Click(object sender, EventArgs e) { if (Block == false) { if (Clear == true) textBox1.Text = ""; if (ClearResult == true) textBox1.Text = ""; if (ClearRecPrecent == true) { textBox1.Text = ""; ClearRecPrecent = false; } if (textBox1.Text == "0") textBox1.Text = ""; if (Continue == true) { Clear = Add = Continue = false; } textBox1.Text += "7"; a = double.Parse(textBox1.Text); Clear = false; if (Judge1 == false && Judge2 == true) { i = ""; Judge1 = true; } } } private void BtnNum8_Click(object sender, EventArgs e) { if (Block == false) { if (Clear == true) textBox1.Text = ""; if (ClearResult == true) textBox1.Text = ""; if (ClearRecPrecent == true) { textBox1.Text = ""; ClearRecPrecent = false; } if (textBox1.Text == "0") textBox1.Text = ""; if (Continue == true) { Clear = Add = Continue = false; } textBox1.Text += "8"; a = double.Parse(textBox1.Text); Clear = false; if (Judge1 == false && Judge2 == true) { i = ""; Judge1 = true; } } } private void BtnNum9_Click(object sender, EventArgs e) { if (Block == false) { if (Clear == true) textBox1.Text = ""; if (ClearResult == true) textBox1.Text = ""; if (ClearRecPrecent == true) { textBox1.Text = ""; ClearRecPrecent = false; } if (textBox1.Text == "0") textBox1.Text = ""; if (Continue == true) { Clear = Add = Continue = false; } textBox1.Text += "9"; a = double.Parse(textBox1.Text); Clear = false; if (Judge1 == false && Judge2 == true) { i = ""; Judge1 = true; } } } private void BtnNum4_Click(object sender, EventArgs e) { if (Block == false) { if (Clear == true) textBox1.Text = ""; if (ClearResult == true) textBox1.Text = ""; if (ClearRecPrecent == true) { textBox1.Text = ""; ClearRecPrecent = false; } if (textBox1.Text == "0") textBox1.Text = ""; if (Continue == true) { Clear = Add = Continue = false; } textBox1.Text += "4"; a = double.Parse(textBox1.Text); Clear = false; if (Judge1 == false && Judge2 == true) { i = ""; Judge1 = true; } } } private void BtnNum5_Click(object sender, EventArgs e) { if (Block == false) { if (Clear == true) textBox1.Text = ""; if (ClearResult == true) textBox1.Text = ""; if (ClearRecPrecent == true) { textBox1.Text = ""; ClearRecPrecent = false; } if (textBox1.Text == "0") textBox1.Text = ""; if (Continue == true) { Clear = Add = Continue = false; } textBox1.Text += "5"; a = double.Parse(textBox1.Text); Clear = false; if (Judge1 == false && Judge2 == true) { i = ""; Judge1 = true; } } } private void BtnNum6_Click(object sender, EventArgs e) { //e.x==BtnNum6.x if (Block == false) { if (Clear == true) textBox1.Text = ""; if (ClearResult == true) textBox1.Text = ""; if (ClearRecPrecent == true) { textBox1.Text = ""; ClearRecPrecent = false; } if (textBox1.Text == "0") textBox1.Text = ""; if (Continue == true) { Clear = Add = Continue = false; } textBox1.Text += "6"; a = double.Parse(textBox1.Text); Clear = false; if (Judge1 == false && Judge2 == true) { i = ""; Judge1 = true; } } } private void BtnNum1_Click(object sender, EventArgs e) { if (Block == false) { if (Clear == true) textBox1.Text = ""; if (ClearResult == true) textBox1.Text = ""; if (ClearRecPrecent == true) { textBox1.Text = ""; ClearRecPrecent = false; } if (textBox1.Text == "0") textBox1.Text = ""; if (Continue == true) { Clear = Add = Continue = false; } textBox1.Text += "1"; a = double.Parse(textBox1.Text); Clear = false; if (Judge1 == false && Judge2 == true) { i = ""; Judge1 = true; } } } private void BtnNum2_Click(object sender, EventArgs e) { if (Block == false) { if (Clear == true) textBox1.Text = ""; if (ClearResult == true) textBox1.Text = ""; if (ClearRecPrecent == true) { textBox1.Text = ""; ClearRecPrecent = false; } if (textBox1.Text == "0") textBox1.Text = ""; if (Continue == true) { Clear = Add = Continue = false; } textBox1.Text += "2"; a = double.Parse(textBox1.Text); if (Judge1 == false && Judge2 == true) { i = ""; Judge1 = true; } } } private void BtnNum3_Click(object sender, EventArgs e) { if (Block == false) { if (Clear == true) textBox1.Text = ""; if (ClearResult == true) textBox1.Text = ""; if (ClearRecPrecent == true) { textBox1.Text = ""; ClearRecPrecent = false; } if (textBox1.Text == "0") textBox1.Text = ""; if (Continue == true) { Clear = Add = Continue = false; } textBox1.Text += "3"; a = double.Parse(textBox1.Text); Clear = false; if (Judge1 == false && Judge2 == true) { i = ""; Judge1 = true; } } } private void BtnZero_Click(object sender, EventArgs e) { if (Block == false) { if (Clear == true) textBox1.Text = ""; if (ClearResult == true) textBox1.Text = ""; if (ClearRecPrecent == true) { textBox1.Text = ""; ClearRecPrecent = false; } if (textBox1.Text == "0") textBox1.Text = ""; if (Continue == true) { Clear = Add = Continue = false; } textBox1.Text += "0"; a = double.Parse(textBox1.Text); Clear = false; if (Judge1 == false && Judge2 == true) { i = ""; Judge1 = true; } } } private void BtnAdd_Click(object sender, EventArgs e) { if (Block == false) { { if (Clear == false && i == "") { i = "+"; b = a; Clear = true; } else if (Clear == false && i != "") { switch (i) { case "+": textBox1.Text = Convert.ToString(b + a); Clear = true; break; case "-": textBox1.Text = Convert.ToString(b - a); Clear = true; break; case "*": textBox1.Text = Convert.ToString(b * a); Clear = true; break; case "/": if (a == 0) { textBox1.Text = Convert.ToString("想除以0?作死-。-"); Clear = true; Block = true; } else { textBox1.Text = Convert.ToString(b / a); Clear = true; } break; } i = "+"; b = Convert.ToDouble(textBox1.Text); Clear = true; } else if (Clear == true && i != "") { i = "+"; } } if (Judge1 == false) Judge2 = false; } } private void BtnSub_Click(object sender, EventArgs e) { if (Block == false) { { if (Clear == false && i == "") { i = "-"; b = a; Clear = true; } else if (Clear == false && i != "") { switch (i) { case "+": textBox1.Text = Convert.ToString(b + a); Clear = true; break; case "-": textBox1.Text = Convert.ToString(b - a); Clear = true; break; case "*": textBox1.Text = Convert.ToString(b * a); Clear = true; break; case "/": if (a == 0) { textBox1.Text = Convert.ToString("想除以0?作死-。-"); Clear = true; Block = true; } else { textBox1.Text = Convert.ToString(b / a); Clear = true; } break; } i = "-"; b = Convert.ToDouble(textBox1.Text); Clear = true; } else if (Clear == true && i != "") { i = "-"; } if (Judge1 == false) Judge2 = false; } } } private void BtnMul_Click(object sender, EventArgs e) { if (Block == false) { { if (Clear == false && i == "") { i = "*"; b = a; Clear = true; } else if (Clear == false && i != "") { switch (i) { case "+": textBox1.Text = Convert.ToString(b + a); Clear = true; break; case "-": textBox1.Text = Convert.ToString(b - a); Clear = true; break; case "*": textBox1.Text = Convert.ToString(b * a); Clear = true; break; case "/": if (a == 0) { textBox1.Text = Convert.ToString("想除以0?作死-。-"); Clear = true; Block = true; } else { textBox1.Text = Convert.ToString(b / a); Clear = true; } break; } i = "*"; b = Convert.ToDouble(textBox1.Text); Clear = true; } else if (Clear ==true && i != "") { i = "*"; } if (Judge1 == false) Judge2 = false; } } } private void BtnDiv_Click(object sender, EventArgs e) { if (Block == false) { { if (Clear == false && i == "") { i = "/"; b = a; Clear = true; } else if (Clear == false && i != "") { switch (i) { case "+": textBox1.Text = Convert.ToString(b + a); Clear = true; break; case "-": textBox1.Text = Convert.ToString(b - a); Clear = true; break; case "*": textBox1.Text = Convert.ToString(b * a); Clear = true; break; case "/": if (a == 0) { textBox1.Text = Convert.ToString("想除以0?作死-。-"); Clear = true; Block = true; } else { textBox1.Text = Convert.ToString(b / a); Clear = true; } break; } i = "/"; b = Convert.ToDouble(textBox1.Text); Clear = true; } else if (Clear ==true && i != "") { i = "/"; } if (Judge1 == false) Judge2 = false; } } } private void BtnSqrt_Click(object sender, EventArgs e) { if (Block == false&&textBox1.Text!="-") { a = Convert.ToDouble(textBox1.Text); if (a < 0) { textBox1.Text = "无效输入"; MessageBox.Show("负数开个鸟平方啊,搞笑!"); Block = true; } else { textBox1.Text = Convert.ToString(Math.Sqrt(a)); a= double.Parse(textBox1.Text); Clear =false; } ClearRecPrecent = true; if (Judge1 == false && Judge2 == true) { i = ""; Judge1 = true; } } } private void BtnResult_Click(object sender, EventArgs e) { if (Block == false) { if (Clear == false && Add == false && i != "") { switch (i) { case "+": textBox1.Text = Convert.ToString(a + b); Clear = true; Add = true; b = Convert.ToDouble(textBox1.Text); break; case "-": textBox1.Text = Convert.ToString(b - a); Clear = true; Add = true; b = Convert.ToDouble(textBox1.Text); break; case "*": textBox1.Text = Convert.ToString(b * a); Clear = true; Add = true; b = Convert.ToDouble(textBox1.Text); break; case "/": if (a == 0) { textBox1.Text = Convert.ToString("想除以0?作死-。-"); Block = true; } else { textBox1.Text = Convert.ToString(b / a); Clear = true; Add = true; b = Convert.ToDouble(textBox1.Text); } break; } Continue = true; } else if (Clear == true && Add == true) { switch (i) { case "+": textBox1.Text = Convert.ToString(b + a); break; case "-": textBox1.Text = Convert.ToString(b - a); break; case "*": textBox1.Text = Convert.ToString(b * a); break; case "/": if (a == 0) { textBox1.Text = Convert.ToString("想除以0?作死-。-"); Block = true; } else textBox1.Text = Convert.ToString(b / a); break; } b = Convert.ToDouble(textBox1.Text); Continue = true; } else if (Clear == true && Add == false && i != "") { switch (i) { case "+": textBox1.Text = Convert.ToString(Convert.ToDouble(textBox1.Text) + b); break; case "-": textBox1.Text = Convert.ToString(Convert.ToDouble(textBox1.Text) - b); break; case "*": textBox1.Text = Convert.ToString(Convert.ToDouble(textBox1.Text) * b); break; case "/": if (b == 0) { textBox1.Text = Convert.ToString("想除以0?作死-。-"); Block = true; } else textBox1.Text = Convert.ToString(Convert.ToDouble(textBox1.Text) / b); break; } b = Convert.ToDouble(textBox1.Text); Continue = true; } else if (i == "") { textBox1.Text = textBox1.Text; ClearResult = true; a = double.Parse(textBox1.Text); } Judge1 = false; Judge2 = true; } } private void BtnClear_Click(object sender, EventArgs e) { textBox1.Text = "0"; i = ""; a = b = 0; Clear = false; Add = false; Continue = false; Block = false; Judge2 = true; Judge1 = true; } private void BtnRec_Click(object sender, EventArgs e) { if (Block == false&&textBox1.Text!="-") { if (textBox1.Text == "0") { textBox1.Text = "Error"; Block = true; MessageBox.Show("想除以0?作死-。-"); } else { a = double.Parse(textBox1.Text); textBox1.Text = Convert.ToString(1 / a); a = double.Parse(textBox1.Text); Clear = false; } if (Judge1 == false && Judge2 == true) { i = ""; Judge1 = true; } BlockBackspace = true; ClearRecPrecent = true; } } private void BtnSign_Click(object sender, EventArgs e) { if (Block == false&&textBox1.Text!="0") { if (Clear == true) { textBox1.Text = ""; Clear = false; } if (textBox1.Text == "0") textBox1.Text = ""; if (textBox1.Text.Contains("-")) textBox1.Text = textBox1.Text.Replace("-", ""); else textBox1.Text = "-" + textBox1.Text; if(textBox1.Text.Length>1) a = double.Parse(textBox1.Text); } } private void BtnBackspace_Click(object sender, EventArgs e) { if (Block == false&&BlockBackspace==true) { if (textBox1.Text.Length > 1) textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1); else textBox1.Text = "0"; a = double.Parse(textBox1.Text); } } private void BtnPrecent_Click(object sender, EventArgs e) { if (Block == false) { if (textBox1.Text != "0"&&textBox1.Text!="-") { double k = double.Parse(textBox1.Text); textBox1.Text = (k * 0.01).ToString(); a = double.Parse(textBox1.Text); } BlockBackspace = true; ClearRecPrecent = true; } } private void ClearEnter_Click(object sender, EventArgs e) { textBox1.Text = "0"; Block = false; a = double.Parse(textBox1.Text); } private void BtnPoint_Click(object sender, EventArgs e) { if (Block == false) { if (Clear == false && !textBox1.Text.Contains(".")) textBox1.Text = textBox1.Text + "."; if (Clear == true) { textBox1.Text = "0."; Clear = false; } } } private void BtnNumber_Click(object sender, EventArgs e) { Button a = (Button)sender; textBox1.Text =textBox1.Text+ a.Text; } } }