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

stack assignment

The document contains C++ code for converting infix expressions to postfix and prefix notations using stacks. It includes functions to determine operator precedence and processes a sample expression 'a+b*(c^d-e)'. The outputs for the conversions are 'abcd^e-*+' for postfix and '+a*b-^cde' for prefix.

Uploaded by

OML series
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 views

stack assignment

The document contains C++ code for converting infix expressions to postfix and prefix notations using stacks. It includes functions to determine operator precedence and processes a sample expression 'a+b*(c^d-e)'. The outputs for the conversions are 'abcd^e-*+' for postfix and '+a*b-^cde' for prefix.

Uploaded by

OML series
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/ 4

Assignment Stack

infix to postfix

#include<iostream>
#include<stack>
using namespace std;

int prec(char c)
{
if (c == '^')
return 3;
else if (c == '/' || c == '*')
return 2;
else if (c == '+' || c == '-')
return 1;
else
return -1;
}

void infixToPostfix(string s)
{

stack<char> st;
string result;

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


char c = s[i];

if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
|| (c >= '0' && c <= '9'))
result += c;

else if (c == '(')
st.push('(');

else if (c == ')') {
while (st.top() != '(') {
result += st.top();
st.pop();
}
st.pop();
}

else {
while (!st.empty()
&& prec(s[i]) <= prec(st.top())) {
result += st.top();
st.pop();
}
st.push(c);
}
}

while (!st.empty()) {
result += st.top();
st.pop();
}

cout << result << endl;


}

int main()
{
string exp = "a+b*(c^d-e)";

infixToPostfix(exp);

return 0;
}

Output : abcd^e-*+

Infix to Prefix

#include <algorithm>
#include <iostream>
#include <stack>
using namespace std;

int prec(char c)
{
if (c == '^')
return 3;
else if (c == '/' || c == '*')
return 2;
else if (c == '+' || c == '-')
return 1;
else
return -1;
}

string infixToPrefix(string s)
{
stack<char> st;
string result;
reverse(s.begin(), s.end());

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


char c = s[i];

if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >=
'0' && c <= '9'))
result += c;
else if (c == ')')
st.push(c);
else if (c == '(') {
while (!st.empty() && st.top() != ')') {
result += st.top();
st.pop();
}
st.pop();
} else {
while (!st.empty() && prec(s[i]) < prec(st.top())) {
result += st.top();
st.pop();
}
st.push(c);
}
}

while (!st.empty()) {
result += st.top();
st.pop();
}

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

return result;
}

int main()
{
string exp = "a+b*(c^d-e)";

string prefixExp = infixToPrefix(exp);

cout << prefixExp << endl;


return 0;
}

Output:

+a*b-^cde

You might also like