fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Net.Sockets;
  5. using System.Reflection.Emit;
  6.  
  7.  
  8. namespace MyApp
  9. {
  10. ///****************************(Classes)********************************/
  11. class polyNode
  12. {
  13. public int coff;
  14. public int ex;
  15. public polyNode next;
  16.  
  17. public polyNode(int co, int e)
  18. {
  19. next = null;
  20. coff = co;
  21. ex = e;
  22. }
  23. }
  24.  
  25. class Polynomial
  26. {
  27. public polyNode head;
  28.  
  29. public void add_term(int coff, int exp)
  30. {
  31. polyNode p = new polyNode(coff, exp);
  32. if (head == null || head.ex < exp)
  33. {
  34. p.next = head;
  35. head = p;
  36. return;
  37. }
  38. polyNode tmp = head;
  39. while(tmp.next != null && tmp.next.ex > exp)
  40. {
  41. tmp = tmp.next;
  42. }
  43. if(tmp.next != null && tmp.next.ex == exp)
  44. {
  45. tmp.next.coff += coff;
  46. return;
  47. }
  48. p.next = tmp.next;
  49. tmp.next = p;
  50. }
  51. }
  52.  
  53. class Add
  54. {
  55. private Polynomial result = new Polynomial();
  56.  
  57. public Add(Polynomial p1, Polynomial p2)
  58. {
  59. polyNode tmp1 = p1.head , tmp2 = p2.head;
  60. while (tmp1 != null || tmp2 != null)
  61. {
  62. if (tmp1 == null)
  63. {
  64. result.add_term(tmp2.coff, tmp2.ex);
  65. tmp2 = tmp2.next;
  66. }
  67. else if (tmp2 == null)
  68. {
  69. result.add_term(tmp1.coff, tmp1.ex);
  70. tmp1 = tmp1.next;
  71. }
  72. else if (tmp1.ex == tmp2.ex)
  73. {
  74. int ncoff = tmp1.coff + tmp2.coff;
  75. result.add_term(ncoff, tmp1.ex);
  76. tmp1 = tmp1.next;
  77. tmp2 = tmp2.next;
  78. }
  79. else if (tmp1.ex > tmp2.ex)
  80. {
  81. result.add_term(tmp1.coff, tmp1.ex);
  82. tmp1 = tmp1.next;
  83. }
  84. else
  85. {
  86. result.add_term(tmp2.coff, tmp2.ex);
  87. tmp2 = tmp2.next;
  88.  
  89. }
  90. }
  91. }
  92.  
  93. public void display()
  94. {
  95. polyNode tmp = result.head;
  96. if (tmp == null)
  97. {
  98. Console.WriteLine("Empty");
  99. return;
  100. }
  101. while (tmp != null)
  102. {
  103. Console.Write("{0}x^{1} ", tmp.coff, tmp.ex);
  104. tmp = tmp.next;
  105. if (tmp != null)
  106. {
  107. Console.Write("+ ");
  108. }
  109. }
  110. }
  111. }
  112. /********************************************************************/
  113. internal class Program
  114. {
  115.  
  116. ///-----------------------(funcoins )--------------------------------/
  117.  
  118.  
  119. //------------------------------------------------------------------------/
  120.  
  121. static void Main()
  122. {
  123. Polynomial p1 = new Polynomial();
  124. p1.add_term(2, 3);
  125. p1.add_term(3, 2);
  126. p1.add_term(4, 1);
  127. Polynomial p2 = new Polynomial();
  128. p2.add_term(2, 0);
  129. p2.add_term(3, 5);
  130. p2.add_term(7, 1);
  131. Add add = new Add(p1, p2);
  132. add.display();
  133. }
  134.  
  135. }
  136. }
  137.  
  138.  
Success #stdin #stdout 0.05s 30772KB
stdin
Standard input is empty
stdout
3x^5 + 2x^3 + 3x^2 + 11x^1 + 2x^0