ChameL1oN

ЯПЛаба3_2

Apr 13th, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7.  
  8. //Составить описание класса многочленов от одной переменной, задаваемых степенью многочлена и массивом коэффициентов
  9. //. Предусмотреть методы, реализующие:
  10. //• вычисление значения многочлена для заданного аргумента;
  11. //• операцию сложения, вычитания и умножения многочленов с получением нового объекта-многочлена;
  12. //• вывод на экран описания многочлена.
  13. //Написать программу, демонстрирующую работу с этим классом.
  14.  
  15.  
  16.  
  17. namespace CHarpZakaz1
  18. {
  19. public class Mnog
  20. {
  21. public int[] x;
  22.  
  23. public double Score(int a)
  24. {
  25. double sum=0;
  26. for (int i = 0; i < x.Length; i++)
  27. {
  28. sum += Math.Pow(a, i);
  29. }
  30. return sum;
  31. }
  32. public void Print()
  33. {
  34. for (int i = x.Length-1; i >= 0; i--)
  35. {
  36. if (i > 0)
  37. {
  38. Console.Write(x[i] + "x^" + i + " + ");
  39. }
  40. else
  41. {
  42. Console.Write(x[i]);
  43. }
  44. }
  45. Console.WriteLine(" ");
  46. }
  47.  
  48. }
  49. class Program
  50. {
  51. static void Main(string[] args)
  52. {
  53. Mnog a1 = new Mnog();
  54. Mnog a2 = new Mnog();
  55. Console.WriteLine("Коэффициент первого многочлена");
  56. int n, n2;
  57. n = int.Parse(Console.ReadLine());
  58. Console.WriteLine("Коэффициент второго многочлена");
  59. n2 = int.Parse(Console.ReadLine());
  60. int[] c = new int[n+1];
  61. int[] c2 = new int[n2+1];
  62. for (int i = c.Length-1; i >= 0; i--)
  63. {
  64. Console.WriteLine("Коэффициент при x^" + i);
  65. c[i] = int.Parse(Console.ReadLine());
  66. }
  67. for (int i = c2.Length-1; i >= 0; i--)
  68. {
  69. Console.WriteLine("Коэффициент при x^" + i);
  70. c2[i] = int.Parse(Console.ReadLine());
  71. }
  72. a1.x = c;
  73. a2.x = c2;
  74. a1.Print();
  75. a2.Print();
  76. Mnog a3 = new Mnog();
  77. if (n > n2)
  78. {
  79. Console.Write("Сумма = ");
  80. int[] c3 = new int[n];
  81. for (int i = 0; i < n2; i++)
  82. {
  83. c3[i] = c[i] + c2[i];
  84. }
  85. for (int i = n2; i < n; i++)
  86. {
  87. c3[i] = c[i];
  88. }
  89. a3.x = c3;
  90. a3.Print();
  91. Console.Write("Разность = ");
  92. for (int i = 0; i < n2; i++)
  93. {
  94. c3[i] = c[i] - c2[i];
  95. }
  96. for (int i = n2; i < n; i++)
  97. {
  98. c3[i] = c[i];
  99. }
  100. a3.x = c3;
  101. a3.Print();
  102. }
  103. else
  104. {
  105. Console.Write("Сумма = ");
  106. int[] c3 = new int[n2];
  107. for (int i = 0; i < n; i++)
  108. {
  109. c3[i] = c[i] + c2[i];
  110. }
  111. for (int i = n; i < n2; i++)
  112. {
  113. c3[i] = c2[i];
  114. }
  115. a3.x = c3;
  116. a3.Print();
  117. Console.Write("Разность = ");
  118. for (int i = 0; i < n; i++)
  119. {
  120. c3[i] = c[i] - c2[i];
  121. }
  122. for (int i = n; i < n2; i++)
  123. {
  124. c3[i] = -c2[i];
  125. }
  126. a3.x = c3;
  127. a3.Print();
  128. }
  129. int[] c4 = new int[n + n2-1];
  130. Console.Write("Произведение = ");
  131. for (int i = 0; i < n; i++)
  132. {
  133. for (int j = 0; j < n2; j++)
  134. {
  135. c4[i + j] = c[i] * c2[j];
  136. }
  137. }
  138. a3.x = c4;
  139. a3.Print();
  140. }
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment