Thread: fix logical error in infix to postfix notation program

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    10

    Unhappy fix logical error in infix to postfix notation program

    Code:
    #include<stdio.h>
    #include<ctype.h>
    #include<stdlib.h>
    #define max 100
    int stackpoint=0;
    int stackbuff[max];
    void push(int a);
    int pop();
    char getop(char infix[max]);
    int precedance(char l);
    void intopostfix(char infix[max],float postfix[max]);
    void main()
    {
        int i=0;
        float postfix[max];
        char infix[max];
        intopostfix(infix,postfix);
        printf("\n POSTFIX NOTATION IS\n");
        while(postfix[i]!='#')
        {if(postfix[i]!='(');
        printf("%f",postfix[i++]);i++;}
    }
    void intopostfix(char infix[max],float postfix[max])
    {
        char l;
         int k=0,j;
         float operand;
         push('#');
        while((l=getop(infix))!=EOF)
        {
          if(l!='#'&&l!='e')
          {
          switch(l)
          {
          case 'o':
           operand=atof(infix);
           postfix[k++]=operand;
           break;
          case '(': case '^': case '*': case'/': case'%': case '+': case '-':
           if((j=precedance(l)>=0))
                 push(l);
            while(j=precedance(l)<0)
            postfix[k++]=pop();
            break;
           case ')':
            break;
            default :
            printf("INVALID STATEMENT%c\n",l);
            break;
          }
          }
          else if(l=='#')
          {
              postfix[k]='#';
              return;
          }
          else if(l=='e');
        }
    }
    char getop(char infix[max])
    {
        int i=0;
        char c;
        c=getchar();
        if(c=='('||c=='^'||c=='*'||c=='/'||c=='+'||c=='+'||c=='#')
           return c;
           if(isdigit(c)||c=='.')
           {
            while(isdigit(infix[i++]=c))
            {c=getchar();}
        if(c=='.')
        {infix[i++]=c;
            while(isdigit(infix[i++]=c=getchar()));
        }
    
        infix[i]='\0';
        return 'o';
           }
           else
           return 'e';
    
    }
    void push(int oprater)
    {
        if(stackpoint<max)
        {
            stackbuff[stackpoint++]=oprater;
        }
        else
        {
           printf("STACK LIMIT EXCEED\n");
        }
    }
    int pop()
    {
        int temp;
        if(stackpoint>0)
           return stackbuff[--stackpoint];
    
        else
        {
           printf("STACK EMPTY\n");
           return 0.0;
        }
    }
    int precedance(char l)
    {
       int precedbuff[3][8]={{'(','^','*','/','%','+','-','#'},
                             {5,4,3,3,3,2,2,0},
                             {1,4,3,3,3,2,2,0}},j,val1,val2;
    
       for(j=0;j<8;j++)
       {
           if(l==precedbuff[0][j])
           val1=precedbuff[1][j];
           if(stackbuff[stackpoint]==precedbuff[0][j])
           val2=precedbuff[2][j];
       }
       return val1-val2;
    }
    cant fix the logical error pls help

  2. #2
    Registered User
    Join Date
    Jul 2011
    Posts
    10
    do anyone knows what to fix for output to work

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    you need to be a little more specific about what logical error you are talking about. and question #1 : does your program compile without errors or warnings? if not, then fix that first.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    10
    there no syntax error in program and program is compiling but when intopostfix function is called in it getop is called which should return desired value i.e if
    value entered is digit it returns 'o' and if any operator should return the operater else return 'e'implies error.But dont know for wat reason getop is only returning 'e' inspite of entering values of operand and operator using getchar function.and dont know for what reason while loop in intopostfix is not terminating on EOF NOTATION.
    LASTLY precedance function to note the precedance of a operater is not called .
    hence on compiling program takes infix notation but dont gives output as specified in main as intopostfix function do not terminate due to infinite while loop maybe

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    while(j=precedance(l)<0)Turn the warning level up on your compiler, your code is NOT error and warning free. Even if it has no syntax errors and compiles, it may not be doing what you think:
    Code:
    $ make calc
    gcc -Wall -g -std=c99  -lssl -lm -lpthread -lcurses -lefence  calc.c   -o calc
    calc.c:12: warning: return type of ‘main’ is not ‘int’
    calc.c: In function ‘intopostfix’:
    calc.c:42: warning: suggest parentheses around assignment used as truth value
    calc.c: In function ‘pop’:
    calc.c:96: warning: unused variable ‘temp’
    [list]
    First, it's int main(void) and return an integer at the end, usually 0. Read this link. I sure hope you aren't using void main because you're compiling with Turbo C. If you are, get a more modern compiler, like Pelles C, Code::Blocks with MinGW or Visual C++ Express.

    Then, take a look at this line:
    Code:
    while(j=precedance(l)<0)
    That is equivalent to
    Code:
    while(j=(precedance(l)<0))
    Notice that the parentheses are around the comparison -- you are assigning the boolean result (0 or 1) of the less-than comparison to j. You are not assigning the result of precedance(l) to j, then comparing that to 0, which would look like:
    Code:
    while((j=precedance(l))<0)
    I'm not sure which way you intend it to work, but you should be explicit in such cases. You use complex conditionals like that in several places, make sure they're all doing what you want.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by SUVIR
    and dont know for what reason while loop in intopostfix is not terminating on EOF NOTATION.
    Code:
    ...
    
    void intopostfix(char infix[max],float postfix[max])
    {
        char l;
        int k=0,j;
        float operand;
        push('#');
        while((l=getop(infix))!=EOF)
        {
    ...
    
    char getop(char infix[max])
    {
        int i=0;
        char c;
        c=getchar();
        if(c=='('||c=='^'||c=='*'||c=='/'||c=='+'||c=='+'||c=='#')
           return c;
    The getchar function returns an int, not a char. Your getop function returns this char value and tries to compare against EOF which is problematic. At the least, getop needs to return an int, not a char; variable c in getop needs to be an int; variable l in function intopostfix needs to be an int.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 05-12-2010, 01:30 AM
  2. Expression: Convert infix notation to postfix notation.
    By Nutshell in forum C Programming
    Replies: 7
    Last Post: 02-27-2010, 07:44 AM
  3. Replies: 4
    Last Post: 03-12-2006, 02:17 PM
  4. Infix to postfix Q
    By Liberty4all in forum C++ Programming
    Replies: 8
    Last Post: 11-03-2002, 08:34 AM
  5. Infix to postfix
    By Liberty4all in forum C Programming
    Replies: 2
    Last Post: 11-01-2002, 08:50 AM