**

Wednesday, 26 February 2014

Program to evaluvate postfix expression

//Program to evaluvate postfix expression:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>

#define max 20

int s[max],top=0;

void main()
{
        char postfix[max],ch;
        int i,op1,op2,res;
        printf("\n Enter the postfix expn:  ");
        scanf("%s",postfix);
        for(i=0;i<strlen(postfix);i++)
        {
        ch=postfix[i];
        if(isdigit(ch))
                 push(ch-'0');
                 else
                 {
                 op2=pop();
                 op1=pop();
                 switch(ch)
                 {
                 case'+':res=op1+op2;
                 break;
                 case'-':res=op1-op2;
                 break;
                 case'*':res=op1*op2;
                 break;
                 case'/':res=op1/op2;
                 break;
                 case'^':res=op1^op2;
                 break;
                 default:printf("\nInvalid character\n\n");
                 exit(0);
                 }
               
                 push(res);
                 }
                 }
                 printf("Result of above expn is %d\n",pop());
                 }
                 push(int element)
                 {
                 ++top;
                 s[top]=element;
                 }
                
                 int pop()
                 {
                 int element;
                 element=s[top];
                 --top;
                 return(element);
                 }

Program to convert infix to postfix expression

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#include<math.h>
#define max 20

char s[max];
int top=0;

int precedence(char element)
                {
                        switch(element)
                        {
                        case '+' :
                        case '-' :return(1);
                        case '*' :
                        case '/' :return(2);
                        case '^' :return(3);
                        case '(' :
                        case '#' :return(0);
                        }
                     }
                    
                   push(char element)
                   {
                   ++top;
                   s[top]=element;
                   }
                  
                   int pop()
                   {
                   char element;
                   element=s[top];
                   --top;
                   return(element);
                   }


int main()
{
char infix[max],ch,postfix[max],element;
int i=0,j=0;
printf("\n Enter the infix expression:  ");
scanf("%s",infix);
push('#');

for(i=0;i<strlen(infix);i++)
        {
        ch=infix[i];
        if(isalnum(ch))   //alphanumeric character
        postfix[j++]=ch;
        else
        if(ch=='(')
                push(ch);
                     else if(ch==')')
                     {
                       while(s[top]!='(')
                        postfix[j++]=pop();
                        element=pop();
                        }
                else
                        {
                        while(precedence(s[top])>=precedence(ch))
                        postfix[j++]=pop();
                        push(ch);
                         }
          }
                while(s[top]!='#')
                postfix[j++]=pop();
                postfix[i]='\0';
                printf("postfix expn is %s \n\n",postfix);
                return 0;
                 }