以下是使用 C# 编写的一个基于事件机制的高级计算器程序的示例:

using System;

namespace Calculator
{
    public class CalculatorEventArgs : EventArgs
    {
        public double Result { get; set; }
    }

    public class Calculator
    {
        public event EventHandler<CalculatorEventArgs> CalculationPerformed;

        public void Add(double x, double y)
        {
            double result = x + y;
            OnCalculationPerformed(result);
        }

        public void Subtract(double x, double y)
        {
            double result = x - y;
            OnCalculationPerformed(result);
        }

        public void Multiply(double x, double y)
        {
            double result = x * y;
            OnCalculationPerformed(result);
        }

        public void Divide(double x, double y)
        {
            if (y != 0)
            {
                double result = x / y;
                OnCalculationPerformed(result);
            }
            else
            {
                Console.WriteLine('除数不能为零!');
            }
        }

        protected virtual void OnCalculationPerformed(double result)
        {
            CalculationPerformed?.Invoke(this, new CalculatorEventArgs { Result = result });
        }
    }

    public class Program
    {
        public static void Main(string[] args)
        {
            Calculator calculator = new Calculator();
            calculator.CalculationPerformed += Calculator_CalculationPerformed;

            Console.WriteLine('欢迎使用计算器!');
            Console.WriteLine('请选择操作:1. 加法  2. 减法  3. 乘法  4. 除法');
            string operation = Console.ReadLine();

            if (operation != '1' && operation != '2' && operation != '3' && operation != '4')
            {
                Console.WriteLine('无效操作!');
                return;
            }

            Console.WriteLine('请输入第一个数字:');
            double num1 = double.Parse(Console.ReadLine());

            Console.WriteLine('请输入第二个数字:');
            double num2 = double.Parse(Console.ReadLine());

            switch (operation)
            {
                case '1':
                    calculator.Add(num1, num2);
                    break;
                case '2':
                    calculator.Subtract(num1, num2);
                    break;
                case '3':
                    calculator.Multiply(num1, num2);
                    break;
                case '4':
                    calculator.Divide(num1, num2);
                    break;
            }
        }

        private static void Calculator_CalculationPerformed(object sender, CalculatorEventArgs e)
        {
            Console.WriteLine('结果:' + e.Result);
        }
    }
}

在上述代码中,我们定义了一个Calculator类来执行计算操作。每个计算方法都会触发一个CalculationPerformed事件,并传递计算结果。

Main方法中,我们创建了一个Calculator实例,并订阅了CalculationPerformed事件。然后,我们通过控制台输入选择计算操作和输入数字,并根据选择调用相应的计算方法。

当计算完成后,CalculationPerformed事件将被触发,并在事件处理程序Calculator_CalculationPerformed中打印计算结果。

这个示例中的高级计算器程序使用了事件机制来实现计算结果的传递和处理。你可以根据需要扩展和修改代码,添加更多的计算方法和功能。

C# 事件机制实现高级计算器 - 代码示例及详解

原文地址: https://www.cveoy.top/t/topic/bBAh 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录