Assignment 2
Submitted to:
Sir Ashar
Submitted by:
Ayesha Fayaz 22_Arid_4354
Khansa Rehman 22_Arid_4368
Areeba Noor 22_Arid_4353
Mahnoor Nadeem 22_Arid_4369
Course Title:
Data Structure and Algorithm
Submission Date:
15_11_2023
Gujrat Institution of Management and Sciences
• POSTFIX TO INFIX
#include<iostream>
#include<stack>
using namespace std;
class PostfixToInfixConverter {
public:
string postfixToInfix(string postfix) {
stack<string> st;
for (int i = 0; i < postfix.length(); i++) {
char ch = postfix[i];
if (isOperand(ch)) {
string operand(1, ch);
st.push(operand);
} else if (isOperator(ch)) {
string operand2 = st.top();
st.pop();
string operand1 = st.top();
st.pop();
string result = "(" + operand1 + ch + operand2 +
")";
st.push(result);
}
}
return st.top();
}
bool isOperand(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
bool isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/');
}
};
int main() {
PostfixToInfixConverter converter;
string postfix;
cout << "Enter the postfix expression: ";
cin >> postfix;
string infixResult = converter.postfixToInfix(postfix);
cout << "Infix Expression: " << infixResult << endl;
return 0;
}
OUTPUT
Enter the postfix expression: ab+
Infix Expression: (a+b)
• Prefix to infix
#include <iostream>
#include <stack>
#include <string>
using namespace std;
bool isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c ==
'/');
}
string prefixToInfix(const string& prefix) {
stack<string> stack;
// Traverse the expression from right to left
for (int i = prefix.length() - 1; i >= 0; --i) {
char current = prefix[i];
if (isOperator(current)) {
// Pop two operands from the stack
string operand1 = stack.top();
stack.pop();
string operand2 = stack.top();
stack.pop();
// Combine operands with the operator
and add parentheses
string result = "(" + operand1 + current +
operand2 + ")";
// Push the result back onto the stack
stack.push(result);
} else {
// Operand, push onto the stack
stack.push(std::string(1, current));
}
}
// The final result is on the stack
return stack.top();
}
int main() {
string prefixExpression = "*+AB-CD";
string infixExpression =
prefixToInfix(prefixExpression);
cout << "Prefix Expression: " <<
prefixExpression <<endl;
cout << "Infix Expression: " << infixExpression
<<endl;
return 0;
}
OUTPUT:
Prefix Expression: *+AB-CD
Infix Expression: ((A+B)*(C-D))
• POSTFIX TO PREFIX
#include <iostream>
#include <stack>
#include <string>
using namespace std;
bool isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c ==
'/');
}
string postfixToPrefix(const string& postfix) {
stack<string> stack;
// Traverse the expression from left to right
for (char current : postfix) {
if (isOperator(current)) {
// Pop two operands from the stack
string operand2 = stack.top();
stack.pop();
string operand1 = stack.top();
stack.pop();
// Combine operands with the operator
and add to the stack
string result = current + operand1 +
operand2;
stack.push(result);
} else {
// Operand, push onto the stack
stack.push(string(1, current));
}
}
// The final result is on the stack
return stack.top();
}
int main() {
string postfixExpression = "AB+C*";
string prefixExpression =
postfixToPrefix(postfixExpression);
cout << "Postfix Expression: " <<
postfixExpression << endl;
cout << "Prefix Expression: " <<
prefixExpression << endl;
return 0;
}
OUTPUT:
Postfix Expression: AB+C*
Prefix Expression: *+ABC
• PREVIX TO POSTFIX
#include <iostream>
#include <stack>
#include <string>
using namespace std;
bool isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c ==
'/');
}
string prefixToPostfix(const string& prefix) {
stack<string> stack;
// Traverse the expression from right to left
for (int i = prefix.length() - 1; i >= 0; --i) {
char current = prefix[i];
if (isOperator(current)) {
// Pop two operands from the stack
string operand1 = stack.top();
stack.pop();
string operand2 = stack.top();
stack.pop();
// Combine operands with the operator
and add to the stack
string result = operand1 + operand2 +
current;
stack.push(result);
} else {
// Operand, push onto the stack
stack.push(string(1, current));
}
}
// The final result is on the stack
return stack.top();
}
int main() {
string prefixExpression = "*+AB-CD";
string postfixExpression =
prefixToPostfix(prefixExpression);
cout << "Prefix Expression: " <<
prefixExpression <<endl;
cout << "Postfix Expression: " <<
postfixExpression <<endl;
return 0;
}
OUTPUT
Prefix Expression: *+AB-CD
Postfix Expression: AB+CD-*