#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<process.h>
char operator_stack[50];
int operator_top=-1;
int isoperator(char s)
{
if(s=='^'||s=='*'||s=='/'||s=='%'||s=='+'||s=='-')
{
return 1;
}
else
{
return 0;
}
}
int isparenthesis(char pa)
{
if(pa=='(')
return (1);
else if(pa==')')
return(2);
else
return(0);
}
void operator_push(char n)
{
if(operator_top==49)
{
printf("\Overflow condition");
exit(0);
}
operator_stack[++operator_top]=n;
}
int priority(char p)
{
if(p=='^')
return 3;
else if(p=='*'||p=='/'||p=='%')
return 2;
else if(p=='+'||p=='-')
return 1;
else
return 0;
}
char operator_pop()
{
if(operator_top==-1)
{
printf("\nUnderflow condition");
exit(0);
}
return(operator_stack[operator_top--]);
}
void main()
{
int x;
char *exp_infix;
char postfix[50][50];
int digit;
char dig[50];
int y;
printf("\nEnter the infix notation");
scanf("%[^\n]s",exp_infix);
for(x=0,y=0;exp_infix[x]!='\0';)
{
if(isdigit(exp_infix[x]))
{
digit=0;
while((exp_infix[x]>=48)&&(exp_infix[x]<=57))
{
dig[digit]=exp_infix[x];
x++;
digit=digit+1;
}
dig[digit]='\0';
//puts(dig);
strcpy(postfix[y],dig);
// puts(postfix[y]);
//postfix[y]=exp_infix[x];
y++;
}
else if(isoperator(exp_infix[x])||isparenthesis(exp_infix[x]))
{
if(operator_top==-1||isparenthesis(exp_infix[x])==1)
{
operator_push(exp_infix[x]);
x++;
}
else
{
if(isparenthesis(exp_infix[x])==2)
{
while(operator_stack[operator_top]!='(')
{
digit=0;
dig[digit]=operator_pop();
strcpy(postfix[y],dig);
y++;
}
operator_pop();
x++;
continue;
}
if(priority(exp_infix[x])<priority(operator_stack[operator_top]))
{
while((priority(exp_infix[x])<priority(operator_stack[operator_top])&&(operator_top>-1)))
{
digit=0;
dig[digit]=operator_pop();
digit=digit+1;
dig[digit]='\0';
strcpy(postfix[y],dig);
y++;
}
operator_push(exp_infix[x]);
x++;
}
else if(priority(exp_infix[x])==priority(operator_stack[operator_top]))
{
digit=0;
dig[digit]=operator_pop();
digit=digit+1;
dig[digit]='\0';
strcpy(postfix[y],dig);
y++;
operator_push(exp_infix[x]);
x++;
}
else if(priority(exp_infix[x])>priority(operator_stack[operator_top]))
{
operator_push(exp_infix[x]);
x++;
}
}
}
}
while(operator_top!=-1)
{
digit=0;
dig[digit]=operator_pop();
digit++;
dig[digit]='\0';
strcpy(postfix[y],dig);
y++;
}
//postfix[y]='\0';
for(x=0;x<y;x++)
{
printf("%s",postfix[x]);
}
getch();
}
output:
Enter the infix notation 5*2+6
652*+
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<process.h>
char operator_stack[50];
int operator_top=-1;
int isoperator(char s)
{
if(s=='^'||s=='*'||s=='/'||s=='%'||s=='+'||s=='-')
{
return 1;
}
else
{
return 0;
}
}
int isparenthesis(char pa)
{
if(pa=='(')
return (1);
else if(pa==')')
return(2);
else
return(0);
}
void operator_push(char n)
{
if(operator_top==49)
{
printf("\Overflow condition");
exit(0);
}
operator_stack[++operator_top]=n;
}
int priority(char p)
{
if(p=='^')
return 3;
else if(p=='*'||p=='/'||p=='%')
return 2;
else if(p=='+'||p=='-')
return 1;
else
return 0;
}
char operator_pop()
{
if(operator_top==-1)
{
printf("\nUnderflow condition");
exit(0);
}
return(operator_stack[operator_top--]);
}
void main()
{
int x;
char *exp_infix;
char postfix[50][50];
int digit;
char dig[50];
int y;
printf("\nEnter the infix notation");
scanf("%[^\n]s",exp_infix);
for(x=0,y=0;exp_infix[x]!='\0';)
{
if(isdigit(exp_infix[x]))
{
digit=0;
while((exp_infix[x]>=48)&&(exp_infix[x]<=57))
{
dig[digit]=exp_infix[x];
x++;
digit=digit+1;
}
dig[digit]='\0';
//puts(dig);
strcpy(postfix[y],dig);
// puts(postfix[y]);
//postfix[y]=exp_infix[x];
y++;
}
else if(isoperator(exp_infix[x])||isparenthesis(exp_infix[x]))
{
if(operator_top==-1||isparenthesis(exp_infix[x])==1)
{
operator_push(exp_infix[x]);
x++;
}
else
{
if(isparenthesis(exp_infix[x])==2)
{
while(operator_stack[operator_top]!='(')
{
digit=0;
dig[digit]=operator_pop();
strcpy(postfix[y],dig);
y++;
}
operator_pop();
x++;
continue;
}
if(priority(exp_infix[x])<priority(operator_stack[operator_top]))
{
while((priority(exp_infix[x])<priority(operator_stack[operator_top])&&(operator_top>-1)))
{
digit=0;
dig[digit]=operator_pop();
digit=digit+1;
dig[digit]='\0';
strcpy(postfix[y],dig);
y++;
}
operator_push(exp_infix[x]);
x++;
}
else if(priority(exp_infix[x])==priority(operator_stack[operator_top]))
{
digit=0;
dig[digit]=operator_pop();
digit=digit+1;
dig[digit]='\0';
strcpy(postfix[y],dig);
y++;
operator_push(exp_infix[x]);
x++;
}
else if(priority(exp_infix[x])>priority(operator_stack[operator_top]))
{
operator_push(exp_infix[x]);
x++;
}
}
}
}
while(operator_top!=-1)
{
digit=0;
dig[digit]=operator_pop();
digit++;
dig[digit]='\0';
strcpy(postfix[y],dig);
y++;
}
//postfix[y]='\0';
for(x=0;x<y;x++)
{
printf("%s",postfix[x]);
}
getch();
}
output:
Enter the infix notation 5*2+6
652*+
No comments:
Post a Comment