#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Postfix to Prefix Conversion


    I have written a program for converting an expression which is given in postfix form to a prefix form. The thing is that the program compiles successfully but when i enter the postfix expression it does not return back the expression in prefix form. The code for the prgrm is as follows

    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>

    #define MAX 50

    struct postfix{

    char stack[MAX], target[MAX];
    char temp1[2], temp2[2];
    char str1[MAX], str2[MAX], str3[MAX];
    int i, top;
    };

    void initpostfix(struct postfix *);
    void setexpr(struct postfix *, char *);
    void push(struct postfix *, char *);
    void pop(struct postfix *, char *);
    void convert(struct postfix *);
    void show(struct postfix);

    void main(){

    struct postfix q;
    char expr[MAX];

    initpostfix(&q);

    printf("Enter the expression in postfix form.");
    gets(expr);

    setexpr(&q, expr);
    convert(&q);

    printf("The prefix expression is:");
    show(q);
    getch();
    }

    void initpostfix (struct postfix *p){

    p->i=0;
    p->top=-1;
    strcpy(p->target,"");
    }

    void setexpr(struct postfix *p, char *c){

    strcpy(p->target,c);
    }

    void push(struct postfix *p, char c){

    if(p->top==MAX-1)
    printf("/n Stack is full");
    else{
    p->top++;
    p->stack[p->top]=c;
    }
    }

    void pop(struct postfix *p,char c){

    if(p->top==-1)
    printf("\nStack is empty");
    else {
    c=p->stack[p->top];
    p->top--;
    }
    }

    void convert(struct postfix *p){

    while(p->target[p->i]!='\0'){
    if(p->target[p->i]==' ')
    p->i++;

    if(p->target[p->i]=='%' || p->target[p->i]=='+' ||p->target[p->i]=='-' ||p->target[p->i]=='*' ||p->target[p->i]=='/' ||p->target[p->i]=='$'){
    pop(p,*(p->str2));
    pop(p,*(p->str3));
    p->temp1[0]=p->target[p->i];
    p->temp1[1]='\0';
    strcpy(p->str1,p->temp1);
    strcat(p->str1,p->str3);
    strcat(p->str1,p->str2);
    push(p,*(p->str1));
    }

    else{
    p->temp1[0]=p->target[p->i];
    p->temp1[1]='\0';
    strcpy(p->temp2,p->temp1);
    push(p,*(p->temp2));
    }
    p->i++;
    }
    }

    void show(struct postfix p){

    char temp=p.stack[0];
    while(temp){
    printf("%c", temp);
    temp++;
    }
    }

    Comments on this post

    • sizablegrin disagrees : You have been told before to read the "New Users - HOW TO POST A QUESTION - READ THIS FIRST" thread. Put your code in code tags.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    1. Read the READ THIS FIRST sticky thread on how to use code tags to preserve your code's formatting. Just exactly what part of "READ THIS FIRST" did you fail to understand?

    2. Lose the tab characters and use space characters instead to indent your code. Your editor should have an option setting for that.

    3. So you didn't get the output you expected. So why keep it a secret from us what you did get? You just want to waste everybody's time with guessing games?

    Here's an idea: show us sample input, the output you expected that input to produce, and the actual output you got. That way, we'd have something to work with and might be able to find an answer for you a lot quicker. But if you're not really interested in getting a meaningful answer, then go right ahead and play your guessing games.


    PS
    My last question would be: why would anyone want to convert from postfix to prefix? Usually it's from infix to postfix so that you can evaluate it with a stack. Oh well, your homework is what's assigned to you.
    Last edited by dwise1_aol; January 4th, 2009 at 02:28 AM.

IMN logo majestic logo threadwatch logo seochat tools logo