Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gyak;
- import java.util.Scanner;
- public class poliosztas {
- private int[] coef; // együttható
- private int deg; // polinom foka (0, ha nincs fokszáma)
- // a * x^b
- public poliosztas(int a, int b) {
- coef = new int[b+1];
- coef[b] = a;
- deg = degree();
- }
- // visszaadja a polinom fokszámát (0 for the zero polynomial)
- public int degree() {
- int d = 0;
- for (int i = 0; i < coef.length; i++)
- if (coef[i] != 0) d = i;
- return d;
- }
- // return c = a + b
- public poliosztas plusz(poliosztas b) {
- poliosztas a = this;
- poliosztas c = new poliosztas(0, Math.max(a.deg, b.deg));
- for (int i = 0; i <= a.deg; i++) c.coef[i] += a.coef[i];
- for (int i = 0; i <= b.deg; i++) c.coef[i] += b.coef[i];
- c.deg = c.degree();
- return c;
- }
- // return (a - b)
- public poliosztas minusz(poliosztas b) {
- poliosztas a = this;
- poliosztas c = new poliosztas(0, Math.max(a.deg, b.deg));
- for (int i = 0; i <= a.deg; i++) c.coef[i] += a.coef[i];
- for (int i = 0; i <= b.deg; i++) c.coef[i] -= b.coef[i];
- c.deg = c.degree();
- return c;
- }
- // return (a * b)
- public poliosztas szorzas(poliosztas b) {
- poliosztas a = this;
- poliosztas c = new poliosztas(0, a.deg + b.deg);
- for (int i = 0; i <= a.deg; i++)
- for (int j = 0; j <= b.deg; j++)
- c.coef[i+j] += (a.coef[i] * b.coef[j]);
- c.deg = c.degree();
- return c;
- }
- // Egyezőségvizsgálat
- public boolean eq(poliosztas b) {
- poliosztas a = this;
- if (a.deg != b.deg) return false;
- for (int i = a.deg; i >= 0; i--)
- if (a.coef[i] != b.coef[i]) return false;
- return true;
- }
- // 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.
- public int evaluate(int x) {
- int p = 0;
- for (int i = deg; i >= 0; i--)
- p = coef[i] + (x * p);
- return p;
- }
- // Deriválja a polinomot és visszaadja.
- public poliosztas differentiate() {
- if (deg == 0) return new poliosztas(0, 0);
- poliosztas deriv = new poliosztas(0, deg - 1);
- deriv.deg = deg - 1;
- for (int i = 0; i < deg; i++)
- deriv.coef[i] = (i + 1) * coef[i + 1];
- return deriv;
- }
- // Stringgé konvertálás.
- public String toString() {
- if (deg == 0) return "" + coef[0];
- if (deg == 1) return coef[1] + "x + " + coef[0];
- String s = coef[deg] + "x^" + deg;
- for (int i = deg-1; i >= 0; i--) {
- if (coef[i] == 0) continue;
- else if (coef[i] > 0) s = s + " + " + ( coef[i]);
- else if (coef[i] < 0) s = s + " - " + (-coef[i]);
- if (i == 1) s = s + "x";
- else if (i > 1) s = s + "x^" + i;
- }
- return s;
- }
- //Osztási művelet.
- public poliosztas divide(poliosztas b) {
- poliosztas a = this;
- if ((b.deg == 0) && (b.coef[0] == 0))
- throw new RuntimeException("Nullával való osztás"); //Mind az együttható, mind pedig a polinom nulla
- if (a.deg < b.deg) return new poliosztas(0,0);
- int coefficient = a.coef[a.deg]/(b.coef[b.deg]);
- int exponent = a.deg - b.deg;
- poliosztas c = new poliosztas(coefficient, exponent);
- return c.plusz( (a.minusz(b.szorzas(c)).divide(b)) );
- }
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String testString=sc.next();
- testString = testString.replace("-", "+-");
- String[] arr = testString.split("\\+");
- poliosztas testPoli = null;
- for (String partString : arr)
- {
- String[] aString = partString.split("x");
- int size = aString.length;
- int a = 0;
- int b = 0;
- a = Integer.parseInt(aString[0]);
- b = ((size > 1) ? Integer.parseInt(aString[1].replace("^", "")) : ((size == 1 && partString.contains("x")) ? 1 : 0));
- System.out.println("A: " + a + " | B: " + b);
- System.out.println("D: " + partString);
- poliosztas tempPoli = new poliosztas(a, b);
- if(testPoli == null)
- testPoli = tempPoli;
- else
- testPoli = testPoli.plusz(tempPoli);
- }
- System.out.println("Teszt: " + testString);
- System.out.println("Teszt2: " + testPoli);
- /*első polinom*/
- poliosztas p1 = new poliosztas(1, 2);
- poliosztas p2 = new poliosztas(3, 1);
- poliosztas p3 = new poliosztas(2, 0);
- poliosztas p = p1.plusz(p2).plusz(p3);
- /*második poliom*/
- poliosztas q1 = new poliosztas(1, 1);
- poliosztas q2 = new poliosztas(-2, 0);
- poliosztas q = q1.plusz(q2);
- System.out.println("p(x) = " + p);
- System.out.println("q(x) = " + q);
- System.out.println("Maradék: "+q.divide(p));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement