/** * 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; } }
/** * 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 toAdd the information to add to the Formula : either operator or inputs * @exception SyntaxErrorException: this exception is thrown if the method is called trying to add * an input where the last entry in the formula is an input already, or an operator following * another operator. it also throws the exception if the first operator added is * or / and no * inputs have been entered before. those are all errors that are dealt with in the AMT code, * and the method should never be called in those instances. */ public void addToFormula(String toAdd) throws SyntaxErrorException { if (toAdd.length() == 1) { addToFormula(toAdd.charAt(0)); return; } // 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 { currentCell.addInputInCell(toAdd); if ((currentCell.getstatusCell() == TreeCell.RIGHT_COMPLETED) && ((currentCell.getcellValue().length() > 1))) nextEntry++; if ((currentCell.getstatusCell() == TreeCell.RIGHT_COMPLETED) && ((currentCell.getcellValue().length() == 1)) && (currentCell.getrightSon() != null)) nextEntry++; this.addLeaveTotal(); } catch (SyntaxErrorException e) { throw e; } }
/** * Method that copies the content of a TreeCell given as parameter to the current formula * * @param cellCopy the main operator or first input to copy * @throws SyntaxErrorException the formula is not syntactically correct */ public void copyTreeCell(TreeCell cellCopy) throws SyntaxErrorException { if (cellCopy != null) { if (cellCopy.getleftSon() != null) copyTreeCell(cellCopy.getleftSon()); String cellValue = cellCopy.getcellValue(); // holds the value of the current cell if (cellValue != null) if (cellValue.length() > 1) addToFormula(cellValue); else addToFormula(cellValue.charAt(0)); if (cellCopy.getrightSon() != null) copyTreeCell(cellCopy.getrightSon()); } }
/** * 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()); }
/* * 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; }