深入.NET 第六章 上机4

这篇博客展示了如何用C#编写一个简单的计算器程序。通过创建一个基础操作类`Operation`和多个子类来处理加、减、乘、除四种运算。在用户界面中,根据选择的运算符实例化相应的操作类并进行计算。程序处理了除数为0的情况,并通过异常显示错误信息。
摘要由CSDN通过智能技术生成
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 上机4_计算器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.comboBox1.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Operation opr = new Operation();
                switch (this.comboBox1.SelectedItem.ToString().Trim())
                { 
                    case "+":
                        opr = new OperationAdd();
                        break;
                    case  "-":
                        opr = new Operationjian();
                        break;

                    case "*":
                        opr = new Operationcheng();
                        break;
                    case "/":
                        opr = new OperationDiv();
                        break;
                }
                opr.NumberA = double.Parse(this.textBox1.Text.Trim());
                opr.NumberB = double.Parse(this.textBox2.Text.Trim());
                this.label2.Text = opr.GetResult().ToString();
                this.label1.Visible = true;
                this.label2.Visible = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 上机4_计算器
{
   public  class Operation
    {
       public double NumberA { get; set; }
       public double NumberB { get; set; }
       public virtual double GetResult() { 
          
           double result=0;
           return result;
       }

    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 上机4_计算器
{
    class OperationAdd:Operation
    {
        public override double GetResult()
        {
            double result = NumberA + NumberB;
            return result;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 上机4_计算器
{
    class Operationcheng : Operation
    {
        public override double GetResult()
        {
            double result = NumberA * NumberB;
            return result;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 上机4_计算器
{
    class OperationDiv:Operation
    {
        public override double GetResult()
        {
            if (NumberB == 0) {
                throw new Exception("除数不能为0");
            }
            double result=NumberA/NumberB;
            return result;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 上机4_计算器
{
    class Operationjian:Operation
    {
        public override double GetResult()
        {
            double result = NumberA - NumberB;
            return result;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值