forked from vidu171/Spoj-200
-
Notifications
You must be signed in to change notification settings - Fork 0
/
q5-ONP.cpp
61 lines (55 loc) · 1.26 KB
/
q5-ONP.cpp
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
60
61
#include<iostream>
#include<cstring>
#include<cctype>
using namespace std;
int top=-1;
char stack[200];
void push(int data)
{
stack[++top]=data;
}
char pop()
{
char data=stack[top];
top--;
return data;
}
int main(){
int T;
cin>>T;
char str[1000];
while(T--){
cin>>str;
int len = strlen(str);
for(int i=0;i<len;i++){
if(isalpha(str[i]))
cout<<str[i];
else if(str[i]=='('){
push('(');
}
else if(str[i]==')'){
while(stack[top]!='(')
printf("%c", pop());
pop();
}
else if(str[i]=='^'){
while(stack[top]=='^')
printf("%c", pop());
push(str[i]);
}
else if(str[i]=='/'|| str[i]=='*'){
while(stack[top]=='/'||stack[top]=='*'||stack[top]=='^'){
printf("%c",pop());
}
push(str[i]);
}
else if(str[i]=='+'|| str[i]=='-'){
while(stack[top]!='(')
printf("%c",pop());
push(str[i]);
}
}
printf("\n");
}
}
// (a+(b*c+d))