Beispiel #1
0
        public virtual void GetPolishNotation(List <Token> source, Stack <Token> temp, Queue <Token> rezult)
        {
            foreach (Token token in source)
            {
                if (token is TokenOperand)
                {
                    //rezult.Enqueue
                    rezult.Enqueue(token);
                }
                if (token is TokenBracket)
                {
                    if ((token as TokenBracket).isOpened)
                    {
                        temp.Push(token);
                    }
                    else
                    {
                        while (!(temp.Peek() is TokenBracket))
                        {
                            rezult.Enqueue(temp.Pop());
                        }
                        temp.Pop();
                    }
                }

                if (token is TokenOperation && !(token is TokenBracket))
                {
                    if (temp.Count != 0)
                    {
                        TokenOperation operTop = temp.Peek() as TokenOperation;
                        if (operTop.Priority < (token as TokenOperation).Priority)
                        {
                            temp.Push(token);
                        }
                        else
                        {
                            TokenOperation oper = temp.Pop() as TokenOperation;
                            rezult.Enqueue(oper);
                            while (temp.Count != 0 && (temp.Peek() as TokenOperation).Priority >= (token as TokenOperation).Priority)
                            {
                                oper = temp.Pop() as TokenOperation;
                                rezult.Enqueue(oper);
                            }
                            temp.Push(token);
                        }
                    }

                    else
                    {
                        TokenOperation operTop2 = token as TokenOperation;
                        temp.Push(operTop2);
                    }
                }
            }
            while (temp.Count != 0)
            {
                TokenOperation oper = temp.Pop() as TokenOperation;
                rezult.Enqueue(oper);
            }
        }
Beispiel #2
0
        public virtual int Calc(List <Token> source)
        {
            Stack <Token> temp        = new Stack <Token>();
            Queue <Token> polNotation = new Queue <Token>();

            GetPolishNotation(source, temp, polNotation);
            temp = new Stack <Token>();
            while (polNotation.Count != 0)
            {
                if (polNotation.Peek() is TokenOperand)
                {
                    temp.Push(polNotation.Dequeue());
                }
                else
                {
                    TokenOperation operation = polNotation.Dequeue() as TokenOperation;
                    ExecuteOperation(temp, operation);
                }
            }
            if (temp.Count == 1)
            {
                int rez = Convert.ToInt32(temp.Pop().Value);
                return(rez);
            }
            else
            {
                throw new CalcException("количество значений в результирующем стэке не равно 1");
            }
        }
Beispiel #3
0
 protected override void SetOperationPriority(TokenOperation oper)
 {
     if (oper.Value == "^")
     {
         oper.Priority = 20;
     }
     else
     {
         base.SetOperationPriority(oper);
     }
 }
Beispiel #4
0
        protected virtual void SetOperationPriority(TokenOperation oper)
        {
            switch (oper.Value)
            {
            case "+":
            case "-":
                oper.Priority = 10;
                break;

            case "*":
            case "/":
                oper.Priority = 20;
                break;

            case "(":
            case ")":
                oper.Priority = 5;
                break;

            default:
                break;
            }
        }
Beispiel #5
0
        public virtual Token GetToken(string expression)
        {
            string       expressionNew = expression.Replace(" ", "");
            TokenOperand operand       = new TokenOperand();

            operand.Value = "";
            TokenOperation operation = new TokenOperation();

            operation.Value = "";
            TokenBracket tokenBracket = new TokenBracket();

            tokenBracket.Value = "";
            string tempToken = "";

            for (int i = 0; i < expressionNew.Length; i++)
            {
                Char cCh = expressionNew[i];
                if (Char.IsDigit(cCh))
                {
                    operand.Value = String.Format("{0}{1}", operand.Value, cCh);
                }
                else
                {
                    if (CheckEligibleOperation(cCh))
                    {
                        if (operand.Value.ToString() != "")
                        {
                            break;
                        }
                        if (cCh == '(')
                        {
                            tokenBracket.isOpened = true;
                            tokenBracket.Value    = String.Format("{0}", cCh);
                            break;
                        }
                        if (cCh == ')')
                        {
                            tokenBracket.isOpened = false;
                            tokenBracket.Value    = String.Format("{0}", cCh);
                            break;
                        }
                        else
                        {
                            operation.Value = String.Format("{0}", cCh);
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (operation.Value != "")
            {
                SetOperationPriority(operation);
                return(operation);
            }
            if (operand.Value != "")
            {
                return(operand);
            }
            if (tokenBracket.Value != "")
            {
                SetOperationPriority(tokenBracket);
                return(tokenBracket);
            }
            else
            {
                throw new CalcBadSyntaxException();
            }
        }
Beispiel #6
0
        public virtual void ExecuteOperation(Stack <Token> temp, TokenOperation operation)
        {
            TokenOperand op2 = temp.Pop() as TokenOperand;
            TokenOperand op1 = temp.Pop() as TokenOperand;

            switch (operation.Value.ToString())
            {
            case "+":
            {
                int r = Convert.ToInt32(op1.Value) + Convert.ToInt32(op2.Value);
                temp.Push(new TokenOperand()
                    {
                        Value = r
                    });
                break;
            }

            case "-":
            {
                int r = Convert.ToInt32(op1.Value) - Convert.ToInt32(op2.Value);
                temp.Push(new TokenOperand()
                    {
                        Value = r
                    });
                break;
            }

            case "*":
            {
                int r = Convert.ToInt32(op1.Value) * Convert.ToInt32(op2.Value);
                temp.Push(new TokenOperand()
                    {
                        Value = r
                    });
                break;
            }

            case "/":
            {
                int r = Convert.ToInt32(op1.Value) / Convert.ToInt32(op2.Value);
                temp.Push(new TokenOperand()
                    {
                        Value = r
                    });
                break;
            }

            case "^":
            {
                int r = (int)Math.Round(Math.Pow(Convert.ToInt32(op1.Value), Convert.ToInt32(op2.Value)), 0);
                temp.Push(new TokenOperand()
                    {
                        Value = r
                    });
                break;
            }

            default:
                break;
            }
        }