public string GetEquationSide(EquationSide side) { string pattern = @"(?<left>.+)=(?<right>.+)"; Match m = Regex.Match(AsString, pattern); if (side == EquationSide.Left) { return(m.Groups["left"].Value); } else { return(m.Groups["right"].Value); } }
public List <Term> Terms(EquationSide side) { string pattern = Term.Pattern; string equation = GetEquationSide(side); MatchCollection matches = Regex.Matches(equation, pattern, RegexOptions.None); List <Term> terms = new List <Term>(); foreach (Match m in matches) { terms.Add(new Term(m)); } return(terms); }
/* * public string AddTerms(string input) * { * //expecting input to be already in correct order * * string pattern = @"[\+\-]?((?<coef>[0-9]+)((?<variable>[a-z]+))(\^((?<power>[0-9]))))"; * * RegexOptions options = RegexOptions.None; * * MatchCollection matches = Regex.Matches(input, pattern, options); * * string result = ""; * * for (int i = int.Parse(matches[0].Groups["power"].Value); i >= 0; i--) * { * IEnumerable<Match> query = * from match in matches.Cast<Match>() * where int.Parse(match.Groups["power"].Value) == i * select match; * * int value = 0; * * foreach (Match m in query) * { * value += int.Parse(m.Groups["coef"].Value); * } * * if (value != 0) * { * result += string.Format("{0}x^{1}", value, i); * } * * } * Console.WriteLine(result); * return result; * * } */ /* * public List<Term> GetTermsOfPower(List<Term> terms, int power) * { * List<Term> result = new List<Term>(); * * foreach(Term t in terms) * { * if(t.Power == power) * { * result.Add(t); * } * } * * return result; * } */ public Term AddTermsOfPower(EquationSide side, char var, int power) { int coef = 0; List <Term> terms = Terms(side); var query = from t in terms where t.Power == power where t.VariableSymbol == var select t; foreach (Term t in query) { coef += t.Coef; } Term result = new Term(coef, var, power); return(result); }
public void AddTermToOneSide(EquationSide side, Term term) { string sideStr = GetEquationSide(side); string result = sideStr; if (sideStr != "0") { if (term.Coef < 0) { //result += "-"; } else { result += "+"; } } else { result = ""; } result += term.ToStringSimplified(); string left, right; if (side == EquationSide.Left) { left = result; right = GetEquationSide(EquationSide.Right); } else { left = GetEquationSide(EquationSide.Left); right = result; } AsString = string.Format("{0}={1}", left, right); }
/* * public static string ConvertToStandardForm(string input) * { * string result; * * result = OrderByPower(input); * result = AddTerms(result); * * return result; * * } */ public void OrderTermsByPower(EquationSide side) { AsString = TermListToEquation(Terms(side).OrderByDescending(t => t.Power).ToList()).AsString; }