Esempio n. 1
0
        static void Main(string[] args)
        {
            Console.Write("Please input the first number: ");
            string firstNumber = Console.ReadLine();

            Console.Write("Please input the operator: ");
            string opt = Console.ReadLine();

            Console.Write("Please input the second number: ");
            string secondNumber = Console.ReadLine();

            try
            {
                Operation oper = OperationFactory.CreateOperation(opt);
                oper.NumA = Convert.ToDouble(firstNumber);
                oper.NumB = Convert.ToDouble(secondNumber);
                double result = oper.GetResult();
                // result = Operation.GetResult(Convert.ToDouble(firstNumber), Convert.ToDouble(secondNumber), opt);
                // switch (opt)
                // {
                //     case "+":
                //         result = Convert.ToDouble(firstNumber) + Convert.ToDouble(secondNumber);
                //         break;
                //     case "-":
                //         result = Convert.ToDouble(firstNumber) - Convert.ToDouble(secondNumber);
                //         break;
                //     case "*":
                //         result = Convert.ToDouble(firstNumber) * Convert.ToDouble(secondNumber);
                //         break;
                //     case "/":
                //         result = Convert.ToDouble(firstNumber) / Convert.ToDouble(secondNumber);
                //         break;
                //     default:
                //         Console.WriteLine("Unsupported operator.");
                //         return;
                // }
                Console.WriteLine(String.Format("The result is {0:F}", result));
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Esempio n. 2
0
 static void Main(string[] args)
 {
     try
     {
         Console.WriteLine("请输入第一个数");
         double numA = Convert.ToDouble(Console.ReadLine());
         Console.WriteLine("请输入操作符");
         string operate = Console.ReadLine();
         Console.WriteLine("请输入第二个数");
         double    numB = Convert.ToDouble(Console.ReadLine());
         Operation oper = OperationFactory.CreateOperate(operate);
         oper.NumberA = numA;
         oper.NumberB = numB;
         Console.WriteLine(oper.GetResult());
         Console.ReadKey();
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.ToString());
         Console.ReadKey();
     }
 }