0% found this document useful (0 votes)
3 views7 pages

Data Structure Assignment

The document contains C++ code for converting between infix, postfix, and prefix expressions, as well as evaluating prefix expressions. It includes functions for determining operator precedence and converting expressions in both directions. The code is structured with multiple main functions for user input and output of the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Data Structure Assignment

The document contains C++ code for converting between infix, postfix, and prefix expressions, as well as evaluating prefix expressions. It includes functions for determining operator precedence and converting expressions in both directions. The code is structured with multiple main functions for user input and output of the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Name : Ahmed mohamed Elsabaa

ID:202304295

Section : Saturday 12:20 pm

#include <iostream>

#include <stack>

#include <string>

#include <cctype>

using namespace std;

int precedence(char op) {

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

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

return 0;

string infixToPostfix(const string& infix) {

stack<char> s;

string postfix;

for (char token : infix) {

if (isalnum(token)) {

postfix += token;

} else if (token == '(') {

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

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

postfix += s.top();

s.pop();

s.pop();

} else {

while (!s.empty() && precedence(s.top()) >= precedence(token)) {

postfix += s.top();

s.pop();

s.push(token);

while (!s.empty()) {

postfix += s.top();

s.pop();

return postfix;

int main() {

string infix;

cout << "Enter infix expression: ";


cin >> infix;

cout << "Postfix expression: " << infixToPostfix(infix) << endl;

return 0;

#include <iostream>

#include <stack>

#include <cctype>

using namespace std;

int evaluatePrefix(const string& prefix) {

stack<int> s;

for (int i = prefix.length() - 1; i >= 0; i--) {

if (isdigit(prefix[i])) {

s.push(prefix[i] - '0');

} else {

int operand1 = s.top(); s.pop();

int operand2 = s.top(); s.pop();

switch (prefix[i]) {

case '+': s.push(operand1 + operand2); break;

case '-': s.push(operand1 - operand2); break;

case '*': s.push(operand1 * operand2); break;

case '/': s.push(operand1 / operand2); break;

}
}

return s.top();

int main() {

string prefix;

cout << "Enter prefix expression: ";

cin >> prefix;

cout << "Result: " << evaluatePrefix(prefix) << endl;

return 0;

#include <iostream>

#include <stack>

#include <string>

using namespace std;

string postfixToInfix(const string& postfix) {

stack<string> s;

for (char token : postfix) {

if (isalnum(token)) {

s.push(string(1, token));

} else {

string operand2 = s.top(); s.pop();

string operand1 = s.top(); s.pop();


string expr = "(" + operand1 + token + operand2 + ")";

s.push(expr);

return s.top();

int main() {

string postfix;

cout << "Enter postfix expression: ";

cin >> postfix;

cout << "Infix expression: " << postfixToInfix(postfix) << endl;

return 0;

#include <iostream>

#include <stack>

#include <string>

#include <algorithm>

using namespace std;

int precedence(char op) {

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

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

return 0;

}
string infixToPrefix(const string& infix) {

stack<char> s;

string prefix;

string reversedInfix = string(infix.rbegin(), infix.rend());

for (char token : reversedInfix) {

if (isalnum(token)) {

prefix += token;

} else if (token == ')') {

s.push(token);

} else if (token == '(') {

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

prefix += s.top();

s.pop();

s.pop();

} else {

while (!s.empty() && precedence(s.top()) > precedence(token)) {

prefix += s.top();

s.pop();

s.push(token);

}
while (!s.empty()) {

prefix += s.top();

s.pop();

reverse(prefix.begin(), prefix.end());

return prefix;

int main() {

string infix;

cout << "Enter infix expression: ";

cin >> infix;

cout << "Prefix expression: " << infixToPrefix(infix) << endl;

return 0;

You might also like