Ejemplo n.º 1
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;
   }
 }
Ejemplo n.º 2
0
 /**
  * method will set the current cell to all the values of the leftSon, causing the leftSon to
  * replace the currentCell
  */
 public void swapWithLeftSon() {
   setrightSon(leftSon.getrightSon());
   setcellValue(leftSon.getcellValue());
   setstatusCell(leftSon.getstatusCell());
   setleftSon(leftSon.getleftSon());
 }
Ejemplo n.º 3
0
  /*
   * Checks if formula made matches a passed formula
   * @param correctFormula: The formula to be compared
   */
  public boolean checkFormulaCorrect(Formula correctFormula) {
    Formula goodF, userF;
    try {
      goodF = new Formula(correctFormula);
      userF = new Formula(this);
      if (goodF.opList.size() > 0) {
        TreeCell firstCell = goodF.opList.get(0);
        if ((firstCell.getcellValue().length() > 1)
            || (firstCell.getcellValue().charAt(0) == '*')
            || (firstCell.getcellValue().charAt(0) == '/')) {

          TreeCell topCell = new TreeCell("+");
          topCell.setleftSon(null);
          topCell.setrightSon(firstCell);
          if (firstCell.getstatusCell() == TreeCell.RIGHT_COMPLETED)
            topCell.setstatusCell(TreeCell.RIGHT_COMPLETED);
          else topCell.setstatusCell(TreeCell.LEFT_COMPLETED);
          goodF.opList.set(0, topCell);
        }
      }

      if (userF.opList.size() > 0) {
        TreeCell firstCell = userF.opList.get(0);
        if ((firstCell.getcellValue().length() > 1)
            || (firstCell.getcellValue().charAt(0) == '*')
            || (firstCell.getcellValue().charAt(0) == '/')) {

          TreeCell topCell = new TreeCell("+");
          topCell.setleftSon(null);
          topCell.setrightSon(firstCell);
          if (firstCell.getstatusCell() == TreeCell.RIGHT_COMPLETED)
            topCell.setstatusCell(TreeCell.RIGHT_COMPLETED);
          else topCell.setstatusCell(TreeCell.LEFT_COMPLETED);
          userF.opList.set(0, topCell);
        }
      }
      /*System.out.println("the formulas after init");
      System.out.println(userF.FormulaToString());
      System.out.println(goodF.FormulaToString());*/
      boolean opChecked[] = new boolean[userF.nextEntry];

      boolean opCheckedCorrect = false;
      // compare the number of leaves: if not identical we know it is not good.
      if (userF.nb_leaves != goodF.nb_leaves) {
        // System.out.println("nbLeaves not equal");
        return false;
      }
      // if the two formulas do not have the same number of top operators, we know it is not good.
      else if (userF.opList.size() != goodF.opList.size()) {
        // System.out.println("size of opList not equal");
        return false;
      } else {
        // System.out.println("first tests passed");

        for (int i = 0; i < goodF.opList.size(); i++) {
          String correctvalue = goodF.opList.get(i).getcellValue();
          // System.out.println("test of operator: "+ correctvalue);
          if (correctvalue.length() == 1) {
            opCheckedCorrect = false;
            for (int j = 0; j < userF.opList.size(); j++) {
              if ((!opCheckedCorrect) && (!opChecked[j])) {
                String currentValue = userF.opList.get(j).getcellValue();
                // System.out.println("test of user operator: "+ currentValue+ "" + j);

                if (correctvalue.equals(currentValue)) {
                  // System.out.println("test of the cells:");
                  // System.out.println("user"+ userF.opList.get(j).turnToString());
                  // System.out.println("user"+ goodF.opList.get(i).turnToString());
                  if (userF.opList.get(j).cellCorrect(goodF.opList.get(i))) {
                    // System.out.println("this cell is correct");
                    opChecked[j] = true;
                    opCheckedCorrect = true;
                  }
                }
              }
            }
            if (!opCheckedCorrect) return false;
          }
        }
        return true;
      }

    } catch (SyntaxErrorException ex) {
      java.util.logging.Logger.getLogger(Formula.class.getName()).log(Level.SEVERE, null, ex);
    }
    return false;
  }