Not logged inCSS-Forum
Forum CSS-Online Help Search Login
CSS-Shop Impressum Datenschutz
Up Topic Hauptforen / Schachprogrammierung / Python auf dem iPad:
- - By Lothar Jung Date 2023-03-09 11:37
Hier ein guter Calculator:

import re
from math import *

functions = {"sin": sin, "cos": cos, "tan": tan, "arcsin": asin, "arccos": acos, "arctan": atan, "sinh": sinh, "cosh": cosh, "tanh": tanh, "arcsinh": asinh, "arccosh": acosh, "arctanh": atanh}

def solve(equation):
    left, right = equation.split("=")
    return eval(right.strip(), {"x": eval(left.strip())})

def main():
    print("Welcome to the calculator program!")
    print("Type 'help' to see a list of available functions")

    while True:
        expression = input("Enter an expression to evaluate: ")
        if expression.lower() == "exit":
            break

        elif expression.lower() == "help":
            print("Available functions:")
            print("-" * 30)
            print("Standard functions:")
            print("addition(a, b)")
            print("subtraction(a, b)")
            print("multiplication(a, b)")
            print("division(a, b)")
            print("exponent(a, b)")
            print("square_root(a)")
            print("sin(a)")
            print("cos(a)")
            print("tan(a)")
            print("arcsin(a)")
            print("arccos(a)")
            print("arctan(a)")
            print("sinh(a)")
            print("cosh(a)")
            print("tanh(a)")
            print("arcsinh(a)")
            print("arccosh(a)")
            print("arctanh(a)")
            print("ln(a)")
            print("log10(a)")
            print("-" * 30)
            print("Other functions:")
            print("matrix_addition(matrix_a, matrix_b)")
            print("matrix_subtraction(matrix_a, matrix_b)")
            print("matrix_multiplication(matrix_a, matrix_b)")
            print("matrix_transpose(matrix)")
            print("matrix_determinant(matrix)")
            print("matrix_inverse(matrix)")
            print("-" * 30)

        else:
            try:
                # Überprüfung auf ungültige mathematische Funktionen
                if any(func in expression.lower() for func in functions.keys()):
                    # Konvertiere Argumente in Zahlen
                    expression = re.sub(r"([a-zA-Z]+)", r"functions['\1']", expression)
                    result = eval(expression)
                    print(f"{expression} = {result}")
                # Überprüfung auf gültige Gleichung
                elif "=" in expression:
                    if expression.count("=") > 1:
                        raise ValueError("Invalid equation, please use only one '=' sign")
                    left, right = expression.split("=")
                    if not left.strip() or not right.strip():
                        raise ValueError("Invalid equation, please use the form 'equation = 0'")
                    result = solve(expression)
                    print(result)
                else:
                    result = eval(expression)
                    print(f"{expression} = {result}")
            except ValueError as ve:
                print(ve)
            except SyntaxError:
                print("Invalid expression")
            except ZeroDivisionError:
                print("Cannot divide by zero")
            except Exception as e:
                print(f"An unexpected error occurred: {e}")

if __name__ == "__main__":
    main()

Auf dem iPad mit diesem hochgelobten Programm ausführbar:

https://apps.apple.com/de/app/pythonista-3/id1085978097

Zusammen mit ChatGPT ergibt sich die Möglichkeit auf iOS Python Programme zu generieren, testen und auszuführen.

Das IPad erhält dadurch eine größere Funktionsbreite.

Lothar
Parent - By Lothar Jung Date 2023-03-09 17:46
Hier ein umfangreiches Kalkulationsprogramm.
Es wurde getestet.
Es ist jedenfalls lauffähig.
Anhand der Beispiele und des Ausdrucks im Programm erkennt man die Funktionen.

import ast
import math
import cmath

# Beispiel-Ausdrücke
examples = [
    "2 + 3 * 4",
    "math.sin(45) + math.cos(45)",
    "2 * math.sin(math.radians(30)) * math.cos(math.radians(60))",
    "4**3 + 3*2//4",
    "x + y",
    "math.sin(x) + math.cos(y)",
    "math.sqrt(25)",
    "myfunc(2, 3, 4)",
    "z**2 + 2*z + 1",
    "z**3 + 2*z**2 + 3*z + 4",
    "cmath.exp(cmath.pi * 1j / 3)",
    "cmath.log10(100 + 50j)",
    "math.log(100, 10)",
    "math.exp(2)",
    "math.factorial(5)",
    "math.gcd(12, 18)",
]

# Variablen
x = 3
y = 4
z = 5
variables = {'x': x, 'y': y, 'z': z}

# Funktionen
def myfunc(a, b, c):
    return a * b + c

functions = {'myfunc': myfunc}

# Ausdrücke auswerten
for example in examples:
    try:
        if "cmath." in example:
            result = eval(example, variables, {'math': math, 'cmath': cmath, **functions})
        else:
            result = eval(example, variables, {'math': math, **functions})
        print(f"{example.ljust(40)} = {result}")
    except Exception as e:
        print(f"Fehler beim Auswerten von '{example}': {str(e)}")

# Eingabe-Schleife
while True:
    expression = input("Ausdruck eingeben (exit zum Beenden): ")
    if expression == 'exit':
        break
    try:
        # Variablen definieren
        if '=' in expression:
            var_name, var_value = expression.split('=')
            var_name = var_name.strip()
            var_value = var_value.strip()
            if var_name.isalpha():
                variables[var_name] = eval(var_value, variables, {'math': math, 'cmath': cmath, **functions})
                print(f"{var_name.ljust(20)} = {variables[var_name]}")
            else:
                print(f"Ungültiger Variablenname '{var_name}'")
        else:
            if "cmath." in expression:
                result = eval(expression, variables, {'math': math, 'cmath': cmath, **functions})
            else:
                result = eval(expression, variables, {'math': math, **functions})
            print(f"{expression.ljust(40)} = {result}")
    except Exception as e:
        print(f"Fehler beim Auswerten von '{expression}': {str(e)}")
- By Lothar Jung Date 2023-03-09 19:58
Hier ein Link auf Pythonista:

https://omz-software.com/pythonista/
Up Topic Hauptforen / Schachprogrammierung / Python auf dem iPad:

Powered by mwForum 2.29.3 © 1999-2014 Markus Wichitill