/* c program to conver prefix to postfix notation */
#include<stdio.h>
#include<string.h>
void push(char item[],int *top,char s[][20])
{
*top=*top+1;
strcpy(s[*top],item);
}
char *pop(int *top,char s[][20])
{
char *item;
item=s[*top];
*top=*top-1;
return item;
}
void stringrev(char str[])
{
int len,i=0;
char t[100];
len=strlen(str);
len--;
while(len>=0)
{
t[len--]=str[i++];
}
strcpy(str,t);
}
void prefix_postfix(char prefix[],char postfix[])
{
char s[20][20];
int top;
char symbol;
char temp[2];
char *op1;
char *op2;
int i;
top=-1;
stringrev(prefix);
for(i=0;i<strlen(prefix);i++)
{
symbol=prefix[i];
temp[0]=symbol;
temp[1]='\0';
switch(symbol)
{
case '+':
case '-':
case '*':
case '/':
case '^':
op1=pop(&top,s);
op2=pop(&top,s);
strcpy(postfix,op1);
strcat(postfix,op2);
strcat(postfix,temp);
push(postfix,&top,s);
break;
default:push(temp,&top,s);
}
}
}
int main()
{
char prefix[20];
char postfix[20];
printf("enter prefix expression\n");
scanf("%s",prefix);
prefix_postfix(prefix,postfix);
printf("the postfix expression is:%s\n",postfix);
return 0;
}
#include<stdio.h>
#include<string.h>
void push(char item[],int *top,char s[][20])
{
*top=*top+1;
strcpy(s[*top],item);
}
char *pop(int *top,char s[][20])
{
char *item;
item=s[*top];
*top=*top-1;
return item;
}
void stringrev(char str[])
{
int len,i=0;
char t[100];
len=strlen(str);
len--;
while(len>=0)
{
t[len--]=str[i++];
}
strcpy(str,t);
}
void prefix_postfix(char prefix[],char postfix[])
{
char s[20][20];
int top;
char symbol;
char temp[2];
char *op1;
char *op2;
int i;
top=-1;
stringrev(prefix);
for(i=0;i<strlen(prefix);i++)
{
symbol=prefix[i];
temp[0]=symbol;
temp[1]='\0';
switch(symbol)
{
case '+':
case '-':
case '*':
case '/':
case '^':
op1=pop(&top,s);
op2=pop(&top,s);
strcpy(postfix,op1);
strcat(postfix,op2);
strcat(postfix,temp);
push(postfix,&top,s);
break;
default:push(temp,&top,s);
}
}
}
int main()
{
char prefix[20];
char postfix[20];
printf("enter prefix expression\n");
scanf("%s",prefix);
prefix_postfix(prefix,postfix);
printf("the postfix expression is:%s\n",postfix);
return 0;
}
THANK YOU SO MUCH FOR THIS PROGRAM. I SEARCHED GOOGLE BUT THE ONLY GOOD PROGRAM I COULD SEE WAS YOURS...KEEP UP THE GOOD WORK
ReplyDelete