Saturday, 27 September 2014

Postfix Evaluation

$$$ POSTFIX EVALUATION $$$

PROGRAM:

#include<iostream>
#include<string.h>
int stack[20],top=-1;
int len,a,b,c,i,x;
char expr[20];

class postfix
{
      public:
      void evaluate()
      {
            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
                  {
                        cout<<"Enter the value of"<<expr[i]<<":";
                        cin>>x;
                        push(x);
                  }
            }
      }

      void push(int item)
      {
            stack[++top]=item;
      }
      int pop()
      {
            int a;
            a=stack[top];
            top=top-1;
            return a;
      }


};
void main()
{
      postfix p;
      cout<<"$$$ POSTFIX EVALUATION $$$";
      cout<<"\nEnter the postfix expression:";
      cin>>expr;
      p.evaluate();
      cout<<"The resultant value is :"<<stack[top];
}

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

Infix to Postfix Conversion

$$$         Infix to Postfix Conversion           $$$

PROGRAM:

#include<iostream>
#include<string.h>
#include<ctype.h>
#define SIZE 50
char s[SIZE],infix[50],postfix[50],ch,item;
int i=0,k=0,len,top=-1;
class itop
{
      public:
      int convert()
      {
            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';
      }

      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;
            }
      }
};

void main()
{
      itop ip;
      cout<<"\n\n $$$ INFIX TO POSTFIX CONVERSION $$$ \n\n";
      cout<<"Enter infix expression:\t";
      cin>>infix;
      ip.convert();
      cout<<"Postfix expression is:"<<postfix;
}


OUTPUT:

$$$ INFIX TO POSTFIX CONVERSION $$$

Enter infix expression: (A+B^C)*D+E^5


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

Merge Sort

$$$       MERGE SORT        $$$

PROGRAM:

#include<iostream>
int a[20],temp[20],i,n;
class mergesorting
{
public:
void getdata()
{
      cout<<"\nEnter the array limit:";
      cin>>n;
      cout<<"Enter array elements:";
      for(i=0;i<n;i++)
      {
            cin>>a[i];
      }
}
void mergesort(int a[],int temp[],int n)
{
      msort(a,temp,0,n-1);
}

void msort(int a[],int temp[],int left,int right)
{
      int center;
      if(left<right)
      {
            center=(left+right)/2;
            msort(a,temp,left,center);
            msort(a,temp,center+1,right);
            merge(a,temp,left,center+1,right);
      }
}

void merge(int a[],int temp[],int left,int center,int right)
{
      int i,left_end,num_elements,tmp_pos;
      left_end=center-1;
      tmp_pos=left;
      num_elements=right-left+1;
      while((left<=left_end)&&(center<=right))
      {
            if(a[left]<=a[center])
            {
                  temp[tmp_pos]=a[left];
                  tmp_pos++;
                  left++;
            }
            else
            {
                  temp[tmp_pos]=a[center];
                  tmp_pos++;
                  center++;
            }
      }
      while(left<=left_end)
      {
            temp[tmp_pos]=a[left];
            left++;
            tmp_pos++;
      }
      while(center<=right)
      {
            temp[tmp_pos]=a[center];
            center++;
            tmp_pos++;
      }
      for(i=0;i<=num_elements;i++)
      {
            a[right]=temp[right];
            right--;
      }
}

void putdata()
{
      cout<<"The Sorted elements are:\t";
      for(i=0;i<n;i++)
      {
            cout<<a[i]<<"\t";
      }
}
};

void main()
{
            mergesorting ms;
            cout<<"$$$ MERGE SORT $$$";
            ms.getdata();
            ms.mergesort(a,temp,n);
            ms.putdata();
}

OUTPUT:

$$$ MERGE SORT $$$

Enter the array limit:       8
Enter array elements:    40           20           70           14           60           61           97           30
The Sorted elements are:            14           20           30           40           60           61           70           97


Quick Sort

$$$ QUICK SORT $$$

PROGRAM:

#include<iostream>
int i,a[20],n;
class quicksort
{
public:
void getdata()
{
      cout<<"\nEnter the array limit:";
      cin>>n;
      cout<<"Enter array elements:";
      for(i=0;i<n;i++)
      {
            cin>>a[i];
      }

}

void qsort(int a[],int left,int right)
{
      int i,j,pivot,temp;
      if(left<right)
      {
            pivot=left;
            i=left+1;
            j=right;
            for(;;)
            {
                  while(a[i]<=a[pivot])
                        i=i+1;
                  while(a[j]>a[pivot])
                        j=j-1;
                  if(i<j)
                  {
                        temp=a[i];
                        a[i]=a[j];
                        a[j]=temp;
                  }
                  else
                  {
                        break;
                  }
            }
            temp=a[pivot];
            a[pivot]=a[j];
            a[j]=temp;
            qsort(a,left,j-1);
            qsort(a,j+1,right);
      }
}

void putdata()
{
      cout<<"The Sorted elements are:\t";
      for(i=0;i<n;i++)
      {
            cout<<a[i]<<"\t";
      }
}
};

void main()
{
      quicksort qs;
      cout<<"$$$ QUICK SORT $$$";
      qs.getdata();
      qs.qsort(a,0,n-1);
      qs.putdata();
}

OUTPUT:

$$$ QUICK SORT $$$
Enter the array limit:       8
Enter array elements:    40           20           70           14           60           61           97           30
The Sorted elements are:            14           20           30           40           60           61           70           97


Insertion Sort

$$$   INSERTION SORT    $$$

PROGRAM:

#include<iostream>
class Insertion
{
      public:int i,j,k,n,a[20],data;
      public:
      void getdata()
      {
            cout<<"\nEnter the array limit:";
            cin>>n;
            cout<<"Enter array elements:";
            for(i=0;i<n;i++)
            {
                  cin>>a[i];
            }
      }
      void sorting()
      {
            cout<<"Passes of insertion sort:\n";
            for(i=1;i<n;i++)
            {
                  data=a[i];
                  for(j=i-1;(j>=0)&&(data<a[j]);j--)
                  {
                        a[j+1]=a[j];
                  }
                  a[j+1]=data;
                  cout<<"\nPass="<<i<<",\t";
                  for(k=0;k<n;k++)
                  {
                        cout<<"\t"<<a[k];
                  }
            }
      }

      void putdata()
      {
            cout<<"\nThe sorted elements are:\t";
            for(i=0;i<n;i++)
            {
                  cout<<a[i]<<"\t";
            }
      }
};
void main()
{
      Insertion is;
      cout<<"$$$ INSERTION SORT $$$";
      is.getdata();
      is.sorting();
      is.putdata();
}

OUTPUT:

$$$ INSERTION SORT $$$

Enter the array limit:       6
Enter array elements:    23           78           45           8              32           56
Passes of insertion sort:
Pass=1,                 23           78           45           8              32           56
Pass=2,                 23           45           78           8              32           56
Pass=3,                 8              23           45           78           32           56
Pass=4,                 8              23           32           45           78           56
Pass=5,                 8              23           32           45           56           78
The sorted elements are:             8              23           32           45           56           78

Wednesday, 24 September 2014

Binary Search

PROGRAM:

#include<iostream>
int c,first,last,middle,n,f=0,key,array[100];
class binary
{
            public:
            void getdata()
            {
                        cout<<"Enter number of elements:\t";
                        cin>>n;
                        cout<<"Enter "<<n<<" integers\n";
                        for(c=0;c<n;c++)
                        {
                                    cin>>array[c];
                        }
                        cout<<"Enter value to find:\t";
                        cin>>key;
            }
            void search()
            {
                        first=0;
                        last=n-1;
                        while(first<=last)
                        {
                                    middle=(first+last)/2;
                                    if(key<array[middle])
                                    {
                                                last=middle-1;
                                    }
                                    else if(key>array[middle])
                                    {
                                                first=middle+1;
                                    }
                                    else
                                    {
                                                cout<<key<<" found at location "<<middle+1<<endl;
                                                f=1;
                                                break;
                                    }
                        }
                        if ( f==0 )
                        {
                                    cout<<"Not found! "<<key<<" is not present in the list.\n";
                        }
            }
};

void main()
{
            binary bs;
            cout<<"\n\n\n Binary Search using Array \n\n\n";
            bs.getdata();
            bs.search();
}


OUTPUT:

Binary Search using Array

Enter number of elements:       5
Enter 5 integers
2
4
6
8
10
Enter value to find: 10
10 found at location 5.
 

 Binary Search using Array

Enter number of elements:       5
Enter 5 integers
1
3
5
7
9
Enter value to find: 8
Not found! 8 is not present in the list.

Linear Search

PROGRAM:

#include<iostream>
int a[10],i,n,key,f=0;
class linear
{
            public:
            void getdata()
            {
                        cout<<"Enter the array limit:\t";
                        cin>>n;
                        cout<<"Enter array elements:\n";
                        for(i=0;i<n;i++)
                        {
                                    cin>>a[i];
                        }
                        cout<<"Enter the element to be searched:\t";
                        cin>>key;
            }
            void search()
            {
                        for(i=0;i<n;i++)
                        {
                                    if(a[i]==key)
                                    {
                                                cout<<"Element found\n";
                                                f=1;
                                                break;
                                    }
                        }
                        if(f==0)
                        {
                                    cout<<"Element not found\n";
                        }
            }
};

 void main()
{
    linear ls;
    cout<<"\n\n LINEAR SEARCH \n\n";
    ls.getdata();
    ls.search();
}


OUTPUT:

LINEAR SEARCH

Enter the array limit:  5
Enter array elements:
1
3
5
7
9
Enter the element to be searched:       9
Element found

 
LINEAR SEARCH

Enter the array limit:  5
Enter array elements:
2
4
6
8
10
Enter the element to be searched:       9
Element not found