Showing posts with label STACK APPLICATIONS. Show all posts
Showing posts with label STACK APPLICATIONS. Show all posts

Monday, 12 May 2014

Postfix Evaluation

PROGRAM:

#include<stdio.h>
#include<string.h>
#include<conio.h>
void push(int);
int pop();
int stack[20],top=-1;
void main()
{
 char expr[20];
 int len,a,b,c,i,x;
 clrscr();
 printf("$$$ POSTFIX EVALUATION $$$");
 printf("\nEnter the postfix expression:");
 scanf("%s",expr);
 len=strlen(expr);
 for(i=0;i<len;i++)
 {
  if(expr[i]=='+'||expr[i]=='-'||expr[i]=='*'||expr[i]=='/')
  {
   b=pop();
   a=pop();
   switch(expr[i])
   {
    case '+':c=a+b;
      push(c);
      break;
    case '-':c=a-b;
      push(c);
      break;
    case '*':c=a*b;
      push(c);
      break;
    case '/':c=a/b;
      push(c);
      break;
   }
  }
  else
  {
   printf("Enter the value of %c:",expr[i]);
   scanf("%d",&x);
   push(x);
  }
 }
 printf("The resultant value is : %d",stack[top]);
 getch();
}
void push(int item)
{
 stack[++top]=item;
}
int pop()
{
 int a;
 a=stack[top];
 top=top-1;
 return a;
}


OUTPUT:

$$$ POSTFIX EVALUATION $$$

Enter the postfix expression: abc+*
Enter the value of a: 10
Enter the value of b: 20
Enter the value of c: 30
The resultant value is : 500

Saturday, 12 April 2014

Balancing Symbol

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
                int top=-1,d=0;
                char ch,stack[30];
                clrscr();
                printf("\n\n$$$ BALANCING SYMBOLS $$$\n\n");
                printf("Check whether the given expression is balanced or not\n");
                printf("Enter the expression:\t");
                while(ch=getchar(),ch!='#')
                {
                                switch(ch)
                                {
                                                case '(':stack[++top]=ch;break;
                                                case '{':stack[++top]=ch;break;
                                                case '[':stack[++top]=ch;break;
                                                case ')':if(stack[top]=='(')
                                                                 {
                                                                                top=top-1;
                                                                 }
                                                                 else
                                                                 {
                                                                                d=d+1;
                                                                 }
                                                                 break;
                                                case '}':if(stack[top]=='{')
                                                                 {
                                                                                top=top-1;
                                                                 }
                                                                 else
                                                                 {
                                                                                d=d+1;
                                                                 }
                                                                 break;
                                                case ']':if(stack[top]=='[')
                                                                 {
                                                                                top=top-1;
                                                                 }
                                                                 else
                                                                 {
                                                                                d=d+1;
                                                                 }
                                                                 break;
                                }
                }
                if(top==-1 && d==0)
                {
                                printf("Balanced Expression\n");
                }
                else
                {
                                printf("Unbalanced Expression\n");
                }
                getch();
}

OUTPUT:
$$$ BALANCING SYMBOLS $$$

Check whether the given expression is balanced or not
Enter the expression:   {[(a+b)*c]-[(c*d)+b]}#
Balanced Expression


$$$ BALANCING SYMBOLS $$$

Check whether the given expression is balanced or not
Enter the expression:   {(a*b]/(c+d]}#

Unbalanced Expression

Infix to Postfix Conversion

PROGRAM:
#include<stdio.h>
#include<conio.h>
#define SIZE 50
void push(char);
char pop();
int priority(char);
char s[SIZE];
int top=-1;
void main()
{
                char infix[50],postfix[50],ch,item;
                int i=0,k=0,len;
                clrscr();
                printf("\n\n $$$ INFIX TO POSTFIX CONVERSION $$$ \n\n");
                printf("Enter infix expression:\t");
                scanf("%s",infix);
                push('#');
                len=strlen(infix);
                for(i=0;i<len;i++)
                {
                                ch=infix[i];
                                if(isalnum(ch))
                                {
                                                postfix[k]=ch;
                                                k++;
                                }
                                else if(ch=='(')
                                {
                                                push(ch);
                                }
                                else if(ch==')')
                                {
                                                while(s[top]!='(')
                                                {
                                                                postfix[k]=pop();
                                                                k++;
                                                }
                                                item=pop();
                                }
                                else
                                {
                                                while(priority(s[top])>=priority(ch))
                                                {
                                                                postfix[k]=pop();
                                                                k++;
                                                }
                                                push(ch);
                                }
                }
                while(s[top]!='#')
                {
                                postfix[k]=pop();
                                k++;
                }
                postfix[k]='\0';
                printf("Postfix expression is: %s",postfix);
                getch();
}
void push(char a)
{
                top=top+1;
                s[top]=a;
}
char pop()
{
                char a;
                a=s[top];
                top=top-1;
                return a;
}
int priority(char a)
{
                switch(a)
                {
                                case '#':return 0;
                                case '(':return 1;
                                case '+':return 2;
                                case '-':return 2;
                                case '*':return 3;
                                case '/':return 3;
                                case '^':return 4;
                }
}

OUTPUT:
$$$ INFIX TO POSTFIX CONVERSION $$$
Enter infix expression: (A+B^C)*D+E^5

Postfix expression is: ABC^+D*E5^+