0% found this document useful (0 votes)
0 views

Experiment-6 dsa

The document outlines an experiment to convert infix expressions to postfix expressions using C++. It includes a detailed pseudo code and actual code implementation, explaining operator precedence and the role of stacks in the conversion process. The conclusion emphasizes the advantages of postfix notation for evaluating mathematical expressions without the need for parentheses.

Uploaded by

rustagipranav37
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Experiment-6 dsa

The document outlines an experiment to convert infix expressions to postfix expressions using C++. It includes a detailed pseudo code and actual code implementation, explaining operator precedence and the role of stacks in the conversion process. The conclusion emphasizes the advantages of postfix notation for evaluating mathematical expressions without the need for parentheses.

Uploaded by

rustagipranav37
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment-6

AIM:

To perform stack operations: Convert Infix to Postfix expressions.

Support expressions with and without parentheses/brackets.

Supported operators: +, -, *, /, %, ^.

LANGUAGE USED: C++

PSEUDO CODE:

BEGIN
FUNCTION precedence(op)

1. IF op is '+' OR op is '-' THEN

o RETURN 1

2. ELSE IF op is '*' OR op is '/' OR op is '%' THEN

o RETURN 2

3. ELSE IF op is '^' THEN


o RETURN 3

4. ELSE
o RETURN 0

FUNCTION is Operand(c)

1. RETURN TRUE IF c is a letter (A-Z, a-z) OR a digit (0-9).


2. ELSE RETURN FALSE.

FUNCTION infix To Postfix(infix)


1. CREATE an empty stack for operators.

2. CREATE an empty string for postfix result.

3. FOR each character c in infix:

o IF c is an operand THEN add c to postfix string.

o ELSE IF c is '(' THEN PUSH '(' onto the stack.

o ELSE IF c is ')' THEN WHILE the top of the stack is not '(':

 POP and add it to postfix string.


 POP '(' from the stack (discard it).
o ELSE IF c is an operator THEN:

 WHILE the stack is not empty AND precedence of top of stack >= precedence
of c:

 POP and add it to postfix string.


 PUSH c onto the stack.

4. WHILE the stack is not empty:


o POP and add to postfix string.

5. RETURN postfix string.


MAIN

1. READ infix expression from user.

2. CALL infixToPostfix with the infix expression.


3. PRINT the postfix expression.

THEORY:

The process of converting an infix expression (where operators are placed between operands) into a
postfix expression (where operators follow the operands) is essential for evaluating arithmetic
expressions without the need for parentheses or operator precedence during evaluation.

Infix Expression:

Infix notation is the common way of writing expressions where operators are placed between
operands. Example: A + B, 3 * (A + B), etc. It requires parentheses to explicitly define the order of
operations.

Postfix Expression:

In postfix notation, operators come after operands. Example: A B + (instead of A + B). Postfix
expressions do not require parentheses because the order of operations is clearly defined by the
position of the operators.

Operator Precedence:
In infix expressions, different operators have different levels of precedence (e.g., * and / have higher
precedence than + and -). In postfix expressions, the position of operators ensures that the correct
precedence is automatically followed, eliminating the need for parentheses.

CODE:

#include <iostream>

#include <stack>
#include <string>
#include <cctype>

#include <cmath>

using namespace std;

// Function to return precedence of operators

int precedence(char op) {

if (op == '+' || op == '-') return 1;


if (op == '*' || op == '/' || op == '%') return 2;

if (op == '^') return 3;

return 0;}

// Function to check if a character is an operand (alphabet or digit)


bool is Operand(char c) {

return isalpha(c) || is digit(c);

// Function to perform Infix to Postfix conversion

string infixToPostfix(string infix) {

stack<char> s;
string postfix = "";

for (int i = 0; i < infix.length(); i++) {


char current = infix[i];

if (isOperand(current)) {

postfix += current;
} else if (current == '(') {

s.push(current);
} else if (current == ')') {

while (!s.empty() && s.top() != '(') {

postfix += s.top();

s.pop();

s.pop(); // Discard the '(' from stack

} else {
while (!s.empty() && precedence(s.top()) >= precedence(current)) {
postfix += s.top();

s.pop();

s.push(current);

while (!s.empty()) {

postfix += s.top();

s.pop();
}

return postfix;

int main() {
string infix;

cout << "Enter infix expression: ";


cin >> infix;

string postfix = infixToPostfix(infix);


cout << "Postfix expression: " << postfix << endl;

return 0;

CONCLUSION:
Converting an infix expression (where operators like +, -, *, / are placed between operands) into a
postfix expression (where operators come after the operands) makes it easier for computers to
evaluate math expressions. This is because, in postfix notation, we don’t need parentheses or worry
about operator precedence. The order of operations is already clear.

Using a stack (a simple data structure that works like a pile), we can easily manage the operators and
make sure they are used in the correct order. This makes the conversion process straightforward and
quick.

REFERENCE:
1. TUF (Take U Forward)

2. Geeks for Geeks

You might also like