题目:
设计一个简单计算器
思路:
首先在应用框体中设计自己喜欢的计算器格式,接着编辑其中的函数。抽取一个Call函数用来显示从键盘输入的数字,cleanall()函数进行清屏操作,mode()函数进行四则运算,switchmode()函数显示符号,最后将结果输出即可。
示例代码:
using System; using System.Windows.Forms; namespace Calculator { public partial class FormMain : Form { double number1 = 0,number2 = 0,result; int inputnumber; enum Operator { none,plus,minus,multiplication,division} Operator mode = Operator.none; bool isequal = false; public FormMain() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { inputnumber =4; call(inputnumber); } private void button4_Click(object sender, EventArgs e) { inputnumber = 1; call(inputnumber); } private void button16_Click(object sender, EventArgs e) { inputnumber = 9; call(inputnumber); } private void button2_Click(object sender, EventArgs e) { inputnumber = 5; call(inputnumber); } private void button3_Click(object sender, EventArgs e) { inputnumber = 6; call(inputnumber); } private void button5_Click(object sender, EventArgs e) { inputnumber =2; call(inputnumber); } private void button6_Click(object sender, EventArgs e) { inputnumber =3; call(inputnumber); } private void button7_Click(object sender, EventArgs e) { inputnumber = 0; call(inputnumber); } private void button12_Click(object sender, EventArgs e) { mode = Operator.plus; switchmode(); } private void button11_Click(object sender, EventArgs e) { mode = Operator.minus; switchmode(); } private void button10_Click(object sender, EventArgs e) { mode = Operator.multiplication; switchmode(); } private void button9_Click(object sender, EventArgs e) { mode = Operator.division; switchmode(); } private void button8_Click(object sender, EventArgs e) { switch (mode) { case Operator.plus: result = number1 + number2; break; case Operator.minus: result = number1 - number2; break; case Operator.multiplication: result = number1 * number2; break; case Operator.division: result = number1 / number2; break; } number1 = 0; number2 = 0; isequal = true; labelbefore.Text = " "; labelmode.Text = " "; labelout.Text = Convert.ToString(result); } private void button14_Click(object sender, EventArgs e) { inputnumber = 8; call(inputnumber); } private void button13_Click(object sender, EventArgs e) { inputnumber = 7; call(inputnumber); } private void button15_Click(object sender, EventArgs e) { cleanall(); } public void call(int an) { if (mode == Operator.none) { number1 = number1 * 10 + an; labelout.Text = Convert.ToString(number1); } else { number2 = number2 * 10 + an; labelout.Text = Convert.ToString(number2); } } private void labelout_Click(object sender, EventArgs e) { labelout.Text = Convert.ToString(number1); } public void cleanall() { number1 = 0; number2 = 0; labelout.Text = Convert.ToString(number1); labelbefore.Text = " "; labelmode.Text = " "; isequal = false; mode = Operator.none; } public void switchmode() { switch (mode) { case Operator.plus: labelmode.Text = "+"; break; case Operator.minus: labelmode.Text = "-"; break; case Operator.multiplication: labelmode.Text = "x"; break; case Operator.division: labelmode.Text = "/"; break; } if(isequal == true) { number1 = result; } labelbefore.Text = Convert.ToString(number1); labelout.Text = Convert.ToString(number2); } } }
测试: