Ejemplo n.º 1
0
 /**
  * Method exists to add an operator to an equation and keep the tree correct. If the cellValue has
  * multiplication or division, the method will call itself with an attempt to put in RightSon If
  * the cellValue has addition or subtraction and the rightSon has an operator, call again with
  * attempt to put in rightSon. If neither are true, create new TreeCell for rightSong, add the
  * operator to rightSon, and have the rightSon's leftSon be the previous rightSon's value
  *
  * @param op: operator to be added to the tree
  */
 public void addOpAndReform(char op) {
   if (cellValue.length() == 1) {
     if ((cellValue.charAt(0) == '*') || (cellValue.charAt(0) == '/')) {
       rightSon.addOpAndReform(op);
       status = LEFT_COMPLETED;
     } else {
       if (rightSon.cellValue.length() == 1) {
         rightSon.addOpAndReform(op);
         status = LEFT_COMPLETED;
       } else {
         String value = rightSon.cellValue;
         this.rightSon = new TreeCell();
         this.rightSon.cellValue = op + "";
         this.rightSon.leftSon = new TreeCell(value);
         this.rightSon.status = LEFT_COMPLETED;
         status = LEFT_COMPLETED;
       }
     }
   } else {
     String value = cellValue;
     this.leftSon = new TreeCell();
     this.leftSon.cellValue = value;
     this.leftSon.status = RIGHT_COMPLETED;
     this.cellValue = op + "";
     this.rightSon = null;
     status = LEFT_COMPLETED;
   }
 }
Ejemplo n.º 2
0
 /**
  * This method exists to make it easier to see when and where classes are changing the formula
  * This also serves another purpose by adding spaces and formatting when needed, something that
  * the normal setFormula() method cannot do.
  *
  * @param op
  */
 public void addToFormula(char op) throws SyntaxErrorException {
   // if the cell to add the operator in does not exist yet, create it
   if ((opList == null) || (nextEntry >= opList.size())) opList.add(new TreeCell());
   TreeCell currentCell = opList.get(nextEntry); // get the current cell that we will be using
   try {
     if (((op == '/') || (op == '*')) && (nextEntry > 0)) {
       if ((opList.get(nextEntry).getleftSon() == null)
           && (opList.get(nextEntry - 1).getcellValue().length() == 1)) {
         opList.remove(nextEntry);
         nextEntry--;
         currentCell = opList.get(nextEntry);
         currentCell.addOpAndReform(op);
       } else if ((opList.get(nextEntry).getcellValue() == null)
           && (opList.get(nextEntry - 1).getcellValue().length() >= 1)) {
         // this is the first operator to be entered, the first entry being an input.
         opList.remove(nextEntry);
         nextEntry--;
         currentCell = opList.get(nextEntry);
         currentCell.setleftSon(new TreeCell(currentCell.getcellValue()));
         currentCell.setrightSon(null);
         currentCell.setcellValue(op + "");
         currentCell.setstatusCell(TreeCell.LEFT_COMPLETED);
       }
     } else {
       currentCell.addOpInCell(op);
     }
   } catch (SyntaxErrorException e) {
     throw e;
   }
 }