#include #include #include void to_postfix(std::stack&, std::string&, std::string&); bool is_operator(char); int priority(char); int main(int argc, char *argv[]) { if (argc != 2) { std::cerr << "Args: String_to_postfix" < expression; std::string my_postfix; std::string my_infix = argv[1]; to_postfix(expression, my_postfix, my_infix); std::cout << my_postfix << std::endl; return 0; } void to_postfix(std::stack& s, std::string& postfix, std::string& infix) { unsigned int i = 0; s.push('('); infix = infix + ')'; while (!s.empty()) { if (infix[i] >= '0' && infix[i] <= '9') postfix.append(1, infix[i]); else if (infix[i] == '(') s.push(infix[i]); else if (is_operator(infix[i])) { while (priority(infix[i]) >= priority(s.top())) { postfix.append(1, s.top()); s.pop(); } s.push(infix[i]); } else if (infix[i] == ')') { while (s.top() != '(') if (is_operator(s.top())) { postfix.append(1, s.top()); s.pop(); } s.pop(); } i++; } } int priority(char op) { if(op == '+' || op == '-') return 1; else if(op == '*' || op == '/') return 2; else if(op == '%' || op == '^') return 3; else return 4; } bool is_operator(char op) { if(op == '+' || op == '-' || op == '*' || op == '/' || op == '^' || op == '%') return true; else return false; }