Vous êtes sur la page 1sur 9

//===========================================================================

// Name
: exp1.cpp
// Title
: Sort numbers in an array
// Description : Hello World in C++, Ansi-style
//===========================================================================
#include <iostream>
using namespace std;
void
void
void
void

read(int [] ,int &);


display(int [],int );
swap(int &,int &);
sort(int [],int &n);

int main() {
int n,arr[100];
read(arr,n);
display(arr,n);
sort(arr,n);
display(arr,n);
return 0;
}
//Function to read
void read(int a[], int &n)
{
cout<<"Enter number of elements in array: ";
cin>>n;
cout<<"Enter data :"<<endl;
for(int i=0;i<n;i++)
cin>>a[i];
}
//Function to display
void display(int a[], int n)
{
cout<<endl<<"Array is..."<<endl;
for(int i=0;i<n;i++)
cout<<a[i]<<"\t";
cout<<endl<<"---------------";
}
//Function to swap
void swap(int &i,int &j)
{
int temp;
/* OR
temp=i;
i=i+j;
i=j;
j=i-j;
j=temp;
i=i-j;
}
*/
//Bubble sort
void sort(int a[],int &n)
{
for(int i=0;i<n-1;i++)
{
for(int j=0;j<(n-i-1);j++)
{
if(a[j]>a[j+1])
swap(a[j],a[j+1]);
}
}
}

//===========================================================================
// Name
: exp2.cpp
// Title
: Operations on Complex Numbers
// Description : Hello World in C++, Ansi-style
//===========================================================================
#include <iostream>
#include <math.h>
using namespace std;
class complex
{
int real,img;
public:
void display();
void read();
void add(complex);
void subt(complex);
void multiply(complex);
complex multiply1(complex);
void divide(complex);
void conjugate();
};
//Read Function
void complex :: read()
{
cout<<"Enter real and imaginary part : ";
cin>>real>>img;
}
//Display function
void complex :: display()
{
int i;
if(img>0)
cout<<real<<"+i"<<img<<endl;
else
{
i=-img;
cout<<real<<"-i"<<i<<endl;
}
}
//Function to Add
void complex :: add(complex c1)
{
complex sum;
sum.real = real+c1.real;
sum.img = img+c1.img;
cout<<"Sum is : ";
sum.display();
}
//Function to Subtract
void complex :: subt(complex c1)
{
complex diff;
diff.real = real-c1.real;
diff.img = img-c1.img;
cout<<"Difference is : ";
diff.display();
}
//Function to Multiply
void complex :: multiply(complex c1)
{
complex product;

product.real = (real*c1.real)-(img*c1.img);
product.img = (img*c1.real)+(real*c1.img);
cout<<"Product is : ";
product.display();
}
//Function to Divide
void complex :: divide(complex c1)
{
float r,i,ii,din;
din=(pow(c1.real,2))+pow(c1.img,2);
complex c,c2;
c2.real = c1.real;
c2.img = -c1.img;
c= multiply1(c2);
r=c.real/din;
i=c.img/din;
cout<<"Quotient is : ";
if(i>0)
cout<<r<<"+i"<<i<<endl;
else
{
ii=-i;
cout<<r<<"-i"<<ii<<endl;
}
}
complex complex :: multiply1(complex c1)
{
complex product;
product.real = (real*c1.real)-(img*c1.img);
product.img = (img*c1.real)+(real*c1.img);
return(product);
}
//Function to find conjugate
void complex :: conjugate()
{
complex conj;
conj.real = real;
conj.img = -img;
cout<<"Complex Conjugate is : ";
conj.display();
}
int main()
{
complex c,c1;
c.read();
c1.read();
c.add(c1);
c.subt(c1);
c.multiply(c1);
c.divide(c1);
c.conjugate();
c1.conjugate();
return 0;
}

//===========================================================================
// Name
: exp3.cpp
// Title
: Stack operations
// Description : Hello World in C++, Ansi-style
//===========================================================================
#include<iostream>
#define size 10
using namespace std;
class stack
{
int *s;
int top;
public:
stack()
{
cout<<"\n I am in constructor";
s=new int[size];//creation of dynamic stack
top=-1;
}
void push(int item)
{
top++;
s[top]=item;
}
int pop()
{
int item;
if(!stempty())
{
item=s[top];
top--;
return item;
}
else
cout<<"\n stack is empty";
return -1;
}
int stempty()
{
if(top==-1)
return 1;
else
return 0;
}
void display()
{
if(!stempty())
{
for(int i=0;i<=top;i++)
cout<<" "<<s[i];
}
else
cout<<"\n stack is empty";
}
~stack()

};

for(int i=0;i<size;i++)
{
delete []s;
delete s;
}

int main()
{
stack obj;
int choice,val;
char ans;
do
{
cout<<"\n\t Main Menu";
cout<<"\n 1.Push \n2.Pop \n3.Display";
cout<<"\n Enter Your choice :";
cin>>choice;
switch(choice)
{
case 1: cout<<"\n Enter the item to be pushed :";
cin>>val;
obj.push(val);
break;
case 2:
cout<<"\n popped element :"<<obj.pop();
break;
case 3: obj.display();
break;
}
cout<<"\n Do you want to continue ?";
cin >>ans;
}while(ans=='y'||ans=='Y');
return 0;
}

//===========================================================================
// Name
: exp4.cpp
// Title
: Operations on Complex Numbers
// Description : Hello World in C++, Ansi-style
//===========================================================================
#include <iostream>
#include <cmath>
using namespace std;
class complex
{
int real,img;
public:
//Read Function
void read()
{
cout<<"Enter real and imaginary part : ";
cin>>real>>img;
}
//Display function
void display()
{
int i;
if(img>0)
cout<<real<<"+i"<<img<<endl;
else
{
i=-img;
cout<<real<<"-i"<<i<<endl;
}
}
//Operator Overloading
complex operator+(complex);
complex operator-(complex);
complex operator*(complex);
complex operator/(complex);
};
complex complex :: operator+(complex c)
{
complex sum;
sum.real = real+c.real;
sum.img = img+c.img;
return (sum);
}
complex complex :: operator-(complex c1)
{
complex diff;
diff.real = real-c1.real;
diff.img = img-c1.img;
return (diff);
}
complex complex :: operator*(complex c1)
{

complex product;
product.real = (real*c1.real)-(img*c1.img);
product.img = (img*c1.real)+(real*c1.img);
return (product);
}
complex complex :: operator/(complex c1)
{
float r,i,ii,din;
din=(pow(c1.real,2))+pow(c1.img,2);
complex c,c2;
c2.real = c1.real;
c2.img = -c1.img;
c= (*this)*c2;
r=c.real/din;
i=c.img/din;
cout<<"Quotient is : ";
if(i>0)
cout<<r<<"+i"<<i<<endl;
else
{
ii=-i;
cout<<r<<"-i"<<ii<<endl;
}
}
int main() {
complex c,c1,ans;
c.read();
c1.read();
ans = c+c1;
cout<<"Sum is : ";ans.display();
ans = c-c1;
cout<<"Difference is : ";ans.display();
ans = c*c1;
cout<<"Product is : ";ans.display();
ans = c/c1;
return 0;
}

//===========================================================================
// Name
: exp5.cpp
// Title
: Database(Multiple inheritance)
// Description : Hello World in C++, Ansi-style
//===========================================================================
#include <iostream>
using namespace std;
class doctor
{
protected:
string name_d;
int age_d;
public:
void get_d()
{
cout<<"Enter name and age : <doctor> ";
cin>>name_d>>age_d;
}
void display()
{
cout<<"\nDisplay function of class doctor...\n";
cout<<name_d<<" ";
cout<<age_d<<endl;
}
};
class engineer
{
protected:
string name_e;
int age_e;
public:
void get_e()
{
cout<<"Enter name and age : <engg.> ";
cin>>name_e>>age_e;
}
};
class database : public doctor,public engineer
{
public:
void display();
};
void database :: display()
{
cout<<name_e<<"\t"<<age_e<<endl;
cout<<name_d<<"\t"<<age_d;
cout<<endl<<"------------------";
}
int main()
{
database p;
p.get_d();
p.get_e();
p.display();
p.doctor :: display();
//invokes display of class doctor
return 0;
}