diff --git a/Other/WeishauptOrlando_1/src/Count_ED.py b/Other/WeishauptOrlando_1/src/Count_ED.py new file mode 100644 index 0000000000000000000000000000000000000000..3b6973ac45bfddec3ddb0d1e95f281e494809eca --- /dev/null +++ b/Other/WeishauptOrlando_1/src/Count_ED.py @@ -0,0 +1,8 @@ +from src.interfaces import ICounter + + +class Counter(ICounter): + def count_ed(self, s: str) -> int: + """Zählt die Anzahl der Buchstaben 'E' und 'D' in einem String (Case-Insensitive).""" + s = s.upper() + return s.count("D") + s.count("E") diff --git a/Other/WeishauptOrlando_1/src/RomanConverter.py b/Other/WeishauptOrlando_1/src/RomanConverter.py new file mode 100644 index 0000000000000000000000000000000000000000..e45a5e2e995d29ad5f6296ecbaaf09f86fd4eed2 --- /dev/null +++ b/Other/WeishauptOrlando_1/src/RomanConverter.py @@ -0,0 +1,50 @@ +from src.interfaces import IRomanConverter + + +def int_to_roman(num: int) -> str: + """Konvertiert eine Dezimalzahl in eine römische Zahl.""" + val = [ + 1000, 900, 500, 400, + 100, 90, 50, 40, + 10, 9, 5, 4, + 1 + ] + syb = [ + "M", "CM", "D", "CD", + "C", "XC", "L", "XL", + "X", "IX", "V", "IV", + "I" + ] + roman_num = '' + i = 0 + while num > 0: + for _ in range(num // val[i]): + roman_num += syb[i] + num -= val[i] + i += 1 + return roman_num + + +class RomanConverter(IRomanConverter): + def roman_to_int(self, s: str) -> int: + """Konvertiert eine römische Zahl (String) in eine Dezimalzahl.""" + roman_values = { + 'I': 1, 'V': 5, 'X': 10, 'L': 50, + 'C': 100, 'D': 500, 'M': 1000 + } + + total = 0 + prev_value = 0 + + # Iteriere über die Zeichen der römischen Zahl von rechts nach links + for char in reversed(s.upper()): + value = roman_values.get(char) + if value is None: + raise ValueError(f"Ungültiges Zeichen '{char}' in der römischen Zahl.") + if value < prev_value: + total -= value + else: + total += value + prev_value = value + + return total diff --git a/Other/WeishauptOrlando_1/src/__init__.py b/Other/WeishauptOrlando_1/src/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..1b733abe8858fb6a04665bbb645814ee7457e95a --- /dev/null +++ b/Other/WeishauptOrlando_1/src/__init__.py @@ -0,0 +1,53 @@ +from src.calculator import Calculator +from src.Count_ED import Counter +from src.RomanConverter import RomanConverter, int_to_roman + +if __name__ == "__main__": + # Erstelle eine Instanz der Calculator-Klasse + calc = Calculator() + + # Eine Liste von Test-Ausdrücken, die überprüft werden sollen + test_expressions = [ + "2+3", # Einfacher Additionstest + "10-4/2", # Test mit Division und Subtraktion + "(3+5)*2", # Komplexerer Ausdruck mit Klammern + "3++5", # Ungültiger Ausdruck (doppelte Operatoren) + "3--5", # Ungültiger Ausdruck (doppelte Operatoren) + "10*/2", # Ungültiger Ausdruck (Operatorenkombinationen) + "5/0", # Division durch Null (Fehler) + "(3+5))", # Ungültiger Ausdruck (zu viele Klammern) + "abc", # Ungültiger Ausdruck (nur Buchstaben) + "-3*-3" # Gültiger Ausdruck mit Vorzeichen + ] + + # Schleife, um alle Test-Ausdrücke zu durchlaufen + for expr in test_expressions: + try: + # Versuche, den Ausdruck zu berechnen und gebe das Ergebnis aus + print(f"Eingabe: '{expr}' → Ausgabe: {calc.calculate(expr)}") + except Exception as e: + # Wenn ein Fehler auftritt, gebe die Fehlermeldung aus + print(f"Eingabe: '{expr}' → Fehler: {e}") +print("______________________________________________________________") +if __name__ == "__main__": + counter = Counter() + test_strings = ["Decker", "", "Hallo", "Der Esel", "D", "E", "d", "e"] + + for string in test_strings: + print(f"Eingabe: '{string}' → Ausgabe: {counter.count_ed(string)}") + +print("______________________________________________________________") + +if __name__ == "__main__": + converter = RomanConverter() + # Test römische Zahl in Dezimalzahl umwandeln + print("Test römische Zahl in Dezimalzahl umwandeln") + print(converter.roman_to_int("MCMXCIV")) # Ausgabe: 1994 + print(converter.roman_to_int("XIV")) # Ausgabe: 14 + print(converter.roman_to_int("CDXLIV")) # Ausgabe: 444 + + # Test Dezimalzahl in römische Zahl umwandeln + print("Test Dezimalzahl in römische Zahl umwand") + print(int_to_roman(1994)) # Ausgabe: MCMXCIV + print(int_to_roman(14)) # Ausgabe: XIV + print(int_to_roman(444)) # Ausgabe: CDXLIV \ No newline at end of file diff --git a/Other/WeishauptOrlando_1/src/calculator.py b/Other/WeishauptOrlando_1/src/calculator.py new file mode 100644 index 0000000000000000000000000000000000000000..f4b478561c819a9985b2e7466e9a3ad0cc363e1c --- /dev/null +++ b/Other/WeishauptOrlando_1/src/calculator.py @@ -0,0 +1,44 @@ +import re +from src.interfaces import ICalculator + + +class Calculator(ICalculator): + """Ein Taschenrechner, der mathematische Ausdrücke berechnet.""" + + def calculate(self, expression: str) -> float: + """Berechnet einen mathematischen Ausdruck als String und überprüft auf ungültige Eingaben.""" + try: + # Entfernt überflüssige Leerzeichen + expression = expression.replace(" ", "") + + # Nur erlaubte Zeichen (Zahlen, Operatoren, Klammern) und Zahlen mit Vorzeichen + if not re.match(r'^[0-9+\-*/().]+$', expression): + raise ValueError("Ungültige Zeichen im Ausdruck.") + + # Überprüfung auf doppelte Operatoren, aber keine Vorzeichen wie -* oder -+ + # Hier wird jetzt auch sichergestellt, dass -3*-3 gültig bleibt + if re.search(r'(?<!\d)[+\-*/]{2,}', expression): # Erfasst auch doppelte Operatoren wie ++, --, **, etc. + raise SyntaxError("Ungültige doppelte Operatoren im Ausdruck.") + + # Sicherstellen, dass Klammern ausgeglichen sind + if expression.count("(") != expression.count(")"): + raise ValueError("Fehlende oder zu viele Klammern.") + + # Sicherstellen, dass der Ausdruck nicht mit einem Operator beginnt oder endet, + # aber das Minuszeichen als Teil der Zahl akzeptiert wird + if re.match(r'^[*/]', expression) or re.match(r'[*/]$', expression): # Weitere Kontrolle + raise ValueError("Der Ausdruck darf nicht mit einem Operator beginnen oder enden.") + + # Evaluierung des mathematischen Ausdrucks mit eval() + result = eval(expression, {"__builtins__": None}, {}) + + # Sicherstellen, dass das Ergebnis numerisch ist + if isinstance(result, (int, float)): + return float(result) + else: + raise ValueError("Ungültiger Ausdruck.") + + except ZeroDivisionError: + raise ZeroDivisionError("Division durch Null ist nicht erlaubt.") + except (SyntaxError, TypeError, NameError) as e: + raise ValueError(f"Ungültiger mathematischer Ausdruck: {str(e)}") diff --git a/Other/WeishauptOrlando_1/src/interfaces.py b/Other/WeishauptOrlando_1/src/interfaces.py new file mode 100644 index 0000000000000000000000000000000000000000..e9c300fa4c5fe3a1808a70b66a310f34ebd431ee --- /dev/null +++ b/Other/WeishauptOrlando_1/src/interfaces.py @@ -0,0 +1,17 @@ +from abc import ABC, abstractmethod + +class ICounter(ABC): + @abstractmethod + def count_ed(self, s: str) -> int: + """Zählt die Buchstaben 'E' und 'D' in einem String (Case-Insensitive).""" + pass +class IRomanConverter(ABC): + @abstractmethod + def roman_to_int(self, s: str) -> int: + """Konvertiert eine römische Zahl (String) in eine Dezimalzahl.""" + pass + +class ICalculator(ABC): + @abstractmethod + def calculate(self, expression: str) -> float: + pass \ No newline at end of file