calc v1.0.0 Calculator

Basic idea: convert the infix expression to the postfix expression. Reference: https://leetcode.com/problems/basic-calculator-iii/discuss/113598/

Link to this section Summary

Functions

build the postfix list

build the postfix list when the head of current expression list is an operation, and the ops stack is not empty

Calculate using postfix

When the postfix is not empty, calculate using the top two elements in temp, and push the result to temp

Concat the digit that split apart

deal with the situation that first character is a

Main Evaluation function. Given an expression

Main function

When the operation stack is not empty, keep pushing the peek to postfix

If the string is a integer string, transform it to a integer. Otherwise return current str(operation)

Link to this section Functions

Link to this function build_postfix(exp_list, postfix, ops)

build the postfix list

Link to this function build_postfix_op(exp_list, postfix, ops)

build the postfix list when the head of current expression list is an operation, and the ops stack is not empty

Link to this function compute(postfix, temp)

Calculate using postfix

Link to this function compute_portion(postfix, temp)

When the postfix is not empty, calculate using the top two elements in temp, and push the result to temp

Link to this function concat_number_str(exp_list, new_list)

Concat the digit that split apart

Link to this function deal_first_neg(exp_list)

deal with the situation that first character is a

Main Evaluation function. Given an expression

  1. remove all the space

  2. return all the characters in the string as a list

  3. as we will hit enter in the terminal and consider as a character, remove the enter.

  4. Concat the integer string that split up

  5. parse each character in the list

  6. build a postfix list using current expression list, an empty postfix list and an empty stack

  7. compute using postfix list

Main function.

Link to this function push_ops(postfix, ops)

When the operation stack is not empty, keep pushing the peek to postfix

If the string is a integer string, transform it to a integer. Otherwise return current str(operation)