#include <iostream>
#include <stack> // don't have time to implement a stack by myself
using namespace std;
// function to check precedence
int pre(char op);
int main()
{
string infix;
cout << "Expression: ";
cin >> infix;
stack<char> st;
string postfix = "";
for (int i = 0; i < infix.length(); i++)
{
char c = infix[i];
if (c >= '0' && c <= '9') // To make sure we are adding any number we meet
postfix += c;
else if (c == '(') // open prenthesess are added to our cup
st.push(c);
else if (c == ')')
{
// making sure we are poping from not empty stack
while (!st.empty() && st.top() != '(')
{
postfix += st.top();
st.pop();
}
st.pop(); // to remove one '(', aka remove one open prentheesss
}
else if (c == '+' || c == '-' || c == '*' || c == '/' || c == '^')
{
while (!st.empty() && pre(st.top()) >= pre(c))
{
postfix += st.top();
st.pop();
}
st.push(c);
}
}
while (!st.empty())
{
postfix += st.top();
st.pop();
}
cout << "After Converting To Postfix: " << postfix;
return 0;
}
// What comes before the other ..uuuh
int pre(char op)
{
if (op == '^')
return 3;
if (op == '*' || op == '/')
return 2;
if (op == '+' || op == '-')
return 1;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RhY2s+IC8vIGRvbid0IGhhdmUgdGltZSB0byBpbXBsZW1lbnQgYSBzdGFjayBieSBteXNlbGYKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCi8vIGZ1bmN0aW9uIHRvIGNoZWNrIHByZWNlZGVuY2UKaW50IHByZShjaGFyIG9wKTsKCmludCBtYWluKCkKewogICAgc3RyaW5nIGluZml4OwogICAgY291dCA8PCAiRXhwcmVzc2lvbjogIjsKICAgIGNpbiA+PiBpbmZpeDsKCiAgICBzdGFjazxjaGFyPiBzdDsKICAgIHN0cmluZyBwb3N0Zml4ID0gIiI7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IGluZml4Lmxlbmd0aCgpOyBpKyspCiAgICB7CiAgICAgICAgY2hhciBjID0gaW5maXhbaV07CiAgICAgICAgaWYgKGMgPj0gJzAnICYmIGMgPD0gJzknKSAvLyBUbyBtYWtlIHN1cmUgd2UgYXJlIGFkZGluZyBhbnkgbnVtYmVyIHdlIG1lZXQKICAgICAgICAgICAgcG9zdGZpeCArPSBjOwoKICAgICAgICBlbHNlIGlmIChjID09ICcoJykgLy8gb3BlbiBwcmVudGhlc2VzcyBhcmUgYWRkZWQgdG8gb3VyIGN1cAogICAgICAgICAgICBzdC5wdXNoKGMpOwoKICAgICAgICBlbHNlIGlmIChjID09ICcpJykKICAgICAgICB7CiAgICAgICAgICAgIC8vIG1ha2luZyBzdXJlIHdlIGFyZSBwb3BpbmcgZnJvbSBub3QgZW1wdHkgc3RhY2sKICAgICAgICAgICAgd2hpbGUgKCFzdC5lbXB0eSgpICYmIHN0LnRvcCgpICE9ICcoJykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgcG9zdGZpeCArPSBzdC50b3AoKTsKICAgICAgICAgICAgICAgIHN0LnBvcCgpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHN0LnBvcCgpOyAvLyB0byByZW1vdmUgb25lICcoJywgYWthIHJlbW92ZSBvbmUgb3BlbiBwcmVudGhlZXNzcwogICAgICAgIH0KICAgICAgICBlbHNlIGlmIChjID09ICcrJyB8fCBjID09ICctJyB8fCBjID09ICcqJyB8fCBjID09ICcvJyB8fCBjID09ICdeJykKICAgICAgICB7CiAgICAgICAgICAgIHdoaWxlICghc3QuZW1wdHkoKSAmJiBwcmUoc3QudG9wKCkpID49IHByZShjKSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgcG9zdGZpeCArPSBzdC50b3AoKTsKICAgICAgICAgICAgICAgIHN0LnBvcCgpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHN0LnB1c2goYyk7CiAgICAgICAgfQogICAgfQogICAgd2hpbGUgKCFzdC5lbXB0eSgpKQogICAgewogICAgICAgIHBvc3RmaXggKz0gc3QudG9wKCk7CiAgICAgICAgc3QucG9wKCk7CiAgICB9CgogICAgY291dCA8PCAiQWZ0ZXIgQ29udmVydGluZyBUbyBQb3N0Zml4OiAiIDw8IHBvc3RmaXg7CgogICAgcmV0dXJuIDA7Cn0KCi8vIFdoYXQgY29tZXMgYmVmb3JlIHRoZSBvdGhlciAuLnV1dWgKaW50IHByZShjaGFyIG9wKQp7CiAgICBpZiAob3AgPT0gJ14nKQogICAgICAgIHJldHVybiAzOwogICAgaWYgKG9wID09ICcqJyB8fCBvcCA9PSAnLycpCiAgICAgICAgcmV0dXJuIDI7CiAgICBpZiAob3AgPT0gJysnIHx8IG9wID09ICctJykKICAgICAgICByZXR1cm4gMTsKICAgIHJldHVybiAwOwp9