Upload files to "java/expression"

This commit is contained in:
2026-04-13 10:51:09 +03:00
parent a373740014
commit 62c0366b8d
5 changed files with 230 additions and 0 deletions

47
java/expression/Pow2.java Normal file
View File

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

View File

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

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

View File

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

View File

@@ -0,0 +1,7 @@
/**
* Tests for <a href="https://www.kgeorgiy.info/courses/prog-intro/homeworks.html#expressions">Expressions</a> homework
* of <a href="https://www.kgeorgiy.info/courses/prog-intro/">Introduction to Programming</a> course.
*
* @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
*/
package expression;