예제 #1
0
파일: Factor.java 프로젝트: OakOnEll/FindX
  @Override
  public MoveResult applyMove(
      Equation equation, int moveNum, List<Operation> operations, Operation appliedOrNull) {
    Expression lhs = equation.getLhs();
    Expression rhs = equation.getRhs();
    int num = 0;
    Expression lhsResult = factor(lhs);
    if (lhsResult != null) {
      num++;
    }
    Expression rhsResult = factor(rhs);
    if (rhsResult != null) {
      num++;
    }

    if (num == 1) {
      Expression zero = new Expression(0);
      if (lhs.isZero()) {
        String factored = "0 = (" + rhsResult + ")(" + expr + ")";
        return new MoveResult( //
            new MultipleSolutionMove(
                equation, (appliedOrNull == null ? this : appliedOrNull), factored, moveNum), //
            new SecondaryEquationMove(new Equation(zero, rhsResult), 1), //
            new SecondaryEquationMove(new Equation(zero, expr), 2));
      }
      if (rhs.isZero()) {
        String factored = "(" + lhsResult + ")(" + expr + ") = 0";
        return new MoveResult( //
            new MultipleSolutionMove(
                equation, (appliedOrNull == null ? this : appliedOrNull), factored, moveNum), //
            new SecondaryEquationMove(new Equation(lhsResult, zero), 1), //
            new SecondaryEquationMove(new Equation(expr, zero), 2));
      }
    }
    StringBuilder builder = new StringBuilder();
    if (lhsResult != null) {
      builder.append("(");
      builder.append(lhsResult);
      builder.append(")(");
      builder.append(expr);
      builder.append(")");
    } else {
      builder.append(lhs);
    }
    builder.append(" = ");
    if (rhsResult != null) {
      builder.append("(");
      builder.append(rhsResult);
      builder.append(")(");
      builder.append(expr);
      builder.append(")");
    } else {
      builder.append(rhs);
    }

    FormattedMove move =
        new FormattedMove(
            equation, (appliedOrNull == null ? this : appliedOrNull), builder.toString(), moveNum);
    return new MoveResult(move);
  }
예제 #2
0
파일: Factor.java 프로젝트: OakOnEll/FindX
 @Override
 public boolean canApply(Equation equation) {
   return canApply(equation.getLhs()) || canApply(equation.getRhs());
 }