Ejemplo n.º 1
0
        /// <summary>
        /// 四則運算事件
        /// </summary>
        /// <param name="newNumber">待處理數字</param>
        /// <param name="nextOperator">下次要執行的運算子</param>
        public void OperateEvent(NumberField newNumber, IOperator nextOperator)
        {
            //RightNumber一定會變null
            //無限大時,Expression的處理被Compute負責了(?)
            if (newNumber.isInput == false)
            {
                //如果等號後input, LeftNumber不變,並將RightNumber清空
                RightNumber = null;
                Operator    = nextOperator;
            }
            else if (LeftNumber == null && Operator != null && RightNumber != null)
            {
                //等號後Input + Interrupt的狀況
                LeftNumber  = newNumber;
                Operator    = nextOperator;
                RightNumber = null;
            }
            else if (LeftNumber == null && Operator == null && RightNumber == null)
            {
                //第一次輸進數字,不做運算
                LeftNumber = newNumber;
                Operator   = nextOperator;
            }
            else if (LeftNumber != null && Operator != null && RightNumber == null)
            {
                //左數 四則運算 右數
                LeftNumber = Compute(LeftNumber, newNumber);
                Operator   = nextOperator;
            }
            else if (LeftNumber != null && Operator != null && RightNumber != null)
            {
                //negate case
                LeftNumber  = newNumber;
                Operator    = nextOperator;
                RightNumber = null;
            }

            //這個階段若LeftNumber.NaN 為true ,代表除到0了
            if (!LeftNumber.NaN)
            {
                Expression = $"{LeftNumber.ToString()} {nextOperator.Mark()}";
            }

            //無限大處理,後續還未做
            if (LeftNumber.NaN)
            {
                Expression += $" 0 {Operator.Mark()}";
            }
        }