//주어진 Polynomial 의 출력을 제어하는 알고리즘을 작성하시오 public Polynomial Reverse() { Polynomial s = new Polynomial(); for (Term t = first; t != null; t = t.next) { double coef = t.coef; int exp = t.exp; s.AddTerm(coef, exp); } return s; }
public Polynomial Simplify() { Polynomial resPoly = new Polynomial(); for (Term t = first; t != null; t = t.next) { double coef = t.coef; int exp = t.exp; for (Term s = t.next; s != null; s = s.next) { if (exp == s.exp) { coef += s.coef; s.coef = 0; } } if (coef != 0) // Term 을 삭제할 필요가 있을 때 coefficient 를 0 으로 지정한 후에 일괄삭제하는 방식으로 작성하시오 { resPoly.AddTerm(coef, exp); } } return resPoly; }
static void Main(string[] args) { Polynomial p = new Polynomial(); p.AddTerm(1, 2); p.AddTerm(-1, 1); p.AddTerm(-1, 1); p.AddTerm(-1, 0); p.PrintPoly(); Console.WriteLine("-------------------"); p.Simplify().PrintPoly(); }
public Polynomial Simplify() { Polynomial resPoly = new Polynomial(); for (Term t = first; t != null; t = t.next) { double coef = t.coef; int exp = t.exp; for (Term s = t.next; s != null; s = s.next) { if (exp == s.exp) { coef += s.coef; s.coef = 0; } } if (coef != 0) { resPoly.AddTerm(coef, exp); } } return resPoly; }