Infix to Postfix Help?

This is a discussion on Infix to Postfix Help? within the C Programming forums, part of the General Programming Boards category; Hey guys, i have a project to convert an Infix Expression to Postfix Expression or the Reverse Polish Notation. I'm ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    32

    Infix to Postfix Help?

    Hey guys, i have a project to convert an Infix Expression to Postfix Expression or the Reverse Polish Notation.

    I'm almost done but my code only reads single digits..

    For example, if i input 12+3, it converts to 123+ and gives 5 as a result.

    How do i read the number '12' as one so it can be 12 3 + = 15?

    Here's my code..if you find something wrong with it please tell me too so i can fix it..

    Code:
    QUEUE head=NULL;
    
    void push(STACK *top, char c)
    {
        STACK temp;
        temp=(stacks*)malloc(sizeof(stacks));
        temp->x= c;
        temp->next = *top;
        *top=temp;
    }
    
    void pop(STACK *top)
    {
        STACK temp;
    
        if(!isEmpty(*top))
        {
            temp=*top;
        
            if(temp->x!='(')
            {
                enqueue(&head,(*top)->x);
            }
    
            *top=(*top)->next;
            free(temp);
        }
    
    }
    
    void intPush(STACK2 *top, float a)
    {
        STACK2 temp;    
        temp=(stacks2*)malloc(sizeof(stacks2));
        
        if(temp==NULL)
        {
            printf("no memory!");
        }
    
        temp->x= a;
        temp->next = *top;
        *top=temp;
    }
    
    
    float intPop(STACK2 *top)
    {
        STACK2 temp;
        float valuePopped;
    
        if(*top!=NULL)
        {
            temp=*top;
            valuePopped=(*top)->x;
            *top=(*top)->next;
            free(temp);
        }
    
        return valuePopped;
    
    }
    
    void enqueue(QUEUE *head, char x)
    {    
        QUEUE p;
        QUEUE temp;
        
        temp = (queues*) malloc(sizeof(queues));
    
        if(*head==NULL)
        {
            temp->x = x;
            temp->next = NULL;
            *head=temp;
        }
        
        else
        {
            p=*head;
            while(p->next!=NULL)
            {
                p=p->next;
            }
    
            temp->x = x;
            temp->next = NULL;
            p->next = temp;    
        }
    }
    
    void toRPN(STACK *top, char *x, STACK2 *top2)
    {
        HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    
        SetConsoleTextAttribute(hOut,FGI);
    
        printf("\n\nIn Postfix Notation\n ");
    
        while(*x!='\0')
        {
            if(*x=='(')
            {
                push(&(*top),*x);
            }
    
            else if(*x==')')
            {
                while(!isEmpty(*top) && (*top)->x!='(')
                {
                    pop(&(*top)); 
                    
                }
                
                if((*top)->x=='(')
                {
                    pop(&(*top));
                }
            }
    
            else if(isdigit(*x))
            {
                enqueue(&head,*x);
            }   
    
            else
            {
                if(*x=='+' || *x=='-')
                {
                    while(!isEmpty(*top) && priority((*top)->x)>0)
                    {
                        pop(&(*top));
                    }
        
                    push(&(*top),*x);
                }
    
                else if(*x=='*' || *x=='/' || *x=='%')
                {
                    while(!isEmpty(*top) && priority((*top)->x)>1)
                    {
                        pop(&(*top));
                    }
    
                    push(&(*top),*x); 
                }
            }
    
            x++;
        }
    
        while(!isEmpty(*top))
        {
            pop(&(*top));
        }
    
        solve(&head, &(*top2));
    }
    
    float solve(QUEUE *head, STACK2 *top)
    {
        QUEUE p = *head;
    
        float x;
        float y;
        float result;
    
        HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    
        while(p!=NULL)
        {
            SetConsoleTextAttribute(handle,FCI);
            printf("%c",p->x);
            
            if(isalnum(p->x))
            {
                x = (float)atof(&(p->x));
                intPush(&(*top),x);
            }
    
            else if(p->x!='(')
            {
                y=intPop(&(*top));
                x=intPop(&(*top));
                result = calculate(x,p->x,y);
    
                intPush(&(*top),result);
            }
    
            p=p->next;
        }
    
        return (*top)->x;
    }
    
    int priority(char c)
    {
        if(c=='*' || c=='/' || c=='%')
            return 2;
        else if(c=='+' || c=='-')
            return 1;
        else
            return 0;
    }
    
    bool isEmpty(STACK top)
    {
        if(top==NULL)
            return true;
        else
            return false;
    }
    
    float calculate(float x, char op, float y)
    {
        if(op=='+')
            return x + y;
        else if(op=='-')
            return x - y;
        else if(op=='*')
            return x * y;
        else if(op=='/')
            return x / y;
        else if(op=='%')
            return float((int)x % int(y));
        else
            return -1;
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Your using isdigit, then push that one digit on the stack. You can use strtol or strtof (if you want to accept floats) once you pass the is digit test. With these function you can parse the string x and update it with the endptr argument that these function have.

    But you might still get into problem if someone enters say: -12

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    32
    Quote Originally Posted by Subsonics View Post
    Your using isdigit, then push that one digit on the stack. You can use strtol or strtof (if you want to accept floats) once you pass the is digit test. With these function you can parse the string x and update it with the endptr argument that these function have.

    But you might still get into problem if someone enters say: -12
    Sir, can i ask how to use strtof and how to apply it in my code?

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Well suppose you have reached the point where you check the string for digits, then doing something like this:

    Code:
        if(isdigit(*x)) {
            char *endp = NULL;
            float f = strtof(x, &endp);
            //push f to stack
            x = endp;
        }
    This might not just drop in to your current code, but you get the idea. One thing I noticed was that you increment x at the end, with this step you would need to skip that step since: x = endp; will update x to the next token.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    32
    If i had
    Code:
    char a[6] = { '1','2','+','3','+','4'};
    , how do i turn '1' and '2' into just "12" then push it to the stack? I tried using strcat() and made changes to my code, it didnt have any errors at compile time but it crashed at runtime -_-

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    If you make that a[7] and add a terminating zero, then you can do what I just showed you!

    Code:
    char *x = a;

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    32
    rp.h
    colors.h
    rpnmain.cpp
    rpnsource.cpp

    This is my whole program so far..i undid the changes i made..so its functioning again now but only reads single characters..

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Why are you showing this? It's not related to your problem. Try this if you still don't get what it is I'm trying to tell you.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char **argv)
    {
        if(argc < 2) return 0;
    
        char *str = argv[1];
        char *endp = NULL;
        float f = strtof(str, &endp);
        str = endp;
    
        printf("%f, %s\n", f, str);
    
    
        return 0;
    }
    It demonstrates the idea in a standalone program. Just add an argument that starts with a number and have characters follow immediately, example: ./a.out 123hello

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 05-12-2010, 01:30 AM
  2. Infix - Postfix
    By Suchy in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2008, 11:41 PM
  3. infix to postfix
    By Haytham in forum C Programming
    Replies: 0
    Last Post: 04-29-2007, 08:53 AM
  4. Replies: 4
    Last Post: 03-12-2006, 01:17 PM
  5. From postfix to infix
    By yescha in forum C Programming
    Replies: 1
    Last Post: 11-18-2001, 11:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21