fertig
parent
d9ab6149ed
commit
d1be7e437d
|
@ -0,0 +1,57 @@
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
public class Aufgaben
|
||||
{
|
||||
public static void buchstabenZählen(String text)
|
||||
{
|
||||
var häufigkeiten = Zählen.countEachLetter(text);
|
||||
|
||||
Zählen.printZahl(häufigkeiten);
|
||||
}
|
||||
|
||||
public static void binärbaum(String text)
|
||||
{
|
||||
Map<Character, Integer> zahlen = Zählen.countEachLetter(text);
|
||||
Node wurzel = Node.erstellen(zahlen);
|
||||
|
||||
HashMap<Character, String> codes = new HashMap<Character, String>();
|
||||
|
||||
if(wurzel.getLeft() == null && wurzel.getRight() == null)
|
||||
{
|
||||
codes.put(wurzel.getBuchstabe(), "0");
|
||||
}
|
||||
else
|
||||
{
|
||||
Komprimierung.generieren(codes,wurzel,"");
|
||||
}
|
||||
|
||||
for (var entry : codes.entrySet())
|
||||
{
|
||||
System.out.println("Buchstabe: " + entry.getKey() + ", Code: " + entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public static void huffmanCodierun (String text)
|
||||
{
|
||||
Map<Character, Integer> zahlen = Zählen.countEachLetter(text);
|
||||
Node wurzel = Node.erstellen(zahlen);
|
||||
|
||||
System.out.println("Huffman-Codierung: " + Komprimierung.codierung(text,wurzel));
|
||||
}
|
||||
|
||||
public static void decodierung (String text)
|
||||
{
|
||||
System.out.println("Kodierter Text: " + Komprimierung.codierungInklBaum(text)); //soviele Zeichen aufgrund von UTF-16
|
||||
System.out.println("Decodierter Text: " + Komprimierung.decodierungInlkBaum(Komprimierung.codierungInklBaum(text)));
|
||||
}
|
||||
|
||||
public static void dateinKomprimierung (String source, String target)
|
||||
{
|
||||
Komprimierung.compressFile(source,target);
|
||||
}
|
||||
|
||||
public static void dateinLesen (String file)
|
||||
{
|
||||
Komprimierung.readFile(file);
|
||||
}
|
||||
}
|
|
@ -5,7 +5,7 @@ import java.util.Stack;
|
|||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class Main
|
||||
public class Komprimierung
|
||||
{
|
||||
public static byte[] nullCharBytes = {(byte)0xFF,(byte)0xFF};
|
||||
public static void test(String text)
|
||||
|
@ -157,38 +157,37 @@ public class Main
|
|||
//decodiert Buchstaben und speichert den Baum
|
||||
public static String decodierungInlkBaum(String code)
|
||||
{
|
||||
String codierterTextErstIndex = code.substring(0,32);
|
||||
int textErstIndex = binärString_to_int(codierterTextErstIndex);
|
||||
String codierterTextErstIndex = code.substring(0,32); //holt binärcode vom TextErstIndex
|
||||
int textErstIndex = binärString_to_int(codierterTextErstIndex); //wandelt codierterTextErstIndex in eine Integer
|
||||
|
||||
String codierterBaum = code.substring(32, textErstIndex);
|
||||
Node decodierterBaum = binärString_to_baum(codierterBaum);
|
||||
|
||||
String codierterText = code.substring(textErstIndex, code.length());
|
||||
String decodierterText = decodierungBaum(codierterText,decodierterBaum);
|
||||
String decodierterText = decodierungBaum(codierterText,decodierterBaum); //decodiert den Text mit dem Baum
|
||||
|
||||
return decodierterText;
|
||||
}
|
||||
|
||||
//decodiert den Baum
|
||||
public static String decodierungBaum(String code,Node baum)
|
||||
{
|
||||
Node current = baum;
|
||||
String decoded = "";
|
||||
|
||||
boolean hatNurEinBuchstabenart = current.getRight() == null && current.getLeft() == null;
|
||||
if(hatNurEinBuchstabenart)
|
||||
if(hatNurEinBuchstabenart) //nimmt den einen Buchstabe und fügt ihn für n mal ein
|
||||
{
|
||||
for(int i=0;i<code.length();i++)
|
||||
{
|
||||
decoded += baum.getBuchstabe();
|
||||
}
|
||||
System.out.println(decoded);
|
||||
return decoded;
|
||||
|
||||
}
|
||||
|
||||
for(int i =0;i<code.length();i++)
|
||||
for(int i =0;i<code.length();i++) //deocdiert den code einzelnd
|
||||
{
|
||||
if(code.charAt(i) == '1')
|
||||
if(code.charAt(i) == '1') //wenn eine 1 im Code steht geht es nach rechts, da 1 für den rechten Ast steht
|
||||
{
|
||||
current= current.getRight();
|
||||
}
|
||||
|
@ -196,13 +195,12 @@ public class Main
|
|||
{
|
||||
current = current.getLeft();
|
||||
}
|
||||
if(current.getRight() == null && current.getLeft() == null)
|
||||
if(current.getRight() == null && current.getLeft() == null) //sind bei einem Blatt angekommen
|
||||
{
|
||||
decoded += current.getBuchstabe();
|
||||
current = baum;
|
||||
}
|
||||
}
|
||||
System.out.println(decoded);
|
||||
return decoded;
|
||||
}
|
||||
|
||||
|
@ -210,14 +208,14 @@ public class Main
|
|||
|
||||
public static Byte binärString_to_Byte (String binärString)
|
||||
{
|
||||
if(binärString.length() != 8)
|
||||
if(binärString.length() != 8) //überprüft ob es ein byte lang ist
|
||||
{
|
||||
System.out.println(">8");
|
||||
return null;
|
||||
}
|
||||
|
||||
byte result = 0;
|
||||
for(byte i=0 ;i<8;i++)
|
||||
for(byte i=0 ;i<8;i++) //geht den binärString durch und wandelt ihn in byte um
|
||||
{
|
||||
if(binärString.charAt(7-i) == '1')result|=1<<i; //verschiebt die 1 um i binärpositionen nach links
|
||||
else if(binärString.charAt(7-i) != '0')
|
||||
|
@ -236,7 +234,7 @@ public class Main
|
|||
|
||||
for(byte i=0;i<8;i++)
|
||||
{
|
||||
boolean bit= ((b>>(7-i))&1)==1;
|
||||
boolean bit= ((b>>(7-i))&1)==1; //schaut ob der 7-i ste bit 1 ist
|
||||
result+= bit?'1': '0'; //falls bit wert 1 hat addiert es 1 zu dem string, wenn nicht dann addiertes es 0
|
||||
}
|
||||
|
||||
|
@ -248,7 +246,7 @@ public class Main
|
|||
|
||||
byte[] b = nullCharBytes;
|
||||
|
||||
if(c != null)
|
||||
if(c != null) //wandelt character in "byte"
|
||||
{
|
||||
var byteBuffer = ByteBuffer.allocate(2);
|
||||
byteBuffer.putChar(c);
|
||||
|
@ -256,7 +254,7 @@ public class Main
|
|||
}
|
||||
|
||||
String result = "";
|
||||
for(int i=0;i<b.length;i++)
|
||||
for(int i=0;i<b.length;i++) //wandelt byte zu String und addiert sie
|
||||
{
|
||||
result += Byte_to_binärString(b[i]);
|
||||
}
|
||||
|
@ -268,10 +266,10 @@ public class Main
|
|||
byte a = binärString_to_Byte(binärString.substring(0,8));
|
||||
byte b = binärString_to_Byte(binärString.substring(8,16));
|
||||
byte[] array = new byte[] {a,b};
|
||||
if(array[0] == nullCharBytes[0] && array[1] == nullCharBytes[1])return null;
|
||||
if(array[0] == nullCharBytes[0] && array[1] == nullCharBytes[1])return null;
|
||||
var byteBuffer = ByteBuffer.wrap(array);
|
||||
|
||||
return byteBuffer.getChar(0);
|
||||
return byteBuffer.getChar(0); //erstellt aus den zwei bytes ein char
|
||||
}
|
||||
|
||||
public static String baum_to_binärString (Node baum)
|
||||
|
@ -279,7 +277,7 @@ public class Main
|
|||
if(baum == null)return "";
|
||||
|
||||
String binärString = character_to_binärString(baum.getBuchstabe());
|
||||
binärString += baum_to_binärString(baum.getLeft());
|
||||
binärString += baum_to_binärString(baum.getLeft()); //speichert pre order die Buchstaben als binär ab
|
||||
binärString += baum_to_binärString(baum.getRight());
|
||||
return binärString;
|
||||
}
|
||||
|
@ -287,16 +285,16 @@ public class Main
|
|||
public static Node binärString_to_baum (String s)
|
||||
{
|
||||
Node root = null;
|
||||
Stack<Node> äste = new Stack<Node>();
|
||||
Stack<Node> äste = new Stack<Node>(); //erstellt ein leeres Stack
|
||||
for(int i=0;i<s.length();i+=16)
|
||||
{
|
||||
String characterString = s.substring(i,i+16);
|
||||
Character c = binärString_to_character(characterString);
|
||||
String characterString = s.substring(i,i+16); //geht in 16er schritten den binärString durch
|
||||
Character c = binärString_to_character(characterString); //wandelt characterString zu character um
|
||||
Node node = new Node();
|
||||
node.setBuchstabe(c);
|
||||
if(i == 0)root = node;
|
||||
|
||||
if(äste.isEmpty() == false)
|
||||
if(äste.isEmpty() == false) //baut die Nodes zum Baum bis nur noch die wurzel übrig ist
|
||||
{
|
||||
Node elternAst = äste.peek();
|
||||
|
||||
|
@ -309,11 +307,8 @@ public class Main
|
|||
|
||||
if(c == null)
|
||||
{
|
||||
|
||||
äste.push(node);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return root;
|
||||
|
@ -321,14 +316,14 @@ public class Main
|
|||
|
||||
public static Integer binärString_to_int(String binärString)
|
||||
{
|
||||
if(binärString.length() != 32)
|
||||
if(binärString.length() != 32)
|
||||
{
|
||||
System.out.println(">32");
|
||||
return null;
|
||||
}
|
||||
|
||||
int result = 0;
|
||||
for(int i=0 ;i<32;i++)
|
||||
for(int i=0 ;i<32;i++) //geht den binärString durch und wandelt ihn int um
|
||||
{
|
||||
if(binärString.charAt(31-i) == '1')result|=1<<i; //verschiebt die 1 um i binärpositionen nach links
|
||||
else if(binärString.charAt(31-i) != '0')
|
||||
|
@ -354,12 +349,13 @@ public class Main
|
|||
return result;
|
||||
}
|
||||
|
||||
//komprimiert Datei
|
||||
public static void compressFile (String source, String target )
|
||||
{
|
||||
Path sourcePath = Path.of(source);
|
||||
Path targetPath = Path.of(target);
|
||||
|
||||
if(Files.exists(sourcePath) == false)
|
||||
if(Files.exists(sourcePath) == false) //überprüft ob im Dateipfad eine Datei hinterlegt ist
|
||||
{
|
||||
System.out.println("Datei nicht gefunden");
|
||||
return;
|
||||
|
@ -367,11 +363,11 @@ public class Main
|
|||
|
||||
String text;
|
||||
|
||||
try
|
||||
try
|
||||
{
|
||||
text = Files.readString(sourcePath);
|
||||
}
|
||||
catch(Exception error)
|
||||
catch(Exception error) //gibt error aus falls einer auftrit
|
||||
{
|
||||
System.out.println(error);
|
||||
return;
|
||||
|
@ -390,7 +386,7 @@ public class Main
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
//liest die Datei
|
||||
public static void readFile(String file)
|
||||
{
|
||||
Path path = Path.of(file);
|
|
@ -40,9 +40,10 @@ public class Node
|
|||
|
||||
public static Node erstellen(Map<Character, Integer> häufigkeiten)
|
||||
{
|
||||
PriorityQueue<Node> nodes = new PriorityQueue<Node>(Comparator.comparingInt(n -> n.anzahl));
|
||||
//erstellt PriorityQueue die nach Anzahl sortiert
|
||||
PriorityQueue<Node> nodes = new PriorityQueue<Node>(Comparator.comparingInt(n -> n.anzahl));
|
||||
|
||||
for (var entry : häufigkeiten.entrySet())
|
||||
for (var entry : häufigkeiten.entrySet()) //erstellt die Nodes und brfüllt die queue damit
|
||||
{
|
||||
Node node = new Node();
|
||||
node.setAnzahl(entry.getValue());
|
||||
|
@ -52,7 +53,7 @@ public class Node
|
|||
|
||||
|
||||
|
||||
while (nodes.size() > 1)
|
||||
while (nodes.size() > 1) //erstellt den Baum
|
||||
{
|
||||
Node min1 = nodes.poll();
|
||||
Node min2 = nodes.poll();
|
||||
|
|
|
@ -1 +1 @@
|
|||
000000000000000000000000101100001111111111111111000000000110000111111111111111110000000001100010111111111111111100000000000010101111111111111111000000000110001100000000000011010000101011101111110
|
||||
00000000000000000000000010010000111111111111111100000000011000011111111111111111000000000110001011111111111111110000000001100011000000000000101000001010110111
|
112
package.bluej
112
package.bluej
|
@ -1,32 +1,80 @@
|
|||
#BlueJ package file
|
||||
editor.fx.0.height=0
|
||||
editor.fx.0.width=0
|
||||
editor.fx.0.x=0
|
||||
editor.fx.0.y=0
|
||||
objectbench.height=100
|
||||
objectbench.width=776
|
||||
package.divider.horizontal=0.6
|
||||
package.divider.vertical=0.8003731343283582
|
||||
package.editor.height=422
|
||||
package.editor.width=661
|
||||
package.editor.x=125
|
||||
package.editor.y=189
|
||||
package.frame.height=600
|
||||
package.frame.width=800
|
||||
package.numDependencies=0
|
||||
package.numTargets=1
|
||||
package.showExtends=true
|
||||
package.showUses=true
|
||||
project.charset=UTF-8
|
||||
readme.height=60
|
||||
readme.name=@README
|
||||
readme.width=48
|
||||
readme.x=10
|
||||
readme.y=10
|
||||
target1.height=70
|
||||
target1.name=Main
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.width=120
|
||||
target1.x=70
|
||||
target1.y=10
|
||||
#BlueJ package file
|
||||
dependency1.from=Komprimierung
|
||||
dependency1.to=Node
|
||||
dependency1.type=UsesDependency
|
||||
dependency2.from=Komprimierung
|
||||
dependency2.to=Z\u00E4hlen
|
||||
dependency2.type=UsesDependency
|
||||
dependency3.from=Aufgaben
|
||||
dependency3.to=Node
|
||||
dependency3.type=UsesDependency
|
||||
dependency4.from=Aufgaben
|
||||
dependency4.to=Z\u00E4hlen
|
||||
dependency4.type=UsesDependency
|
||||
dependency5.from=Aufgaben
|
||||
dependency5.to=Komprimierung
|
||||
dependency5.type=UsesDependency
|
||||
editor.fx.0.height=1447
|
||||
editor.fx.0.width=1294
|
||||
editor.fx.0.x=1273
|
||||
editor.fx.0.y=0
|
||||
objectbench.height=198
|
||||
objectbench.width=1254
|
||||
package.divider.horizontal=0.6
|
||||
package.divider.vertical=0.6730462519936204
|
||||
package.editor.height=415
|
||||
package.editor.width=1143
|
||||
package.editor.x=0
|
||||
package.editor.y=0
|
||||
package.frame.height=727
|
||||
package.frame.width=1294
|
||||
package.numDependencies=5
|
||||
package.numTargets=6
|
||||
package.showExtends=true
|
||||
package.showUses=true
|
||||
project.charset=UTF-8
|
||||
readme.height=60
|
||||
readme.name=@README
|
||||
readme.width=49
|
||||
readme.x=10
|
||||
readme.y=10
|
||||
target1.height=70
|
||||
target1.name=Aufgaben
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.width=120
|
||||
target1.x=150
|
||||
target1.y=30
|
||||
target2.height=70
|
||||
target2.name=Z\u00E4hlen
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.width=120
|
||||
target2.x=50
|
||||
target2.y=300
|
||||
target3.height=70
|
||||
target3.name=Node
|
||||
target3.showInterface=false
|
||||
target3.type=ClassTarget
|
||||
target3.width=120
|
||||
target3.x=50
|
||||
target3.y=210
|
||||
target4.height=70
|
||||
target4.name=Text.txt
|
||||
target4.type=TextTarget
|
||||
target4.width=120
|
||||
target4.x=430
|
||||
target4.y=30
|
||||
target5.height=70
|
||||
target5.name=compressed.txt
|
||||
target5.type=TextTarget
|
||||
target5.width=120
|
||||
target5.x=290
|
||||
target5.y=30
|
||||
target6.height=70
|
||||
target6.name=Komprimierung
|
||||
target6.showInterface=false
|
||||
target6.type=ClassTarget
|
||||
target6.width=120
|
||||
target6.x=250
|
||||
target6.y=160
|
||||
|
|
Loading…
Reference in New Issue