public ExpressionPart parseTokens(Vector<Token> input) throws Exception { // TODO INNY PARSER System.out.println("parsing tokens"); if (input.size() == 0) { System.out.println("no tokens to parse!"); return null; } for (int k = 0; k < input.size(); k++) { System.out.println( "next token: " + input.get(k).getToken() + " type: " + input.get(k).getType()); } if (input.size() == 1 && input.get(0).getType() == 0) { System.out.println("only one number to parse"); return new Number(Double.parseDouble(input.get(0).getToken())); } if (input.size() == 1 && input.get(0).getType() == 3) { System.out.println("only one variable to parse"); return new Variable(input.get(0).getToken()); } ExpressionPart returnVal = null; System.out.println( "amount of tokens: " + input.size()); // I'm working on new parser, i don't like the way current one is // functioning /* //int bracketLevel = 0; int tmpBracketLevel = 0; int i = input.size() - 1; boolean bracketFlag = false; if(input.get(0).getToken().equals("(") && input.get(i).getToken().equals(")")){ bracketFlag = true; for(int j = 0; j < i; j++){ Token currToken = input.get(j); if(currToken.getToken().equals("(")){ tmpBracketLevel ++; } if(currToken.getToken().equals(")")){ tmpBracketLevel --; if(tmpBracketLevel == 0 && j < i-1){ bracketFlag = false; break; } } } } if(bracketFlag){ i = input.size() - 2; } String currentOp = input.get(0).getToken(); int k; if(bracketFlag){ k = 2; }else{ k = 1; } Vector<Token> rightSideOfLast = new Vector<Token>(); for(; k < i; k++){ Token currToken = input.get(k); if(getPriority(currentOp) >= getPriority(currToken.getToken())){ System.out.println("token with higer priority!"); if(returnVal instanceof Addition){ Addition tmp = (Addition)returnVal; tmp.addAddend(parseTokens(rightSideOfLast)); returnVal = tmp; }else if(returnVal instanceof Substraction){ Substraction tmp = (Substraction)returnVal; tmp.setSubtrahend(parseTokens(rightSideOfLast)); returnVal = tmp; }else{ returnVal = parseTokens(rightSideOfLast); } currentOp = currToken.getToken(); if(currentOp.equals("+")){ System.out.println("addition"); if(returnVal == null){ Addition tmp = new Addition(); tmp.addAddend(returnVal); returnVal = tmp; System.out.println("returnVal == null"); System.out.println(returnVal); }else if(returnVal instanceof Addition){ Addition tmp = (Addition)returnVal; tmp.addAddend(returnVal); returnVal = tmp; } } if(currentOp.equals("-")){ Substraction tmp = new Substraction(); tmp.setMinuend(returnVal); returnVal = tmp; } if(currentOp.equals("*")){ } if(currentOp.equals("/")){ } if(currentOp.equals("=")){ } rightSideOfLast.clear(); }else{ rightSideOfLast.add(currToken); } } System.out.println("end of parsing!"); return returnVal;*/ String currentOp = ""; int lastOperatorPos = 0; for (int k = 0; k <= 5; k++) { // look only at operators at lowest priority, then ones higher priority Vector<Token> tokensToAdd = new Vector<Token>(); System.out.println("searching for operators with priority: " + k); if (!currentOp.equals("")) { break; } int bracketLevel = 0; int tmpBracketLevel = 0; int i = input.size() - 1; boolean bracketFlag = false; // TODO system struktur, ewentualnie bez tokenow if (input.get(0).getToken().equals("(") && input.get(i).getToken().equals(")")) { bracketFlag = true; for (int j = 0; j < i; j++) { Token currToken = input.get(j); if (currToken .getToken() .equals("(")) { // if thera are brackets surronding expression, ignore them tmpBracketLevel++; } if (currToken.getToken().equals(")")) { tmpBracketLevel--; if (tmpBracketLevel == 0 && j < i - 1) { bracketFlag = false; break; } } } } if (bracketFlag) { i = input.size() - 2; } for (; i >= 0; i--) { // TODO daje odwrotna kolejnosc niz wpisane(nie mozna ) Token currToken = input.get(i); tokensToAdd.add(currToken); // tokens that are between last operator and next one(useful in // addition/multiplications) if (currToken.getToken().equals(")")) { bracketLevel++; } if (currToken.getToken().equals("(")) { bracketLevel--; } if (bracketLevel > 0) { System.out.println("skipping token, searching for end of bracket"); continue; } System.out.println("next token: " + currToken.getToken() + " type: " + currToken.getType()); if (currToken.getType() == 1) { if (getPriority(currToken.getToken()) == k) { lastOperatorPos = i; if (currentOp.equals("")) { // to jest, zeby nie ustawial nowego operatora currentOp = currToken .getToken(); // TODO obsluga pierwiastkowania(a tym samym operacje z innym // zapisem niz arg op arg) } if (currentOp.equals(currToken.getToken())) { Vector<Token> firstPart = new Vector<Token>(); Vector<Token> secondPart = new Vector<Token>(); if (!bracketFlag) { firstPart.addAll(input.subList(0, i)); secondPart.addAll(input.subList(i + 1, input.size())); } else { firstPart.addAll(input.subList(1, i)); secondPart.addAll(input.subList(i + 1, input.size() - 1)); } if (returnVal == null) { // parse diffrent operators System.out.println("first operator"); if (currentOp.equals("\\")) { Fraction returnValTmp = new Fraction(); System.out.println("[\\]part 1"); returnValTmp.setNumerator(parseTokens(firstPart)); System.out.println("[\\]part 2"); returnValTmp.setDenominator(parseTokens(secondPart)); returnVal = returnValTmp; } if (currentOp.equals("+")) { Addition returnValTmp = new Addition(); System.out.println("[+]part 1"); returnValTmp.addAddend(parseTokens(secondPart)); // System.out.println("[+]part 2"); // returnValTmp.addAddend(parseTokens(secondPart)); returnVal = returnValTmp; System.out.println("end of part 2"); tokensToAdd.clear(); } if (currentOp.equals("-")) { Substraction returnValTmp = new Substraction(); System.out.println("[-]part 1"); returnValTmp.setMinuend(parseTokens(firstPart)); System.out.println("[-]part 2"); returnValTmp.setSubtrahend(parseTokens(secondPart)); returnVal = returnValTmp; } if (currentOp.equals("*")) { Multiplication returnValTmp = new Multiplication(); System.out.println("[*]part 1"); returnValTmp.addFactor(parseTokens(secondPart)); // System.out.println("[+]part 2"); // returnValTmp.addAddend(parseTokens(secondPart)); returnVal = returnValTmp; System.out.println("end of part 2"); tokensToAdd.clear(); } if (currentOp.equals("/")) { Division returnValTmp = new Division(); System.out.println("[/]part 1"); returnValTmp.setDividend(parseTokens(firstPart)); System.out.println("[/]part 2"); returnValTmp.setDivisor(parseTokens(secondPart)); returnVal = returnValTmp; } if (currentOp.equals("^")) { Exponentiation returnValTmp = new Exponentiation(); System.out.println("[^]part 1"); returnValTmp.setBase(parseTokens(firstPart)); System.out.println("[^]part 2"); returnValTmp.setExponent(parseTokens(secondPart)); returnVal = returnValTmp; } if (currentOp.equals("=")) { Equation returnValTmp = new Equation(); System.out.println("[=]part 1"); returnValTmp.setSide1(parseTokens(firstPart)); System.out.println("[=]part 2"); returnValTmp.setSide2(parseTokens(secondPart)); returnVal = returnValTmp; } } else { System.out.println("next operator"); if (currentOp.equals("+") && returnVal instanceof Addition) { Addition returnValConverted = (Addition) returnVal; // System.out.println("[+]part 1"); // returnValConverted.addAddend(parseTokens(firstPart)); Vector<Token> tokensInArg = new Vector<Token>(); tokensInArg.addAll(tokensToAdd.subList(0, tokensToAdd.size() - 1)); System.out.println("[+]part 2(adding only second part)"); returnValConverted.addAddend(parseTokens(tokensInArg)); tokensToAdd.clear(); } if (currentOp.equals("*") && returnVal instanceof Multiplication) { Multiplication returnValConverted = (Multiplication) returnVal; // System.out.println("[+]part 1"); // returnValConverted.addAddend(parseTokens(firstPart)); Vector<Token> tokensInArg = new Vector<Token>(); tokensInArg.addAll(tokensToAdd.subList(0, tokensToAdd.size() - 1)); System.out.println("[+]part 2(adding only second part)"); returnValConverted.addFactor(parseTokens(tokensInArg)); tokensToAdd.clear(); } } } } } if (bracketFlag && i <= 1) { break; } } Vector<Token> firstPart = new Vector<Token>(); Vector<Token> secondPart = new Vector<Token>(); if (!bracketFlag) { firstPart.addAll(input.subList(0, lastOperatorPos)); secondPart.addAll(input.subList(lastOperatorPos + 1, input.size())); } else if (lastOperatorPos >= 1) { firstPart.addAll(input.subList(1, lastOperatorPos)); secondPart.addAll(input.subList(lastOperatorPos + 1, input.size() - 1)); } System.out.println("Adding last part"); if (currentOp.equals("+") && returnVal instanceof Addition) { Addition returnValConverted = (Addition) returnVal; System.out.println("[+]part 1(adding only first part)"); returnValConverted.addAddend(parseTokens(firstPart)); tokensToAdd.clear(); } if (currentOp.equals("*") && returnVal instanceof Multiplication) { Multiplication returnValConverted = (Multiplication) returnVal; System.out.println("[*]part 1(adding only first part)"); returnValConverted.addFactor(parseTokens(firstPart)); tokensToAdd.clear(); } if (returnVal != null) { break; } } return returnVal; }
@Override public ExpressionPart nextStepToSimplyfy() { ExpressionPart tmpNumerator = numerator; ExpressionPart tmpDenominator = denominator; if (tmpDenominator instanceof Number) { if (((Number) tmpDenominator).getValue() == 1) { return tmpNumerator; } } boolean end = false; if (tmpNumerator.canBeSimplified()) { tmpNumerator = tmpNumerator.nextStepToSimplyfy(); end = true; } if (tmpDenominator.canBeSimplified()) { tmpDenominator = tmpDenominator.nextStepToSimplyfy(); end = true; } if (end) { Fraction retValue = new Fraction(); retValue.setNumerator(tmpNumerator); retValue.setDenominator(tmpDenominator); return retValue; } if (tmpNumerator instanceof Number && tmpDenominator instanceof Number) { Number numeratorConverted = (Number) tmpNumerator; Number denominatorConverted = (Number) tmpDenominator; if (Operation.isInteger(numeratorConverted.getValue()) && Operation.isInteger(denominatorConverted.getValue())) { ExpressionPart gcd = new GreatestCommonDivisor(numeratorConverted, denominatorConverted).simplyfy(); if (gcd instanceof Number && ((Number) gcd).getValue() > 1) { tmpNumerator = new Division(numeratorConverted, gcd); tmpDenominator = new Division(denominatorConverted, gcd); } } else { while (!(Operation.isInteger(numeratorConverted.getValue()) && Operation.isInteger(denominatorConverted.getValue()))) { numeratorConverted = new Number(numeratorConverted.getValue() * 10); denominatorConverted = new Number(denominatorConverted.getValue() * 10); } return new Fraction(numeratorConverted, denominatorConverted); } } if (tmpNumerator instanceof Fraction && tmpDenominator instanceof Fraction) { Fraction tmpNumeratorConverted = (Fraction) tmpNumerator; Fraction tmpDenominatorConverted = (Fraction) tmpDenominator; return new Fraction( new Multiplication(tmpNumeratorConverted.numerator, tmpDenominatorConverted.denominator), new Multiplication(tmpNumeratorConverted.denominator, tmpDenominatorConverted.numerator)); } if (tmpNumerator instanceof Fraction) { Fraction tmpNumeratorConverted = (Fraction) tmpNumerator; return new Fraction( tmpNumeratorConverted.numerator, new Multiplication(tmpNumeratorConverted.denominator, tmpDenominator)); } if (tmpDenominator instanceof Fraction) { Fraction tmpDenominatorConverted = (Fraction) tmpDenominator; return new Fraction( new Multiplication(tmpDenominatorConverted.denominator, tmpNumerator), tmpDenominatorConverted.numerator); } Fraction retValue = new Fraction(); retValue.setNumerator(tmpNumerator); retValue.setDenominator(tmpDenominator); retValue.setSign(this.isPositive); if (tmpNumerator instanceof Number) { if (((Number) tmpNumerator).getValue() < 0) { retValue.setSign(false); tmpNumerator = new Number(Math.abs(((Number) tmpNumerator).getValue())); retValue.setSignChecked(true); } } return retValue; }