闲着无聊做一个简单版计算器用C#winform实现(面向对象)
简单版计算器,四则运算功能。
具体怎么做出来的呢,嗯,我先说一说布局。显示框用两个label控件,label1显示操作运算,label2显示运算值。设置其AutoSize属性为false,就可以把label控件延伸扩大了。
操作运算符和数字都是button控件,外观设置就不说了,最基础的,小白不会的就去查资料。
然后现在就说一下代码
先添加一个操作运算基类Oper,两个属性表示操作数
public class Oper { private double _numberA = 0; private double _numberB = 0; public double NumberA { get { return _numberA; } set { _numberA = value; } } public double NumnberB { get { return _numberB; } set { _numberB = value; } } public virtual double GetResult() { double result = 0d; return result; } }
添加四个加减乘除类继承Oper类,重写父类方法。
//加法类 public class OperAdd : Oper { /// <summary> /// 重写父方法:GetResult /// </summary> /// <returns></returns> public override double GetResult() { double result = 0; result = NumberA + NumnberB; return result; } } //减法类 public class OperSub : Oper { public override double GetResult() { double result = 0; result = NumberA - NumnberB; return result; } } //乘法类 public class OperMul : Oper { public override double GetResult() { double result = 0; result = NumberA * NumnberB; return result; } } //除法类 public class OperationDiv : Oper { public override double GetResult() { double result = 0; if (NumnberB == 0) throw new Exception("除数不能为0."); result = NumberA * 1.0 / NumnberB; return result; } }
添加一个运算符枚举类,代表四则运算符号
/// <summary> /// 运算符枚举 /// </summary> public enum EnumOperator { None = 0, Add = 1, Minus = 2, Multiply = 3, Divide = 4 }
再添加个运算工厂类OperFactory作为中转站,输入运算符,工厂就能实例化出合适的对象,通过多态,返回父类的方式实现计算器的结果。
public class OperFactory { /// <summary> /// 只需输入运算符,工厂就能实例化出合适的对象,通过多态,返回父类的方式实现了计算器的结果 /// </summary> /// <param name="operate">运算符</param> /// <returns></returns> public static Oper createOpeate(EnumOperator op) { Oper oper = null; switch (op) { case EnumOperator.Add: oper = new OperationAdd(); break; case EnumOperator.Minus: oper = new OperationSub(); break; case EnumOperator.Multiply: oper = new OperationMul(); break; case EnumOperator.Divide: oper = new OperationDiv(); break; default: break; } return oper; } }
添加一个string类型的有参无返函数——NumberClick(string)
double currentValue = 0; //第1个数 double numSum = 0; //第2个数 private EnumOperator currentOperator; //当前操作符 /// <summary> /// 按数字的时候 /// </summary> /// <param name="strNumber"></param> private void NumberClick(string strNumber) { label1.Text = label1.Text + strNumber; currentValue = Convert.ToDouble(label1.Text); label2.Text += strNumber; }
然后在每个数字按钮的点击事件中调用函数,显示运算过程
private void button6_Click(object sender, EventArgs e) { NumberClick(button6.Text); } private void button7_Click(object sender, EventArgs e) { NumberClick(button7.Text); } private void button11_Click(object sender, EventArgs e) { NumberClick(button11.Text); } private void button9_Click(object sender, EventArgs e) { NumberClick(button9.Text); } private void button1_Click(object sender, EventArgs e) { NumberClick(button1.Text); } private void button2_Click(object sender, EventArgs e) { NumberClick(button2.Text); } private void button3_Click(object sender, EventArgs e) { NumberClick(button3.Text); } private void button4_Click(object sender, EventArgs e) { NumberClick(button4.Text); } private void button5_Click(object sender, EventArgs e) { NumberClick(button5.Text); } private void button8_Click(object sender, EventArgs e) { NumberClick(button8.Text); }
定义一个枚举运算符参数的函数OperatorClick(EnumOperator op),里面调用DisplayOperator(EnumOperator op)方法(见下下)
private void OperatorClick(EnumOperator op) { if (currentOperator != EnumOperator.None) { Evaluate();//计算 } else { double.TryParse(label1.Text, out numSum); } DisplayOperator(op); label1.Text = ""; currentOperator = op; }
四则运算符按钮的点击事件中调用代表运算符号的枚举
//+ private void button12_Click(object sender, EventArgs e) { OperatorClick(EnumOperator.Add); } //- private void button13_Click(object sender, EventArgs e) { OperatorClick(EnumOperator.Minus); } //X private void button14_Click(object sender, EventArgs e) { OperatorClick(EnumOperator.Multiply); } //÷ private void button15_Click(object sender, EventArgs e) { OperatorClick(EnumOperator.Divide); }
根据运算符号来执行四则运算显示的内容 函数 DisplayOperator(EnumOperator op)
private void DisplayOperator(EnumOperator op) { switch (op) { case EnumOperator.None: label2.Text = label1.Text; label2.Text += ""; break; case EnumOperator.Add: if (label1.Text != "") { label2.Text = label1.Text; } label2.Text += "+"; break; case EnumOperator.Minus: if (label1.Text != "") { label2.Text = label1.Text; } label2.Text += "-"; break; case EnumOperator.Multiply: label2.Text = label1.Text; label2.Text += "x"; break; case EnumOperator.Divide: label2.Text = label1.Text; label2.Text += "➗"; break; default: break; } }
根据运算符和两个数字的计算结果
调用简单工厂模式生成的类 Evaluate()
private void Evaluate() { Operation oper; //根据不同的对象生成不同的类,多态! switch (currentOperator) { case EnumOperator.None: break; case EnumOperator.Add: oper = OperationFactory.createOpeate(EnumOperator.Add); oper.NumberA = numSum; oper.NumnberB = currentValue; numSum = oper.GetResult(); break; case EnumOperator.Minus: oper = OperationFactory.createOpeate(EnumOperator.Minus); oper.NumberA = numSum; oper.NumnberB = currentValue; numSum = oper.GetResult(); break; case EnumOperator.Multiply: oper = OperationFactory.createOpeate(EnumOperator.Multiply); oper.NumberA = numSum; oper.NumnberB = currentValue; numSum = oper.GetResult(); break; case EnumOperator.Divide: if (currentValue != 0) { oper = OperationFactory.createOpeate(EnumOperator.Divide); oper.NumberA = numSum; oper.NumnberB = currentValue; numSum = oper.GetResult(); } else { MessageBox.Show("除数不能为0哦,亲~", "出错了~"); } break; } currentValue = 0; currentOperator = EnumOperator.None; }
在 “=” 按钮中添加点击事件,调用Evaluate()计算结果
private void button10_Click(object sender, EventArgs e) { Evaluate(); label1.Text = numSum.ToString(); //防止重复按 = if (label1.Text.Length > 1 && label1.Text.Substring(label1.Text.Length - 1) != "=") { label1.Text += ""; } }
在C按钮中添加点击事件,写入执行代码如下
private void button16_Click(object sender, EventArgs e) { currentOperator = EnumOperator.None; label1.Text = ""; label2.Text = ""; numSum = 0; }
ok,这就是简单版计算器的全部解析和代码量了。
源码分享链接 https://pan.baidu.com/s/1vspuwqVDKh4b6BpTxBR1Uw
有问题的可以参考源码,当然源码也只是实现了四则运算功能,但用户体验较差
面向对象编程实用性很广,大家可以多参考学习一下
推荐一个七夕福利——女朋友3D相册链接
https://www.cnblogs.com/wl192/p/11511200.html