示例#1
0
 /**
  * Method exists to find the correct TreeCell that the operation should be assigned
  *
  * @param op set to its correct location in tree, current location if a math symbol, left if no
  *     value entered, and right if one value already entered
  * @exception SyntaxErrorException : Exception thrown if -char not passed -multiplication or
  *     division occurs and there is no left value entered -addition or subtraction and there is
  *     already a left value entered
  */
 void addOpInCell(char op) throws SyntaxErrorException {
   SyntaxErrorException e = null;
   if (this.getcellValue() == null) { // if the operator is null (default)
     // fill in the operator
     this.setcellValue(op + "");
     if ((op == '/') || (op == '*')) {
       if (this.getleftSon() == null) {
         Logger.getLogger()
             .concatOut(
                 amt.log.Logger.DEBUG,
                 "Formula",
                 "SyntaxErrorException: addition of operator "
                     + op
                     + " where the left input has not been given");
         e.fillInStackTrace();
         throw e;
       } else {
         this.status = LEFT_COMPLETED;
       }
     } else {
       if (this.getleftSon() != null) {
         Logger.getLogger()
             .concatOut(
                 amt.log.Logger.DEBUG,
                 "Formula",
                 "SyntaxErrorException: addition of operator "
                     + op
                     + " where the left subtree already has something in it");
         e.fillInStackTrace();
         throw e;
       } else this.status = LEFT_COMPLETED;
     }
   } else {
     if (status == NOT_COMPLETED) leftSon.addOpInCell(op);
     else if (status == LEFT_COMPLETED) rightSon.addOpInCell(op);
     else {
       // this cell becomes the left part of the new cell
       this.setleftSon(new TreeCell(this.cellValue));
       this.status = LEFT_COMPLETED;
       this.setcellValue(op + "");
     }
   }
 }
示例#2
0
  /**
   * Method exists to remove the value in the Cell
   *
   * @exception SytaxErrorException: thrown if -the cell's status is NOT_COMPLETED -the cell is
   *     RIGHT_COMPLETED, the right is null, and the cellValue is a math operator
   * @returns true if a value can be removed, or false if a math operator is in the cell
   */
  public boolean removeInCell() throws SyntaxErrorException {

    SyntaxErrorException e = null;

    switch (status) {
      case NOT_COMPLETED:
        Logger.getLogger()
            .concatOut(
                amt.log.Logger.DEBUG,
                "Formula",
                "SyntaxErrorException: try to remove a cell which is void, method fails");
        e.fillInStackTrace();
        throw e;
      case LEFT_COMPLETED:
        if (this.getleftSon() == null) {
          // we are in a cell that only has either an operator + or -, or an input
          if (this.cellValue.length() > 1) {
            // the element to remove is an input
            this.cellValue = null;
            status = NOT_COMPLETED;
            return false;
          } else if ((this.cellValue.charAt(0) != '*') && (this.cellValue.charAt(0) != '/')) {
            // there might be something else in the right son that needs to be removed.
            if (rightSon == null) {
              // the element to remove is an operator
              this.cellValue = null;
              status = NOT_COMPLETED;
              return true;
            } else {
              TreeCell currentCell = rightSon;
              boolean res = currentCell.removeInCell();
              if (currentCell.getstatusCell() == NOT_COMPLETED) {
                rightSon = null;
                this.status = LEFT_COMPLETED;
              }
              return res;
            }
          } else {
            this.cellValue = null;
            status = NOT_COMPLETED;
            return true;
          }
        } else if (this.rightSon != null) {
          swapWithInputs();
          return true;
        } else {
          // the element to remove is an operator, and there is something in the left-son, the cell
          // is taken away and the content of the son is put one up
          swapWithLeftSon();
          return true;
        }
      case RIGHT_COMPLETED:
        if (this.getrightSon() == null) {
          // this should be a leaf, containning an input only
          if (this.cellValue.length() > 1) {
            // the element to remove is an input
            this.cellValue = null;
            status = NOT_COMPLETED;
            return false;
          } else {
            Logger.getLogger()
                .concatOut(
                    amt.log.Logger.DEBUG,
                    "Formula",
                    "SyntaxErrorException: try to remove an input in an operator "
                        + this.cellValue
                        + " cell");
            e.fillInStackTrace();
            throw e;
          }
        } else {
          boolean res = this.rightSon.removeInCell();
          if (rightSon.status == NOT_COMPLETED) {
            rightSon = null;
          }
          this.status = LEFT_COMPLETED;
          return res;
        }
    }
    return false;
  }
