Esempio n. 1
0
        static void Main(string[] args)
        {
            while (true)
            {
                //获取需要运算的数字
                Console.WriteLine("请输入第一个数字");
                double numberA = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("请输入运算符");
                string operate = Console.ReadLine();
                Console.WriteLine("请输入第二个数字");
                double numberB = Convert.ToDouble(Console.ReadLine());

                //判断被除数是否为0
                while (operate == "/" && numberB == 0)
                {
                    Console.WriteLine("被除数不能为0,请重新输入");
                    numberB = Convert.ToDouble(Console.ReadLine());
                }
                //获取对应的工厂
                Operation operation = OperationFactory.CreateOperation(operate);
                operation.Number1 = numberA;
                operation.Number2 = numberB;
                double result = operation.GetResult();
                Console.WriteLine("计算结果是:" + result);
                Console.ReadKey();
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Operation oper = OperationFactory.CreateOperation("/");

            oper.NumberA = 10;
            oper.NumberB = 2;
            double result = oper.GetResult();
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            Operation oper;

            oper         = OperationFactory.CreateOperation(Console.ReadLine());
            oper.NumberA = Convert.ToDouble(Console.ReadLine());
            oper.NumberB = Convert.ToDouble(Console.ReadLine());
            Console.Write(oper.GetResult());
            Console.ReadLine();
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            Operation oper = null;

            oper         = OperationFactory.createOperate("+");
            oper.NumberA = 1;
            oper.NumberB = 2;
            double result = oper.GetResult();

            Console.WriteLine(result);
            Console.ReadLine();
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            Operation op;

            op         = OperationFactory.CreateOperate("+");
            op.NumberA = 1;
            op.NumberB = 2;
            Console.WriteLine(op.GetResult());
            Console.WriteLine("__________________");
            op         = OperationFactory.CreateOperate("-");
            op.NumberA = 5;
            op.NumberB = 3;
            Console.WriteLine(op.GetResult());
            Console.ReadKey();
        }
Esempio n. 6
0
        static void Main(string[] args)
        {
            #region 过程式
            //Console.WriteLine("请输入数字A: ");
            //string A = Console.ReadLine();
            //Console.WriteLine("请选择运算符号(+、-、*、/): ");
            //string B = Console.ReadLine();
            //Console.WriteLine("请输入数字B: ");
            //string C = Console.ReadLine();
            //string D = "";

            //if (B == "+")
            //{
            //    D = Convert.ToString(Convert.ToDouble(A) + Convert.ToDouble(C));
            //}
            //if (B == "-")
            //{
            //    D = Convert.ToString(Convert.ToDouble(A) - Convert.ToDouble(C));
            //}
            //if (B == "*")
            //{
            //    D = Convert.ToString(Convert.ToDouble(A) * Convert.ToDouble(C));
            //}
            //if (B == "/")
            //{
            //    D = Convert.ToString(Convert.ToDouble(A) / Convert.ToDouble(C));
            //}

            //Console.WriteLine("结果是: " + D);
            //Console.ReadKey();
            #endregion

            #region 修改后的过程式
            //try
            //{
            //    Console.WriteLine("请输入数字A: ");
            //    string strNumberA = Console.ReadLine();
            //    Console.WriteLine("请选择运算符号(+、-、*、/): ");
            //    string strOperate = Console.ReadLine();
            //    Console.WriteLine("请输入数字B: ");
            //    string strNumberB = Console.ReadLine();
            //    string strResult = "";

            //    switch (strOperate)
            //    {
            //        case "+":
            //            strResult = Convert.ToString(Convert.ToDouble(strNumberA) + Convert.ToDouble(strNumberB));
            //            break;
            //        case "-":
            //            strResult = Convert.ToString(Convert.ToDouble(strNumberA) - Convert.ToDouble(strNumberB));
            //            break;
            //        case "*":
            //            strResult = Convert.ToString(Convert.ToDouble(strNumberA) * Convert.ToDouble(strNumberB));
            //            break;
            //        case "/":
            //            strResult = Convert.ToString(Convert.ToDouble(strNumberA) / Convert.ToDouble(strNumberB));
            //            break;
            //        default:
            //            break;
            //    }

            //    Console.WriteLine("结果是: " + strResult);
            //    Console.ReadKey();
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("您的输入有错: " + ex.Message);
            //    throw;
            //}
            #endregion


            #region 使用面向对象类
            //try
            //{
            //    Console.WriteLine("请输入数字A: ");
            //    string strNumberA = Console.ReadLine();
            //    Console.WriteLine("请选择运算符号(+、-、*、/): ");
            //    string strOperate = Console.ReadLine();
            //    Console.WriteLine("请输入数字B: ");
            //    string strNumberB = Console.ReadLine();
            //    string strResult = "";
            //    strResult = Convert.ToString(Operate.GetResult(Convert.ToDouble(strNumberA), Convert.ToDouble(strNumberB), strOperate));

            //    Console.WriteLine("结果是: " + strResult);
            //    Console.ReadKey();
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("您的输入有错: " + ex.Message);
            //    throw;
            //}
            #endregion


            #region 使用松耦合类 + 简单工厂
            try
            {
                Console.WriteLine("请输入数字A: ");
                string strNumberA = Console.ReadLine();
                Console.WriteLine("请选择运算符号(+、-、*、/): ");
                string strOperate = Console.ReadLine();
                Console.WriteLine("请输入数字B: ");
                string strNumberB = Console.ReadLine();

                Operation oper;
                oper         = OperationFactory.createOperate(strOperate);
                oper.NumberA = Convert.ToDouble(strNumberA);
                oper.NumberB = Convert.ToDouble(strNumberB);
                string result = Convert.ToString(oper.GetResult());

                Console.WriteLine("结果是: " + result);
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine("您的输入有错: " + ex.Message);
                Console.ReadKey();
            }
            #endregion
        }