写在前面

在2020年的寒假,我的一个计算机老师,给咱班布置了个"小作业",这其中一个就是,用面向对象的方式写一个简单的计算器(C#Winfrom)。以下为该计算器的源码。毕竟还在学习中,写的不好,请别介意。咕咕咕!

界面

这界面吧没什么好说的,就计算器的样子。
喏

那么在这里我主要写了这几个类

  1. Operation (操作运算基类)
  2. EnumOperator (运算符枚举)
  3. OperationAdd (加法类)
  4. OperationSub (减法类)
  5. OperationMul (乘法类)
  6. OperationDiv (除法类)
  7. OperationFactory (运算工厂类)

Operation(操作运算基类)

首先添加一个操作运算基类Operation,两个属性表示操作数

public class Operation
    {
        //两个操作数
        private double _numberA = 0;
        private double _numberB = 0;

        //定义操作数A
        public double NumberA
        {
            get { return _numberA; }
            set { _numberA = value; }
        }

        //定义操作数B
        public double NumnberB
        {
            get { return _numberB; }
            set { _numberB = value; }
        }

        public virtual double GetResult()
        {
            double result = 0d;
            return result;
        }
    }

加减乘除

添加四个加减乘除类继承Operation类,重写父类方法。

    /// <summary>
    /// 加法类,继承自Operation运算类基类
    /// </summary>
    public class OperationAdd : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA + NumnberB;
            return result;
        }
    }

    /// <summary>
    /// 减法类,继承自Operation运算类基类
    /// </summary>
    public class OperationSub : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA - NumnberB;
            return result;
        }
    }

    /// <summary>
    /// 乘法类,继承自运算类Operation基类
    /// </summary>
    public class OperationMul : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA * NumnberB;
            return result;
        }
    }

    /// <summary>
    /// 除法类,继承自元算类Operation基类
    /// </summary>
    public class OperationDiv : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            if (NumnberB == 0)
                throw new Exception("除数不能为0.");
            result = NumberA * 1.0 / NumnberB;
            return result;
        }
    }

运算符枚举类

接着添加一个运算符枚举类,代表四则运算符号

public enum EnumOperator
        {
            //空
            None = 0,

            //加
            Add = 1,

            //减
            Minus = 2,

            //乘
            Multiply = 3,

            //除
            Divide = 4
        }

运算工厂类

再添加个运算工厂类OperationFactory作为中转站,输入运算符,工厂就能实例化出合适的对象,通过多态,返回父类的方式实现计算器的结果

    /// <summary>
    /// 运算工厂类
    /// </summary>
    public class OperationFactory
    {
        /// <summary>
        /// 只需输入运算符,工厂就能实例化出合适的对象,通过多态,返回父类的方式实现了计算器的结果
        /// </summary>
        /// <param name="operate">运算符</param>
        /// <returns></returns>
        public static Operation createOpeate(EnumOperator op)
        {
            Operation 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)
        {
            lblMain.Text = lblMain.Text + strNumber;
            currentValue = Convert.ToDouble(lblMain.Text);
            lblFu.Text += strNumber;
        }

然后在每个数字按钮的点击事件中调用函数,显示运算过程

        //数字0
        private void button9_Click(object sender, EventArgs e)
        {
            NumberClick(btn0.Text);
        }
        //数字1
        private void button11_Click(object sender, EventArgs e)
        {
            NumberClick(btn1.Text);
        }
        //数字2
        private void button6_Click(object sender, EventArgs e)
        {
            NumberClick(btn2.Text);
        }
        //数字3
        private void button7_Click(object sender, EventArgs e)
        {
            NumberClick(btn3.Text);
        }
        //数字4
        private void button8_Click(object sender, EventArgs e)
        {
            NumberClick(btn4.Text);
        }
        //数字5
        private void button5_Click(object sender, EventArgs e)
        {
            NumberClick(btn5.Text);
        }
        //数字6
        private void button4_Click(object sender, EventArgs e)
        {
            NumberClick(btn6.Text);
        }
        //数字7
        private void button3_Click(object sender, EventArgs e)
        {
            NumberClick(btn7.Text);
        }
        //数字8
        private void button2_Click(object sender, EventArgs e)
        {
            NumberClick(btn8.Text);
        }
        //数字9
        private void button1_Click(object sender, EventArgs e)
        {
            NumberClick(btn9.Text);
        }

某个点

        //点.
        private void btnDian_Click(object sender, EventArgs e)
        {
            NumberClick(btnDian.Text);
            //MessageBox.Show("该按钮尚未完善哦亲~\n\t请不要瞎点喔::>_<::","系统提示");
        }

继续

定义一个枚举运算符参数的函数OperatorClick(EnumOperator op),里面调用DisplayOperator(EnumOperator op)方法
(下面有)

private void OperatorClick(EnumOperator op)
        {
            if (currentOperator != EnumOperator.None)
            {
                //计算
                Evaluate();
            }
            else
            {
                //numSum = double.Parse(lblResult.Text);
                double.TryParse(lblMain.Text, out numSum);
            }

            DisplayOperator(op);

            lblMain.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);
        }

我们可以做个swich选择,根据运算符号来执行四则运算显示的内容 函数 DisplayOperator(EnumOperator op)

private void DisplayOperator(EnumOperator op)
        {
            //选择判断
            switch (op)
            {
                case EnumOperator.None:
                    lblFu.Text = lblMain.Text;
                    lblFu.Text += "";
                    break;
                case EnumOperator.Add:
                    if (lblMain.Text != "")
                    {
                        lblFu.Text = lblMain.Text;
                    }
                    lblFu.Text += "+";
                    break;
                case EnumOperator.Minus:
                    if (lblMain.Text != "")
                    {
                        lblFu.Text = lblMain.Text;
                    }
                    lblFu.Text += "-";
                    break;
                case EnumOperator.Multiply:
                    lblFu.Text = lblMain.Text;
                    lblFu.Text += "x";
                    break;
                case EnumOperator.Divide:
                    lblFu.Text = lblMain.Text;
                    lblFu.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();
            lblMain.Text = numSum.ToString();

            //防止某些傻逼一直重复按 = 
            if (lblMain.Text.Length > 1 && lblMain.Text.Substring(lblMain.Text.Length - 1) != "=")
            {
                lblMain.Text += "=";
            }
        }

在C按钮中添加点击事件,代码如下

private void gui0()
        {
            currentOperator = EnumOperator.None;
            lblMain.Text = "";
            lblFu.Text = "";
            numSum = 0;
        }

下载地址

最后,附上源码的下载地址,该源码我已经在GitHub上开源,共学习研究。

百度网盘:https://pan.baidu.com/s/1d_SHo6bzdjMoVTdp8dhFWQ 提取码:osp2

总结

好了搞定,以上就是咱们盾哥布置的寒假作业第一个任务,计算器 的代码分析了。
本人还是个学计算机的学生,如有描述的不好的地方,请帮我指出来,感谢!

咕咕咕!❣

Last modification:January 21st, 2020 at 12:52 am
如果觉得我的文章对你有用,请赏我桶泡面吃o((>ω< ))o