Sunday, October 6, 2024

SYBSc (CS) Sem III : Data Structure Slip 3 Que - 2

 SAVITIBAI PHULE UNIVERSITY OF PUNE

S. Y. B.Sc. (Computer Science) Semester III

Practical Examination

      SUBJECT: CS-233 Practical course based on CS231 

/* Q.2 Write a C program to evaluate postfix expression*/

#include<stdio.h>

#include<stdlib.h>

#define MAX 30


struct Stack

{

       char data[MAX];

       int top;

};

int init(struct Stack *s)

{

    s->top = -1;

}

int isEmpty(struct Stack *s)

{

    if(s->top == -1)

    {

       return 1;

    }

    else{

       return 0;

    }

}

int isFull(struct Stack *s)

{

    if(s->top == MAX-1)

    {

       return 1;

    }

    else{

       return 0;

    }

}

int Push(struct Stack *s, char x)

{

    if(isFull(s) == 1)

    {

       printf("The stack is overflow...");

       return 1;

    }

    else{

         s->top++;

         s->data[s->top] = x;

    }    

    return 0;

}

char Pop(struct Stack *s)

{

    if(isEmpty(s) == 1)

    {

       printf("The stack is underflow...");

       return 1;

    }

    else{

         return s->data[(s->top)--];

    }

}

char Peek(struct Stack *s)

{

    if(isEmpty(s) == 1)

    {

       printf("The stack is underflow...");

       return 1;

    }

    else{

         return s->data[s->top];

    }

}

int Priority(char x)

{

    if(x == '(')

         return 0;

    else if(x == '+' || x == '-')

         return 1;

    else if(x == '*' || x == '/')

         return 2;

    else if(x == '^' || x == '$')

         return 3;

    else

        return -1;

}

int main()

{

    struct Stack s;

    char exp[MAX], temp[MAX];

    int i = 0, n1, n2, n3, n;    

    init(&s);    

    printf("Enter the postfix expression without space :: ");

    scanf("%s", exp);    

    strcpy(temp, exp);       

    while(temp[i] != '\0')

    {

       if(isdigit(temp[i]))

       {

           Push(&s, temp[i]-48);

       }

       else

       {

          n2 = Pop(&s);        // Operand 1 

          n1 = Pop(&s);        // OPerand 2

          

          switch(temp[i])

          {

              case '+':

              {

                   n3 = n1 + n2;

                   break;

              }              

              case '-':

              {

                   n3 = n1 - n2;

                   break;

              }

              case '*':

              {

                   n3 = n1 * n2;

                   break;

              }

              case '/':

              {

                   n3 = n1 / n2;

                   break;

              }

          }

          

          Push(&s, n3);

       }

       i++;

    }    

    printf("\n The result of Postfix expression %s = %d \n\n", exp, Pop(&s));

    system("pause");

    return 0;

}

No comments:

Post a Comment

To Connect Java program with the MYSQL Database

To connect java application with the MYSQL database, first of all to install MYSQL database in your machine. To install MYSQL in your mach...