public void insertOperandToStack(ListRepresentation listExpression) {

    for (int x = 0; x < listExpression.operands.size(); x++) {
      ListRepresentation current = listExpression.operands.get(x);
      if (current.getNodeVal().getAtomType() == AtomType.OPERAND) {
        computationStack.push(current.getNodeVal());
      }
    }
  }
 private static void validateParent(ListRepresentation list) {
   for (int x = 0; x < list.operands.size(); x++) {
     ListRepresentation current = list.operands.get(x);
     if (current.getNodeVal().getAtomType() == AtomType.OPERATOR) {
       if (current
           .getNodeVal()
           .getVariablesOrOperator()
           .equals(list.getNodeVal().getVariablesOrOperator())) {
         list.operands.addAll(0, current.operands);
         list.operands.remove(current);
       }
     }
   }
 }
예제 #3
0
 @Override
 protected Representation dispatchStringArrayProperty(String[] property, Void param) {
   return ListRepresentation.strings(property);
 }
  public void processOperator(ListRepresentation listExpression) {
    String operator = listExpression.getNodeVal().getVariablesOrOperator();
    if (computationStack.isEmpty()) {
      List<List<ExpressionAtom>> all = new ArrayList<List<ExpressionAtom>>();
      while (!expressionQueue.isEmpty()) {
        all.add(expressionQueue.dequeue());
      }
      List<ExpressionAtom> main = null;
      if (operator.equals("*")) {
        for (int x = 0; x < all.size() - 1; x++) {
          if (x + 1 >= all.size()) {
            System.out.println("ERROR @ Multiplicaiton NO DATA");
          } else {
            if (main == null) {
              main = operatorsMap.get(operator).evaluate(all.get(x), all.get(x + 1));
            } else {
              main = operatorsMap.get(operator).evaluate(main, all.get(x + 1));
            }
          }
        }
        expressionQueue.enqueue(main);
      } else if (operator.equals("+")) {
        main = new ArrayList<ExpressionAtom>();
        for (int x = 0; x < all.size(); x++) {
          for (int y = 0; y < all.get(x).size(); y++) {
            main.add(all.get(x).get(y));
          }
        }
        expressionQueue.enqueue(operatorsMap.get(operator).evaluate(main, null));
      }
    }

    while (!computationStack.isEmpty()) {
      if (computationStack.size() == 1) {
        List<ExpressionAtom> prev = expressionQueue.dequeue();
        List<ExpressionAtom> loperands = new ArrayList<ExpressionAtom>();
        while (!prev.isEmpty()) {
          loperands.add(prev.get(0));
          prev.remove(0);
        }
        List<ExpressionAtom> roperands = new ArrayList<ExpressionAtom>();
        while (!computationStack.isEmpty()) {
          roperands.add(computationStack.pop());
        }
        if (operator.equals("^")) {
          ExpressionAtom degree = roperands.get(0);
          List<ExpressionAtom> copy = null;
          for (int x = 1; x < degree.getCoefficient(); x++) {
            if (x == 1) {
              copy = operatorsMap.get(operator).evaluate(loperands, loperands);
            } else {
              operatorsMap.get(operator).evaluate(copy, loperands);
            }
          }
          expressionQueue.enqueue(copy);
        } else {
          expressionQueue.enqueue(operatorsMap.get(operator).evaluate(loperands, roperands));
        }

      } else {
        List<ExpressionAtom> loperands = new ArrayList<ExpressionAtom>();
        while (!computationStack.isEmpty()) {
          loperands.add(computationStack.pop());
        }
        if (operator.equals("^")) {
          int degree = loperands.get(0).getCoefficient();
          List<ExpressionAtom> main = new ArrayList<ExpressionAtom>();
          for (int ope = 1; ope < loperands.size(); ope++) {
            main.add(loperands.get(ope));
          }

          List<ExpressionAtom> copy = null;
          for (int c = 1; c < degree; c++) {
            if (c == 1) {
              copy = operatorsMap.get(operator).evaluate(main, null);
            } else {
              copy = operatorsMap.get(operator).evaluate(main, copy);
            }
          }

          if (listExpression.isNegative()) {
            List<ExpressionAtom> mult = new ArrayList<ExpressionAtom>();
            mult.add(new ExpressionAtom("", AtomType.OPERAND, -1));
            copy = operatorsMap.get("*").evaluate(copy, mult);
          }

          expressionQueue.enqueue(copy);
        } else {
          expressionQueue.enqueue(operatorsMap.get(operator).evaluate(loperands, null));
        }
      }
    }

    Utils.printExpression(expressionQueue.peek());
  }
  private ListRepresentation convertToListRepresentation() {
    SimpleQueue<ExpressionAtom> output = new SimpleQueue<ExpressionAtom>();
    SimpleStack<ExpressionAtom> stack = new SimpleStack<ExpressionAtom>();

    for (ExpressionAtom atom : infixExpression) {
      Utils.printAtom(atom);
      if (atom.getAtomType() == AtomType.OPERAND) {
        output.enqueue(atom);
      } else if (atom.getAtomType() == AtomType.OPERATOR
          && !atom.getVariablesOrOperator().equals("(")
          && !atom.getVariablesOrOperator().equals(")")) {
        while (!stack.isEmpty()) {
          ExpressionAtom peek = stack.peek();
          if (isTopwithHigerPrecedence(peek, atom)) {
            output.enqueue(stack.pop());
          } else break;
        }
        stack.push(atom);
      } else if (atom.getVariablesOrOperator().equals("(")) {
        stack.push(atom);
      } else if (atom.getVariablesOrOperator().equals(")")) {
        boolean isLeftFound = false;
        while (!stack.isEmpty()) {
          ExpressionAtom top = stack.pop();
          if (!top.getVariablesOrOperator().equals("(")) {
            output.enqueue(top);
          } else {
            isLeftFound = true;
            break;
          }
        }
        if (!isLeftFound) throw new RuntimeException("Parantheses are not balanced");
      }
    }

    while (!stack.isEmpty()) {
      ExpressionAtom top = stack.pop();
      if (top.getVariablesOrOperator().equals("(") || top.getVariablesOrOperator().equals(")")) {
        throw new RuntimeException("Parantheses are not balanced");
      }
      output.enqueue(top);
    }

    ListRepresentation temp = new ListRepresentation();
    temp.setNodeVal(new ExpressionAtom("", null, 0));
    SimpleStack<ListRepresentation> operands = new SimpleStack<ListRepresentation>();
    List<ListRepresentation> copy = new ArrayList<ListRepresentation>();

    System.out.println();

    while (!output.isEmpty()) {
      ExpressionAtom atom = output.dequeue();
      Utils.printAtom(atom);
      if (atom.getAtomType() == AtomType.OPERAND) {
        ListRepresentation operand = new ListRepresentation();
        operand.setNodeVal(atom);
        operands.push(operand);
      } else {
        if (atom.getVariablesOrOperator().equals("-")
            || atom.getVariablesOrOperator().equals("%")) {
          ListRepresentation peek = operands.peek();
          if (peek.operands.size() == 0) {
            peek.getNodeVal().setCoefficient(peek.getNodeVal().getCoefficient() * -1);
          } else {
            peek.setNegative(true);
          }

          if (atom.getVariablesOrOperator().equals("%")) {
            if (peek.operands.size() == 0) continue;
            else {
              continue;
            }
          }
          atom = new ExpressionAtom("+", AtomType.OPERATOR, 1);
        }
        ListRepresentation operator = new ListRepresentation();
        operator.setNodeVal(atom);

        ListRepresentation op = operands.pop();

        operator.operands.add(operands.pop());
        operator.operands.add(op);

        copy.add(operator);
        operands.push(operator);
        temp = operator;
      }
    }

    ListRepresentation cop = operands.pop();
    validate(cop, cop);
    listRepresentation = cop;

    return listRepresentation;
  }