Calcy

logo_512x512

A calculator for Android

Making a calculator is not the most impressive software project and I absolutely agree! That is why when my Android class (University of Utah) was tasked with making one for our first assignment, mine had to be very, very good.

I spent a lot of time considering all possible use-cases and how a calculator should intuitively handle them for the user. It is filled with defensive coding and is topped off with a very clean and intuitive interface.

Some of the Code

Writing the expression solver code was a lot of fun. It recursively solves the inner-most parenthesis and then replaces that parenthesis block with its value. This continues until there are no parenthesis and all that remains are numbers separated by operators. To give you an idea of what the code I wrote looks like, here is the fun, recursive part…

/* Evaluates an expression
 *
 * @param expression
 *	A list of strings representing each component of the expression
 *	2*(3+4) => {"2", "*", "(", "3", "+", "4", ")"}
 */
public static String solve(List<String> expression) {

	// Evaluate parenthesis enclosed sections of expression
	// recursively until expression becomes parenthesis-free.
	while (containsParenthesis(expression))
	{
		// Gets the indices of the first paren and its closing counterpart
		// then make a list of the expression it contains
		Pair<Integer, Integer> parens = getFirstAndClosingParenIndices(expression);
		List<String> innerExpression = new ArrayList<String>(
				expression.subList(parens.first + 1, parens.second));

		// Remove the paren block from the main expression
		expression.subList(parens.first, parens.second + 1).clear();

		// Solve the extracted inner-expression (recursive) and place
		// that value where the paren block was removed
		String innerExpressionValue = solve(innerExpression);
		expression.add(parens.first, innerExpressionValue);
	}

	. . .

}

With the parenthesis now resolved, the rest was pretty straight forward on how to handle order of operations.

I also wrote in an auto-brace completion which I think is awesome. I can’t take all the credit though since I got my inspiration for that from Google’s calculator. But again, writing the code to make it work very well for the user was a lot of fun.

Download and Try it

Get it on Google Play

Some Screenshots

Screen-Shot-1Screen-Shot-2

Leave a Reply

Your email address will not be published. Required fields are marked *