<commit_before>package cal;

import java.util.ArrayList;
import java.util.Stack;

// TODO: Auto-generated Javadoc
/**
 * ExpressionTree Class model the users input expression as an expression tree
 * It firstly convert the infix input into postfix input. Then it transforms the
 * postfix input as a tree After conversion into an expression tree, it then
 * evaluates the tree and return the result
 * 
 * This is a <ConcreteProduct> that should be created by ParserFactory<Factory>
 */
public class ExpressionTreeController implements Parser {

    private ExpressionTree exprTree;

    /** The postfix. */
    private ArrayList<String> postfix;

    /** The result. */
    private double result;

    /**
     * Constructor for Expression Tree.
     */
    public ExpressionTreeController() {
	exprTree = new ExpressionTree();
    }

    /**
     * Read input string from caller, convert infix to post-fix and finally
     * build the expression tree.
     * 
     * @param infixInput
     *            , String of equation in infix form
     * @return true, If build tree is finished successfully
     */
    @Override
    public boolean read(String infixInput) {
	String input = infixInput.replace(" ", "");
	postfix = AffixConverter.toPostfix(input);
	buildTree();
	this.result = evaluate(exprTree.getHeadNode());
	return true; //XXX meaningless, don't always true;
    }

    /**
     * Method to build the tree and calculate the result First use the post-fix
     * expression to build tree Then evaluate the result.
     */
    private void buildTree() {
	try {
	    if (postfix.size() > 0) {
		exprTree.setHeadNode(buildTree(new ArrayList<String>(postfix)));
	    }
	} catch (Exception e) {
	    System.out.println("Invalid Expression" + e);
	}
    }

    private TreeNode buildTree(ArrayList<String> postfix) throws InstantiationException, IllegalAccessException {
	String val = postfix.get(postfix.size()-1);
	postfix.remove(postfix.size()-1);

	if (MathHelper.isDigit(val)) {
	    return new Operand(Double.parseDouble(val));
	} else {
	    Operator node = OperatorFactory.typeOfOperator(val.charAt(0));
	    node.addRight(buildTree(postfix));
	    node.addLeft(buildTree(postfix));
	    return node;
	}
    }

    /*
     * (non-Javadoc)
     * 
     * @see cal.Parser#execute()
     */
    @Override
    public String execute() {
	return String.valueOf(this.result);
    }



    /**
     * Evaluate method is to calculate the result of the tree.
     * 
     * @param node
     *            represents root node of the expression tree to be calculated
     * @return the evaluated result
     */
    private double evaluate(TreeNode node) {
	double result = 0; 

	if (node == null) {
	    return 0.0; // TODO: check if node itself == null
	}

	if (node.getLeft() == null && node.getRight() == null) { 
	    result = ((Operand) node).getVal();
	} else {
	    char operator = ((Operator) node).getVal();
	    double leftVal = evaluate(node.getLeft());
	    double rightVal = evaluate(node.getRight());
	    result = ((Operator) node).calculate(leftVal, rightVal);
	}
	return result;
    }

}<commit_msg>[NEW] added steps counter with steps<commit_after>package cal;

import java.util.ArrayList;
import java.util.Stack;

// TODO: Auto-generated Javadoc
/**
 * ExpressionTree Class model the users input expression as an expression tree
 * It firstly convert the infix input into postfix input. Then it transforms the
 * postfix input as a tree After conversion into an expression tree, it then
 * evaluates the tree and return the result
 * 
 * This is a <ConcreteProduct> that should be created by ParserFactory<Factory>
 */
public class ExpressionTreeController implements Parser {

    private ExpressionTree exprTree;

    /** The postfix. */
    private ArrayList<String> postfix;

    /** The result. */
    private double result;
    
    /** The step counter */
    private int stepCounter = 1;

    /**
     * Constructor for Expression Tree.
     */
    public ExpressionTreeController() {
	exprTree = new ExpressionTree();
    }

    /**
     * Read input string from caller, convert infix to post-fix and finally
     * build the expression tree.
     * 
     * @param infixInput
     *            , String of equation in infix form
     * @return true, If build tree is finished successfully
     */
    @Override
    public boolean read(String infixInput) {
	String input = infixInput.replace(" ", "");
	postfix = AffixConverter.toPostfix(input);
	buildTree();
	this.result = evaluate(exprTree.getHeadNode());
	return true; //XXX meaningless, don't always true;
    }

    /**
     * Method to build the tree and calculate the result First use the post-fix
     * expression to build tree Then evaluate the result.
     */
    private void buildTree() {
	try {
	    if (postfix.size() > 0) {
		exprTree.setHeadNode(buildTree(new ArrayList<String>(postfix)));
	    }
	} catch (Exception e) {
	    System.out.println("Invalid Expression" + e);
	}
    }

    private TreeNode buildTree(ArrayList<String> postfix) throws InstantiationException, IllegalAccessException {
	String val = postfix.get(postfix.size()-1);
	postfix.remove(postfix.size()-1);

	if (MathHelper.isDigit(val)) {
	    return new Operand(Double.parseDouble(val));
	} else {
	    Operator node = OperatorFactory.typeOfOperator(val.charAt(0));
	    node.addRight(buildTree(postfix));
	    node.addLeft(buildTree(postfix));
	    return node;
	}
    }

    /*
     * (non-Javadoc)
     * 
     * @see cal.Parser#execute()
     */
    @Override
    public String execute() {
	return String.valueOf(this.result);
    }



    /**
     * Evaluate method is to calculate the result of the tree.
     * 
     * @param node
     *            represents root node of the expression tree to be calculated
     * @return the evaluated result
     */
    private double evaluate(TreeNode node) {
	double result = 0; 

	if (node == null) {
	    return 0.0; // TODO: check if node itself == null
	}

	if (node.getLeft() == null && node.getRight() == null) { 
	    result = ((Operand) node).getVal();
	} else {
	    char operator = ((Operator) node).getVal();
	    double leftVal = evaluate(node.getLeft());
	    double rightVal = evaluate(node.getRight());
	    
	    // Printing the steps
	    System.out.println("Step "+ stepCounter++ +": " + leftVal + " " + operator + " " + rightVal);
	    
	    result = ((Operator) node).calculate(leftVal, rightVal);
	}
	return result;
    }

}
{'fraction_non_alphanumeric': 0.1114723641430562, 'fraction_numerical': 0.0023223409196470044, 'mean_word_length': 2.891566265060241, 'pattern_counts': {'":': 1, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3816057', 'n_tokens_mistral': 1927, 'n_tokens_neox': 1827, 'n_words': 578}