School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

C Source Code


Welcome to Dream.In.Code
Become an Expert!

Join 340,156 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,994 people online right now. Registration is fast and FREE... Join Now!




Postfix to infix conversion

This programs gets in a postfix expression and converts it to its infix expression

Submitted By: rameshg87
Actions:
Rating:
Views: 9,735

Language: C

Last Modified: June 8, 2007

Snippet


  1. #include<conio.h>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. int top=10;
  5. struct tree
  6. {
  7.   char ch;
  8.   struct tree *next;
  9.   struct tree *prev;
  10. }*stack[11];
  11. typedef struct tree tree;
  12.  
  13. void push(tree *str)
  14. {
  15.   if(top<=0)
  16.     printf("Stack is Full ");
  17.   else
  18.     {
  19.       stack[top]=str;
  20.       top--;
  21.     }
  22. }
  23.  
  24. tree* pop()
  25. {
  26.   tree *exp;
  27.   if(top>=10)
  28.     printf("Stack is Empty ");
  29.   else
  30.     exp=stack[++top];
  31.   return exp;
  32. }
  33.  
  34.  
  35. void convert(char exp[])
  36. {
  37.   tree *op1,*op2;
  38.   tree *temp;
  39.   int i;
  40.   for(i=0;exp[i]!='\0';i++)
  41.     if(exp[i]>='a'&& exp[i]<='z'||exp[i]>='A'&& exp[i]<='Z')
  42.       {
  43.      temp=(tree*)malloc(sizeof(tree));
  44.      temp->ch=exp[i];
  45.      temp->next=NULL;
  46.      temp->prev=NULL;
  47.      push(temp);
  48.       }
  49.     else if(exp[i]=='+'||exp[i]=='-'||exp[i]=='*'||exp[i]=='/'||exp[i]=='^')
  50.       {
  51.      op1=pop();
  52.      op2=pop();
  53.      temp=(tree*)malloc(sizeof(tree));
  54.      temp->ch=exp[i];
  55.      temp->next=op1;
  56.      temp->prev=op2;
  57.      push(temp);
  58.       }
  59. }
  60.  
  61. void display(tree *temp)
  62. {
  63.   if(temp!=NULL)
  64.     {
  65.       display(temp->prev);
  66.       printf("%c",temp->ch);
  67.       display(temp->next);
  68.     }
  69. }
  70.  
  71. main()
  72. {
  73.   char exp[50];
  74.   printf("Enter the expression :");
  75.   scanf("%s",exp);
  76.   convert(exp);
  77.   printf("\n\n");
  78.   display(pop());
  79.   printf("\n\n");
  80.   getch();
  81. }

Copy & Paste


Comments

bsit-ia barkada 2008-09-28 03:14:17

heller your code is not running.. but then thanks anyway...

bsit-ia barkada 2008-09-28 03:14:20

heller your code is not running.. but then thanks anyway...

Angel_Erikku 2009-03-05 08:20:27

....your code is not running...there are errors... hehe


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month