using System; using System.Collections.Generic; using System.Diagnostics; using System.Net.Sockets; using System.Reflection.Emit; namespace MyApp { ///****************************(Classes)********************************/ class polyNode { public int coff; public int ex; public polyNode next; public polyNode(int co, int e) { next = null; coff = co; ex = e; } } class Polynomial { public polyNode head; public void add_term(int coff, int exp) { polyNode p = new polyNode(coff, exp); if (head == null || head.ex < exp) { p.next = head; head = p; return; } polyNode tmp = head; while(tmp.next != null && tmp.next.ex > exp) { tmp = tmp.next; } if(tmp.next != null && tmp.next.ex == exp) { tmp.next.coff += coff; return; } p.next = tmp.next; tmp.next = p; } } class Add { private Polynomial result = new Polynomial(); public Add(Polynomial p1, Polynomial p2) { polyNode tmp1 = p1.head , tmp2 = p2.head; while (tmp1 != null || tmp2 != null) { if (tmp1 == null) { result.add_term(tmp2.coff, tmp2.ex); tmp2 = tmp2.next; } else if (tmp2 == null) { result.add_term(tmp1.coff, tmp1.ex); tmp1 = tmp1.next; } else if (tmp1.ex == tmp2.ex) { int ncoff = tmp1.coff + tmp2.coff; result.add_term(ncoff, tmp1.ex); tmp1 = tmp1.next; tmp2 = tmp2.next; } else if (tmp1.ex > tmp2.ex) { result.add_term(tmp1.coff, tmp1.ex); tmp1 = tmp1.next; } else { result.add_term(tmp2.coff, tmp2.ex); tmp2 = tmp2.next; } } } public void display() { polyNode tmp = result.head; if (tmp == null) { Console.WriteLine("Empty"); return; } while (tmp != null) { Console.Write("{0}x^{1} ", tmp.coff, tmp.ex); tmp = tmp.next; if (tmp != null) { Console.Write("+ "); } } } } /********************************************************************/ internal class Program { ///-----------------------(funcoins )--------------------------------/ //------------------------------------------------------------------------/ static void Main() { Polynomial p1 = new Polynomial(); p1.add_term(2, 3); p1.add_term(3, 2); p1.add_term(4, 1); Polynomial p2 = new Polynomial(); p2.add_term(2, 0); p2.add_term(3, 5); p2.add_term(7, 1); Add add = new Add(p1, p2); add.display(); } } }