-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathONP.cpp
More file actions
61 lines (56 loc) · 1.1 KB
/
ONP.cpp
File metadata and controls
61 lines (56 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<bits/stdc++.h>
using namespace std;
char s[400];
int top = -1;
void push(char c){
s[++top] = c;
}
char pop(){
if(top == -1)
return -1;
else
return s[top--];
}
int precedence(char ch){
if(ch == '(')
return 1;
else if(ch=='+' or ch=='-')
return 2;
else if(ch=='*' or ch=='/' or ch=='%')
return 3;
else if(ch=='^')
return 4;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
char infix[400],postfix[400];
int t;
for(cin>>t;t--;)
{
char ch, x;
int i=0,j=0;
cin>>infix;
while((ch=infix[i++]) != '\0'){
if(isalnum(ch)){
postfix[j++]=ch;
}else if(ch == '('){
push(ch);
}else if(ch == ')'){
while((x=pop()) != '(')
postfix[j++]=x;
}else{
while(precedence(s[top])>=precedence(ch) and top!=-1) {
postfix[j++] =pop();
}
push(ch);
}
}
while(top!=-1) {
postfix[j++] = pop();
}
postfix[j]='\0';
cout<<postfix<<endl;
}
return 0;
}