This project is an advanced Command-Line Interface (CLI) calculator implemented in C++. The calculator can evaluate mathematical expressions using infix notation, supporting basic arithmetic operations like addition, subtraction, multiplication, division, and exponentiation. It also handles operator precedence and parentheses.
- Supports the following operators:
- Addition (
+) - Subtraction (
-) - Multiplication (
*) - Division (
/) - Exponentiation (
^) 
 - Addition (
 - Handles operator precedence (e.g., multiplication and division before addition and subtraction).
 - Supports parentheses for grouping expressions.
 - Error handling for invalid expressions, including division by zero.
 
- 
Tokenization: The input expression is first converted into tokens, separating numbers, operators, and parentheses.
 - 
Infix to Postfix Conversion: The infix expression (which is human-readable) is converted into postfix notation using the Shunting Yard algorithm. Postfix notation is easier to evaluate programmatically because it doesn't require handling operator precedence.
 - 
Postfix Evaluation: The postfix expression is then evaluated using a stack-based approach.
 
For an expression like 12 + 2 - ( 2 + 2 ) * 2, the calculator processes it as follows:
- 
Tokenize: Break down the expression into individual components:
["12", "+", "2", "-", "(", "2", "+", "2", ")", "*", "2"]. - 
Convert to Postfix: Convert the infix expression to postfix notation:
["12", "2", "+", "2", "2", "+", "2", "*", "-"]. - 
Evaluate Postfix: Evaluate the postfix expression to get the result:
8. 
precedence(): Determines the precedence of operators (+,-,*,/,^).applyOp(): Applies the operator to two operands.tokenize(): Converts the input string into tokens.infixToPostfix(): Converts the tokenized infix expression to postfix notation using the Shunting Yard algorithm.evaluatePostfix(): Evaluates the postfix expression to produce a numerical result.calculator(): The main function that handles user input and integrates all the above functions.
To compile and run this program, you'll need:
- A C++ compiler (e.g., 
clang++,g++) - A terminal or command prompt
 
- Clone or download the repository to your local machine.
 - Open your terminal or command prompt and navigate to the directory containing 
main.cpp. - Compile the code using a C++ compiler. For example, using 
clang++: 
# With clang++ compiler
clang++ -std=c++20 main.cpp -o calculator
# or with g++ compiler
g++ main.cpp -o calculator- Run the program by executing the compiled file:
 
./calculator- Example program structure
 
$ ./calculator
Enter a mathematical expression (or type 'exit' to quit): 12 + 2 - (2 + 2) * 2
Result: 8
Enter a mathematical expression (or type 'exit' to quit): exit