/// <summary> /// Determines whether the specified <see cref="System.Object" />, is equal to this instance. /// </summary> /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param> /// <returns> /// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>. /// </returns> public override bool Equals(object obj) { UnaryOperator uo = obj as UnaryOperator; if (uo != null) { return(uo.sign == this.sign && uo.left.Equals(this.left)); } return(false); }
/// <summary> /// проверяет, является ли заданное выражение атомарным (константа, переменная, оператор "-"). /// </summary> /// <param name="exp">Выражение.</param> /// <returns>Возвращает <c>true</c>, если заданное выражение является атомарным.</returns> private bool IsAtomic(Expression exp) { UnaryOperator uo = exp as UnaryOperator; if (uo != null) { return(uo.Left is UnaryOperator || (uo.Left as BinaryOperator != null && ((BinaryOperator)uo.Left).Sign == "-")); } BinaryOperator bo = exp as BinaryOperator; if (bo != null && bo.Sign == "-") { return(true); } return(exp is Constant || exp is Variable); }
/// <summary> /// Применяет логическое НЕ к заданному выражению. /// </summary> /// <param name="expression">Исходное выражение.</param> /// <returns>Возвращает НЕ(выражение).</returns> private Expression Reverse(Expression expression) { Expression e = expression.Copy(); BinaryOperator bo = e as BinaryOperator; if (bo != null) { switch (bo.Sign) { case ("&"): { bo.Sign = "|"; bo.Action = binaryOperators["|"].Action; bo.Left = Reverse(bo.Left); bo.Right = Reverse(bo.Right); break; } case ("|"): { bo.Sign = "&"; bo.Action = binaryOperators["&"].Action; bo.Left = Reverse(bo.Left); bo.Right = Reverse(bo.Right); break; } case ("-"): { UnaryOperator uo = (UnaryOperator)unaryOperators["!"].Clone(); uo.Left = bo; return(uo); } } return(bo); } else { UnaryOperator uo = expression as UnaryOperator; if (uo.Sign == "!") { return(uo.Left); } } return(e); }
/// <summary> /// Восстанавливает дерево выражения. /// </summary> /// <returns>Возвращает готовое выражение.</returns> private Expression ConstructExpressionTree() { Expression currExp = null; Stack <Expression> stack = new Stack <Expression>(); while (output.Count > 0) { currExp = output.Dequeue(); FunctionCall func = currExp as FunctionCall; if (func != null) //function on the top (requieres some arguments) { for (int i = func.ArgumentsCount - 1; i >= 0; i--) { func.Arguments[i] = stack.Pop(); } } else { UnaryOperator so = currExp as UnaryOperator; if (so != null) { so.Left = stack.Pop(); } else { BinaryOperator bo = currExp as BinaryOperator; if (bo != null) { bo.Right = stack.Pop(); bo.Left = stack.Pop(); } } } stack.Push(currExp); } if (stack.Count > 1) { throw new ArgumentException("provided string contains incorrect expression."); } return(stack.Peek()); }
/// <summary> /// Проверяет, является ли заданная дизъюнкция атомарной. /// </summary> /// <param name="bo">Дихъюнкция.</param> /// <returns>Возвращет <c>true</c>, если дизъюнкция является атомарной.</returns> private bool IsCNFOr(BinaryOperator bo) { if (bo.Sign != "|") { return(false); } Stack <Expression> stack = new Stack <Expression>(); stack.Push(bo.Left); stack.Push(bo.Right); while (stack.Count > 0) { Expression currExp = stack.Pop(); BinaryOperator sbo = currExp as BinaryOperator; if (sbo != null) { if (sbo.Sign == "&") { return(false); } stack.Push(sbo.Left); stack.Push(sbo.Right); } else { UnaryOperator uo = currExp as UnaryOperator; if (uo != null && uo.Sign == "!") { if (!IsAtomic(uo.Left)) { return(false); } } } } return(true); }
/// <summary> /// Creates a new object that is a copy of the current instance. /// </summary> /// <returns> /// A new object that is a copy of this instance. /// </returns> public override object Clone() { UnaryOperator uo = new UnaryOperator(action, sign, associativity, precendence); return(uo); }