Skip to content

Commit

Permalink
Added Lab06
Browse files Browse the repository at this point in the history
  • Loading branch information
rajatkhanna1999 committed Jan 4, 2019
1 parent d4125ee commit 5e32102
Show file tree
Hide file tree
Showing 2 changed files with 294 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Lab-06/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Lab-06

## Question
My computer, Mac, really hates everytime I write an infix expression. You have to ease the task for Mac by transforming a given Infix expression to Postfix.

Instead of numeric variables the expression given is using lowercase english letters (a to z). The priority of operators is as follows '+', '-', '', '/', '^' (increasing left to right) '^' is for exponentiation, where '+', '-', '', and '/' have left associativity and '^' has right associativity.

Each case would be a string without space in valid form, i.e., containing only variables (a-z), operators ('+', '-', '', '/', '^') and parenthesis ( '(', ')' ).

Input Format

First line containing a number T, number of test cases. Then T lines with test cases in form of strings (say S).

Constraints

1 ≤ T ≤ 1000
1 ≤ length of S ≤ 1000
Subtask 1 There won't be any case with ambiguity i.e., such that two different answers are posible e.g., (a+b+c), it would be well parenthesised in that case.

Subtask 2 Original constraints applicable. Use appropritae precedence and associativity orders.

Output Format

For each expression of T given test cases print the output postfix form in new line.

Sample Input 0

4
(a+b)
(a+b*c)
(r+(s+t))
((r+s)+t)
Sample Output 0

ab+
abc*+
rst++
rs+t+
256 changes: 256 additions & 0 deletions Lab-06/lab06.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
#include <bits/stdc++.h>
using namespace std;

#define mod 1000000007
#define pb push_back
#define mp make_pair
#define PI 3.14159265358979323
#define debug(x) cout<<"Case "<<x<<": "
#define For(i,n) for(long long i=0;i<n;i++)
#define Frabs(i,a,b) for(long long i = a; i < b; i++)
#define Frabr(i,a,b) for(long long i = a; i >=b; i--)
#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector < pii > vpii;
typedef vector < pll > vpll;
typedef vector <string> vs;

//Handle:cyber_rajat


int main()
{
sync;
ll t;
cin>>t; //Taking input
while(t--)
{
//+', '-', '' and '/' have left associativity and '^' has right associativity.
stack<char> s;
string s1;
cin>>s1;
string s2="";
s1=s1+')'; //adding closing bracket at end
s.push('(');//pushing opening bracket in stack
ll i=0;
while(i<s1.size())
{
if(s1[i]=='(') //if opening then push it
{
s.push('(');
}
else if(s1[i]=='+')//if +
{
if(s.size()!=0){
char c=s.top();
if(c=='-')
{
while(c=='-' || c=='*' || c=='/' || c=='^' || c=='+'){//Checking precedence
s.pop();
s2=s2+c;
c=s.top();
}
s.push('+');
}
else if(c=='*')
{
while(c=='-' || c=='*' || c=='/' || c=='^' || c=='+'){
s.pop();
s2=s2+c;
c=s.top();
}
s.push('+');
}
else if(c=='/')
{
while(c=='-' || c=='*' || c=='/' || c=='^' || c=='+'){
s.pop();
s2=s2+c;
c=s.top();
}
s.push('+');
}
else if(c=='^')
{
while(c=='-' || c=='*' || c=='/' || c=='^' || c=='+'){
s.pop();
s2=s2+c;
c=s.top();
}
s.push('+');
}
else if(c=='+')
{
while(c=='-' || c=='*' || c=='/' || c=='^' || c=='+'){
s.pop();
s2=s2+c;
c=s.top();
}
s.push('+');
}
else
{
s.push('+');
}
}
else
{
s.push('+');
}
}
else if(s1[i]=='-')//if -
{
char c=s.top();
if(s.size()!=0){
if(c=='*')
{
while(c=='*' || c=='/' || c=='^' || c=='-'){//checking precedence
s.pop();
s2=s2+c;
c=s.top();
}
s.push('-');

}
else if(c=='-')
{
while(c=='*' || c=='/' || c=='^' || c=='-'){
s.pop();
s2=s2+c;
c=s.top();
}
s.push('-');
}
else if(c=='/')
{
while(c=='*' || c=='/' || c=='^' || c=='-'){
s.pop();
s2=s2+c;
c=s.top();
}
s.push('-');

}
else if(c=='^')
{
while(c=='*' || c=='/' || c=='^' || c=='-' ){
s.pop();
s2=s2+c;
c=s.top();
}
s.push('-');

}
else
{
s.push('-');
}
}
else
{
s.push('-');
}
}
else if(s1[i]=='*')//if *
{
if(s.size()!=0){
char c=s.top();
if(c=='/')
{
while(c=='/' || c=='^' || c=='*'){//checking precedence
s.pop();
s2=s2+c;
c=s.top();
}
s.push('*');
}
else if(c=='*')
{
while(c=='/' || c=='^' || c=='*'){
s.pop();
s2=s2+c;
c=s.top();
}
s.push('*');
}
else if(c=='^')
{
while(c=='/' || c=='^' || c=='*'){
s.pop();
s2=s2+c;
c=s.top();
}
s.push('*');
}
else
{
s.push('*');
}
}
else
{
s.push('*');
}
}
else if(s1[i]=='/')//if /
{
if(s.size()!=0){
char c=s.top();
if(c=='^')
{
while(c=='^' || c=='/'){//checking precedence
s.pop();
s2=s2+c;
c=s.top();
}
s.push('/');
}
else if(c=='/')
{
while(c=='^' || c=='/' ){
s.pop();
s2=s2+c;
c=s.top();
}
s.push('/');
}
else
{
s.push('/');
}
}
else
{
s.push('/');
}
}
else if(s1[i]=='^')// if ^ right associativity
{
s.push('^');
}
else if(s1[i]==')')//if closing bracket is reached then popping it
{
while(s.top()!='(')
{
char x=s.top();
s.pop();
s2=s2+x;
}
s.pop();
}
else
{
s2=s2+s1[i];//adding in the string
}
i++;
}
cout<<s2<<endl;//outputting string
}
return 0;
}

0 comments on commit 5e32102

Please sign in to comment.