首页 > 代码库 > 计算器的方式有多少种?

计算器的方式有多少种?

    今天一个同事问了我个问题,问我要实现一个计算器的方式有多少种?突然感到有种想笑的冲动,何故问这样的问题呢?他说C#是种非常美妙的语言,设计的又非常完美,我想知道这样的语言那把这个简单的问题如何的实现!我大致想了想也确实如此。在此想和大家分享一下计算器的实现心得!

    要实现这样一个功能实在是太容易了,就看你怎样的想去实现它了,本人分析了以下八种供大家参考:

001.计算器的面向过程版本?
002.计算器的面向对象版本?
003.计算器的工厂模式版本?
004.计算器的多态抽象方法版本?
005.计算器的多态实现版本?
006.计算器的接口工厂模式版本?
007.计算器的抽象工厂模式版本?
008.计算器的多态接口版本?

    首先看在什么样的情况下,再根据不同的方法去实现!

001.计算器的面向过程版本!

        static string opter;
        static void Main(string[] args)
        {
            double num1 = InputHelper();
            double num2 = InputHelper();
            double res = Results(num1, num2);
            Console.WriteLine("运算结果:{0}{1}{2}={3}",num1,opter,num2,res);
            Console.ReadKey();
        }
        //操作数的输入方法
        public static double InputHelper()
        {
            double result = 0;
            while (true)
            {
                Console.WriteLine("请输入操作数:");
                string numStr = Console.ReadLine();
                if (double.TryParse(numStr, out result))
                {
                    break;
                }
                else
                {
                    Console.Write("输入有误:");
                }
            }
            return result;
        }
        //运算符的输入
        public static double Results(double n1,double n2)
        {
            bool flag = true;
            double res = 0;
            while (flag)
            {
                Console.WriteLine("请输入运算符:");
                opter = Console.ReadLine();
                switch (opter)
                {
                    case "+": res = n1 + n2; flag = false; break;
                    case "-": res = n1 - n2; flag = false; break;
                    case "*": res = n1 * n2; flag = false; break;
                    case "/": res = n1 / n2; flag = false; break;
                    default: Console.Write("操作符输入错误!");
                        break;
                }
            }
            return res;
        }
002.计算器的面向对象版本!

    class Program
    {
        static void Main(string[] args)
        {
            double num1;
            double num2;
            //找对象
            Calculator c = new Calculator();
            //调用方法输入
            num1 = c.Inputer();
            num2 = c.Inputer();
            //调用计算方法,得到结果
            double res = c.Results(num1, num2);
            //输出
            Console.WriteLine("{0}{1}{2}={3}",num1,c.opter,num2,res);
            Console.ReadKey();
        }
    }
    //创建对象
    class Calculator
    {
        public string opter;
        //输入操作数方法
        public double Inputer()
        {
            double n1 = 0;
            while (true)
            {
                Console.WriteLine("请输入操作数:");
                string numStr = Console.ReadLine();
                if (double.TryParse(numStr, out n1))
                {
                    break;
                }
                else
                {
                    Console.Write("输入有误:");
                }
            }
            return n1;
        }
        //根据操作符做计算
        public double Results(double n1,double n2)
        {
            double res = 0;
            bool flag = true;
            while (flag)
            {
                Console.WriteLine("请输入操作符:");
                opter = Console.ReadLine();
                switch (opter)
                {
                    case "+": res = n1 + n2; flag = false; break;
                    case "-": res = n1 - n2; flag = false; break;
                    case "*": res = n1 * n2; flag = false; break;
                    case "/": res = n1 / n2; flag = false; break;
                    default: Console.Write("输入操作符有误!");
                        break;
                }
            }
            return res;
        }
    }
003.计算器的工厂模式版本!

    class Program
    {
        static void Main(string[] args)
        {
            //数据的初始化
            double num1 = InputNums();
            double num2 = InputNums();
            Console.WriteLine("请输入操作符!");
            string opter = Console.ReadLine();
            //调用工厂模式
            Calculator c = Factory.GetOperational(opter,num1,num2);
            double res = c.CalculationResult();
            Console.WriteLine(res);
            Console.ReadKey();
        }
        //输入数据方法
        public static double InputNums()
        {
            double nums = 0;
            while (true)
            {
                Console.WriteLine("请输入操作数");
                string numStr = Console.ReadLine();
                if (double.TryParse(numStr, out nums))
                {
                    break;
                }
                else
                {
                    Console.Write("输入有误!");
                }
            }
            return nums;
        }
    }
    //工厂模式
    class Factory
    {
        public static Calculator GetOperational(string operational,double n1,double n2)
        {
            switch (operational)
            {
                case "+": return new Addition(n1,n2);
                case "-": return new Subtraction(n1, n2);
                case "*": return new Multiplication(n1, n2);
                case "/": return new Division(n1, n2);
                default: return new Calculator(n1, n2);
            }
        }
    }
    //父类
    class Calculator
    {
        double num1;

        public double Num1
        {
            get { return num1; }
            set { num1 = value; }
        }
        double num2;

        public double Num2
        {
            get { return num2; }
            set { num2 = value; }
        }
        public Calculator(double num1, double num2)
        {
            this.num1 = num1;
            this.num2 = num2;
        }
        public virtual double CalculationResult()
        {
            return 0;
        }
    }
    //子类的加(Addition)、减(Subtraction)、乘(Multiplication)、除(Division)
    class Addition:Calculator
    {
        public Addition(double n1, double n2)
            : base(n1, n2)
        { }
        public override double CalculationResult()
        {
            return Num1 + Num2;
        }
    }
    class Subtraction:Calculator
    {
        public Subtraction(double n1, double n2)
            : base(n1, n2)
        { }
        public override double CalculationResult()
        {
            return Num1 - Num2;
        }
    }
    class Multiplication:Calculator
    {
        public Multiplication(double n1, double n2)
            : base(n1, n2)
        { }
        public override double CalculationResult()
        {
            return Num1 * Num2;
        }
    }
    class Division:Calculator
    {
        public Division(double n1, double n2):base(n1,n2)
        {
        }
        public override double CalculationResult()
        {
            return Num1 / Num2;
        }
    }
004.计算器的多态抽象方法版本!

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //新建找对象
                Calculator c = null;
                //调用输入的方法
                double num1 = Inputer();
                double num2 = Inputer();
                Console.WriteLine("请输入操作符:");
                string oper = Console.ReadLine();
                //判断操作符
                switch (oper)
                {
                    case "+": c = new Addition(); break;
                    case "-": c = new Subtraction(); break;
                    case "*": c = new Multiplication(); break;
                    case "/": c = new Division(); break;//为了避免输入操作符的错误采用try{}catch{}
                }
                if (c == null)
                {
                    Console.WriteLine("操作符输入有误!");
                }
                else
                {
                    Console.WriteLine(c.GetResults(num1, num2));
                }
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }
            Console.ReadKey();
        }
        public static double Inputer()
        {
            double nums = 0;
            while (true)
            {
                Console.WriteLine("请输入操作数:");
                string numStr = Console.ReadLine();
                if (double.TryParse(numStr, out nums))
                {
                    break;
                }
                else
                {
                    Console.Write("输入的操作数有误:");
                }
            }
            return nums;
        }
    }
    //抽象方法
    public abstract class Calculator
    {
        public abstract double GetResults(double n1,double n2);
    }
    //子类的实现
    class Addition:Calculator
    {
        public override double GetResults(double n1, double n2)
        {
            return n1 + n2;
        }
    }
    class Subtraction:Calculator
    {
        public override double GetResults(double n1, double n2)
        {
            return n1 - n2;
        }
    }
    class Multiplication:Calculator
    {
        public override double GetResults(double n1, double n2)
        {
            return n1 * n2;
        }
    }
    class Division:Calculator
    {
        public override double GetResults(double n1, double n2)
        {
            return n1 / n2;
        }
    }
005.计算器的多态实现版本!

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //多态的实现新建父类对象
                Calculator c = null;
                double num1 = Inputer();
                double num2 = Inputer();
                Console.WriteLine("请输入运算符:");
                string oper = Console.ReadLine();
                switch (oper)
                {
                    case "+": c = new Addition(num1, num2); break;
                    case "-": c = new Subtraction(num1, num2); break;
                    case "*": c = new Multiplication(num1, num2); break;
                    case "/": c = new Division(num1, num2); break;
                }
                if (c != null)
                {
                    double res = c.GetResults();
                    Console.WriteLine(res);
                }
                else
                {
                    Console.WriteLine("操作符有误!");
                }
            }
            catch
            {
                Console.WriteLine("操作符输入有误,请按任意键接受!");
            }
            Console.ReadKey();
        }
        public static double Inputer()
        {
            double nums = 0;
            while (true)
            {
                Console.WriteLine("请输入操作数:");
                string numStr = Console.ReadLine();
                if (double.TryParse(numStr, out nums))
                {
                    break;
                }
                else
                {
                    Console.Write("输入有误:");
                }
            }
            return nums;
        }
    }
    //创建父类对象
    class Calculator
    {
        double num1;

        public double Num1
        {
            get { return num1; }
            set { num1 = value; }
        }
        double num2;

        public double Num2
        {
            get { return num2; }
            set { num2 = value; }
        }
        public Calculator(double n1, double n2)
        {
            this.num1 = n1;
            this.num2 = n2;
        }
        public virtual double GetResults()
        {
            return 0;
        }
    }
    //创建子类对象加(Addition)、减(Subtraction)、乘(Multiplication)、除(Division)
    class Addition:Calculator
    {
        public Addition(double n1, double n2)
            : base(n1, n2)
        { }
        public override double GetResults()
        {
            return Num1 + Num2;
        }
    }
    class Subtraction:Calculator
    {
        public Subtraction(double n1, double n2)
            : base(n1, n2)
        { }
        public override double GetResults()
        {
            return Num1 - Num2;
        }
    }
    class Multiplication:Calculator
    {
        public Multiplication(double n1, double n2)
            : base(n1, n2)
        { }
        public override double GetResults()
        {
            return Num1 * Num2;
        }
    }
    class Division:Calculator
    {
        public Division(double n1, double n2)
            : base(n1, n2)
        { }
        public override double GetResults()
        {
            return Num1 / Num2;
        }
    }


006.计算器的接口工厂模式版本!

    class Program
    {
        static void Main(string[] args)
        {
            //计算器的接口工厂模式版本
            //找对象
            ICalculatorable c = null;
            double num1 = Inputer();
            double num2 = Inputer();
            Console.WriteLine("请输入操作符:");
            string oper = Console.ReadLine();
            c = FactoryCalculator.InitialCalculatorResult(oper, num1, num2);
            double result = c.ICalculating();
            Console.WriteLine(result);
            Console.ReadKey();
        }
        public static double Inputer()
        {
            double nums = 0;
            while (true)
            {
                Console.WriteLine("请输入操作数:");
                string numStr = Console.ReadLine();
                if (!double.TryParse(numStr, out nums))
                {
                    Console.Write("输入有误!");
                }
                else
                {
                    break;
                }
            }
            return nums;
        }
    }
    //接口版的工厂模式
    class FactoryCalculator
    {
        public static Calculator InitialCalculatorResult(string oper, double n1, double n2)
        {
            switch (oper)
            {
                case "+": return new Addition(n1, n2);
                case "-": return new Subtraction(n1, n2);
                case "*": return new Multiplication(n1, n2);
                case "/": return new Division(n1, n2);
                default: throw new Exception("操作符输入有误!");
            }
        }
    }
    //接口
    interface ICalculatorable
    {
        double ICalculating();
    }
    //抽象类
    abstract class Calculator:ICalculatorable
    {
        double num1;
        double num2;

        public double Num2
        {
            get { return num2; }
            set { num2 = value; }
        }
        public double Num1
        {
            get { return num1; }
            set { num1 = value; }
        }
        public Calculator(double n1, double n2)
        {
            this.num1 = n1;
            this.num2 = n2;
        }
        public abstract double ICalculating();
    }
    //子类的实现
    class Addition:Calculator
    {
        public Addition(double n1, double n2)
            : base(n1, n2)
        { }
        public override double ICalculating()
        {
            return Num1 + Num2;
        }
    }
    class Subtraction:Calculator
    {
        public Subtraction(double n1, double n2)
            : base(n1, n2)
        { }
        public override double ICalculating()
        {
            return Num1 - Num2;
        }
    }
    class Multiplication:Calculator
    {
        public Multiplication(double n1, double n2)
            : base(n1, n2)
        { }
        public override double ICalculating()
        {
            return Num1 * Num2;
        }
    }
    class Division:Calculator
    {
        public Division(double n1, double n2)
            : base(n1, n2)
        { }
        public override double ICalculating()
        {
            return Num1 / Num2;
        }
    }


