#include<iostream>
using namespace std;
#include<conio.h>
char stack[30];
int main()
{
char a[30],ch='y',ar[30];
void pop(char d);
while(ch=='y' || ch=='Y')
{
int j=-1,i=0,m=0,b=-1;
cout<<"\n"<<"Enter the infix expression with brackets:";
cin>>a;
//*****************************
while(a[++j]!='\0')
{
if(a[j]=='(' || a[j]==')')
ar[i++]=a[j];
}
for(int m=0;m<i-1;m++)
{
if(ar[m]=='(')
++b;
else if(ar[m]==')')
--b;
}
j=0;i=0;m=0;
//******************************
if(b==0)
{
cout<<"\n"<<"Postfix expression:";
while(a[i]!='\0')
{
if(a[i]=='(')
{
stack[j++]='(';
}
else if((a[i]>64 && a[i]<91) || (a[i]>96 && a[i]<123))
{
pop(a[i]);
}
else if(a[i]=='+' || a[i]=='-')
{
if(stack[j-1]==40)
stack[j++]=a[i];
else
{
while(stack[j-1]!=40)
{
pop(stack[--j]);
// j--;
}
stack[j++]=a[i];
}
}
else if(a[i]=='*')
{
if(stack[j-1]>45 || stack[j-1]==37)
{
while(stack[j-1]!=40)
{
pop(stack[--j]);
//j--;
}
stack[j++]=a[i];
}
else
stack[j++]=a[i];
}
else if(a[i]=='^')
{
if(stack[j-1]<94 && stack[j-1]!=40)
{
while(stack[j-1]!=40)
{
pop(stack[--j]);
//j--;
}
stack[j++]=a[i];
}
else
stack[j++]=a[i];
}
else if(a[i]=='/')
{
if(stack[j-1]>47 || stack[j-1]==42 || stack[j-1]==37)
{
while(stack[j-1]!=40)
{
pop(stack[--j]);
//j--;
}
stack[j++]=a[i];
}
else
stack[j++]=a[i];
}
else if(a[i]=='%')
{
if(stack[j-1]>37 && stack[j-1]!=43 && stack[j-1]!=45)
{
while(stack[j-1]!=40)
{
pop(stack[--j]);
//j--;
}
stack[j++]=a[i];
}
else
stack[j++]=a[i];
}
else if(a[i]==41)
{
if(stack[j-1]!=40)
{
cout<<stack[--j];
if(stack[j-1]==40)
j--;
}
while(stack[j]!=40)
if(stack[--j]!=40)
cout<<stack[j];
}
i++;
}
}
else
cout<<"\nBrackets are not properly open or closed...Please check!";
cout<<"\n\nWant to Re-enter the Infix expression (Y/N)?:";
cin>>ch;
system("cls");
}
}
void pop(char a)
{
if(a!=40)
cout<<a;
}
using namespace std;
#include<conio.h>
char stack[30];
int main()
{
char a[30],ch='y',ar[30];
void pop(char d);
while(ch=='y' || ch=='Y')
{
int j=-1,i=0,m=0,b=-1;
cout<<"\n"<<"Enter the infix expression with brackets:";
cin>>a;
//*****************************
while(a[++j]!='\0')
{
if(a[j]=='(' || a[j]==')')
ar[i++]=a[j];
}
for(int m=0;m<i-1;m++)
{
if(ar[m]=='(')
++b;
else if(ar[m]==')')
--b;
}
j=0;i=0;m=0;
//******************************
if(b==0)
{
cout<<"\n"<<"Postfix expression:";
while(a[i]!='\0')
{
if(a[i]=='(')
{
stack[j++]='(';
}
else if((a[i]>64 && a[i]<91) || (a[i]>96 && a[i]<123))
{
pop(a[i]);
}
else if(a[i]=='+' || a[i]=='-')
{
if(stack[j-1]==40)
stack[j++]=a[i];
else
{
while(stack[j-1]!=40)
{
pop(stack[--j]);
// j--;
}
stack[j++]=a[i];
}
}
else if(a[i]=='*')
{
if(stack[j-1]>45 || stack[j-1]==37)
{
while(stack[j-1]!=40)
{
pop(stack[--j]);
//j--;
}
stack[j++]=a[i];
}
else
stack[j++]=a[i];
}
else if(a[i]=='^')
{
if(stack[j-1]<94 && stack[j-1]!=40)
{
while(stack[j-1]!=40)
{
pop(stack[--j]);
//j--;
}
stack[j++]=a[i];
}
else
stack[j++]=a[i];
}
else if(a[i]=='/')
{
if(stack[j-1]>47 || stack[j-1]==42 || stack[j-1]==37)
{
while(stack[j-1]!=40)
{
pop(stack[--j]);
//j--;
}
stack[j++]=a[i];
}
else
stack[j++]=a[i];
}
else if(a[i]=='%')
{
if(stack[j-1]>37 && stack[j-1]!=43 && stack[j-1]!=45)
{
while(stack[j-1]!=40)
{
pop(stack[--j]);
//j--;
}
stack[j++]=a[i];
}
else
stack[j++]=a[i];
}
else if(a[i]==41)
{
if(stack[j-1]!=40)
{
cout<<stack[--j];
if(stack[j-1]==40)
j--;
}
while(stack[j]!=40)
if(stack[--j]!=40)
cout<<stack[j];
}
i++;
}
}
else
cout<<"\nBrackets are not properly open or closed...Please check!";
cout<<"\n\nWant to Re-enter the Infix expression (Y/N)?:";
cin>>ch;
system("cls");
}
}
void pop(char a)
{
if(a!=40)
cout<<a;
}
No comments:
Post a Comment