From 62c0366b8dffc5fb0a9eab7e651fcf2e717dc3b1 Mon Sep 17 00:00:00 2001 From: codejava Date: Mon, 13 Apr 2026 10:51:09 +0300 Subject: [PATCH] Upload files to "java/expression" --- java/expression/Pow2.java | 47 +++++++++++++++++ java/expression/Power.java | 46 +++++++++++++++++ java/expression/Reverse.java | 84 +++++++++++++++++++++++++++++++ java/expression/SetBit.java | 46 +++++++++++++++++ java/expression/package-info.java | 7 +++ 5 files changed, 230 insertions(+) create mode 100644 java/expression/Pow2.java create mode 100644 java/expression/Power.java create mode 100644 java/expression/Reverse.java create mode 100644 java/expression/SetBit.java create mode 100644 java/expression/package-info.java diff --git a/java/expression/Pow2.java b/java/expression/Pow2.java new file mode 100644 index 0000000..2dba043 --- /dev/null +++ b/java/expression/Pow2.java @@ -0,0 +1,47 @@ +package expression; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.List; + +/** + * @author Doschennikov Nikita (me@fymio.us) + */ +public class Pow2 extends AbstractExpression { + private final AbstractExpression operand; + + public Pow2(AbstractExpression operand) { + this.operand = operand; + } + + private static int pow2(int n) { + if (n < 0) throw new ArithmeticException("pow₂ of negative number: " + n); + if (n >= 31) throw new OverflowException("pow₂"); + return 1 << n; + } + + @Override public int evaluate(int x) { return pow2(operand.evaluate(x)); } + @Override public int evaluate(int x, int y, int z) { return pow2(operand.evaluate(x,y,z)); } + @Override public int evaluate(List vars) { return pow2(operand.evaluate(vars)); } + @Override public BigInteger evaluateBi(List vars) { throw new UnsupportedOperationException(); } + @Override public BigDecimal evaluateBd(List vars) { throw new UnsupportedOperationException(); } + + @Override public String toString() { return "pow₂(" + operand + ")"; } + + @Override + public String toMiniString() { + if (operand instanceof AbstractBinaryOperation) { + return "pow₂(" + operand.toMiniString() + ")"; + } + return "pow₂ " + operand.toMiniString(); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (!(obj instanceof Pow2)) return false; + return operand.equals(((Pow2) obj).operand); + } + + @Override public int hashCode() { return operand.hashCode() ^ 0x504F5732; } +} \ No newline at end of file diff --git a/java/expression/Power.java b/java/expression/Power.java new file mode 100644 index 0000000..f3935c2 --- /dev/null +++ b/java/expression/Power.java @@ -0,0 +1,46 @@ +package expression; + +import java.math.BigDecimal; +import java.math.BigInteger; + +/** + * @author Doschennikov Nikita (me@fymio.us) + */ +public class Power extends AbstractBinaryOperation { + public Power(AbstractExpression l, AbstractExpression r) { super(l, r); } + + @Override protected String getOperator() { return "**"; } + @Override protected int getPriority() { return 3; } + @Override protected boolean isRightAssoc() { return true; } + + @Override + protected int applyInt(int base, int exp) { + if (exp < 0) throw new ArithmeticException("negative exponent"); + if (base == 0 && exp == 0) throw new ArithmeticException("zero to the power of zero"); + if (exp == 0) return 1; + if (base == 0) return 0; + int result = 1; + int b = base; + int e = exp; + while (e > 0) { + if ((e & 1) == 1) { + result = checkedMul(result, b); + } + if (e > 1) b = checkedMul(b, b); + e >>= 1; + } + return result; + } + + private static int checkedMul(int a, int b) { + if (a == 0 || b == 0) return 0; + if (a == Integer.MIN_VALUE && b == -1) throw new OverflowException("power"); + if (b == Integer.MIN_VALUE && a == -1) throw new OverflowException("power"); + int result = a * b; + if (result / a != b) throw new OverflowException("power"); + return result; + } + + @Override protected BigInteger applyBi(BigInteger a, BigInteger b) { return a.pow(b.intValueExact()); } + @Override protected BigDecimal applyBd(BigDecimal a, BigDecimal b) { return a.pow(b.intValueExact()); } +} \ No newline at end of file diff --git a/java/expression/Reverse.java b/java/expression/Reverse.java new file mode 100644 index 0000000..eaacdf1 --- /dev/null +++ b/java/expression/Reverse.java @@ -0,0 +1,84 @@ +package expression; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.List; + +/** + * @author Doschennikov Nikita (me@fymio.us) + */ +public class Reverse extends AbstractExpression { + + private final AbstractExpression operand; + + public Reverse(AbstractExpression operand) { + this.operand = operand; + } + + private static int reverseInt(int n) { + boolean negative = n < 0; + long abs = Math.abs((long) n); + long reversed = 0; + while (abs > 0) { + reversed = reversed * 10 + (abs % 10); + abs /= 10; + } + long result = negative ? -reversed : reversed; + return (int) result; + } + + @Override + public int evaluate(int x) { + return reverseInt(operand.evaluate(x)); + } + + @Override + public int evaluate(int x, int y, int z) { + return reverseInt(operand.evaluate(x, y, z)); + } + + @Override + public int evaluate(List vars) { + return reverseInt(operand.evaluate(vars)); + } + + @Override + public BigInteger evaluateBi(List vars) { + throw new UnsupportedOperationException( + "reverse not supported for BigInteger" + ); + } + + @Override + public BigDecimal evaluateBd(List vars) { + throw new UnsupportedOperationException( + "reverse not supported for BigDecimal" + ); + } + + @Override + public String toString() { + return "reverse(" + operand + ")"; + } + + @Override + public String toMiniString() { + if (operand instanceof AbstractBinaryOperation) { + return "reverse(" + operand.toMiniString() + ")"; + } + + return "reverse " + operand.toMiniString(); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (!(obj instanceof Reverse)) return false; + return operand.equals(((Reverse) obj).operand); + } + + @Override + public int hashCode() { + return operand.hashCode() ^ 0x52455645; + } +} diff --git a/java/expression/SetBit.java b/java/expression/SetBit.java new file mode 100644 index 0000000..78c07d8 --- /dev/null +++ b/java/expression/SetBit.java @@ -0,0 +1,46 @@ +package expression; + +import java.math.BigDecimal; +import java.math.BigInteger; + +/** + * @author Doschennikov Nikita (me@fymio.us) + */ +public class SetBit extends AbstractBinaryOperation { + + public SetBit(AbstractExpression l, AbstractExpression r) { + super(l, r); + } + + @Override + protected String getOperator() { + return "set"; + } + + @Override + protected int getPriority() { + return 0; + } + + @Override + protected boolean isRightAssoc() { + return true; + } + + @Override + protected int applyInt(int a, int b) { + return a | (1 << b); + } + + @Override + protected BigInteger applyBi(BigInteger a, BigInteger b) { + return a.setBit(b.intValueExact()); + } + + @Override + protected BigDecimal applyBd(BigDecimal a, BigDecimal b) { + throw new UnsupportedOperationException( + "set not supported for BigDecimal" + ); + } +} diff --git a/java/expression/package-info.java b/java/expression/package-info.java new file mode 100644 index 0000000..8514a1c --- /dev/null +++ b/java/expression/package-info.java @@ -0,0 +1,7 @@ +/** + * Tests for Expressions homework + * of Introduction to Programming course. + * + * @author Georgiy Korneev (kgeorgiy@kgeorgiy.info) + */ +package expression;