Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <queue>
- #include <stack>
- #include <string.h>
- #include <string>
- using namespace std;
- ifstream in("input.in");
- ifstream output("output.out");
- queue<char> infix;
- queue<string> postfix;
- void toPostFix();
- int priority(char c);
- bool isOperand(char c);
- int main(){
- char s[100001];
- in.getline(s, 100001);
- for(int i = 0; i < strlen(s); i++){
- infix.push(s[i]);
- }
- toPostFix();
- bool b = false;
- while(!postfix.empty()){
- cout<<postfix.front()<<" ";
- postfix.pop();
- }
- return 0;
- }
- void toPostFix(){
- stack<char> s;
- while(!infix.empty()){
- char c = infix.front();
- infix.pop();
- if(isOperand(c)){
- int n = (c - '0');
- while(isOperand(infix.front())){
- int ch = infix.front();
- n = n * 10 + (int)(ch - '0');
- infix.pop();
- }
- string st = to_string(n);
- postfix.push( st );
- } else if(c == '('){
- s.push(c);
- } else if(c == ')') {
- while(s.top() != '('){
- string st(1, s.top());
- postfix.push( st );
- s.pop();
- }
- s.pop();
- } else {
- while( !s.empty() && priority(s.top()) >= priority(c) ){
- string st(1, s.top());
- postfix.push( st );
- s.pop();
- }
- s.push(c);
- }
- }
- while(!s.empty()){
- string st(1, s.top());
- postfix.push( st );
- s.pop();
- }
- }
- bool isOperand(char c){
- if(strchr("+-*/^()", c) != NULL){
- return false;
- }
- return true;
- }
- int priority(char c){
- if(c == '+' || c == '-')
- return 1;
- if(c == '*' || c == '/')
- return 2;
- if(c == '^')
- return 3;
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement