Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("请输入第一个数:");
            double dNum1 = Convert.ToDouble(Console.ReadLine());

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

            Console.WriteLine("请输入操作符:");
            string  strOp   = Console.ReadLine();
            Factory factory = null;

            switch (strOp)
            {
            case "+":
                factory = new AddFactory();
                break;

            case "-":
                factory = new SubFactory();
                break;

            case "*":
                factory = new MulFactory();
                break;

            case "/":
                factory = new DivFactory();
                break;

            case "%":
                factory = new ModuloFactory();
                break;

            default:
                Console.WriteLine("没有此工厂方法");
                factory = null;
                break;
            }
            if (factory != null)
            {
                Operation op = factory.CreateOperation();
                Console.WriteLine("{0}{1}{2}={3}", dNum1, strOp, dNum2, op.GetResult(dNum1, dNum2));
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.WriteLine("---简单计算器---");
            Console.Write("请输入左值:");
            string numLeftStr = Console.ReadLine();
            Console.WriteLine();
            Console.Write("请输入运算符:");
            string oper = Console.ReadLine();
            Console.WriteLine();
            Console.Write("请输入右值:");
            string numRightStr = Console.ReadLine();
            double numLeft = 0, numRight = 0;
            double.TryParse(numLeftStr, out numLeft);
            double.TryParse(numRightStr, out numRight);
            IFactory operFactory;
            switch (oper)
            {
                case "+":
                    operFactory = new AddFactory();
                    break;
                case "-":
                    operFactory = new SubFactory();
                    break;
                case "*":
                    operFactory = new MulFactory();
                    break;
                case "/":
                    operFactory = new DivFactory();
                    break;
                default:
                    operFactory = new AddFactory();
                    break;

            }
            Operate op = operFactory.CreateOperate();
            op.NumLeft = numLeft;
            op.NumRight = numRight;
            Console.WriteLine("结果为{0}", op.GetResult());
        }