calc v1.0.0 MyCalculator

This is a calculator that can deal with +, -, *, / support negative number and parentheses. https://leetcode.com/problems/basic-calculator-iii/discuss/113590

Link to this section Summary

Functions

The function to build the number stack and operation stack

Compute next number to push into stack. GIVEN : an operation string, current number, and the nums stack

Deal with the situation when come up with a integer

Deal with the situation when come up with (

Deal with the situation when come up with operators

Deal with the situation when come up with )

Execute the division result. GIVEN : a number and a stack

Function to evaluate the string expression. GIVEN : a string expression

Get the sum of all the elements in the stack

Main function of the application

Execute the multiplication result. GIVEN : a number and a stack

Round a number

Deal with different situation when build the stack

Parse a string to integer if possible. GIVEN : a string

Link to this section Functions

Link to this function build(exp_list, num, op, numStack, opStack)

The function to build the number stack and operation stack.

GIVEN : the expression list (like the character array in Java), current num, current operation, a number stack, an operation stack

RETURNS : a stack of numbers

Examples

iex> MyCalculator.build(["1", "-", "2", "+", "3"], 0, "+", %Stack{elements: []}, %Stack{elements: []})
%Stack{elements: [3, -2, 1]}
Link to this function compute(op, number, numStack)

Compute next number to push into stack. GIVEN : an operation string, current number, and the nums stack

RETURNS : The new stack after execute the operation

Note : If the operation is +, push the num to numStack, cause we will add all the number in the numStack together

If the operation is -, it is equal to add a negative number, so push the inverse sign number to numStack

If the operation is * or /, we need to calculate first, so first operate with current numStack top, then push the result to the numStack

Examples

iex> MyCalculator.compute("+", 2, %Stack{elements: [10,20,30,40]})
%Stack{elements: [2, 10, 20, 30, 40]}

iex> MyCalculator.compute("-", 2, %Stack{elements: [10,20,30,40]})
%Stack{elements: [-2, 10, 20, 30, 40]}

iex> MyCalculator.compute("*", 2, %Stack{elements: [10,20,30,40]})
%Stack{elements: [20, 20, 30, 40]}

iex> MyCalculator.compute("/", 2, %Stack{elements: [10,20,30,40]})
%Stack{elements: [5.0, 20, 30, 40]}
Link to this function deal_int(exp_list, num, op, numStack, opStack, current_digit)

Deal with the situation when come up with a integer

Link to this function deal_left_para(exp_list, num, op, numStack, opStack)

Deal with the situation when come up with (

Link to this function deal_operator(exp_list, num, op, numStack, opStack, next_op)

Deal with the situation when come up with operators

Link to this function deal_right_para(exp_list, num, op, numStack, opStack)

Deal with the situation when come up with )

Link to this function divide(number, numStack)

Execute the division result. GIVEN : a number and a stack

WHERE : the top of the stack is a integer

RETURNS : The quotient of given number and the top element of the stack

Examples

iex> MyCalculator.divide(5, %Stack{elements: [10,3,4]})
2.0

Function to evaluate the string expression. GIVEN : a string expression

RETURNS : the result of the string expression

Link to this function get_sum(numStack)

Get the sum of all the elements in the stack

GIVEN : a stack of numbers

RETURNS : the rounded sum of all the numbers in the stack

Examples

iex> MyCalculator.get_sum(%Stack{elements: [1,2,3,4]})
10

iex> MyCalculator.get_sum(%Stack{elements: [1,2,3,4.5578]})
10.558

Main function of the application.

Link to this function multiply(number, numStack)

Execute the multiplication result. GIVEN : a number and a stack

WHERE : the top of the stack is a integer

RETURNS : The product of given number and the top element of the stack

Examples

iex> MyCalculator.multiply(1, %Stack{elements: [10,2,3,4]})
10
Link to this function round_number(num)

Round a number.

GIVEN : a number

RETURNS : the rounded result of the number

Note : when the num is integer, just return the num.

Link to this function stack_establish(exp_list, num, op, numStack, opStack)

Deal with different situation when build the stack.

Case 1 : Keep adding digit * base + num based on current num

Case 2 : ( push operator in opStack, push ( in numStack

Case 3 : ) sum value before ( in the numStack

Case 4 : Operator: update stack based on operator

Case 4.1 : + push num

Case 4.2 : - push -num

Case 4.3 : * push (numStack.pop() * num)

Case 4.4 : / push (numStack.pop() / num)

Case 5 : Space, continue to the rest of exp_list, although I handle this situation before by replace the space in exp_list

Parse a string to integer if possible. GIVEN : a string

RETURNS :

  1. If the string contains an integer, return the integer;

  2. Else return the original string.

Examples

iex> MyCalculator.str_parse("1")
1

iex> MyCalculator.str_parse("(")
"("