Friday, August 21, 2015

String Operation

// String Operation.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<stdio.h>
#include<conio.h>
#include<string.h>
using namespace std;
   int STRLEN(char*);
   int STRCPY(char*,char*);
   int STRCMP(char*,char*);
   int STRCAT(char*,char*,char*);
   int STRREV(char*);
   void main()
   {
     int c;
     char str[20],str1[10],str2[10],str3[20];
     re:printf("Enter choice=>");
     printf(" 1:string len. 2:string copy 3:string cmp. 4:string cat. 5:string rev.");
     printf("6:for exit=>");
     scanf("%d",&c);
     switch(c)
     {
       case 1:
         printf("Enter the string=>");
         scanf("%s",&str1);
         printf("string length=>%d",STRLEN(str1));
         break;
       case 2:
         printf("Enter the string=>");
         scanf("%s",str1);
         STRCPY(str2,str1);
         printf("copied string=>");
         puts(str2);
         break;
       case 3:
         printf("Enter two string=>");
         scanf("%s",&str1);
         scanf("%s",&str2);
         if(STRCMP(str2,str1))
           printf("string is equal");
         else
           printf("String is not equal");
         break;
       case 4:
         printf("Enter two string=>");
         scanf("%s",str1);
         scanf("%s",str2);
         STRCAT(str3,str2,str1);
         puts(str3);
         break;
       case 5:
         printf("Enter the string=>");
         scanf("%s",str1);
         STRREV(str1);
         printf("Reverse stringis=>");
         puts(str1);
         break;
       default:
         goto end;
     }
     goto re;
     end:
     getch();
   }
   int STRLEN(char *s)
   {
     int i=0;
     while(*s!=NULL)
     {
       i++;
       s++;
     }
     return i;
   }
   int STRCPY(char *s2,char *s1)
   {
     while(*s1!=NULL)
     {
       *s2=*s1;
       s2++;
       s1++;
     }
     *s2=NULL;
     return 1;
   }
   int STRCMP(char *s2,char *s1)
   {
     int i=0,len1,len2;
     len1=strlen(s1);
     len2=strlen(s2);
     if(len1==len2)
     {
       while(*s2==*s1 && *s2 != NULL && *s1!=NULL)
       {
         i++;
         s1++;
         s2++;
       }
       if(i==len1)
         return 1;
       else
         return 0;
     }
     else
     {
       return 0;
     }
   }
   int STRREV(char *s)
   {
     int len;
     char *s1;
     char *ptr;
     len=strlen(s);
     s1=(char *) malloc(sizeof(char));
     strcpy(s1,s);
     ptr=s1+len-1;
     while(*s!=NULL)
     {
       *s=*ptr;
       ptr--;
       s++;
       s1++;
     }
     *s=NULL;
     return 1;
   }
   int STRCAT(char *s3,char *s2,char *s1)
   {
     while(*s1!=NULL)
     {
       *s3=*s1;
       s3++;
       s1++;
     }
     s3++;
     while(*s2!=NULL)
     {
       *s3=*s2;
       s3++;
       s2++;
     }
     *s3=NULL;
     return 1;
   }

Linear Search


   #include < iostream.h > 
   #include < constream.h > 
   void read(int a[10],int n) 
   { 
     cout<<"reading\n"; 
     for(int i=0;i < n;i++) 
       cin>>a[i]; 
   } 
   void display(int a[10],int n) 
   { 
     for(int i=0;i < n;i++) 
       cout << a[i]<<"\t"; 
   } 
   void linearsearch ( int a[10],int n ) 
   { 
     int k,flag=0; 
     read(a,n); 
     display(a,n); 
     cout<<"\nenter an element to be search\n"; 
     cin>>k; 
     for(int i=0;i < n;i++) 
     { 
       if(a[i]==k) 
         flag=1; 
       break; 
     } 
     if(flag==1) 
       cout << "\nsearching is successful,element is found at position " << i +1 << endl; 
     else 
       cout<<"searching not successful\n"; 
   } 
   void main() 
   { 
     int a[10], n; 
     clrscr(); 
     cout <<"enter n value..\n"; 
     cin>>n; 
     linearsearch(a,n); 
     getch(); 
   }

Overloads assignment operator (=)


   #include <iostream> 
   using namespace std; 
   class alpha 
   { 
     private: 
       int data; 
     public: 
       alpha(){ } 
       alpha(int d){ data = d; } 
       void display(){ cout << data; } 
       alpha operator = (alpha& a) 
       { 
         data = a.data; 
         cout << "\nAssignment operator invoked"; 
         return alpha(data); 
       } 
   }; 
   int main() 
   { 
     alpha a1(37); 
     alpha a2; 
     a2 = a1; 
     cout << "\na2="; 
     a2.display(); 
     alpha a3 = a2; 
     cout << "\na3="; 
     a3.display(); 
     cout << endl; 
     return 0; 
   }

Octal - Hexadecimal - Decimal conversion



  #include <iostream.h>
  #include <iomanip.h>
  int main()
  {
    cout<<"The decimal value of 15 is "<<15<<endl;
    cout<<"The octal value of 15 is "<<setiosflags(ios::oct)<<15<<endl;
    cout<<"The hex value of 15 is "<<setiosflags(ios::hex)<<15<<endl;
    return 0;
  }

Polymorphism


   #include <iostream> 
   using namespace std; 
   class Shape 
   { 
     protected: 
       int width, height; 
     public: 
       Shape( int a=0, int b=0) 
       { 
         width = a; 
         height = b; 
       } 
       int area() 
       { 
         cout << "Parent class area :" <<endl; 
         return 0; 
       } 
   }; 
   class Rectangle: public Shape 
   { 
     public: 
       Rectangle( int a=0, int b=0) 
       { 
         Shape(a, b); 
       } 
       int area () 
       { 
         cout << "Rectangle class area :" <<endl; 
         return (width * height); 
       } 
   }; 
   class Triangle: public Shape 
   { 
     public: 
       Triangle( int a=0, int b=0) 
       { 
         Shape(a, b); 
       } 
       int area () 
       { 
         cout << "Rectangle class area :"<<endl; 
         return (width * height / 2); 
       } 
   }; 
   // Main function for the program 
   int main( ) 
   { 
     Shape *shape; 
     Rectangle rec(10,7); 
     Triangle tri(10,5); 
     // store the address of Rectangle 
     shape = &rec; 
     // call rectangle area. 
     shape->area(); 
     // store the address of Triangle 
     shape = &tri; 
     // call triangle area. 
     shape->area(); 
     return 0; 
   }

Namespace can be nested within another


   #include <iostream> 
   using namespace std; 
   namespace MyNameSpace1 
   { 
     int i; 
     namespace MyNameSpace2 
     { // a nested namespace 
       int j; 
     } 
   } 
   int main() 
   { 
     MyNameSpace1::i = 19; 
     MyNameSpace1::MyNameSpace2::j = 10; 
     cout << MyNameSpace1::i << " "<< MyNameSpace1::MyNameSpace2::j << "\n"; 
     using namespace MyNameSpace1; 
     cout << i * MyNameSpace2::j; 
     return 0; 
   }

Multiple Inheritance


   #include <iostream> 
   using namespace std; 
   class base1 
   { 
     protected: 
       int x; 
     public: 
       void showx() { cout << x << "\n"; } 
   }; 
   class base2 
   { 
     protected: 
       int y; 
     public: 
       void showy() { cout << y << "\n"; } 
   }; 
   // Inherit multiple base classes. 
   class derived: public base1, public base2 
   { 
     public: 
       void set(int i, int j) { x = i; y = j; } 
   }; 
   int main() 
   { 
     derived ob; 
     ob.set(10, 20); // provided by derived 
     ob.showx(); // from base1 
     ob.showy(); // from base2 
     return 0; 
   }

Tower Of Hanoi Alogithm Using Recursion.

 #include<iostream.h> 
   #include<stdio.h> 
   #include<conio.h> 
   class tower 
   { 
     int *t1,*t2,*t3; 
     int x,y,z; 
     public: 
     void disp_tower(); 
     void move_disk(int tx,int ty); 
     void toh(int n,int a,int b,int c); 
     tower(int no); 
     ~tower(); 
   }; 
   tower :: tower(int no) 
   { 
     t1 = new int[no+1]; 
     t2 = new int[no+1]; 
     t3 = new int[no+1]; 
     x = no; 
     y = z = 0; 
     for(int i=0,j=no ; i     {
       t1[i] = j;
       t2[i] = t2[i] = 0;
     }
     t1[no] = t2[no] = t3[no] = 0;
   }
   tower :: ~tower()
   {
     delete []t1;
     delete []t2;
     delete []t3;
   }
   void tower :: disp_tower()
   {
     clrscr();
     cout<<"
     X :: ";
     for(int i=0;i     {
       cout<<" "<     }
     cout<<"
     Y :: ";
     for(i=0;i     {
       cout<<" "<     }
     cout<<"
     Z :: ";
     for(i=0;i     {
       cout<<" "<     }
     getch();
   }
   void tower :: toh(int n,int tx,int ty,int tz) //x to y using z
   {
     if(n>=1)
     {
       toh(n-1,tx,tz,ty);
       move_disk(tx,ty); //x to y
       disp_tower();
       toh(n-1,tz,ty,tx);
     }
   }
   void tower :: move_disk(int tx,int ty)
   {
     switch(tx)
     {
       case 1:
       {
         if(ty==2)
           t2[y++] = t1[--x];
         else
           t3[z++] = t1[--x];
       }break;
       case 2:
       {
         if(ty==1)
           t1[x++] = t2[--y];
         else
           t3[z++] = t2[--y];
       }break;
       case 3:
       {
         if(ty==1)
           t1[x++] = t3[--z];
         else
           t2[y++] = t3[--z];
       }break;
     }//end of switch
   }

Thursday, August 20, 2015

FUNCTIONAL MEMEBERS

In C++ language the elements of the class can be member functions, sometimes still called methods. They define a set of operations which can be done on the object. Take, for example, a program that makes extensive use of vector calculus. For simplicity, we will only consider a case of the vector in the plane. Each vector can be represented by an object of a class Vector. Our focus will be vectors in Descartes coordinate system where each vector will be represented by two real numbers that is:
·         nx which is a projection on the x-axis,
·         ny which is a projection of a vector on y-axis.
If we want to accomplish scalar multiplication of vectors that could be done by generating the following code.
class Vector {
public:
       float nx, ny;
       void scalarmultiplication(float scalar);
};
void Vector::scalarmultiplication(float scalar){
       nx *= scalar;
       ny *= scalar;
}
The definition of one vector and his multiplication in that case looks like this:
       Vector v;
       v.scalarmultiplication(5.0);
The access to functions of a class is done with previously described operators that we use to access the class members.On the left side of the operator we put the object, reference or pointer on the objects, while on the left side we put the name of member functions. After the name in brackets we need to put real arguments.
During the function call scalarmultiplication() it isn’t necessary to mention the vector to which multiplication is applicable, because it’s already on the left side of the operator for accessing the object of the class. The members ny and ny which are in function calling are members of variable v.
       Vector normal, a2;
       normal.scalarmultiplication(7.2);
       a2.scalarmultiplication(3.5);

The name of the functional member must be unique inside the area of the name inside the class. 

MEMBERS THAT RETRIVE OBJECTS

New members of objects have to be of use, it has to be a way that surrounding code can access specific object members. For that reason we use member selection operators. Before we access the object member, we have to know which object members we’re accessing. Some object is referred using the name of the object, reference or pointer to an object.
When we identify the object using the name of the object or reference, to access members we use operator . (dot), so that the left side of operator is name of the object or reference and the right side of the object is a member which we want to access. For example let’s define and declare the class Computer.
class Computer {
public:
       int memory;
       int disknumber;
       int megabyte;
};
Computer myhomecomputer;
Computer &refComputer = myhomecomputer;
In previous example the keyword “public” means that the members of the class are publically available. Now we are going to expand the object memory of “myhomecomputer” in this way:
       myhomecomputer.memory = 64;
       refComputer.disknumber = 5; // refers to the same object
If we have the pointers which point to an object of class Computer, members can be accessed through the operator -> (type minus – and greater than >). On the left side of the operator we put pointer and on the right side we put name of the member:
       Computer *pokComputer = &myhomecomputer;
       pokComputer->megahertz = 16;

For accessing the members through objects and reference we use the operator . (dot), and in case of access through pointers operator -> (sign minus follow by the sign >).