c#简易计算器
c#是老师讲的课,这个老师技术可能很好,但是教的一般般,很多东西都是自己查的,尤其是面向对象那块,一团乱麻。 算上上课总共做了大约9个小时,很多地方都没有优化,不过还是能运行的。\[code lang="csharp"\] using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace text { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public string cout(string a, int c) //获得输入 { //int b = a.ToString().Length; int f; int.TryParse(a, out f); //a当前文本框内容,f内容的int类型 string d = (f * 10 + c).ToString(); //c加上的数 return d; } public void clear() //清屏 { int temp1 = 0; string temp2 = Convert.ToString(temp1); textBox1.Text = temp2; } public void clearall() //清零 { int temp1 = 0; string temp2 = Convert.ToString(temp1); textBox1.Text = temp2; tempdata = temp1; tempdata1 = temp1; } public void clearcache() //清缓存 { int temp1 = 0; tempdata = temp1; tempdata1 = temp1; } public static string tempfunction; //运算符号 public static double tempdata = 0; //存放临时变量 public static double tempdata1 = 0; //存放临时变量 public void datacache(string str) { switch (str) { case "+": tempdata1 = tempdata; double.TryParse(textBox1.Text, out tempdata); tempdata = tempdata1 + tempdata; break; case "-": tempdata1 = tempdata; double.TryParse(textBox1.Text, out tempdata); tempdata = tempdata1 - tempdata; break; case "*": tempdata1 = tempdata; double.TryParse(textBox1.Text, out tempdata); tempdata = tempdata1 * tempdata; break; case "÷": tempdata1 = tempdata; double.TryParse(textBox1.Text, out tempdata); tempdata = tempdata1 / tempdata; break; } clear(); } private void textBox1\_TextChanged(object sender, EventArgs e) { } private void button14\_Click(object sender, EventArgs e) { string temp = textBox1.Text; textBox1.Text = cout(temp, 0); } private void button9\_Click(object sender, EventArgs e) { string temp = textBox1.Text; textBox1.Text = cout(temp, 1); } private void button10\_Click(object sender, EventArgs e) { string temp = textBox1.Text; textBox1.Text = cout(temp, 2); } private void button11\_Click(object sender, EventArgs e) { string temp = textBox1.Text; textBox1.Text = cout(temp, 3); } private void button5\_Click(object sender, EventArgs e) { string temp = textBox1.Text; textBox1.Text = cout(temp, 4); } private void button6\_Click(object sender, EventArgs e) { string temp = textBox1.Text; textBox1.Text = cout(temp, 5); } private void button7\_Click(object sender, EventArgs e) { string temp = textBox1.Text; textBox1.Text = cout(temp, 6); } private void button1\_Click(object sender, EventArgs e) { string temp = textBox1.Text; textBox1.Text = cout(temp, 7); } private void button2\_Click(object sender, EventArgs e) { string temp = textBox1.Text; textBox1.Text = cout(temp, 8); } private void button3\_Click(object sender, EventArgs e) { string temp = textBox1.Text; textBox1.Text = cout(temp, 9); } private void button4\_Click(object sender, EventArgs e) //清空操作 { clearall(); } private void button15\_Click(object sender, EventArgs e) //加 { if (tempdata == 0) { double.TryParse(textBox1.Text, out tempdata); clear(); } else { datacache(tempfunction); tempdata1 = tempdata; double.TryParse(textBox1.Text, out tempdata); tempdata = tempdata + tempdata1; clear(); } tempfunction = "+"; } private void button13\_Click(object sender, EventArgs e) //等 { switch (tempfunction) { case "+": double.TryParse(textBox1.Text, out tempdata1); textBox1.Text = (tempdata1 + tempdata).ToString(); clearcache(); break; case "-": double.TryParse(textBox1.Text, out tempdata1); textBox1.Text = (tempdata - tempdata1).ToString(); clearcache(); break; case "*": double.TryParse(textBox1.Text, out tempdata1); textBox1.Text = (tempdata1 * tempdata).ToString(); clearcache(); break; case "÷": double.TryParse(textBox1.Text, out tempdata1); textBox1.Text = (tempdata / tempdata1).ToString(); clearcache(); break; } } private void button16\_Click(object sender, EventArgs e) //减 { if (tempdata == 0) { double.TryParse(textBox1.Text, out tempdata); clear(); } else { datacache(tempfunction); tempdata1 = tempdata; double.TryParse(textBox1.Text, out tempdata); tempdata = tempdata + tempdata1; clear(); } tempfunction = "-"; } private void button12\_Click(object sender, EventArgs e) //乘 { if (tempdata == 0) { double.TryParse(textBox1.Text, out tempdata); clear(); } else { datacache(tempfunction); tempdata1 = tempdata; double.TryParse(textBox1.Text, out tempdata); tempdata = tempdata1 * tempdata; clear(); } tempfunction = "*"; } private void button8_Click(object sender, EventArgs e) //除 { if (tempdata == 0) { double.TryParse(textBox1.Text, out tempdata); clear(); } else { datacache(tempfunction); tempdata1 = tempdata; double.TryParse(textBox1.Text, out tempdata); tempdata = tempdata1 / tempdata; clear(); } tempfunction = "÷"; } } } \[/code\]