public Factor(string factor, string[] ids, ParsTreeNode parent) : base(factor, ids, parent) { this.Value = factor; double value; if (double.TryParse(factor, out value)) { this.Expansion = FactorExpansion.Number; } else { if (factor.StartsWith("(") && factor.EndsWith(")")) { this.Expansion = FactorExpansion.WrappedExpression; this.WrappedExpression = new Expression(factor.Substring(1, factor.Length - 2), ids, this); } else if (Function.IsFunction(factor, ids)) { this.Expansion = FactorExpansion.Function; this.Function = new Function(factor, ids, this); } else if (factor.StartsWith("-")) { this.Expansion = FactorExpansion.MinuFactor; this.InnerFactor = new Factor(factor.Substring(1, factor.Length - 1), ids, this); } else { this.Expansion = FactorExpansion.ID; } } }
public Function(string function, string[] ids, ParsTreeNode parent) : base(function, ids, parent) { foreach (string func in Enum.GetNames(typeof(FunctionEnum))) { if (function.ToLower().StartsWith(func.ToLower())) { Func = (FunctionEnum)Enum.Parse(typeof(FunctionEnum), func); this.Term = new Term(function.Substring(func.Length), ids, this); break; } } }
public Term(string term, string[] ids, ParsTreeNode parent) : base(term, ids, parent) { this.Value = term; int oprIndx = -1; int brackets = 0; for (int i = term.Length - 1; i > 0; i--) { if ((term[i] == '*' || term[i] == '/' || term[i] == '^') && (brackets == 0)) { oprIndx = i; break; } else if (term[i] == ')') { brackets++; } else if (term[i] == '(') { brackets--; } } if (oprIndx > 0) { string subterm, factor; char opr = term[oprIndx]; subterm = term.Substring(0, oprIndx); factor = term.Substring(oprIndx + 1); this.Factor = new Factor(factor, ids, this); this.SubTerm = new Term(subterm, ids, this); if (opr == '*') { this.Expansion = TermExpansion.TermMulFactor; } else if (opr == '/') { this.Expansion = TermExpansion.TermDivFactor; } else { this.Expansion = TermExpansion.TermPowFactor; } } else { this.Expansion = TermExpansion.Factor; this.Factor = new Factor(term, ids, this); } }
public Expression(string expr, string[] ids, ParsTreeNode parent) : base(expr, ids, parent) { expr = expr.Replace(" ", ""); int oprIndx = -1; int brackets = 0; for (int i = expr.Length - 1; i > 0; i--) { if (((expr[i] == '-' && !IsOperator(expr[i - 1])) || expr[i] == '+') && (brackets == 0)) { oprIndx = i; break; } else if (expr[i] == ')') { brackets++; } else if (expr[i] == '(') { brackets--; } } if (oprIndx > 0) { string subExpr, term; char opr; subExpr = expr.Substring(0, oprIndx); term = expr.Substring(oprIndx + 1); opr = expr[oprIndx]; this.Term = new Term(term, ids, this); this.SubExpression = new Expression(subExpr, ids, this); if (opr == '-') { Expansion = ExpressionExpansion.ExpressionMinusTerm; } else { Expansion = ExpressionExpansion.ExpressionPlusTerm; } } else { Expansion = ExpressionExpansion.Term; this.Term = new Term(expr, ids, this); } }
protected ParsTreeNode(string value, string[] ids, ParsTreeNode parnet) { this.Value = value.Replace(" ", ""); this.Parent = parnet; this.IDs = ids; }