private List<ExpressionAtom> parseInputPolynomial(String inputExpression) { inputExpression = identifyUnaryMinuses(inputExpression); inputExpression = insertMultiplicationSigns(inputExpression); List<ExpressionAtom> inputExpressionTokens = new ArrayList<ExpressionAtom>(); char[] inputChars = inputExpression.toCharArray(); for (int i = 0; i < inputChars.length; ++i) { if (isOperator(inputChars[i]) || inputChars[i] == '%') { inputExpressionTokens.add( new ExpressionAtom(String.valueOf(inputChars[i]), AtomType.OPERATOR, 1)); } else { int lastIndex = inputExpressionTokens.size() - 1; if (lastIndex >= 0 && inputExpressionTokens.get(lastIndex).getAtomType() == AtomType.OPERAND) { ExpressionAtom lastElement = inputExpressionTokens.remove(lastIndex); if (Character.isDigit(inputChars[i])) { lastElement.setCoefficient( lastElement.getCoefficient() * 10 + Character.getNumericValue(inputChars[i])); } else { lastElement.setVariablesOrOperator( lastElement.getVariablesOrOperator() + String.valueOf(inputChars[i])); } inputExpressionTokens.add(lastElement); } else if (Character.isDigit(inputChars[i])) { inputExpressionTokens.add( new ExpressionAtom("", AtomType.OPERAND, Character.getNumericValue(inputChars[i]))); } else { inputExpressionTokens.add( new ExpressionAtom(String.valueOf(inputChars[i]), AtomType.OPERAND, 1)); } } } return inputExpressionTokens; }
private List<ExpressionAtom> simplifyAndNormalize(List<ExpressionAtom> evaluatedExpression) { for (int x = 0; x < evaluatedExpression.size(); x++) { evaluatedExpression .get(x) .setVariablesOrOperator( new StringBuilder(evaluatedExpression.get(x).getVariablesOrOperator()) .reverse() .toString()); String newVar = fixVariablesOrOperator(evaluatedExpression.get(x).getVariablesOrOperator()); evaluatedExpression.get(x).setVariablesOrOperator(newVar); } for (int x = 0; x < evaluatedExpression.size() - 1; x++) { ExpressionAtom first = evaluatedExpression.get(x); for (int y = x + 1; y < evaluatedExpression.size(); y++) { ExpressionAtom second = evaluatedExpression.get(y); if (first.getVariablesOrOperator().equals(second.getVariablesOrOperator())) { first.setCoefficient(first.getCoefficient() + second.getCoefficient()); evaluatedExpression.remove(second); } } } return evaluatedExpression; /* * TODO: Write code here to operate on the List<ExpressionAtom> object obtaind from * 'evaluateExpression'. Specifically, ensure that you combine terms with same variables and * modify coefficients appropriately. */ }
@Override public List<ExpressionAtom> evaluate( List<ExpressionAtom> loperands, List<ExpressionAtom> roperands) { if (roperands == null) { List<ExpressionAtom> product = new ArrayList<ExpressionAtom>(); for (int x = 0; x < loperands.size(); x++) { if (product.size() == 0) { product.add(loperands.get(x)); } else { ExpressionAtom atom = product.get(0); atom.setCoefficient(atom.getCoefficient() * loperands.get(x).getCoefficient()); String var = incrementDegree( loperands.get(x).getVariablesOrOperator(), atom.getVariablesOrOperator()); atom.setVariablesOrOperator(var); } } return product; } else { return multiple(loperands, roperands); } }