add figur & project

This commit is contained in:
Luca Conte 2022-10-27 22:16:50 +02:00
parent 57f9f19454
commit e54b513628
6 changed files with 197 additions and 0 deletions

Binary file not shown.

View File

@ -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 == '/';
}
}

BIN
uebungen/h04_1/t01.class Normal file

Binary file not shown.

13
uebungen/h04_1/t01.java Normal file
View File

@ -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();
}
}
}

Binary file not shown.

View File

@ -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("#");
}
}