Пример #1
0
        /*
         * 请用C++、Java、C#或VB.NET任意一种面向对象语言实现一个计算器控制台程序
         * 要求:输入两个数和运算符号,得到结果
         */
        private static void Main(string[] args)
        {
            try
            {
                Console.Write("请输入数字A:");
                string strNumberA = Console.ReadLine();
                Console.Write("请输入运算符号(+、-、*、/):");
                string strOperate = Console.ReadLine();
                Console.Write("请输入数字B:");
                string strNumberB = Console.ReadLine();
                string strResult  = "";

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

                double result = oper.GetResult();
                strResult = Convert.ToString(result);

                Console.WriteLine("结果是:" + strResult);
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("您的输入有错:" + ex.Message);
            }
        }
Пример #2
0
        /// <summary>
        /// 此函数完成计算器的简单计算
        /// </summary>
        /// <param name="args">输入参数</param>
        static void Main(string[] args)
        {
            /*
             * GetResult();        //利用简单的函数完成计算器功能
             *
             * //下面是利用类的封装完成计算机功能
             * //利用简单的封装完成了业务的封装,使得程序之间的耦合性大大降低,可维护性大大增强,程序变得可复用和可扩展
             * Console.Write("Please input the num A:");
             * string a = Console.ReadLine();
             * Console.Write("Please input the operator:");
             * string operatorStr=Console.ReadLine();
             * Console.Write("Please input the num B:");
             * string b = Console.ReadLine();
             * double result = Operation.GetResult(Convert.ToDouble(a), Convert.ToDouble(b), operatorStr);
             * Console.Write(result.ToString());
             * Console.ReadKey();
             * */
            Console.Write("Please input the num A:");
            string a = Console.ReadLine();

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

            Console.Write("Please input the num B:");
            string    b    = Console.ReadLine();
            Operation oper = OperationFactory.CreateOperation(operatorStr);

            oper.NumberA = Convert.ToDouble(a);
            oper.NumberB = Convert.ToDouble(b);
            double result = oper.GetResult();

            Console.Write(result.ToString());
            Console.ReadKey();
        }
Пример #3
0
        static void Main(string[] args)
        {
            Operation op = OperationFactory.CreateOperation("+");

            op.NumberA = 10;
            op.NumberB = 20;
            Console.WriteLine(op.GetResult());
        }
Пример #4
0
        static void Main(string[] args)
        {
            Operation oper = OperationFactory.createOperate("+");

            oper.NumberA = 1;
            oper.NumberB = 2;
            Console.WriteLine(oper.GetResult());
            Console.ReadKey();
        }
Пример #5
0
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtShow.Text != "")
            {
                oper = OperationFactory.createOperate(((Button)sender).Text);

                oper.NumberA = Convert.ToDouble(txtShow.Text);
                bOperate     = true;
            }
        }
        private static void Main(string[] args)
        {
            //input 3 parameter
            var ss = OperationFactory.CreatOperate("+");

            ss.NumberA = 1;
            ss.NumberB = 2;

            Console.WriteLine(ss.GetResult());
            Console.Read();
        }
Пример #7
0
        static void Main(string[] args)
        {
            Operation o = OperationFactory.CreateOperation("+");

            o.NumberA = 5;

            o.NumberB = 4;

            Console.WriteLine(o.Method());

            Console.Read();
        }
Пример #8
0
        static void Main(string[] args)
        {
            //简单工厂 案例:计算器
            var oper = OperationFactory.CreateOperate("+");

            oper.NumberA = 1;
            oper.NumberB = 2;
            var result = oper.GetResult();

            Console.WriteLine(result);
            Console.ReadKey();
        }
Пример #9
0
        static void Main(string[] args)
        {
            List <OperatorSuperClass> OperList = new List <OperatorSuperClass>();

            OperList.Add(OperationFactory.createOperation("+"));
            OperList.Add(OperationFactory.createOperation("-"));
            OperList.Add(OperationFactory.createOperation("*"));
            OperList.Add(OperationFactory.createOperation("/"));
            foreach (var oper in OperList)
            {
                oper.showExample();
            }
            Console.ReadLine();
        }
Пример #10
0
        private void btnEqual_Click(object sender, EventArgs e)
        {
            if (txtShow.Text != "")
            {
                if (((Button)sender).Text != "=")
                {
                    oper = OperationFactory.createOperate(((Button)sender).Text);
                }

                oper.NumberB = Convert.ToDouble(txtShow.Text);
                txtShow.Text = oper.GetResult().ToString();
                bOperate     = true;
            }
        }
Пример #11
0
        static void Main(string[] args)
        {
            string    oper      = string.Empty;
            Operation operation = new Operation();

            Console.Title = "简单工厂模式";
            Console.WriteLine("加减乘除:+-*/");
            Console.WriteLine("请输入运算法则:");
            oper      = Console.ReadKey().KeyChar.ToString();
            operation = OperationFactory.CreateOperation(oper);
            Console.WriteLine("\n请输入第一个数字:");
            operation.NumberA = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("请输入第二个数字:");
            operation.NumberB = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("最后的结果是" + operation.GetResult() + "按任意键结束");
            Console.ReadKey();
        }
Пример #12
0
        public static void Main(string[] args)
        {
            try {
                Console.WriteLine("Input NumberA:");
                string InputNumberAStr = Console.ReadLine();
                Console.WriteLine("Input Oprator(+ - * /):");
                string InputOper = Console.ReadLine();
                Console.WriteLine("Input NumberB:");
                string InputNumberBStr = Console.ReadLine();

                Operation oper = OperationFactory.createOpration(InputOper);
                oper.NumberA = Convert.ToDouble(InputNumberAStr);
                oper.NumberB = Convert.ToDouble(InputNumberBStr);
                Console.WriteLine(oper.getResult());
            } catch (Exception ex) {
                Console.WriteLine(ex.Message);
            }
        }
Пример #13
0
        static void Main(string[] args)
        {
            Console.WriteLine("请输入数字A:");
            string numberA = Console.ReadLine();

            Console.WriteLine("请输入运算符(+,-,*,/):");
            string operater = Console.ReadLine();

            Console.WriteLine("请输入数字B:");
            string    numberB   = Console.ReadLine();
            Operation operation = null;

            operation         = OperationFactory.CreateOperation(operater);
            operation.NumberA = Convert.ToDouble(numberA);
            operation.NumberB = Convert.ToDouble(numberB);
            Console.WriteLine("计算结果为:" + operation.GetResult());
            Console.ReadKey();
        }
Пример #14
0
        static void Main(string[] args)
        {
            //Operation作为一个父类,也相当于一个接口,子类去实现他们的共同功能
            //然后通过简单工厂来生产出不同的产品。最后通过得到的产品去调用他们的行为方法
            Operation oper;

            oper         = OperationFactory.CreateOperate("+");
            oper.NumberA = 3;
            oper.NumberB = 4;
            Console.WriteLine(oper.GetResult());
            //UML简介
            //1.在UML中,【+】代表着public,【-】代表着prvite,【#】代表着protend
            //2.如果A类继承了B类我们用【空心的三角形+实线】表示
            //3.如果A类实现了接口,则我们用【空心的三角形+虚线】来表示
            //4.如果我们在A类中需要"知道"另外的一个类时我们就用关联,关联的关系我们用【实线箭头】来表示
            //5.聚合关系:表示一种弱的引用关系,体现的是A对象可以包含B对象,但B对象不是A对象的一部分(大雁-雁群),他们的生命周期不一样【空心的菱形+实线箭头来表示】
            //6.合成【组合】关系:是一种强的"拥有关系",体现了严格的部分和整体的关系,整体和部分的生命周期一样。(鸟-翅膀)【实心的菱形+实线箭头】来表示。
        }
Пример #15
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();

            Operation op = OperationFactory.CreateOperation(strOp);

            if (op == null)
            {
                Console.WriteLine("没有对应的操作符.");
                return;
            }
            Console.WriteLine("{0}{1}{2}={3}", dNum1, strOp, dNum2, op.GetResult(dNum1, dNum2));
        }
