fork download
  1.  
  2. //a program for infix to postfix
  3. #include<stdio.h>
  4. #include<string.h>
  5. char in[30],stack[20],out[30];
  6. int top=-1;
  7. int pop(char a);
  8. int output_push(char b);
  9. int push(char c);
  10. int precedence(char d);
  11. int main(){
  12. int i,k,l,j=0;
  13. printf("enter expression:");
  14. fgets(in,sizeof(in),stdin);
  15. for(i=0;i<=strlen(in);i++){
  16. if(in[i]==(int)in[i]){
  17. output_push(in[i]);}
  18. if(in[i]!=(int)in[i]){
  19. push(in[i]);
  20. }
  21. }
  22. printf("the output is:%s",out);
  23.  
  24. }
  25. int push(char c){
  26. if(top==-1){
  27. stack[++top]=c;}
  28. else{
  29. if(precedence(c)>precedence(stack[top])){
  30. stack[++top]=c;
  31. }
  32. while(precedence(c)<=precedence(stack[top])){
  33. int j;
  34. out[++j]=stack[top--];
  35. }
  36. }
  37. }
  38. int output_push(char b){
  39. int j;
  40. out[++j]=b;
  41.  
  42. }
  43. int precedence(char d){
  44. if(d=='+'||d=='-'){
  45. return 1;
  46. }
  47. else if(d=='x'||d=='/'){
  48. return 2;
  49. }
  50. }
  51.  
Success #stdin #stdout 0.01s 5284KB
stdin
2+3
stdout
enter expression:the output is: