Friday 6 February 2015

Program to convet postfix int prefix



#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#define MAX 20

char stack[MAX][MAX];
int top;
void posttopre(char post[30],char prefix[30])
{
      int top=-1,i;
      char x,st1[30];
      for(i=0;post[i]!='\0';i++)
      {
            x=post[i];
            if(isalnum(x))
            {
            st1[0]=x;
            st1[1]=NULL;
            top=top+1;
            strcpy(stack[top],st1);
            }
            else
            {
                  st1[0]=x;
                  st1[1]=NULL;
                  strcat(st1,stack(top-1));
                  strcat(st1,stack(top));
                  top=top-1;
                  strcpy(stack[top],st1);
                  }
            }
      }
       void main()
      {
            char post[30],prefix[30];
            char x;
            printf("\n enter the postfix expression=");
            gets(post);
            posttopre(post,prefix);
            strcpy(prefix,stack[top]);
            printf("\n\n the prefix expression is=5s",prefix);
            getch();

            }

No comments:

Post a Comment