Пример #16
0
 static void Main(string[] args)
 {
     try
     {
         Console.WriteLine("请输入第一个运算数字A:");
         string a = Console.ReadLine();
         Console.WriteLine("请输入第二个运算数字B:");
         string b = Console.ReadLine();
         Console.WriteLine("请输入运算符号(+,-,*,/):");
         string    strOperation = Console.ReadLine();
         string    strResult    = "";
         Operation operation    = OperationFactory.CreateOperation(strOperation);
         strResult = operation.GetResult(Convert.ToDouble(a), Convert.ToDouble(b)).ToString();
         Console.WriteLine("运算结果是:{0}", strResult);
         Console.ReadLine();
     }
     catch (Exception ex)
     {
         Console.WriteLine("你输入的有错:" + ex.Message);
     }
 }
Пример #17
0
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("請輸入數字A:");
                string strNumberA = Console.ReadLine();
                Console.WriteLine("請輸入加減乘除:");
                string strOperate = Console.ReadLine();
                Console.WriteLine("請輸入數字B:");
                string strNumberB = Console.ReadLine();

                Operation operation = OperationFactory.createOperation(strOperate);
                double    result    = operation.GetResult(Convert.ToDouble(strNumberA), Convert.ToDouble(strNumberB));
                Console.WriteLine($"結果: {result}");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }
        }
Пример #18
0
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("請輸入數字A");
                string strNumA = Console.ReadLine();
                Console.WriteLine("請輸入數字運算符號(+、-、*、/):");
                string strOperate = Console.ReadLine();
                Console.WriteLine("請輸入數字B");
                string strNumB = Console.ReadLine();

                var oper = OperationFactory.CreateOperation(strOperate);
                oper.numA = Convert.ToDouble(strNumA);
                oper.numB = Convert.ToDouble(strNumB);
                Console.WriteLine("結果是:" + oper.GetResult());
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("輸入有錯:" + ex.Message);
            }
        }
Пример #19
0
        static void Main(string[] args)
        {
            Operation oper;

            oper         = OperationFactory.CreateOperate("+");
            oper.NumberA = 4;  oper.NumberB = 2;
            double result = oper.GetResult();

            Console.WriteLine(result);

            oper         = OperationFactory.CreateOperate("-");
            oper.NumberA = 4; oper.NumberB = 2;
            result       = oper.GetResult();
            Console.WriteLine(result);

            oper         = OperationFactory.CreateOperate("/");
            oper.NumberA = 4; oper.NumberB = 2;
            result       = oper.GetResult();
            Console.WriteLine(result);

            Console.ReadKey();
        }
Пример #20
0
        static void Main(string[] args)
        {
            try
            {
                Console.Write("Please input a number: ");
                double firstNumber = Convert.ToDouble(Console.ReadLine());
                Console.Write("Please input an operator (+, -, *, /): ");
                string inputOperator = Console.ReadLine();
                Console.Write("Please input the second number: ");
                double secondNumber = Convert.ToDouble(Console.ReadLine());

                Operation oper;
                oper              = OperationFactory.createOperator(inputOperator);
                oper.FirstNumber  = firstNumber;
                oper.SecondNumber = secondNumber;
                double result = oper.getResult();

                Console.WriteLine("The result of " + firstNumber + inputOperator + secondNumber + " is: " + result);
            }
            catch
            {
                Console.WriteLine("There is something wrong about your input.");
            }
        }
Пример #21
0
        static void Main(string[] args)
        {
            // 情境:當我有一個計算機程式 該如何以物件導向寫出來?

            /*
             * 1. 每次都會有兩個數字需要計算
             * 2. 有運算元 [+、-、*、/]
             * 3. 根據不同運算元產生實體,原本為一整個class進行switch,但是這樣耦合性過高,所以拆分成4個class將相同事情封裝到介面再個別實現
             * 4. 讓運算類別關聯簡單工廠類別,若以後需要增加需求只需要到工廠類別進行修改,使用者只需要創建實體並不需要理會內部怎麼操作。
             *
             */

            // 優點: 使用者不需要明確知道怎麼創建,只需要call CreateXXX(),甚至可以配合組態檔參數進行設定
            // 缺點: 當 Facotry 需要增加新的 Product 時,必然需要對 Factory 的程式碼進行變更。

            Operation op = OperationFactory.CreateOperate(Operater.Add);

            op.Number1 = 1;
            op.Number2 = 2;
            var result = op.GetResult();

            Console.WriteLine(result);
            Console.Read();
        }