East West University
Course: CSE207 - Data Structures
Instructor: Dr. Hasan Mahmood Aminul Islam
Teaching Assistant: Abdullah Al Tamim
Student: Md Asaduzzaman Atik (2023-1-60-130)
This project, InfixPostfixWithStack, implements a program that converts mathematical expressions from infix notation (e.g., A + B * (C - D)
) to postfix notation (e.g., A B C D - * +
) using a stack-based approach. The implementation adheres to modular programming practices and is organized into multiple files and folders for better structure and maintainability.
This is part of an assignment for the CSE207 - Data Structures course at East West University.
The program converts an infix expression into a postfix expression using a stack to handle operator precedence and parentheses.
- Precedence of Operators:
*
and/
>+
and-
. Operators with the same precedence are evaluated left-to-right. - Parentheses:
Parentheses override the precedence rules and ensure operations inside them are performed first. - Stack Usage:
The stack is used to store operators and parentheses during conversion.
- A valid infix expression consisting of:
- Operands: Single characters such as
A
,B
,C
, etc. - Operators:
+
,-
,*
,/
. - Parentheses:
(
and)
.
- Operands: Single characters such as
- Input is assumed to be correctly formatted, but the program includes additional validation.
The equivalent postfix expression.
Infix Expression (Input) | Postfix Expression (Output) |
---|---|
A + B |
A B + |
A + B * C |
A B C * + |
(A + B) * (C - D) |
A B + C D - * |
A + B * (C - D) / E |
A B C D - * E / + |
The project is structured as follows:
📁InfixPostfixWithStack
├── 📁include # Header files
│ ├── conversion.h # Conversion logic header
│ ├── helpers.h # Helper functions header
│ ├── stack.h # Stack operations header
│ └── validation.h # Input validation header
├── 📁src # Source files
│ ├── conversion.c # Conversion logic implementation
│ ├── helpers.c # Helper functions implementation
│ ├── stack.c # Stack operations implementation
│ └── validation.c # Input validation implementation
├── InfixPostfixWithStack.cbp # Code::Blocks project file
├── InfixPostfixWithStack.depend # Dependency file for Code::Blocks
└── main.c # Main program file
- A C compiler (e.g.,
gcc
or MinGW). - Code::Blocks IDE or any other IDE supporting
.cbp
files.
- Open Code::Blocks.
- Open the project by loading the file InfixPostfixWithStack.cbp.
- Build the project:
- Go to the Build menu and click on Build.
- Run the project:
- Go to the Build menu and click on Run.
- Enter a valid infix expression when prompted. The program will output the corresponding postfix expression.
To compile manually using gcc
, navigate to the InfixPostfixWithStack
directory and run the following command:
gcc -o postfix main.c src/*.c -Iinclude
Run the program:
./postfix
- Ensures the infix expression:
- Contains only valid characters (operands, operators, and parentheses).
- Has balanced parentheses.
- Does not contain consecutive operators.
- Uses a dynamic stack implemented via a linked list to:
- Handle operator precedence.
- Temporarily store operators and parentheses during conversion.
- Provides descriptive error messages for:
- Invalid characters.
- Unbalanced parentheses.
- Memory allocation failures.
-
stack.h
- Declares functions for stack operations (
push
,pop
,peek
, etc.).
- Declares functions for stack operations (
-
helpers.h
- Declares utility functions for operator precedence and character classification.
-
validation.h
- Declares functions for validating the infix expression.
-
conversion.h
- Declares the main function to convert infix to postfix.
-
stack.c
- Implements stack operations using a linked list.
-
helpers.c
- Implements utility functions like checking operator precedence and character types.
-
validation.c
- Implements input validation, including parentheses checking and operator placement.
-
conversion.c
- Implements the core logic to convert an infix expression to postfix using a stack.
- Handles user interaction and coordinates the execution of the program by invoking validation and conversion functions.
The code in this repository is open for personal use, testing, and educational purposes under the terms of the MIT License.
- You are free to use, modify, and distribute the code for personal or educational purposes.
- Commercial use of the code is prohibited unless explicit permission is granted by the author.
- This repository will not be updated further. Any modifications or improvements made based on this project should be tracked in a separate repository.