From 110790eb94168272d71a3636f60691784793969f Mon Sep 17 00:00:00 2001 From: codejava Date: Mon, 13 Apr 2026 10:49:42 +0300 Subject: [PATCH] Upload files to "java/expression" --- java/expression/Ceiling.java | 75 ++++++++++++++++++++++++++++++++ java/expression/Clear.java | 46 ++++++++++++++++++++ java/expression/Const.java | 84 ++++++++++++++++++++++++++++++++++++ java/expression/Digits.java | 83 +++++++++++++++++++++++++++++++++++ java/expression/Divide.java | 45 +++++++++++++++++++ 5 files changed, 333 insertions(+) create mode 100644 java/expression/Ceiling.java create mode 100644 java/expression/Clear.java create mode 100644 java/expression/Const.java create mode 100644 java/expression/Digits.java create mode 100644 java/expression/Divide.java diff --git a/java/expression/Ceiling.java b/java/expression/Ceiling.java new file mode 100644 index 0000000..c8a747a --- /dev/null +++ b/java/expression/Ceiling.java @@ -0,0 +1,75 @@ +package expression; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.List; + +/** + * @author Doschennikov Nikita (me@fymio.us) + */ +public class Ceiling extends AbstractExpression { + + private final AbstractExpression operand; + + public Ceiling(AbstractExpression operand) { + this.operand = operand; + } + + private static int ceilInt(int n) { + return Math.ceilDiv(n, 1000) * 1000; + } + + @Override + public int evaluate(int x) { + return ceilInt(operand.evaluate(x)); + } + + @Override + public int evaluate(int x, int y, int z) { + return ceilInt(operand.evaluate(x, y, z)); + } + + @Override + public int evaluate(List vars) { + return ceilInt(operand.evaluate(vars)); + } + + @Override + public BigInteger evaluateBi(List vars) { + throw new UnsupportedOperationException( + "ceiling not supported for BigInteger" + ); + } + + @Override + public BigDecimal evaluateBd(List vars) { + throw new UnsupportedOperationException( + "ceiling not supported for BigDecimal" + ); + } + + @Override + public String toString() { + return "ceiling(" + operand + ")"; + } + + @Override + public String toMiniString() { + if (operand instanceof AbstractBinaryOperation) { + return "ceiling(" + operand.toMiniString() + ")"; + } + return "ceiling " + operand.toMiniString(); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (!(obj instanceof Ceiling)) return false; + return operand.equals(((Ceiling) obj).operand); + } + + @Override + public int hashCode() { + return operand.hashCode() ^ 0x43454C47; + } +} diff --git a/java/expression/Clear.java b/java/expression/Clear.java new file mode 100644 index 0000000..4a9aba8 --- /dev/null +++ b/java/expression/Clear.java @@ -0,0 +1,46 @@ +package expression; + +import java.math.BigDecimal; +import java.math.BigInteger; + +/** + * @author Doschennikov Nikita (me@fymio.us) + */ +public class Clear extends AbstractBinaryOperation { + + public Clear(AbstractExpression l, AbstractExpression r) { + super(l, r); + } + + @Override + protected String getOperator() { + return "clear"; + } + + @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.clearBit(b.intValueExact()); + } + + @Override + protected BigDecimal applyBd(BigDecimal a, BigDecimal b) { + throw new UnsupportedOperationException( + "clear not supported for BigDecimal" + ); + } +} diff --git a/java/expression/Const.java b/java/expression/Const.java new file mode 100644 index 0000000..07e93ed --- /dev/null +++ b/java/expression/Const.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 Const extends AbstractExpression { + + private final int intValue; + private final BigInteger biValue; + private final BigDecimal bdValue; + private final String repr; + + public Const(int value) { + this.intValue = value; + this.biValue = BigInteger.valueOf(value); + this.bdValue = BigDecimal.valueOf(value); + this.repr = Integer.toString(value); + } + + public Const(BigInteger value) { + this.intValue = value.intValueExact(); + this.biValue = value; + this.bdValue = new BigDecimal(value); + this.repr = value.toString(); + } + + public Const(BigDecimal value) { + this.bdValue = value; + this.biValue = value.toBigIntegerExact(); + this.intValue = biValue.intValueExact(); + this.repr = value.toPlainString(); + } + + @Override + public int evaluate(int x) { + return intValue; + } + + @Override + public int evaluate(int x, int y, int z) { + return intValue; + } + + @Override + public int evaluate(List vars) { + return intValue; + } + + @Override + public BigInteger evaluateBi(List vars) { + return biValue; + } + + @Override + public BigDecimal evaluateBd(List vars) { + return bdValue; + } + + @Override + public String toString() { + return repr; + } + + @Override + public String toMiniString() { + return repr; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (!(obj instanceof Const)) return false; + return repr.equals(((Const) obj).repr); + } + + @Override + public int hashCode() { + return repr.hashCode(); + } +} diff --git a/java/expression/Digits.java b/java/expression/Digits.java new file mode 100644 index 0000000..5022c06 --- /dev/null +++ b/java/expression/Digits.java @@ -0,0 +1,83 @@ +package expression; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.List; + +/** + * @author Doschennikov Nikita (me@fymio.us) + */ +public class Digits extends AbstractExpression { + + private final AbstractExpression operand; + + public Digits(AbstractExpression operand) { + this.operand = operand; + } + + private static int digitsSum(int n) { + if (n == 0) return 0; + boolean negative = n < 0; + long abs = Math.abs((long) n); + int sum = 0; + while (abs > 0) { + sum += (int) (abs % 10); + abs /= 10; + } + return negative ? -sum : sum; + } + + @Override + public int evaluate(int x) { + return digitsSum(operand.evaluate(x)); + } + + @Override + public int evaluate(int x, int y, int z) { + return digitsSum(operand.evaluate(x, y, z)); + } + + @Override + public int evaluate(List vars) { + return digitsSum(operand.evaluate(vars)); + } + + @Override + public BigInteger evaluateBi(List vars) { + throw new UnsupportedOperationException( + "digits not supported for BigInteger" + ); + } + + @Override + public BigDecimal evaluateBd(List vars) { + throw new UnsupportedOperationException( + "digits not supported for BigDecimal" + ); + } + + @Override + public String toString() { + return "digits(" + operand + ")"; + } + + @Override + public String toMiniString() { + if (operand instanceof AbstractBinaryOperation) { + return "digits(" + operand.toMiniString() + ")"; + } + return "digits " + operand.toMiniString(); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (!(obj instanceof Digits)) return false; + return operand.equals(((Digits) obj).operand); + } + + @Override + public int hashCode() { + return operand.hashCode() ^ 0x44494754; + } +} diff --git a/java/expression/Divide.java b/java/expression/Divide.java new file mode 100644 index 0000000..47e9e03 --- /dev/null +++ b/java/expression/Divide.java @@ -0,0 +1,45 @@ +package expression; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.math.MathContext; + +/** + * @author Doschennikov Nikita (me@fymio.us) + */ +public class Divide extends AbstractBinaryOperation { + + public Divide(AbstractExpression l, AbstractExpression r) { + super(l, r); + } + + @Override + protected String getOperator() { + return "/"; + } + + @Override + protected int getPriority() { + return 2; + } + + @Override + protected boolean isRightAssoc() { + return true; + } + + @Override + protected int applyInt(int a, int b) { + return a / b; + } + + @Override + protected BigInteger applyBi(BigInteger a, BigInteger b) { + return a.divide(b); + } + + @Override + protected BigDecimal applyBd(BigDecimal a, BigDecimal b) { + return a.divide(b, MathContext.DECIMAL128); + } +}