Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ParenthesisToken"/> struct.
        /// </summary>
        /// <param name="parenthesisType">Type of the parenthesis.</param>
        public ParenthesisToken(ParenthesisType parenthesisType)
        {
            if (parenthesisType != ParenthesisType.Left && parenthesisType != ParenthesisType.Right)
            {
                throw new ArgumentException("The parenthesis type must be either Left or Right.", "parenthesisType");
            }

            this._parenthesisType = parenthesisType;
            this._tokenType = (parenthesisType == ParenthesisType.Left) ? TokenType.LeftParenthesis : TokenType.RightParenthesis;
        }
 public ParenthesisView(ParenthesisType type)
     : base(0, 0, 10, 10)
 {
     Type = type;
 }
Пример #3
0
        public void Parse(string input)
        {
            int             len      = input.Length;
            int             i        = 0;
            Operator        opr      = new Operator(OperatorType.Plus);
            Operand         operand  = new Operand(0);
            OperatorType    op_type  = OperatorType.None;
            ParenthesisType par_type = ParenthesisType.None;

            if (input == "")
            {
                return;
            }

            for (; i < len; i++)
            {
                char c = input[i];
                if (c == '.' || c == ',')
                {
                    //if (i == 0 || !Char.IsDigit(input[i - 1]))
                    //    _elements.Add(new Operand(0));
                    continue;
                }
                op_type  = GetOpType(c);
                par_type = GetParType(c);
                if (op_type != OperatorType.None)
                {
                    opr = new Operator(op_type);
                    string term1 = input.Substring(0, i).Trim();
                    if (term1 == string.Empty || term1 == "," || term1 == ".")
                    {
                        if (op_type == OperatorType.Multiply || op_type == OperatorType.Divide)
                        {
                            operand = new Operand(1);
                        }
                        else if (op_type == OperatorType.Minus)
                        {
                            opr     = new Operator(OperatorType.Multiply);
                            operand = new Operand(-1);
                        }
                        else
                        {
                            operand = new Operand(0);
                        }
                    }
                    else
                    {
                        operand = new Operand(Double.Parse(term1, CultureInfo.InvariantCulture));
                    }
                    break;
                }
                else if (par_type != ParenthesisType.None)
                {
                    string term1 = input.Substring(0, i);
                    if (term1 == string.Empty)
                    {
                        operand = null;
                    }
                    else
                    {
                        operand = new Operand(Double.Parse(term1, CultureInfo.InvariantCulture));
                    }
                    break;
                }
            }

            if (par_type == ParenthesisType.Close)
            {
                if (operand != null)
                {
                    _elements.Add(operand);
                }
                else if (_elements.Count == 0 || _elements[_elements.Count - 1].ElementType != ElementType.Operand)
                {
                    _elements.Add(new Operand(0));
                }
                _elements.Add(new Parenthesis(par_type));
                string new_input = input.Substring(i + 1).Trim();

                if (new_input != string.Empty)
                {
                    op_type  = GetOpType(new_input[0]);
                    par_type = GetParType(new_input[0]);
                    if (par_type != ParenthesisType.None)
                    {
                        Parse(new_input);
                    }
                    else if (op_type != OperatorType.None)
                    {
                        _elements.Add(new Operator(op_type));
                        new_input = input.Substring(i + 2);
                        Parse(new_input);
                    }
                    else
                    {
                        _elements.Add(new Operator(OperatorType.Multiply));
                        Parse(new_input);
                    }
                }
            }
            else if (par_type == ParenthesisType.Open)
            {
                if (operand != null)
                {
                    _elements.Add(operand);
                    _elements.Add(new Operator(OperatorType.Multiply));
                }
                _elements.Add(new Parenthesis(par_type));
                string new_input = input.Substring(i + 1);
                Parse(new_input);
            }
            else if (op_type == OperatorType.None) // finish
            {
                double val;
                try
                {
                    val = Double.Parse(input, CultureInfo.InvariantCulture);
                }
                catch
                {
                    val = 0;
                }
                _elements.Add(new Operand(val));
            }
            else
            {
                //if (_elements.Count == 0 || _elements[_elements.Count - 1].ElementType != ElementType.Operand)
                //{
                //    _elements.Add(new Operand(0));
                //}
                _elements.Add(operand);
                _elements.Add(opr);
                string new_input = input.Substring(i + 1).Trim();
                if (new_input == string.Empty)
                {
                    if (opr.Priority == OperatorPriority.High)
                    {
                        _elements.Add(new Operand(1));
                    }
                    else if (opr.Priority == OperatorPriority.Medium)
                    {
                        _elements.Add(new Operand(1));
                    }
                    else
                    {
                        _elements.Add(new Operand(0));
                    }
                }
                else
                {
                    //Parse(new_input);
                    par_type = GetParType(new_input[0]); // parenthesis?
                    if (par_type == ParenthesisType.Open)
                    {
                        _elements.Add(new Parenthesis(par_type));
                        new_input = input.Substring(i + 2).Trim();
                    }
                    else if (par_type == ParenthesisType.Close)
                    {
                        if (opr.Priority == OperatorPriority.Medium)
                        {
                            _elements.Add(new Operand(1));
                        }
                        else
                        {
                            _elements.Add(new Operand(0));
                        }
                        _elements.Add(new Parenthesis(par_type));
                        new_input = input.Substring(i + 2).Trim();

                        if (new_input != string.Empty)
                        {
                            op_type  = GetOpType(new_input[0]);
                            par_type = GetParType(new_input[0]);
                            if (par_type != ParenthesisType.None)
                            {
                                Parse(new_input);
                            }
                            else if (op_type != OperatorType.None)
                            {
                                _elements.Add(new Operator(op_type));
                                new_input = input.Substring(i + 2);
                                Parse(new_input);
                            }
                            else
                            {
                                _elements.Add(new Operator(OperatorType.Multiply));
                                Parse(new_input);
                            }
                        }
                    }
                    if (new_input != string.Empty)
                    {
                        Parse(new_input);
                    }
                }
            }
        }
Пример #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ParenthesisToken"/> class.
 /// </summary>
 protected ParenthesisToken(ParenthesisType parenthesisType, TextRange textRange) : base(textRange)
 {
     ParenthesisType = parenthesisType;
 }
Пример #5
0
 /// <summary>
 /// Initializes a new instance of the Parenthesis class.
 /// </summary>
 /// <param name="parenthesisType">The parenthesis type this instance represents.</param>
 public Parenthesis(ParenthesisType parenthesisType)
 {
     this.ParenthesisType = parenthesisType;
 }
Пример #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ParenthesisToken"/> class.
 /// </summary>
 protected ParenthesisToken(ParenthesisType parenthesisType, TextRange textRange) : base(textRange)
 {
     ParenthesisType = parenthesisType;
 }
Пример #7
0
 /// <summary>
 /// Initializes a new instance of the Parenthesis class.
 /// </summary>
 /// <param name="parenthesisType">The parenthesis type this instance represents.</param>
 public Parenthesis(ParenthesisType parenthesisType)
 {
     this.ParenthesisType = parenthesisType;
 }
Пример #8
0
 public Parenthesis(ParenthesisType type) : base(ElementType.Parenthesis)
 {
     _type = type;
 }