/// <summary> /// Adds a factor. /// eg. {2, x^2}, {4} + {-3, y}, {2} /// </summary> /// <param name="factor"></param> public void Add(Factor factor) { Factor rFactor = this; Factor lFactor = factor; Factor result = new Factor(); if (lFactor.Terms.Count > 0) { for (int i = lFactor.Terms.Count - 1; i >= 0; i--) { bool done = false; if (rFactor.Terms.Count > 0) { for (int j = rFactor.Terms.Count - 1; j >= 0; j--) { if (lFactor.Terms [i].VariablesToString().Equals(rFactor.Terms [j].VariablesToString())) { lFactor.Terms [i].Coefficient += rFactor.Terms [j].Coefficient; if (lFactor.Terms [i].Coefficient != 0) { result.Add(lFactor.Terms [i]); } lFactor.Terms.RemoveAt(i); rFactor.Terms.RemoveAt(j); done = true; break; } } } if (!done) { result.Add(lFactor.Terms [i]); lFactor.Terms.RemoveAt(i); } } } if (rFactor.Terms.Count > 0) { foreach (var rTerm in rFactor.Terms) { result.Add(rTerm); } } this.Terms = result.Terms; }
/// <summary> /// Divide by a factor. /// eg. (2x + 1) / (3x + 2) /// </summary> /// <param name="t">The factor to be devided by.</param> public void Divide(Factor t) { Factor result = new Factor(); Factor rTerm; foreach (var factor in Terms) { rTerm = t.Clone(); rTerm.Divide(factor); result.Add(rTerm); } Terms = result.Terms; }
/// <summary> /// Multiply a factor by a factor. /// (2x + 1) * (x + 3) /// </summary> /// <param name="factor"></param> public void Multiply(Factor factor) { Factor result = new Factor(); Factor rTerm; foreach (var f in Terms) { rTerm = factor.Clone(); rTerm.Multiply(f); result.Add(rTerm); } Terms = result.Terms; }
//SumOp -> + Term SumOp //SumOp -> - Term SumOp //SumOp -> EPSILON Factor SumOp(Factor t) { Factor t2; switch (lookahead.type) { case TokenType.ADD: GetNextToken(); t2 = Term(); t.Add(t2); return(SumOp(t)); case TokenType.SUB: GetNextToken(); t2 = Term(); t.Subtract(t2); return(SumOp(t)); default: return(t); } }
/// <summary> /// Adds a factor. /// eg. {2, x^2}, {4} + {-3, y}, {2} /// </summary> /// <param name="factor"></param> public void Add(Factor factor) { Factor rFactor = this; Factor lFactor = factor; Factor result = new Factor (); if (lFactor.Terms.Count > 0) for (int i = lFactor.Terms.Count - 1; i >= 0; i--) { bool done = false; if (rFactor.Terms.Count > 0) for (int j = rFactor.Terms.Count - 1; j >= 0; j--) { if (lFactor.Terms [i].VariablesToString ().Equals (rFactor.Terms [j].VariablesToString ())) { lFactor.Terms [i].Coefficient += rFactor.Terms [j].Coefficient; if (lFactor.Terms [i].Coefficient != 0) result.Add (lFactor.Terms [i]); lFactor.Terms.RemoveAt (i); rFactor.Terms.RemoveAt (j); done = true; break; } } if (!done) { result.Add (lFactor.Terms [i]); lFactor.Terms.RemoveAt (i); } } if (rFactor.Terms.Count > 0) foreach (var rTerm in rFactor.Terms) { result.Add (rTerm); } this.Terms = result.Terms; }
//SumOp -> + Term SumOp //SumOp -> - Term SumOp //SumOp -> EPSILON Factor SumOp(Factor t) { Factor t2; switch (lookahead.type) { case TokenType.ADD: GetNextToken (); t2 = Term (); t.Add (t2); return SumOp (t); case TokenType.SUB: GetNextToken (); t2 = Term (); t.Subtract (t2); return SumOp (t); default: return t; } }
/// <summary> /// Multiply a factor by a factor. /// (2x + 1) * (x + 3) /// </summary> /// <param name="factor"></param> public void Multiply(Factor factor) { Factor result = new Factor (); Factor rTerm; foreach (var f in Terms) { rTerm = factor.Clone (); rTerm.Multiply (f); result.Add (rTerm); } Terms = result.Terms; }
/// <summary> /// Divide by a factor. /// eg. (2x + 1) / (3x + 2) /// </summary> /// <param name="t">The factor to be devided by.</param> public void Divide(Factor t) { Factor result = new Factor (); Factor rTerm; foreach (var factor in Terms) { rTerm = t.Clone (); rTerm.Divide (factor); result.Add (rTerm); } Terms = result.Terms; }