diff --git a/projekte/rechner/Rechner.class b/projekte/rechner/Rechner.class new file mode 100644 index 0000000..2ab5896 Binary files /dev/null and b/projekte/rechner/Rechner.class differ diff --git a/projekte/rechner/Rechner.java b/projekte/rechner/Rechner.java new file mode 100644 index 0000000..19870a4 --- /dev/null +++ b/projekte/rechner/Rechner.java @@ -0,0 +1,145 @@ +import java.util.Scanner; + +public class Rechner { + + public static final double Add = 0; + public static final double Sub = 1; + public static final double Mul = 2; + public static final double Div = 3; + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + System.out.print("Rechnung: "); + + String rechnung = scan.nextLine(); + + int numOperands = 0; + for (int i = 0; i < rechnung.length(); i++) { + char c = rechnung.charAt(i); + if (isOperand(c)) { + numOperands++; + } + if (c == '(' && i > 0 && ((rechnung.charAt(i - 1) >= '0' && rechnung.charAt(i - 1) <= '9') || rechnung.charAt(i - 1) == '.' )) { + rechnung = rechnung.substring(0, i) + "*" + rechnung.substring(i); + i++; + numOperands++; + } + if (c == ')' && i < rechnung.length() - 1 && ((rechnung.charAt(i + 1) >= '0' && rechnung.charAt(i + 1) <= '9') || rechnung.charAt(i + 1) == '.')) { + rechnung = rechnung.substring(0, i + 1) + "*" + rechnung.substring(i + 1); + } + } + + double[] priorities = new double[numOperands]; + double[] operands = new double[numOperands]; + double[] terms = new double[numOperands + 1]; + + double priority = 0; + double highestPriority = 0; + + int operandIndex = 0; + + String currentNum = ""; + + for (int i = 0; i < rechnung.length(); i++) { + char c = rechnung.charAt(i); + + if (c == '(') { + priority += 2; + continue; + } + if (c == ')') { + priority -= 2; + continue; + } + + if ((c >= '0' && c <= '9') || c == '.') { + currentNum = currentNum + c; + continue; + } + + if (c == '-' && currentNum == "") { + currentNum = "-"; + continue; + } + + if (isOperand(c)) { + switch (c) { + case '+': + priorities[operandIndex] = priority; + if (priority > highestPriority) { + highestPriority = priority; + } + operands[operandIndex] = Rechner.Add; + break; + + case '-': + priorities[operandIndex] = priority; + if (priority > highestPriority) { + highestPriority = priority; + } + operands[operandIndex] = Rechner.Sub; + break; + + case '*': + priorities[operandIndex] = priority + 1; + if (priority + 1> highestPriority) { + highestPriority = priority + 1; + } + operands[operandIndex] = Rechner.Mul; + break; + + case '/': + priorities[operandIndex] = priority + 1; + if (priority + 1> highestPriority) { + highestPriority = priority + 1; + } + operands[operandIndex] = Rechner.Div; + break; + } + terms[operandIndex] = Double.parseDouble(currentNum); + currentNum = ""; + operandIndex++; + } + } + terms[operandIndex] = Double.parseDouble(currentNum); + + for (double prio = highestPriority; prio >= 0; prio--) { + for (int i = 0; i < operands.length; i++) { + if (priorities[i] == prio) { + if (operands[i] == Rechner.Add) { + terms[i + 1] = terms[i] + terms[i + 1]; + } else if (operands[i] == Rechner.Sub) { + terms[i + 1] = terms[i] - terms[i + 1]; + } else if (operands[i] == Rechner.Mul) { + terms[i + 1] = terms[i] * terms[i + 1]; + } else if (operands[i] == Rechner.Div) { + terms[i + 1] = terms[i] / terms[i + 1]; + } + terms = removeArrayElement(terms, i); + operands = removeArrayElement(operands, i); + priorities = removeArrayElement(priorities, i); + i--; + } + } + } + System.out.println(terms[0]); + } + + public static double[] removeArrayElement(double[] arr, int index) { + if (index >= arr.length || index < 0) return arr; + + double[] ret = new double[arr.length - 1]; + + for (int i = 0; i < arr.length; i++) { + if (i == index) continue; + + ret[i < index ? i : i -1] = arr[i]; + } + + return ret; + } + + public static boolean isOperand(char c) { + return c == '+' || c == '-' || c == '*' || c == '/'; + } +} diff --git a/uebungen/h04_1/t01.class b/uebungen/h04_1/t01.class new file mode 100644 index 0000000..c9892cf Binary files /dev/null and b/uebungen/h04_1/t01.class differ diff --git a/uebungen/h04_1/t01.java b/uebungen/h04_1/t01.java new file mode 100644 index 0000000..0661c69 --- /dev/null +++ b/uebungen/h04_1/t01.java @@ -0,0 +1,13 @@ +public class t01 { + public static void main(String[] args) { + for (int i = 1; i <= 5; i++) { + for (int j = 1; j <= (5 - i); j++) { + System.out.print(" "); + } + for (int k = 1; k <= i; k++) { + System.out.print(i); + } + System.out.println(); + } + } +} diff --git a/uebungen/h04_figur/Figur.class b/uebungen/h04_figur/Figur.class new file mode 100644 index 0000000..e1be109 Binary files /dev/null and b/uebungen/h04_figur/Figur.class differ diff --git a/uebungen/h04_figur/Figur.java b/uebungen/h04_figur/Figur.java new file mode 100644 index 0000000..b2e62e1 --- /dev/null +++ b/uebungen/h04_figur/Figur.java @@ -0,0 +1,39 @@ +public class Figur { + public static void main(String[] args) { + + + zeichneBegrenzer(); + + for (int i = 0; i < 8; i++) { + int len = (i >= 4) ? (7 - i) : i; + + System.out.print("|"); + + repeatPrintString(" ",6 - len * 2); + + System.out.print("<>"); + + repeatPrintString(".", len * 4); + + System.out.print("<>"); + + repeatPrintString(" ",6 - len * 2); + + System.out.println("|"); + } + + zeichneBegrenzer(); + } + + public static void repeatPrintString(String s, int i) { + for (; i > 0; i--) { + System.out.print(s); + } + } + + public static void zeichneBegrenzer() { + System.out.print("#"); + repeatPrintString("=",16); + System.out.println("#"); + } +}