Exemple #1
0
        /// <summary>
        /// 要素登録
        /// </summary>
        /// <param name="pItem">要素</param>
        /// <remarks>
        /// CalculatorItemBaseクラスおよび派生クラスを登録します。
        /// 登録する順番は数式に準じます。
        /// </remarks>
        public void Entry(CalculatorItemBase pItem)
        {
            if (pItem is CalculatorValueBase)
            {
                this.mStackValue.Push((CalculatorValue)pItem);
                return;
            }

            CalculatorOperatorBase opeThis = (CalculatorOperatorBase)pItem;

            // (
            if (opeThis.OperatorType == CalculatorOperatorBase.EnumOperatorType.Open)
            {
                this.mStackOperator.Push(opeThis);
                return;
            }

            // )
            if (opeThis.OperatorType == CalculatorOperatorBase.EnumOperatorType.Close)
            {
                while (true)
                {
                    CalculatorOperatorBase ope = this.mStackOperator.Pop();
                    ope.Calculation(this.mStackValue);

                    if (ope.OperatorType == CalculatorOperatorBase.EnumOperatorType.Open)
                    {
                        break;
                    }
                }
                return;
            }

            if (mStackOperator.Count == 0)
            {
                this.mStackOperator.Push(opeThis);
                return;
            }

            CalculatorOperatorBase opeBefore = mStackOperator.Peek();

            int priority = opeBefore.ComparePriority(opeThis);

            if (priority < 0)
            {
                this.mStackOperator.Push(opeThis);
                return;
            }

            opeBefore.Calculation(this.mStackValue);
            mStackOperator.Pop();

            this.mStackOperator.Push(opeThis);
        }
Exemple #2
0
        /// <summary>
        /// 計算結果取得
        /// </summary>
        /// <returns>CalculatorValue</returns>
        /// <remarks>
        /// 計算結果を返します。
        /// </remarks>
        public CalculatorValue GetAnswer()
        {
            while (this.mStackOperator.Count > 0)
            {
                CalculatorOperatorBase ope = this.mStackOperator.Pop();
                ope.Calculation(this.mStackValue);
            }

            if (this.mStackValue.Count != 1)
            {
                throw new InvalidArithmeticExpressionException("値がスタックに残っています");
            }

            return(this.mStackValue.Pop());
        }