#include<stdio.h>
#include<conio.h>
void main(){
char inf[30];
char post[30]="\0";
char stack[30];
int top=0;
int i=0;
int pcount=-1;
char ch;
int j=0;
clrscr();
printf("\nEnter the expression : ");
scanf("%s",inf);
ch=inf[0];
stack[top]='#';
while(ch!='\0'){
switch(ch){
case '^' : top++;
stack[top]=ch;
break;
case '*':
case '/':
if(stack[top]=='^'){
pcount++;
post[pcount]=stack[top];
top--;
pcount++;
top++;
stack[top]=ch;
}
else if(stack[top]=='*' && (ch=='/'|| ch=='*')){
pcount++;
post[pcount]=stack[top];
top--;
top++;
stack[top]=ch;
}
else if(stack[top]=='/' && (ch=='/'|| ch=='*')){
pcount++;
post[pcount]=stack[top];
top--;
top++;
stack[top]=ch;
}
else{
top++;
stack[top]=ch;
}
break;
case '+' :
case '-' :
if(stack[top]=='^'){
pcount++;
post[pcount]=stack[top];
top--;
pcount++;
top++;
stack[top]=ch;
}
else if(stack[top]=='*'|| stack[top]=='/'){
pcount++;
post[pcount]=stack[top];
top--;
pcount++;
top++;
stack[top]=ch;
}
else if(stack[top]=='+' && (ch=='+'|| ch=='-')){
pcount++;
post[pcount]=stack[top];
top--;
top++;
stack[top]=ch;
}
else if(stack[top]=='-' && (ch=='+'|| ch=='-')){
pcount++;
post[pcount]=stack[top];
top--;
top++;
stack[top]=ch;
}
else{
top++;
stack[top]=ch;
}
break;
case '(' : top++;
stack[top]=ch;
break;
case ')' : while(stack[top]!='('){
pcount++;
post[pcount]=stack[top];
top--;
}
top--;
break;
default :
pcount++;
post[pcount]=ch;
break;
}
j++;
ch=inf[j];
}
while(stack[top]!='#'){
pcount++;
post[pcount]=stack[top];
top--;
j++;
}
printf("\nPostfix : ");
for(i=0;i<30;i++){
printf("%c",post[i]);
}
getch();
}
#include<conio.h>
void main(){
char inf[30];
char post[30]="\0";
char stack[30];
int top=0;
int i=0;
int pcount=-1;
char ch;
int j=0;
clrscr();
printf("\nEnter the expression : ");
scanf("%s",inf);
ch=inf[0];
stack[top]='#';
while(ch!='\0'){
switch(ch){
case '^' : top++;
stack[top]=ch;
break;
case '*':
case '/':
if(stack[top]=='^'){
pcount++;
post[pcount]=stack[top];
top--;
pcount++;
top++;
stack[top]=ch;
}
else if(stack[top]=='*' && (ch=='/'|| ch=='*')){
pcount++;
post[pcount]=stack[top];
top--;
top++;
stack[top]=ch;
}
else if(stack[top]=='/' && (ch=='/'|| ch=='*')){
pcount++;
post[pcount]=stack[top];
top--;
top++;
stack[top]=ch;
}
else{
top++;
stack[top]=ch;
}
break;
case '+' :
case '-' :
if(stack[top]=='^'){
pcount++;
post[pcount]=stack[top];
top--;
pcount++;
top++;
stack[top]=ch;
}
else if(stack[top]=='*'|| stack[top]=='/'){
pcount++;
post[pcount]=stack[top];
top--;
pcount++;
top++;
stack[top]=ch;
}
else if(stack[top]=='+' && (ch=='+'|| ch=='-')){
pcount++;
post[pcount]=stack[top];
top--;
top++;
stack[top]=ch;
}
else if(stack[top]=='-' && (ch=='+'|| ch=='-')){
pcount++;
post[pcount]=stack[top];
top--;
top++;
stack[top]=ch;
}
else{
top++;
stack[top]=ch;
}
break;
case '(' : top++;
stack[top]=ch;
break;
case ')' : while(stack[top]!='('){
pcount++;
post[pcount]=stack[top];
top--;
}
top--;
break;
default :
pcount++;
post[pcount]=ch;
break;
}
j++;
ch=inf[j];
}
while(stack[top]!='#'){
pcount++;
post[pcount]=stack[top];
top--;
j++;
}
printf("\nPostfix : ");
for(i=0;i<30;i++){
printf("%c",post[i]);
}
getch();
}
No comments:
Post a Comment