Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. package gyak;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class poliosztas {
  6. private int[] coef; // együttható
  7. private int deg; // polinom foka (0, ha nincs fokszáma)
  8.  
  9. // a * x^b
  10. public poliosztas(int a, int b) {
  11. coef = new int[b+1];
  12. coef[b] = a;
  13. deg = degree();
  14. }
  15.  
  16. // visszaadja a polinom fokszámát (0 for the zero polynomial)
  17. public int degree() {
  18. int d = 0;
  19. for (int i = 0; i < coef.length; i++)
  20. if (coef[i] != 0) d = i;
  21. return d;
  22. }
  23.  
  24. // return c = a + b
  25. public poliosztas plusz(poliosztas b) {
  26. poliosztas a = this;
  27. poliosztas c = new poliosztas(0, Math.max(a.deg, b.deg));
  28. for (int i = 0; i <= a.deg; i++) c.coef[i] += a.coef[i];
  29. for (int i = 0; i <= b.deg; i++) c.coef[i] += b.coef[i];
  30. c.deg = c.degree();
  31. return c;
  32. }
  33.  
  34. // return (a - b)
  35. public poliosztas minusz(poliosztas b) {
  36. poliosztas a = this;
  37. poliosztas c = new poliosztas(0, Math.max(a.deg, b.deg));
  38. for (int i = 0; i <= a.deg; i++) c.coef[i] += a.coef[i];
  39. for (int i = 0; i <= b.deg; i++) c.coef[i] -= b.coef[i];
  40. c.deg = c.degree();
  41. return c;
  42. }
  43.  
  44. // return (a * b)
  45. public poliosztas szorzas(poliosztas b) {
  46. poliosztas a = this;
  47. poliosztas c = new poliosztas(0, a.deg + b.deg);
  48. for (int i = 0; i <= a.deg; i++)
  49. for (int j = 0; j <= b.deg; j++)
  50. c.coef[i+j] += (a.coef[i] * b.coef[j]);
  51. c.deg = c.degree();
  52. return c;
  53. }
  54.  
  55.  
  56. // Egyezőségvizsgálat
  57. public boolean eq(poliosztas b) {
  58. poliosztas a = this;
  59. if (a.deg != b.deg) return false;
  60. for (int i = a.deg; i >= 0; i--)
  61. if (a.coef[i] != b.coef[i]) return false;
  62. return true;
  63. }
  64.  
  65.  
  66. // A Horner féle módszert használjuk az x-ben kiértékelt polinom kiszámításához és visszaküldéséhez.
  67. public int evaluate(int x) {
  68. int p = 0;
  69. for (int i = deg; i >= 0; i--)
  70. p = coef[i] + (x * p);
  71. return p;
  72. }
  73.  
  74. // Deriválja a polinomot és visszaadja.
  75. public poliosztas differentiate() {
  76. if (deg == 0) return new poliosztas(0, 0);
  77. poliosztas deriv = new poliosztas(0, deg - 1);
  78. deriv.deg = deg - 1;
  79. for (int i = 0; i < deg; i++)
  80. deriv.coef[i] = (i + 1) * coef[i + 1];
  81. return deriv;
  82. }
  83.  
  84. // Stringgé konvertálás.
  85. public String toString() {
  86. if (deg == 0) return "" + coef[0];
  87. if (deg == 1) return coef[1] + "x + " + coef[0];
  88. String s = coef[deg] + "x^" + deg;
  89. for (int i = deg-1; i >= 0; i--) {
  90. if (coef[i] == 0) continue;
  91. else if (coef[i] > 0) s = s + " + " + ( coef[i]);
  92. else if (coef[i] < 0) s = s + " - " + (-coef[i]);
  93. if (i == 1) s = s + "x";
  94. else if (i > 1) s = s + "x^" + i;
  95. }
  96. return s;
  97. }
  98.  
  99. //Osztási művelet.
  100. public poliosztas divide(poliosztas b) {
  101. poliosztas a = this;
  102. if ((b.deg == 0) && (b.coef[0] == 0))
  103. throw new RuntimeException("Nullával való osztás"); //Mind az együttható, mind pedig a polinom nulla
  104.  
  105. if (a.deg < b.deg) return new poliosztas(0,0);
  106.  
  107. int coefficient = a.coef[a.deg]/(b.coef[b.deg]);
  108. int exponent = a.deg - b.deg;
  109. poliosztas c = new poliosztas(coefficient, exponent);
  110. return c.plusz( (a.minusz(b.szorzas(c)).divide(b)) );
  111. }
  112.  
  113.  
  114. public static void main(String[] args) {
  115.  
  116. Scanner sc = new Scanner(System.in);
  117. String testString=sc.next();
  118.  
  119. testString = testString.replace("-", "+-");
  120. String[] arr = testString.split("\\+");
  121.  
  122. poliosztas testPoli = null;
  123. for (String partString : arr)
  124. {
  125. String[] aString = partString.split("x");
  126. int size = aString.length;
  127. int a = 0;
  128. int b = 0;
  129.  
  130. a = Integer.parseInt(aString[0]);
  131. b = ((size > 1) ? Integer.parseInt(aString[1].replace("^", "")) : ((size == 1 && partString.contains("x")) ? 1 : 0));
  132.  
  133. System.out.println("A: " + a + " | B: " + b);
  134. System.out.println("D: " + partString);
  135.  
  136. poliosztas tempPoli = new poliosztas(a, b);
  137. if(testPoli == null)
  138. testPoli = tempPoli;
  139. else
  140. testPoli = testPoli.plusz(tempPoli);
  141. }
  142.  
  143. System.out.println("Teszt: " + testString);
  144. System.out.println("Teszt2: " + testPoli);
  145.  
  146. /*első polinom*/
  147. poliosztas p1 = new poliosztas(1, 2);
  148. poliosztas p2 = new poliosztas(3, 1);
  149. poliosztas p3 = new poliosztas(2, 0);
  150.  
  151.  
  152. poliosztas p = p1.plusz(p2).plusz(p3);
  153.  
  154.  
  155. /*második poliom*/
  156. poliosztas q1 = new poliosztas(1, 1);
  157. poliosztas q2 = new poliosztas(-2, 0);
  158.  
  159. poliosztas q = q1.plusz(q2);
  160.  
  161. System.out.println("p(x) = " + p);
  162. System.out.println("q(x) = " + q);
  163. System.out.println("Maradék: "+q.divide(p));
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement