示例#1
0
        static void Main(string[] args)
        {
            //案例 面向对象计算器
            Console.WriteLine("请输入第一个数字");
            double n1 = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine("请输入第二个数字");
            double n2 = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine("请输入运算符");
            string    opera = Console.ReadLine();
            CalFather cal   = GetCal(opera, n1, n2);

            Console.WriteLine(cal.GetResult());
            Console.ReadLine();
        }
示例#2
0
        public static CalFather GetCal(string opera, double n1, double n2)
        {
            CalFather cal = null;

            switch (opera)
            {
            case "+": cal = new Add(n1, n2);
                break;

            case "-": cal = new Sub(n1, n2);
                break;

            case "*": cal = new Multiply(n1, n2);
                break;

            case "/": cal = new Divide(n1, n2);
                break;
            }
            return(cal);
        }