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 == '(') {
[Link](token);
} else if (token == ')') {
while (![Link]() && [Link]() != '(') {
postfix += [Link]();
[Link]();
[Link]();
} else {
while (![Link]() && precedence([Link]()) >= precedence(token)) {
postfix += [Link]();
[Link]();
[Link](token);
while (![Link]()) {
postfix += [Link]();
[Link]();
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 = [Link]() - 1; i >= 0; i--) {
if (isdigit(prefix[i])) {
[Link](prefix[i] - '0');
} else {
int operand1 = [Link](); [Link]();
int operand2 = [Link](); [Link]();
switch (prefix[i]) {
case '+': [Link](operand1 + operand2); break;
case '-': [Link](operand1 - operand2); break;
case '*': [Link](operand1 * operand2); break;
case '/': [Link](operand1 / operand2); break;
}
}
return [Link]();
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)) {
[Link](string(1, token));
} else {
string operand2 = [Link](); [Link]();
string operand1 = [Link](); [Link]();
string expr = "(" + operand1 + token + operand2 + ")";
[Link](expr);
return [Link]();
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([Link](), [Link]());
for (char token : reversedInfix) {
if (isalnum(token)) {
prefix += token;
} else if (token == ')') {
[Link](token);
} else if (token == '(') {
while (![Link]() && [Link]() != ')') {
prefix += [Link]();
[Link]();
[Link]();
} else {
while (![Link]() && precedence([Link]()) > precedence(token)) {
prefix += [Link]();
[Link]();
[Link](token);
}
while (![Link]()) {
prefix += [Link]();
[Link]();
reverse([Link](), [Link]());
return prefix;
int main() {
string infix;
cout << "Enter infix expression: ";
cin >> infix;
cout << "Prefix expression: " << infixToPrefix(infix) << endl;
return 0;