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