From 1ff5ea2a75e0ce48aee3fb5b4032497bcbcb6448 Mon Sep 17 00:00:00 2001 From: Peet Date: Fri, 2 May 2025 13:48:51 +0200 Subject: [PATCH] uebung03 --- uebung03/src/graph/Edge.java | 21 +++ uebung03/src/graph/Graph.java | 70 ++++++++ uebung03/src/graph/Node.java | 44 +++++ uebung03/src/polynomial/Polynomial.java | 182 ++++++++++++++++++++ uebung03/src/polynomial/PolynomialDemo.java | 5 + 5 files changed, 322 insertions(+) create mode 100644 uebung03/src/graph/Edge.java create mode 100644 uebung03/src/graph/Graph.java create mode 100644 uebung03/src/graph/Node.java create mode 100644 uebung03/src/polynomial/PolynomialDemo.java diff --git a/uebung03/src/graph/Edge.java b/uebung03/src/graph/Edge.java new file mode 100644 index 0000000..7d49ebc --- /dev/null +++ b/uebung03/src/graph/Edge.java @@ -0,0 +1,21 @@ +package graph; + +public class Edge { + private int length; + private final Node target; + + public Edge (int lenght, Node target) { + this.target = target; + this.length = lenght; + } + + @Override + public String toString() { + return "-" + length + "->" + target.getName(); + } + + public Node getTarget() { + return target; + } + +} diff --git a/uebung03/src/graph/Graph.java b/uebung03/src/graph/Graph.java new file mode 100644 index 0000000..925951d --- /dev/null +++ b/uebung03/src/graph/Graph.java @@ -0,0 +1,70 @@ +package graph; + +import java.util.ArrayList; + +public class Graph { + private ArrayList nodes; + + public ArrayList getNodes() { + return nodes; + } + + public Node getFirstNode() { + return nodes.get(0); + } + + public void addNode(Node n) { + if (nodes.contains(n)) { + throw new IllegalArgumentException(); + } + nodes.add(n); + } + + public ArrayList depthFirst(Node startNode) { + unmarkAllNodes(); + final ArrayList collect = new ArrayList<>(); + search(startNode, collect); + return collect; + } + + private void search(Node node, ArrayList collect) { + if (node.getMarked()) return; + node.setMarked(true); + collect.add(node); + for (Edge e : node.getOut()) + search(e.getTarget(), collect); + } + + public ArrayList breadthFirst(Node startNode) { + unmarkAllNodes(); + final ArrayList collect = new ArrayList<>(); + startNode.setMarked(true); + collect.add(startNode); + // Die folgende For-Schleife kann nicht als foreach-loop + // realisiert werden, weil sich collect laufend ändert + for (int i = 0; i < collect.size(); i++) + for (Edge edge : collect.get(i).getOut()) { + final Node target = edge.getTarget(); + if (!target.getMarked()) { + target.setMarked(true); + collect.add(target); + } + } + return collect; + } + + private void unmarkAllNodes() { + for (Node n : nodes) + n.setMarked(false); + } + + @Override + public String toString() { + StringBuilder str = new StringBuilder(); + str.append("Grap=\n"); + for (Node n : nodes) { + str.append(n).append(";\n"); + } + return str.toString(); + } +} diff --git a/uebung03/src/graph/Node.java b/uebung03/src/graph/Node.java new file mode 100644 index 0000000..997a1c8 --- /dev/null +++ b/uebung03/src/graph/Node.java @@ -0,0 +1,44 @@ +package graph; + +import java.util.ArrayList; + +public class Node { + private final String name; + private ArrayList out = new ArrayList<>(); + private boolean marked; + + public Node(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public ArrayList getOut() { + return out; + } + + public boolean getMarked() { + return marked; + } + + public void setMarked(boolean n) { + marked = n; + } + + public void addEdge(Edge e) { + if (out.contains(e)) { + throw new IllegalArgumentException(); + } + out.add(e); + } + + @Override + public String toString() { + return name + ": " + out; + } + + +} + diff --git a/uebung03/src/polynomial/Polynomial.java b/uebung03/src/polynomial/Polynomial.java index 5d74bf7..9dfd7d7 100644 --- a/uebung03/src/polynomial/Polynomial.java +++ b/uebung03/src/polynomial/Polynomial.java @@ -1,5 +1,187 @@ package polynomial; +import java.util.Arrays; + public class Polynomial { + private final int[] werte; + private final int degree; + + public Polynomial(int[] w) { + werte = w.clone(); + int deg = 0; + for(int i = 0; i= 0; i--) { + if (w[i] != 0) { + degree = i; + } + }*/ + } + + public Polynomial() { + this(new int[]{0,1}); + } + + //Option 1 Kopie des Arrays + public int[] getCoeef() { + return werte.clone(); + } + + //Option 2 Koeef an der Stelle i + public int getCoeefAt(int i) { + return i > degree ? 0 : werte[i]; + } + + public static Polynomial constant(int v) { + return new Polynomial(new int[]{v}); + } + + @Override + public String toString() { + if (isNull()){return "0";} + String result = ""; + + for (int i = degree; i >= 0; i--) { + + if (werte[i] == 0) {continue;} + + if (!result.isEmpty()) { + if (werte[i] > 0) {result += " + ";} + if (werte[i] < 0) {result += " - ";} + } + + if (result.isEmpty() && werte[i] <0){result += "-";} // Minus am Anfang + + int absCoeff = Math.abs(werte[i]); + + if (absCoeff != 1 && i == 0) {result += absCoeff;} + if (absCoeff == 1 && i == 0) {result += "1";} + if (absCoeff != 1 && i != 0) {result += absCoeff +"*";} + + if (i > 0) { + result += "x"; + if (i > 1) {result += "^" + i;} + } + + } + return result; + } + + public Boolean isNull(){ + for(int i = werte.length-1; i >= 0; i--){ + if (werte[i] != 0) { + return false; + } + } + return true; + + } + + public String toString1() { + StringBuilder result = new StringBuilder(); + + for (int i = degree; i >= 0; i--) { + + if (werte[i] == 0) {continue;} + + if (!result.isEmpty()) { + if (werte[i] > 0) {result.append(" + ");} + if (werte[i] < 0) {result.append(" - ");} + } + + if (result.isEmpty() && werte[i] <0){result.append("-");} // Minus am Anfang + + int absCoeff = Math.abs(werte[i]); + + if (absCoeff != 1 && i == 0) {result.append(absCoeff);} + if (absCoeff == 1 && i == 0) {result.append(1);} + if (absCoeff != 1 && i != 0) { + result.append(absCoeff); + result.append("*");} + + if (i > 0) { + result.append("x"); + if (i > 1) {result.append("^"); + result.append(i); + } + } + + } + + return result.toString(); + } + + public Polynomial add(Polynomial p2){ + int[] p1 = getCoeef(); + int[] p2Coeff = p2.getCoeef(); + int[] p3 = new int[Math.max(p1.length,p2Coeff.length)]; + for(int x = p3.length-1; x >= 0; x --){ + int coeff1 = (x < p1.length) ? p1[x] : 0; + int coeff2 = (x < p2Coeff.length) ? p2Coeff[x] : 0; + p3[x] = coeff1 + coeff2; + } + Polynomial result = new Polynomial(p3); + return result; + } + + public Polynomial sub(Polynomial that) { + int[] res = Arrays.copyOf(werte, Math.max(this.degree, that.degree) + 1); + for (int i = 0; i <= that.degree; i++) + res[i] -= that.werte[i]; + return new Polynomial(res); + } + + public Polynomial mult(Polynomial that) { + int[] res = new int[this.degree + that.degree + 1]; + Arrays.fill(res, 0); + for (int i = 0; i <= this.degree; i++) + for (int j = 0; j <= that.degree; j++) + res[i + j] += this.werte[i] * that.werte[j]; + return new Polynomial(res); + } + + public Polynomial exp(int e) { + if (e < 0) + throw new IllegalArgumentException("Exponent must not be negative"); + else if (e == 0) + return constant(1); + else if (e == 1) + return this; + else if (e % 2 == 0) + return this.mult(this).exp(e / 2); + else + return this.mult(this.exp(e - 1)); + } + + public Polynomial add(int v) { + int[] res = Arrays.copyOf(werte, werte.length); + res[0] += v; + return new Polynomial(res); + } + + public Polynomial sub(int v) { + return this.add(-v); + } + + public Polynomial mult(int v) { + int[] res = Arrays.copyOf(werte, werte.length); + for (int j = 0; j < res.length; j++) + res[j] *= v; + return new Polynomial(res); + } + + public int apply(int v) { + int res = werte[werte.length - 1]; + for (int i = werte.length - 2; i >= 0; i--) + res = res + v * werte[i]; + return res; + } + } diff --git a/uebung03/src/polynomial/PolynomialDemo.java b/uebung03/src/polynomial/PolynomialDemo.java new file mode 100644 index 0000000..c5e1647 --- /dev/null +++ b/uebung03/src/polynomial/PolynomialDemo.java @@ -0,0 +1,5 @@ +package polynomial; + +public class PolynomialDemo { + private Polynomial p1 = new Polynomial(new int[]{1,4}); +}