Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
12 views4 pages

DSLab PR 10 ExpConvert

The document contains a C++ program that converts infix expressions to postfix notation using a stack. It accepts single-character operands and the operators '+', '-', '*', and '/'. The program processes the input infix expression and outputs the corresponding postfix expression.

Uploaded by

dhirajyadav32134
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)
12 views4 pages

DSLab PR 10 ExpConvert

The document contains a C++ program that converts infix expressions to postfix notation using a stack. It accepts single-character operands and the operators '+', '-', '*', and '/'. The program processes the input infix expression and outputs the corresponding postfix expression.

Uploaded by

dhirajyadav32134
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/ 4

/*

class & Branch: SE Computer Engineering


Roll No: ......
Subject: Data Structures Lab
Practical No: 10
Title: Implement C++ program for Expression conversion : Infix to Postfix.

1. Operands and operator, both must be single character.


2. Input Postfix expression must be in a desired format.
3. Only '+', '-', '*' and '/ ' operators are expected.
*/

//..............Header Files
#include<iostream>
using namespace std;

//..............Stack and Top Declaration


char stack[10];
int top = -1;

//.......Function to convert Expression: INFIX - POSTFIX

void expConvert()
{
//....to store Infix expression
char infix[20];

//....to store Postfix expression


char postfix[20];

//....Index of Infix String


int i = 0;

//....Index of Postfix String


int j = 0;

//....Accept Input/Infix String

cout<<"\n\n Enter the Infix Expression: ";


cin>>infix;

//....Visit/Access each character of Infix String

for(i=0; infix[i] != '\0'; i++)


{
//......if Opening Parantheses - Push in Stack

if(infix[i] == '(' || infix[i] == '[' || infix[i] == '{')


{
top++;
stack[top] = infix[i];
} //....if Operand - Copy to Postfix String

else if(infix[i] == 'a' || infix[i] == 'b' || infix[i] == 'c' || infix[i] == 'd')


{
postfix[j] = infix[i];
j++;
} //....if Operator - push in stack

else if(infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/')


{
top++;
stack[top] = infix[i];
}
else //......if Closing Parantheses - Pop until Opening and copy to Postfix String.
{
switch(infix[i])
{
case ')': while(stack[top] != '(')
{
postfix[j] = stack[top];
j++;
top--;
}
top--;
break;
case ']': while(stack[top] != '[')
{
postfix[j] = stack[top];
j++;
top--;
}
top--;
break;
case '}': while(stack[top] != '{')
{
postfix[j] = stack[top];
j++;
top--;
}
top--;
break;
}

}
}

postfix[j] = '\0'; //....Display Postfix String

cout<<"\n\t The Postfix String: "<<postfix;

}
int main()
{
cout<<"\n-----------------------------------------------\n";
cout<<"**** Expression Conversion : Infix to Postfix **** ";
cout<<"\n-----------------------------------------------\n";

expConvert();

cout<<"\n\n";
return 0;
}

/*--------------------OUTPUT-----------------------

student@ioe-melab01-134:~$ ./a

-----------------------------------------------
**** Expression Conversion : Infix to Postfix ****
-----------------------------------------------

Enter the Infix Expression: (a+b)

The Postfix String: ab+

student@ioe-melab01-134:~$ ./a

-----------------------------------------------
**** Expression Conversion : Infix to Postfix ****
-----------------------------------------------

Enter the Infix Expression: [(a+b)*(c-d)]

The Postfix String: ab+cd-*

student@ioe-melab01-134:~$ ./a
-----------------------------------------------
**** Expression Conversion : Infix to Postfix ****
-----------------------------------------------

Enter the Infix Expression: [a*(c-d)]

The Postfix String: acd-*

student@ioe-melab01-134:~$

------------------------------------------------------*/

You might also like