-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathONP.cpp
55 lines (45 loc) · 1.33 KB
/
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
// AC , ALGO : Stack, Postfix Expression.
/*
Helpful Link : http://en.wikipedia.org/wiki/Reverse_Polish_notation
*/
// For any clarifications, contact me at : osinha6792@gmail.com
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std ;
int main( )
{
int i , j , top , t ;
char str[401] , stack[401] , ans[401] ;
cin >> t ;
t += 1 ;
while( t-- )
{
gets( str ) ;
i = j = top = 0 ;
while( str[i] != '\0' )
{
if( str[i] == '(' || str[i] == '-' || str[i] == '+' || str[i] == '*' || str[i] == '/' || str[i] == '^' || str[i] == ')' )
{
stack[top] = str[i] ;
top += 1 ;
}
else
{
ans[j] = str[i] ;
j += 1 ;
}
if( stack[top-1] == ')' )
{
top -= 2 ;
ans[j] = stack[top] ;
top -= 1 ;
j += 1 ;
}
i += 1 ;
}
ans[j] = '\0' ;
puts( ans ) ;
}
return 0 ;
}