示例#3
0
 /**
  * Method exists to find the correct TreeCell that the input value should be assigned
  *
  * @exception SyntaxErrorException: Exception thrown if -string not passed -current location
  *     already has a string -multiplication or division occurs and there is no left value entered
  *     -addition or subtraction and there is already a left value entered -cellValue is null and
  *     both children are not
  * @param input set to its correct location in tree: current location if nothing has been entered,
  *     left if no value entered, right if one value already entered
  */
 void addInputInCell(String input) throws SyntaxErrorException {
   SyntaxErrorException e = null;
   if (this.getcellValue() != null) { // if the operator is null (default)
     if (this.getcellValue().length() > 1) {
       Logger.getLogger()
           .concatOut(
               amt.log.Logger.DEBUG,
               "Formula",
               "SyntaxErrorException: addition of input "
                   + input
                   + " in an inputs cell that already is filled with "
                   + this.getcellValue());
       e.fillInStackTrace();
       throw e;
     } else {
       if ((this.getcellValue().charAt(0) == '/') || (this.getcellValue().charAt(0) == '*')) {
         if (this.getleftSon() == null) {
           Logger.getLogger()
               .concatOut(
                   amt.log.Logger.DEBUG,
                   "Formula",
                   "SyntaxErrorException: addition of input "
                       + input
                       + " in a "
                       + this.getcellValue()
                       + " operator cell with nothing in the left son");
           e.fillInStackTrace();
           throw e;
         } else if (this.getrightSon() == null) {
           // right son of this cell free, this is where it needs to be added
           this.setrightSon(new TreeCell(input));
           this.status = RIGHT_COMPLETED;
         } else {
           // needs to look in the right tree to find the space to insert the input
           this.rightSon.addInputInCell(input);
           if (this.rightSon.getstatusCell() == RIGHT_COMPLETED) this.status = RIGHT_COMPLETED;
         }
       } else {
         if (this.getleftSon() != null) {
           if (this.getrightSon() == null) {
             this.setrightSon(new TreeCell(input));
             this.status = RIGHT_COMPLETED;
           } else {
             Logger.getLogger()
                 .concatOut(
                     amt.log.Logger.DEBUG,
                     "Formula",
                     "SyntaxErrorException: addition of input "
                         + input
                         + " where operator is "
                         + this.getcellValue()
                         + " and the left subtree already has something in it");
             e.fillInStackTrace();
             throw e;
           }
         } else {
           if (this.getrightSon() == null) {
             // right son of this cell free, this is where it needs to be added
             this.setrightSon(new TreeCell(input));
             this.status = RIGHT_COMPLETED;
           } else {
             // needs to look in the right tree to find the space to insert the input
             this.rightSon.addInputInCell(input);
             if (this.rightSon.getstatusCell() == RIGHT_COMPLETED) this.status = RIGHT_COMPLETED;
           }
         }
       }
     }
   } else {
     if ((this.leftSon != null) || (this.rightSon != null)) {
       Logger.getLogger()
           .concatOut(
               amt.log.Logger.DEBUG,
               "Formula",
               "SyntaxErrorException: addition of input "
                   + input
                   + " in an input cell where the left or right son are already filed");
       e.fillInStackTrace();
       throw e;
     } else {
       this.cellValue = input;
       this.status = RIGHT_COMPLETED;
     }
   }
 }