Upload files to "java/expression"

This commit is contained in:
2026-04-13 10:49:42 +03:00
parent b517d51b2e
commit 110790eb94
5 changed files with 333 additions and 0 deletions

View File

@@ -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<Integer> vars) {
return ceilInt(operand.evaluate(vars));
}
@Override
public BigInteger evaluateBi(List<BigInteger> vars) {
throw new UnsupportedOperationException(
"ceiling not supported for BigInteger"
);
}
@Override
public BigDecimal evaluateBd(List<BigDecimal> 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;
}
}

View File

@@ -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"
);
}
}

View File

@@ -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<Integer> vars) {
return intValue;
}
@Override
public BigInteger evaluateBi(List<BigInteger> vars) {
return biValue;
}
@Override
public BigDecimal evaluateBd(List<BigDecimal> 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();
}
}

View File

@@ -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<Integer> vars) {
return digitsSum(operand.evaluate(vars));
}
@Override
public BigInteger evaluateBi(List<BigInteger> vars) {
throw new UnsupportedOperationException(
"digits not supported for BigInteger"
);
}
@Override
public BigDecimal evaluateBd(List<BigDecimal> 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;
}
}

View File

@@ -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);
}
}