007.计算器的抽象工厂模式版本!

    class Program
    {
        static void Main(string[] args)
        {
            //计算器的抽象工厂模式版本
            //找对象
            Calculator c = null;
            double num1 = Inputer();
            double num2 = Inputer();
            Console.WriteLine("请输入操作符:");
            string oper = Console.ReadLine();
            c = FactoryCalculator.InitialCalculator(oper, num1, num2);
            double result = c.InitialCalculating();
            Console.WriteLine(result);
            Console.ReadKey();
        }
        public static double Inputer()
        {
            double nums = 0;
            while (true)
            {
                Console.WriteLine("请输入操作数:");
                string numStr = Console.ReadLine();
                if (!double.TryParse(numStr, out nums))
                {
                    Console.Write("输入有误:");
                }
                else
                {
                    break;
                }
            }
            return nums;
        }
    }
    //工厂
    class FactoryCalculator
    {
        public static Calculator InitialCalculator(string oper,double n1,double n2)
        {
            switch (oper)
            {
                case "+": return new Addition(n1, n2);
                case "-": return new Subtraction(n1, n2);
                case "*": return new Multiplication(n1, n2);
                case "/": return new Division(n1, n2);
                default: throw new Exception("系统找不到指定运算符...");
            }
        }
    }
    //抽象类的工厂模式与多态工厂模式几乎一样
    abstract class Calculator
    {
        double num1;
        double num2;

        public double Num2
        {
            get { return num2; }
            set { num2 = value; }
        }
        public double Num1
        {
            get { return num1; }
            set { num1 = value; }
        }
        public Calculator(double n1, double n2)
        {
            this.num1 = n1;
            this.num2 = n2;
        }
        public abstract double InitialCalculating();
    }
    //子类的实现
    class Addition:Calculator
    {
        public Addition(double n1, double n2)
            : base(n1, n2)
        { }
        public override double InitialCalculating()
        {
            return Num1 + Num2;
        }
    }
    class Subtraction:Calculator
    {
        public Subtraction(double n1, double n2)
            : base(n1, n2)
        { }
        public override double InitialCalculating()
        {
            return Num1 - Num2;
        }
    }
    class Multiplication:Calculator
    {
        public Multiplication(double n1, double n2)
            : base(n1, n2)
        { }
        public override double InitialCalculating()
        {
            return Num1 * Num2;
        }
    }
    class Division:Calculator
    {
        public Division(double n1, double n2)
            : base(n1, n2)
        { }
        public override double InitialCalculating()
        {
            return Num1 / Num2;
        }
    }


008.计算器的多态接口版本!

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //创建对象
                ICalculatorable c = null;
                double num1 = Inputer();
                double num2 = Inputer();
                Console.WriteLine("请输入操作符:");
                string oper = Console.ReadLine();
                switch (oper)
                {
                    case "+": c = new Addition(num1, num2); break;
                    case "-": c = new Subtraction(num1, num2); break;
                    case "*": c = new Multiplication(num1, num2); break;
                    case "/": c = new Division(num1, num2); break;
                }
                if (c != null)
                {
                    double res = c.ICalculating();
                    Console.WriteLine(res);
                }
                else
                {
                    Console.WriteLine("操作符输入有误!");
                }
            }
            catch
            {
                Console.WriteLine("输入有误!");
            }
            Console.ReadKey();
        }
        public static double Inputer()
        {
            double nums = 0;
            while (true)
            {
                Console.WriteLine("请输入操作数:");
                string numStr = Console.ReadLine();
                if (!double.TryParse(numStr, out nums))
                {
                    Console.WriteLine("操作数输入有误!");
                }
                else
                {
                    break;
                }
            }
            return nums;
        }
    }
    //接口的使用
    interface ICalculatorable
    {
        double ICalculating();
    }
    //抽象类的使用
    abstract class Calculator:ICalculatorable
    {
        double num1;

        public double Num1
        {
            get { return num1; }
            set { num1 = value; }
        }
        double num2;

        public double Num2
        {
            get { return num2; }
            set { num2 = value; }
        }
        public Calculator(double n1, double n2)
        {
            this.num1 = n1;
            this.num2 = n2;
        }
        public abstract double ICalculating();
    }
    //子类的实现
    class Addition:Calculator
    {
        public Addition(double n1, double n2)
            : base(n1, n2)
        { }


        public override double ICalculating()
        {
            return Num1 + Num2;
        }
    }
    class Subtraction:Calculator
    {
        public Subtraction(double n1, double n2)
            : base(n1, n2)
        { }
        public override double ICalculating()
        {
            return Num1 - Num2;
        }
    }
    class Multiplication:Calculator
    {
        public Multiplication(double n1, double n2)
            : base(n1, n2)
        { }

        public override double ICalculating()
        {
            return Num1 * Num2;
        }
    }
    class Division:Calculator
    {
        public Division(double n1, double n2)
            : base(n1, n2)
        { }
        public override double ICalculating()
        {
            return Num1 / Num2;
        }
    }

    希望能帮助到各位!