ellapt

T9.11.AddPolynomials

Jan 20th, 2013
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. using System;
  2.  
  3. class AddPolynomials
  4. {
  5. static void AddCoeff(int c1, int c2, string[][] bigDoubleArray)
  6. {
  7. int max=Math.Max(c1,c2);
  8. int min=Math.Min(c1,c2);
  9. double temp = 0;
  10.  
  11. for (int i = 0; i < min; i++)
  12. {
  13. temp=Convert.ToDouble(bigDoubleArray[0][i])+Convert.ToDouble(bigDoubleArray[1][i]);
  14. bigDoubleArray[2][i]=temp.ToString();
  15. }
  16. if (c1 == c2)
  17. {
  18. return;
  19. }
  20. else
  21. {
  22. if (c1 == max)
  23. {
  24. for (int i = min; i <= max; i++)
  25. {
  26. temp = Convert.ToDouble(bigDoubleArray[0][i]);
  27. bigDoubleArray[2][i] = temp.ToString();
  28. }
  29. }
  30. else if (c2 == max)
  31. {
  32. for (int i = min; i <= max; i++)
  33. {
  34. temp = Convert.ToDouble(bigDoubleArray[1][i]);
  35. bigDoubleArray[2][i] = temp.ToString();
  36. }
  37. }
  38. }
  39. return;
  40. }
  41.  
  42. static void PrintPolynomial(int n, int row, string[][] jag)
  43. {
  44. string outStr;
  45. if (n > 2)
  46. {
  47. outStr = ((jag[row][0] == "0") ? "" : (jag[row][0] + "X↑" + (n - 1)));
  48. Console.Write(outStr);
  49. }
  50. for (int i = 1; i < n - 2; i++)
  51. {
  52. outStr=(jag[row][i] == "0") ? "" : (((Convert.ToDouble(jag[row][i]) > 0) ? "+" : ""))
  53. + jag[row][i] + "X↑" + (n - i - 1);
  54. Console.Write(outStr);
  55. }
  56. outStr=(jag[row][n - 2] == "0") ? "" : (((Convert.ToDouble(jag[row][n - 2]) > 0) ? "+" : "")
  57. + jag[row][n - 2] + "X");
  58. Console.Write(outStr);
  59. outStr=(jag[row][n - 1] == "0") ? "" : (((Convert.ToDouble(jag[row][n - 1]) > 0) ? "+" : "")
  60. + jag[row][n - 1]);
  61. Console.WriteLine(outStr);
  62. }
  63.  
  64. static void Main()
  65. {
  66. Console.WriteLine("Write a method that adds two polynomials.");
  67.  
  68. // ***************** TEST DATA *************************************
  69.  
  70. /* string[][] jagged = new string[3][];
  71.  
  72. int n1 = 4;
  73. int n2 = 8;
  74. int max = Math.Max(n1, n2);
  75. int min = Math.Min(n1, n2);
  76. jagged[0] = new string[] { "5.9", "-2", "9", "8", "0" };
  77. jagged[1] = new string[] { "7", "0", "-3.5", "8.12", "2", "1.6", "-43", ".27", "0" };
  78. jagged[2] = new string[max + 1];
  79.  
  80. int n1 = 4;
  81. int n2 = 4;
  82. int max = Math.Max(n1, n2);
  83. int min = Math.Min(n1, n2);
  84. jagged[0] = new string[] { "5", "2", "9", "8", "0" };
  85. jagged[1] = new string[] { "7", "0", "3", "8", "0" };
  86. jagged[2] = new string[max + 1];
  87.  
  88. int n1 = 8;
  89. int n2 = 4;
  90. int max=Math.Max(n1,n2);
  91. int min=Math.Min(n1,n2);
  92. jagged[0] = new string[] {"1", "4", "7", "9", "9", "9", "9", "9", "0"};
  93. jagged[1] = new string[] {"9", "2", "9", "8", "0"};
  94. jagged[2] = new string[max+1];
  95.  
  96. int n1 = 10;
  97. int n2 = 2;
  98. int max = Math.Max(n1, n2);
  99. int min = Math.Min(n1, n2);
  100. jagged[0] = new string[] { "9", "9", "9", "9", "9","9", "9", "9", "9", "9", "0" };
  101. jagged[1] = new string[] { "9", "9", "0" };
  102. jagged[2] = new string[max + 1];
  103. */
  104.  
  105. int n1, n2;
  106. string strNum;
  107. do
  108. {
  109. Console.Write("Enter the length of the coefficients for polynomial1: ");
  110. }
  111. while (!int.TryParse(strNum = Console.ReadLine(), out n1) || n1 < 1);
  112. do
  113. {
  114. Console.Write("Enter the length of the coefficients for polynomial2: ");
  115. }
  116. while (!int.TryParse(strNum = Console.ReadLine(), out n2) || n2 < 1);
  117.  
  118. int max = Math.Max(n1, n2);
  119. int min = Math.Min(n1, n2);
  120.  
  121. // Allocate a jagged array with aray1,array2 and result
  122.  
  123. string[][] jagged = new string[3][] { new string[n1 + 1], new string[n2 + 1], new string[max + 1] };
  124.  
  125. Console.WriteLine("Enter the coefficients of the first polynomial, x0 coeff. having 0 index:"); //first (polyn1) row
  126. for (int i = 0; i < n1; i++)
  127. {
  128. jagged[0][i] = Console.ReadLine();
  129. }
  130.  
  131. Console.WriteLine("Enter the coefficients of the first polynomial, x0 coeff. having 0 index:"); // second (polyn2) row
  132. for (int i = 0; i < n2; i++)
  133. {
  134. jagged[1][i] = Console.ReadLine();
  135. }
  136.  
  137. jagged[2] = new string[max + 1]; // third (resulting polynomial) row
  138.  
  139.  
  140. //*************** Call the method to sum the polynomials **************************
  141.  
  142. AddCoeff(n1, n2, jagged); // call the method to sum the polynomials
  143.  
  144. //************* Print the input data as is ******************
  145.  
  146. //******* Print both polynomials and the result **********
  147.  
  148. Array.Reverse(jagged[0], 0, n1);
  149. Console.Write("Polynomial1\t= ");
  150. PrintPolynomial(n1, 0, jagged);
  151.  
  152. Array.Reverse(jagged[1], 0, n2);
  153. Console.Write("Polynomial2\t= ");
  154. PrintPolynomial(n2, 1, jagged);
  155.  
  156. Array.Reverse(jagged[2], 0, max);
  157. Console.Write("Poly1 + Poly2\t= ");
  158. PrintPolynomial(max, 2, jagged);
  159.  
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment