Infix to postfix problem..

This is a discussion on Infix to postfix problem.. within the C Programming forums, part of the General Programming Boards category; Hi all, this is my fist post here and I am a beginner at C programming. I have the following ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Location
    Delhi, India
    Posts
    59

    Infix to postfix problem..

    Hi all, this is my fist post here and I am a beginner at C programming.
    I have the following code for converting a list of t infix expressions into their postfix form:-

    Code:
    1. #include<stdio.h>
    2. #include<string.h>
    3. #include<ctype.h>
    4. #define MAX 400
    5. struct stack
    6. {
    7. char arr[MAX];
    8. int top;
    9. };
    10. void initstack(struct stack *);
    11. void push(struct stack *,char);
    12. char pop(struct stack *);
    13. void convert(struct stack *s,char *);
    14. int priority(char e);
    15. int main()
    16. {
    17. int t,k=0;
    18. char str[MAX];
    19. struct stack s1;
    20. scanf("%d",&t); //No of infix expressions to be converted in postfix//
    21. initstack(&s1);
    22. while(t)
    23. {
    24. t--;
    25. scanf("%s",str);
    26. convert(&s1,str);
    27. }
    28. return 0;
    29. }
    30. void initstack(struct stack *s)
    31. {
    32. s->top=-1;
    33. }
    34. void push(struct stack *s,char data)
    35. {
    36. if(s->top==MAX-1)
    37. return;
    38. else
    39. {
    40. s->top++;
    41. s->arr[s->top]=data;
    42. }
    43. }
    44. char pop(struct stack *s)
    45. {
    46. char a;
    47. if(s->top==-1)
    48. return -1;
    49. else
    50. {
    51. a=s->arr[s->top];
    52. s->top--;
    53. return a;
    54. }
    55. }
    56. void convert(struct stack *s,char *exp)
    57. {
    58. char op[400],d,p1;
    59. int i=0,j=0,pr;
    60. while(*(exp))
    61. {
    62. if(*(exp)=='(')
    63. {
    64. push(s,*(exp));
    65. exp++;
    66. }
    67. else if(*(exp)=='+'||*(exp)=='-'||*(exp)=='^'||*(exp)=='/'||*(exp)=='*')
    68. {
    69. if(s->top==-1)
    70. {
    71. push(s,*(exp));
    72. exp++;
    73. }
    74. else
    75. {
    76. p1=pop(s);
    77. if(priority(p1)<priority(*(exp)))
    78. {
    79. push(s,p1);
    80. push(s,*(exp));
    81. exp++;
    82. }
    83. else
    84. {
    85. while(priority(p1)>=priority(*(exp)))
    86. {
    87. op[i]=p1;
    88. i++;
    89. p1=pop(s);
    90. }
    91. push(s,p1);
    92. push(s,*(exp));
    93. exp++;
    94. }
    95. }
    96. }
    97. else if(*(exp)==')')
    98. {
    99. p1=pop(s);
    100. while((p1)!='(');
    101. {
    102. op[i]=p1;
    103. i++;
    104. p1=pop(s);
    105. }
    106. exp++;
    107. }
    108. else if(isalpha(*(exp)))
    109. {
    110. while(isalpha(*(exp)))
    111. {
    112. op[i]=*(exp);
    113. exp++;
    114. i++;
    115. }
    116. }
    117. }
    118. while(s->top!=-1)
    119. {
    120. d=pop(s);
    121. op[i]=d;
    122. i++;
    123. }
    124. op[i]='\0';
    125. while(op[j])
    126. printf("%c",op[j++]);
    127. printf("\n");
    128. }
    129. int priority(char e)
    130. {
    131. if(e=='^')
    132. return 3;
    133. else if(e=='*'||e=='/')
    134. return 2;
    135. else if(e=='+'||e=='-')
    136. return 1;
    137. else
    138. return 0;
    139. }
    I am getting correct outputs for non-bracketed inputs.But for bracketed
    inputs I don't get any output. I have checked the code many times but
    couldn't find as to why there is no output for bracketed inputs.Please
    help

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Well deleting your code, and re-posting it without line numbers would be useful.

    As would posting ALL your code. It just stops at line 70.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Location
    Delhi, India
    Posts
    59
    I am reposting the code again. Here it is without line numbers:-

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<ctype.h>
    
    #define MAX 400
    struct stack
    {
        char arr[MAX];
        int top;
    };
    
    void initstack(struct stack *);
    void push(struct stack *,char);
    char pop(struct stack *);
    void convert(struct stack *s,char *);
    int priority(char e);
    
    int main()
    {
       int t;
       char str[MAX];
       struct stack s1;
       scanf("%d",&t);          //No of infix expressions to be converted in postfix//
       initstack(&s1);
       while(t)
       {
           t--;
           scanf("%s",str);
           convert(&s1,str);
       }
       return 0;
    
    }
    
    void initstack(struct stack *s)
    {
        s->top=-1;
    }
    
    void push(struct stack *s,char data)
    {
        if(s->top==MAX-1)
         return;
        else
        {
            s->top++;
            s->arr[s->top]=data;
        }
    }
    
    char pop(struct stack *s)
    {
        char a;
        if(s->top==-1)
         return -1;
        else
        {
            a=s->arr[s->top];
            s->top--;
            return a;
        }
    }
    
    void convert(struct stack *s,char *exp)
    {
        char op[400],d,p1;
        int i=0,j=0,pr;
        while(*(exp))
        {
            if(*(exp)=='(')
             {
              push(s,*(exp));
              exp++;
             }
            else if(*(exp)=='+'||*(exp)=='-'||*(exp)=='^'||*(exp)=='/'||*(exp)=='*')
             {
               if(s->top==-1)
                {
                    push(s,*(exp));
                    exp++;
                }
                else
                {
                    p1=pop(s);
                    if(priority(p1)<priority(*(exp)))
                    {
                        push(s,p1);
                        push(s,*(exp));
                        exp++;
                    }
                    else
                    {
                        while(priority(p1)>=priority(*(exp)))
                        {
                          op[i]=p1;
                          i++;
                          p1=pop(s);
                        }
                        push(s,p1);
                        push(s,*(exp));
                        exp++;
                    }
                }
              }
              else if(*(exp)==')')
              {
                  p1=pop(s);
                  while((p1)!='(');
                  {
                      op[i]=p1;
                      i++;
                      p1=pop(s);
                  }
                  exp++;
               }
               else if(isalpha(*(exp)))
               {
                   while(isalpha(*(exp)))
                   {
                   op[i]=*(exp);
                   exp++;
                   i++;
                   }
                }
            }
            while(s->top!=-1)
            {
                d=pop(s);
                op[i]=d;
                i++;
            }
            op[i]='\0';
            while(op[j])
            printf("%c",op[j++]);
            printf("\n");
    }
    
    
    int priority(char e)
    {
        if(e=='^')
         return 3;
        else if(e=='*'||e=='/')
         return 2;
        else if(e=='+'||e=='-')
         return 1;
        else
         return 0;
    }

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    You didn't wait for a whole day...
    Infix to postfix problem.. - C

    Rather than just looking at the code, try using a debugger (and some simple expressions).

    Like
    (3)

    should be the same as
    3

    Perhaps focus your attention on what happens when you get to )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Location
    Delhi, India
    Posts
    59
    Well, I tried all I could do and that is why I didn't wait for a day.. There is segmentation fault in the code but I can't figure where does the code makes an invalid memory reference. I have been trying to run it since morning but no success. That is why I am here.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by gaurav_13191 View Post
    There is segmentation fault in the code but I can't figure where does the code makes an invalid memory reference.
    If you can't find it by reading, why not have the debugger tell you when the segfault happens?

  7. #7
    Registered User
    Join Date
    Dec 2010
    Location
    Delhi, India
    Posts
    59
    I got the problem with ')' but still the program runs for some inputs and for some inputs it just outputs nothing. Still some other rectification is required so that it runs for all inputs.

  8. #8
    Registered User
    Join Date
    Dec 2010
    Location
    Delhi, India
    Posts
    59
    Well, thanks all with your suggestions I was able to do it correctly. It was my first post but I really liked that I got lot of suggestions within no time and didn't have to wait for the whole day.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-12-2006, 01:17 PM
  2. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  3. infix to postfix
    By condorx in forum C++ Programming
    Replies: 2
    Last Post: 12-18-2002, 11:29 AM
  4. Infix to Postfix
    By mmcgorty in forum C++ Programming
    Replies: 1
    Last Post: 12-08-2002, 10:29 PM
  5. postfix
    By datainjector in forum C Programming
    Replies: 11
    Last Post: 11-20-2002, 07:33 PM

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