This project evaluates arithmetic expressions written in infix notation using a stack-based approach. The goal is to convert the infix expression to postfix form and evaluate it efficiently by considering operator precedence and parentheses. The program can handle addition, subtraction, multiplication, and division.
- Infix Notation: Arithmetic expressions in which operators are placed between operands (e.g.,
8-(3+2*6)/5+4
). - Operator Precedence: Different operators have different priorities. Multiplication and division are evaluated before addition and subtraction unless parentheses override the precedence.
- Stack-Based Evaluation: Stacks are used to hold operands and operators during the evaluation process.
The program is implemented in C++ and uses two stacks:
OPND
: A stack that stores operands (numbers).OPTR
: A stack that stores operators (+, -, *, /, etc.).
-
precede
: Compares the precedence of two operators. It returns:1
if the first operator has higher precedence,0
if both have the same precedence,-1
if the first operator has lower precedence.
-
operate
: Takes two operands and an operator, performs the operation, and returns the result. It handles the four basic operations: addition, subtraction, multiplication, and division. -
calculate
: The core function that processes the infix expression. It:- Scans the input expression,
- Pushes numbers (operands) onto the operand stack (
OPND
), - Pushes operators onto the operator stack (
OPTR
) while considering their precedence, - When an operator with lower precedence is encountered, the program pops operators from the
OPTR
stack, performs the corresponding operations, and pushes the results onto theOPND
stack. - At the end, the operand stack contains the final result.
-
displaySignature
: Displays a special title and signature in the terminal, including your name, ID, and a stylized signature.
- Input:
8-(3+2*6)/5+4
- Output:
9
- The program begins by prompting the user to enter an infix expression.
- It processes each character of the input:
- If it’s a number, the number is pushed onto the operand stack (
OPND
). - If it’s an operator, the program uses the
precede
function to compare its precedence with the operator at the top of the operator stack (OPTR
). - Depending on the comparison, the program either pushes the operator onto the stack or performs the corresponding operations.
- If it’s a number, the number is pushed onto the operand stack (
- The process continues until the input expression is fully parsed and evaluated.
- The result of the expression is printed in yellow to highlight the output.
Here’s an example of what the output would look like:
-
Infix to Postfix Conversion - GeeksforGeeks
A comprehensive guide on converting infix expressions to postfix notation using stacks, which is a core concept in your project. -
Stack Data Structure - Programiz
Learn more about the stack data structure, including how it works and why it's essential for evaluating expressions like in your project. -
Operator Precedence and Associativity in C++ - GeeksforGeeks
Understand how operator precedence and associativity work in C++, which directly relates to evaluating infix expressions correctly. -
C++ Stack Library - C++ Reference
A detailed reference to the C++ standard library’s stack, which you are using in your implementation. -
Evaluating Infix Expressions - Wikipedia
This article explains the Shunting-yard algorithm, a method for parsing infix expressions to postfix (or Reverse Polish Notation), which is closely related to your project.
If you discover a security vulnerability, please email Abdullah Al Raimi at [email protected]. All security vulnerabilities will be promptly addressed.
This project is licensed under the MIT license.