fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. class Polynomial{
  5. int N;
  6. double [] a = new double [N+1];
  7. public void printPolynomial(){
  8. if (this.N > 0){
  9. for (int i = N; i >= 0; i = i -1){
  10. if (i!=0){ if (this.a[i]!= 0) System.out.println("("+this.a[i]+")*X^"+i+"+");}
  11. if (i==0){ if (this.a[i]!= 0) System.out.println("("+this.a[i]+")");}
  12. }
  13. }
  14. if (this.N == 0) System.out.println(+this.a[0]);
  15. }
  16. public void computation(double k){
  17. double s = 0;
  18. if (this.N > 0){
  19. for (int i = 0; i<= N; i++){
  20. s = s + this.a[i]*Math.pow(k,i);
  21. }
  22. System.out.println("Значение равно "+s);
  23. }
  24. if (this.N == 0) System.out.println("Значение равно "+this.a[0]);
  25. }
  26. public void sum(Polynomial cir){
  27. double s;
  28. for (int i = cir.N; i >= 0; i = i -1){
  29. if (cir.N >= this.N){
  30. s = 0;
  31. if (i > this.N){ if (cir.a[i] != 0) System.out.println("("+cir.a[i]+")*X^"+i+"+" );}
  32. if ((i <= this.N) && (i != 0)){ s = this.a[i] + cir.a[i]; if (s != 0) System.out.println("("+s+")*X^"+i+"+");}
  33. if (i == 0){ s = this.a[i] + cir.a[i]; System.out.println(+s);}
  34. }
  35. if (cir.N < this.N){
  36. s = 0;
  37. if (i > cir.N){ if (this.a[i] != 0) System.out.println(+this.a[i]+"*X^"+i+"+" );}
  38. if ((i <= cir.N) && (i != 0)){ s = this.a[i] + cir.a[i]; if (s != 0)System.out.println(+s+"*X^"+i+"+");}
  39. if (i == 0){ s = this.a[i] + cir.a[i]; System.out.println(+s);}
  40. }
  41. }
  42. }
  43. public void difference(Polynomial cir){
  44. double s;
  45. for (int i = cir.N; i >= 0; i = i -1){
  46. if (cir.N >= this.N){
  47. s = 0;
  48. if (i > this.N){ if (cir.a[i] != 0) System.out.println("("+cir.a[i]+")*X^"+i+"+" );}
  49. if ((i <= this.N) && (i != 0)){ s = this.a[i] - cir.a[i]; if (s != 0) System.out.println("("+s+")*X^"+i+"+");}
  50. if (i == 0){ s = this.a[i] - cir.a[i]; System.out.println(+s);}
  51. }
  52. if (cir.N < this.N){
  53. s = 0;
  54. if (i > cir.N){ if (this.a[i] != 0) System.out.println(+this.a[i]+"*X^"+i+"+" );}
  55. if ((i <= cir.N) && (i != 0)){ s = this.a[i] - cir.a[i]; if (s != 0) System.out.println(+s+"*X^"+i+"+");}
  56. if (i == 0){ s = this.a[i] - cir.a[i]; System.out.println(+s);}
  57. }
  58. }
  59. }
  60. public void multiplication(Polynomial cir){
  61. double [] g = new double [cir.N + this.N + 1];
  62. double y;
  63. for (int i = 0; i <= cir.N; i++){
  64. y = 1;
  65. for (int j = 0; j <= this.N; j++){
  66. y = cir.a[i] * this.a[j];
  67. g[i + j] = g[i + j] + y;
  68. }
  69. }
  70. for (int i = cir.N + this.N; i >= 0; i = i -1){
  71. if (cir.N + this.N != 0){
  72. if (i != 0) if (g[i] != 0) System.out.println("("+g[i]+")*X^+");
  73. if (i == 0) if (g[i] != 0) System.out.println("("+g[i]+")");
  74. }
  75. if (cir.N + this.N == 0) System.out.println(+g[0]);
  76. }
  77. }
  78.  
  79.  
  80. }
  81. class Ideone
  82. {
  83. public static void main (String[] args) throws java.lang.Exception
  84. {
  85. Polynomial q = new Polynomial ();
  86. Scanner sc = new Scanner (System.in);
  87. System.out.println("Введите максимальную степень многочлена");
  88. int p = sc.nextInt();
  89. q.N = p;
  90. for (int i = 0; i <= p; i++){
  91. System.out.println("Введите коэффициент при X^"+i);
  92. q.a[i] = sc.nextInt();
  93. }
  94. Polynomial t = new Polynomial ();
  95. System.out.println("Введите максимальную степень второго многочлена");
  96. int f = sc.nextInt();
  97. t.N = f;
  98. for (int i = 0; i <= f; i++){
  99. System.out.println("Введите коэффициент при X^"+i);
  100. t.a[i] = sc.nextInt();
  101. }
  102. System.out.println("Введите номер операции");
  103. int m = sc.nextInt();
  104. switch (m){
  105. case 1 : q.printPolynomial();break;
  106. case 2 : System.out.println("Введите значение"); double r = sc.nextInt(); q.computation(r);break;
  107. case 3: q.sum(t);break;
  108. case 4: q.difference(t);break;
  109. case 5: q.multiplication(t); break;
  110. }
  111. }
  112. }
Success #stdin #stdout 0.13s 57024KB
stdin
0
5
0
10
5
stdout
Введите максимальную степень многочлена
Введите коэффициент при X^0
Введите максимальную степень второго многочлена
Введите коэффициент при X^0
Введите номер операции
50.0