.NET 第六章 上机四 多态计算机

这篇博客展示了如何在.NET环境下实现一个简单的计算器程序,利用多态特性进行加减乘除运算。程序包含了对输入的非空验证,以及针对除数为零的异常处理。用户可以选择不同的运算符,程序会根据选择调用对应的加法、减法、乘法或除法类进行计算。
摘要由CSDN通过智能技术生成
namespace _06_上机四_计算器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.comb.SelectedIndex = 0;

        }

        private void Calc_Click(object sender, EventArgs e)
        {
            try
            {
                //非空验证
                
                if (string.IsNullOrEmpty(this.text1.Text.Trim()))
                {
                    MessageBox.Show("输入不能为空!!!");
                    this.text1.Focus();
                    return;
                }

                if (string.IsNullOrEmpty(this.text2.Text.Trim()))
                {
                    MessageBox.Show("输入不能为空!!!");
                    this.text2.Focus();
                    return;
                }

                //设置符号

                Operation operation = null;
                string yun = this.comb.Text.Trim();
                switch (yun)
                {

                    case "+":

                        operation = new Add();                     
                        break;

                    case "-":

                        operation = new jian();
                        break;

                    case "*":

                        operation = new cheng();                        
                        break;

                    case "/":

                        operation = new chu();
                        break;

                    default:
                        MessageBox.Show("kkkk");
                        break;

                }

                //设置参与计算的数据

                operation.NumberA = double.Parse(this.text1.Text.Trim());
                operation.NumberB = double.Parse(this.text1.Text.Trim());

                this.jieguo.Text = operation.GetResult().ToString();
            }
            catch (Exception ex)
            {

                MessageBox.Show("发生错误!!!"+ex.Message); 
 
            }
            
        }
namespace _06_上机四_计算器
{
    class chu:Operation
    {
        public override double GetResult()
        {
            if (NumberB==0)
            {
                throw new Exception("除数不能为零!");
            }
            double result = NumberA / NumberB;
            return result;
        }
    }
}

namespace _06_上机四_计算器
{
    class cheng:Operation
    {
        public override double GetResult()
        {
            if (NumberB == 0)
            {
                throw new Exception("乘数不能为零!");
            }
            double result = this.NumberA * this.NumberB;
            return result;
        }
    }
}

namespace _06_上机四_计算器
{
    class jian:Operation
    {
        public override double GetResult()
        {
            double result = this.NumberA - this.NumberB;
            return result;
        }
    }
}

namespace _06_上机四_计算器
{
    public class Add:Operation
    {
        public override double GetResult()
        {
            double result = this.NumberA +this.NumberB;
            return result;
        }
    }
}
namespace _06_上机四_计算器
{
    public class Operation
    {
        public double NumberA { get; set; }
        public double NumberB { get; set; }

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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值