Skip to content

Commit 6f1ed56

Browse files
authored
Create infix to prefix expression
1 parent 3956c1a commit 6f1ed56

File tree

1 file changed

+172
-0
lines changed

1 file changed

+172
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
//Author: Fuadul Hasan([email protected])
2+
//BSMRSTU,Gopalganj
3+
//#include<bits/stdc++.h>
4+
#define _USE_MATH_DEFINES
5+
#include <set>
6+
#include <map>
7+
#include <list>
8+
#include <queue>
9+
#include <stack>
10+
#include <cmath>
11+
#include <ctime>
12+
#include <cstdio>
13+
#include <string>
14+
#include <vector>
15+
#include <bitset>
16+
#include <random>
17+
#include<iomanip>
18+
#include <cassert>
19+
#include <cstring>
20+
#include <sstream>
21+
#include <complex>
22+
#include <numeric>
23+
#include <iostream>
24+
#include <algorithm>
25+
#include <functional>
26+
#include <unordered_set>
27+
#include <unordered_map>
28+
using namespace std;
29+
30+
//debug..........
31+
#define error(args...) {vector<string>_v=split(#args,',');err(_v.begin(),args);cout<<endl;}
32+
vector<string> split(const string &s, char c) {vector<string>v; stringstream ss(s); string x;while (getline(ss, x, c))v.emplace_back(x); return move(v);} void err(vector<string>::iterator it) {}
33+
template<typename T, typename... Args>void err(vector<string>::iterator it, T a, Args...args) {cout << it->substr((*it)[0] == ' ', it->length()) << " = " << a << " "; err(++it, args...);}
34+
35+
//............ignore it..................//
36+
#define F first
37+
#define S second
38+
#define Pi atan(1)*4
39+
#define mp make_pair
40+
#define pb push_back
41+
const int M = 1e9 + 7;
42+
#define ld long double
43+
#define ll long long int
44+
#define happy cin.tie(0);
45+
#define point(x) cout<<fixed<<setprecision(x)
46+
int length(string s){return (int)s.size();}
47+
#define mem(a) memset(a , 0 ,sizeof a)
48+
#define memn(a) memset(a , -1 ,sizeof a)
49+
#define coding ios::sync_with_stdio(false);
50+
#define Unique(c) (c).resize(unique(all(c))-(c).begin())
51+
#define vout(v) for (auto z: v) cout << z << " "; cout << endl;
52+
53+
int length(long long x){int l = 0;for(long long i=x;i;i/=10)l++;return l;}
54+
int dx[8]= {1,0,-1,0,-1,-1,1,1};
55+
int dy[8]= {0,1,0,-1,-1,1,-1,1};
56+
57+
#define rep(i,b,e) for(__typeof(e) i = (b) ; i != (e + 1) - 2 * ((b) > (e)) ; i += 1 - 2 * ((b) > (e)))
58+
59+
long long Inv_pow(long long a,long long n){ll res = 1;while(n){if(n&1) res = ((res%M)*(a%M))%M;a = ((a%M)*(a%M))%M;n>>=1;}return res%M;}
60+
// suffix_prefix....
61+
std::vector<ll> prefix_sum(std::vector<ll> a){int n = a.size();std::vector<ll> prf(n,0);for(int i=0;i<n;i++){
62+
if(i == 0){prf[i] = a[i];}else{prf[i] = prf[i-1]+a[i];}}return prf;}
63+
std::vector<ll> suffix_sum(std::vector<ll> a){int n = a.size();std::vector<ll>suf(n,0);for(int i=n-1;i>=0;i--){
64+
if(i == n-1){suf[i] = a[i];}else{suf[i] = suf[i+1]+a[i];}}return suf;}
65+
66+
long long Lcm(long long a,long long b){return (((a)*(b)))/__gcd(a,b);}
67+
68+
#define Test cout<<"Case #"<<tc++<<": ";
69+
int tc = 1;
70+
71+
inline void read(std::vector<int> &v){for(int i=0;i<(int)v.size();i++){cin>>(v[i]);}}
72+
inline void readl(std::vector<ll> &v){for(int i=0;i<(int)v.size();i++){cin>>(v[i]);}}
73+
74+
template<class T> bool remin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
75+
template<class T> bool remax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
76+
77+
inline void read(int v[],int n){for(int i=0;i<n;i++){cin>>(v[i]);}}
78+
inline void readl(ll v[],int n){for(int i=0;i<n;i++){cin>>(v[i]);}}
79+
80+
inline int add(int a, int b, int mod) {a += b ; return a >= mod ? a - mod : a ;}
81+
inline int sub(int a, int b, int mod) {a -= b ; return a < 0 ? a + mod : a ;}
82+
inline int mul(int a, int b, int mod) {return (ll)a * b % mod ;}
83+
84+
#define pr pair<int, int>
85+
#define vpr vector<pr>
86+
#define vi std::vector<int>
87+
#define vll std::vector<ll>
88+
#define all(n) n.begin(),n.end()
89+
90+
91+
const int Inf = (int)2e9 + 5;
92+
const ll Lnf = (ll)2e18 + 5;
93+
const int N = 5e5 + 5;
94+
const int NN = 1e6 + 5;
95+
96+
int prec(char c){
97+
if(c == '^'){
98+
return 3;
99+
}else if(c == '/' or c == '*'){
100+
return 2;
101+
}else if(c == '+' or c == '-'){
102+
return 1;
103+
}else{
104+
return -1;
105+
}
106+
}
107+
108+
string infixToPostfix(string s){
109+
stack<char> st;
110+
string result = "";
111+
112+
for(int i=0;i<s.size();i++){
113+
char c = s[i];
114+
if((c >= 'a' and c <= 'z') || (c >= 'A' and c <= 'Z'))
115+
result += c;
116+
else if(c == '('){
117+
st.push('(');
118+
}else if(c == ')'){
119+
while(st.top() != '('){
120+
result += st.top();
121+
st.pop();
122+
}
123+
st.pop();
124+
}else{
125+
while(!st.empty() and prec(s[i]) <= prec(st.top())){
126+
result += st.top();
127+
st.pop();
128+
}
129+
st.push(c);
130+
}
131+
}
132+
133+
while(!st.empty()){
134+
result += st.top();
135+
st.pop();
136+
}
137+
return result;
138+
}
139+
140+
int solve()
141+
{
142+
143+
//Test
144+
145+
string s;
146+
cin>>s;
147+
148+
string t = infixToPostfix(s);
149+
150+
cout<<t<<endl;
151+
152+
153+
return 0;
154+
//error();
155+
}
156+
int main(){
157+
158+
happy coding
159+
int test = 1;
160+
cin>>test;
161+
while (test--)solve();return 0;
162+
}
163+
164+
/*
165+
1. Everything happens for something good.
166+
2. Don't expect anything from anyone except yourself.
167+
3. Self discipline is the magic power that makes you unstoppable.
168+
*/
169+
170+
/* Note:
171+
-> when you use long long. always keep ll in integer number. like 2LL or (ll)i.
172+
*/

0 commit comments

Comments
 (0)