Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long LL;
- // Return precedence of operator
- int op_prec(string op){
- //if(op == "(" || op == ")")return 0x100;
- if(op == "!")return 0x50;
- if(op == "^")return 0x30;
- if(op == "*" || op == "/" || op == "%")return 0x20;
- if(op == "+" || op == "-")return 0x10;
- if(op == "!" || op == ">" || op == "<" || op == ">=" ||
- op == "<=" || op == "=="){return 0x08;}
- if(op[0] == '&' || op[0] == '|' || op == ">>" || op == "<<")return 0x05;
- return 0;
- }
- bool is_operator(string op){
- if(op == "+" || op == "-" || op == "*" || op == "/" ||
- op == "%" || op == "^" || op == "!" || op == "&" ||
- op == "&&" || op == "|" || op == "||" || op == ">" ||
- op == ">" || op == ">=" || op == "<=" || op == "==" ||
- op == ">>" || op == "<<"){
- return true;
- }
- return false;
- }
- bool is_basic_operator(string op){
- if(op == "+" || op == "-" || op == "*" || op == "/" ||
- op == "%" || op == "^"){return true;}
- return false;
- }
- string infixToPostfix(string str){
- str = "( " + str + " )";
- stringstream ss(str);
- string op, re = "";
- stack<string> op_stack;
- while(ss >> op){
- //cout << op << ' ' << op.length() << '\n';
- if(isdigit(op[0]) || isalpha(op[0])){
- re += op;
- re += ' ';
- }
- else if(op == "("){
- op_stack.push("(");
- }
- else if(op == ")"){
- while(op_stack.top() != "("){
- re += op_stack.top();
- re += ' ';
- op_stack.pop();
- }
- op_stack.pop();
- }
- else{
- while(op_prec(op) <= op_prec(op_stack.top()) ){
- re += op_stack.top();
- re += ' ';
- op_stack.pop();
- }
- op_stack.push(op);
- }
- }
- return re;
- }
- map<string, int> dict;
- int calc_postfix(string str){
- stringstream ss(str);
- stack<int> st;
- while(ss >> str){
- if(!is_basic_operator(str) && !isdigit(str[0])){
- return 0x80000000;
- }
- if(isdigit(str[0])){
- st.push(atoi(str.c_str()));
- }
- else if(str[0] == '+'){
- int b = st.top(); st.pop();
- int a = st.top(); st.pop();
- st.push(a + b);
- }
- else if(str[0] == '-'){
- int b = st.top(); st.pop();
- int a = st.top(); st.pop();
- st.push(a - b);
- }
- else if(str[0] == '*'){
- int b = st.top(); st.pop();
- int a = st.top(); st.pop();
- st.push(a * b);
- }
- else if(str[0] == '/'){
- int b = st.top(); st.pop();
- int a = st.top(); st.pop();
- st.push(a / b);
- }
- else if(str[0] == '%'){
- int b = st.top(); st.pop();
- int a = st.top(); st.pop();
- st.push(a % b);
- }
- else if(str[0] == '^'){
- int b = st.top(); st.pop();
- int a = st.top(); st.pop();
- st.push(pow(a,b));
- }
- }
- return st.top();
- }
- // Convert postfix to Prefix expression
- string postfixToPrefix(string str) {
- stack<string> st;
- stringstream ss(str);
- while(ss >> str){
- if(!is_basic_operator(str) && !isdigit(str[0])){
- return "Not supported";
- }
- if(is_operator(str)){
- string b = st.top(); st.pop();
- string a = st.top(); st.pop();
- st.push(str + ' ' + a + ' '+ b + ' ');
- }
- else{st.push(str);}
- }
- return st.top();
- }
- bool process_input(string &str){
- cout << "Infix: ";
- bool cont = getline(cin, str);
- return cont;
- }
- int main(){
- string str;
- while(process_input(str)){
- string result = infixToPostfix(str);
- string result2 = postfixToPrefix(result);
- cout << "Postfix: " << result << '\n';
- cout << "Prefix: " << result2 << '\n';
- cout << "Result: ";
- int c = calc_postfix(result);
- if(c == 0x80000000){cout << "Not supported\n";}
- else{cout << c << '\n';}
- cout << "---------------\n";
- }
- return 0;
- }
- /*
- 2 + 3 * 4
- 2 * ( 3 + 4 ) * 5 + 2
- 5 * 6 + 7
- ! ( A && ! ( ( B < C ) || ( C > D ) ) ) || ( C >> 2 + 2 > E )
- 234*+
- 234+*5*2+
- 56*7+
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement