From 688478cf801857b5b99be34362dee535d9f94482 Mon Sep 17 00:00:00 2001 From: "egor.rutkowski" Date: Fri, 28 Jun 2024 16:19:39 +0200 Subject: [PATCH] Dateien nach "src/main/java/com/example" hochladen --- src/main/java/com/example/Expression.java | 120 ++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 src/main/java/com/example/Expression.java diff --git a/src/main/java/com/example/Expression.java b/src/main/java/com/example/Expression.java new file mode 100644 index 0000000..7868dd1 --- /dev/null +++ b/src/main/java/com/example/Expression.java @@ -0,0 +1,120 @@ +package de.tudortmund.cs.mnp.project2; + +import java.util.Random; + +/** + * Interface for mathematical Expressions + */ +public sealed interface Expression permits Expression.Add, Expression.Mul, Expression.Sub, Expression.Val { + /** + * Addition term: left + right + * @param left + * @param right + */ + record Add(Expression left, Expression right) implements Expression{ + public String toString(){ + return "(" + left.toString() + "+" + right.toString() + ")"; + } + public int eval() { + return left.eval() + right.eval(); + } + public int runtime(){ + return 1 + Math.max(left.runtime(), right.runtime()); + } + } + + /** + * Multiplication term: left * right + * @param left + * @param right + */ + record Mul(Expression left, Expression right) implements Expression{ + public String toString(){ + return "(" + left.toString() + "*" + right.toString() + ")"; + } + public int eval() { + return left.eval() * right.eval(); + } + + public int runtime(){ + return 1 + Math.max(left.runtime(), right.runtime()); + } + } + + /** + * Subtraction term: left - right + * @param left + * @param right + */ + record Sub(Expression left, Expression right) implements Expression{ + public int runtime(){ + return 1 + Math.max(left.runtime(), right.runtime()); + } + public String toString(){ + return "(" + left.toString() + "-" + right.toString() + ")"; + } + public int eval() { + return left.eval() - right.eval(); + } + } + + /** + * Encapsulates a single integer + * @param inner + */ + record Val(int inner) implements Expression{ + public int runtime(){ + return 0; + } + public String toString(){ + return String.valueOf(inner); + }public int eval() { + return inner; + } + } + + /** + * @return A string representation of the expression + */ + String toString(); + + /** + * @return The computed result of the expression + */ + int eval(); + + /** + * @return The estimated maximal runtime + */ + int runtime(); + + /** + * Build a random expression of max depth `max_depth` and has integer values up to `max_val`. + * @param max_depth + * @param max_val + * @return A random expression + */ + static Expression generateExpression(int max_depth, int max_val){ + var x = new Random(); + int choice = x.nextInt(4); + if (max_depth <= 1) { + choice = 4; + } + + return switch (choice) { + case 0 -> new Add( + generateExpression(max_depth-1, max_val), + generateExpression(max_depth-1, max_val) + ); + case 1 -> new Mul( + generateExpression(max_depth-1, max_val), + generateExpression(max_depth-1, max_val) + ); + case 2 -> new Sub( + generateExpression(max_depth-1, max_val), + generateExpression(max_depth-1, max_val) + ); + default -> new Val(x.nextInt(max_val+1)+1); + }; + } +}