uebung03
This commit is contained in:
parent
2be863dcb0
commit
1ff5ea2a75
21
uebung03/src/graph/Edge.java
Normal file
21
uebung03/src/graph/Edge.java
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
70
uebung03/src/graph/Graph.java
Normal file
70
uebung03/src/graph/Graph.java
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
package graph;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class Graph {
|
||||||
|
private ArrayList<Node> nodes;
|
||||||
|
|
||||||
|
public ArrayList<Node> 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<Node> depthFirst(Node startNode) {
|
||||||
|
unmarkAllNodes();
|
||||||
|
final ArrayList<Node> collect = new ArrayList<>();
|
||||||
|
search(startNode, collect);
|
||||||
|
return collect;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void search(Node node, ArrayList<Node> collect) {
|
||||||
|
if (node.getMarked()) return;
|
||||||
|
node.setMarked(true);
|
||||||
|
collect.add(node);
|
||||||
|
for (Edge e : node.getOut())
|
||||||
|
search(e.getTarget(), collect);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<Node> breadthFirst(Node startNode) {
|
||||||
|
unmarkAllNodes();
|
||||||
|
final ArrayList<Node> 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();
|
||||||
|
}
|
||||||
|
}
|
44
uebung03/src/graph/Node.java
Normal file
44
uebung03/src/graph/Node.java
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package graph;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class Node {
|
||||||
|
private final String name;
|
||||||
|
private ArrayList<Edge> out = new ArrayList<>();
|
||||||
|
private boolean marked;
|
||||||
|
|
||||||
|
public Node(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<Edge> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,187 @@
|
|||||||
package polynomial;
|
package polynomial;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
public class Polynomial {
|
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<w.length; i++){
|
||||||
|
if (werte[i] != 0){
|
||||||
|
deg = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
degree = deg;
|
||||||
|
|
||||||
|
//gleichwertige Variante
|
||||||
|
/*for (int i = w.length; 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
5
uebung03/src/polynomial/PolynomialDemo.java
Normal file
5
uebung03/src/polynomial/PolynomialDemo.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package polynomial;
|
||||||
|
|
||||||
|
public class PolynomialDemo {
|
||||||
|
private Polynomial p1 = new Polynomial(new int[]{1,4});
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user