Вы находитесь на странице: 1из 15

1

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

void read(int [] ,int &);
void display(int [],int );
void swap(int &,int &);
void 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]);
}
}
}
2

//===========================================================================
// 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;
3

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




4

//===========================================================================
// Name : exp3.cpp
// Title : Stack1
// Description : Hello World in C++, Ansi-style
//===========================================================================
#include <iostream.h>
#include <conio.h>
#define MAX 1
class stack
{
int s[MAX];
int top;
public:
stack(int x=-1)
{
top=x;
}
void push(int x)
{
top++;
s[top]=x;
}
int pop()
{
int x=s[top];
top--;
return(x);
}
int stack_full()
{
if(top==MAX-1)
return(1);
else
return(0);
}
int stack_empty()
{
if(top==-1)
return(1);
else
return(0);
}

};
void main()
{ clrscr();
stack s1;
if(!s1.stack_full())
s1.push(10);
if(!s1.stack_full())
s1.push(20);
if(!s1.stack_empty())
cout<<s1.pop()<<endl;
if(!s1.stack_empty())
cout<<s1.pop()<<endl;
getch();
}
5

//===========================================================================
// Name : exp3.cpp
// Title : Stack2
// Description : Hello World in C++, Ansi-style
//===========================================================================
#include <iostream.h>
#include <conio.h>

class stack
{ int *s;
int top,size;
public:
stack(int x=10)
{ top=-1;
size=x;
s=new int[size];
}
~stack()
{ delete s;
}
void push(int x)
{ top++;
s[top]=x;
}
int pop()
{ int x=s[top];
top--;
return(x);
}
int stack_full()
{ if(top==size-1)
return(1);
else
return(0);
}
int stack_empty()
{ if(top==-1)
return(1);
else
return(0);
}
};
void main()
{ clrscr();
stack s1(5);
for(int i=0;i<5;i++)
if(!s1.stack_full())
s1.push(10);
for(int i=0;i<5;i++)
if(!s1.stack_empty())
cout<<s1.pop()<<endl;
getch();
}




6

//===========================================================================
// Name : exp3.cpp
// Title : Stack3
// Description : Hello World in C++, Ansi-style
//===========================================================================

#include <iostream.h>
#include <conio.h>
#define MAX 10
class stk
{ int a[MAX];
int top;
public:
friend class stack;
};
class stack
{ stk s;
public:
stack(int x=-1)
{ s.top=-1;
}
void push(int x)
{ (s.top)++;
s.a[s.top]=x;
}
int pop()
{ int x=s.a[s.top];
(s.top)--;
return(x);
}
int stack_full()
{ if(s.top==MAX-1)
return(1);
else
return(0);
}
int stack_empty()
{ if(s.top==-1)
return(1);
else
return(0);
}
};
void main()
{ clrscr();
stack s1;
for(int i=0;i<5;i++)
if(!s1.stack_full())
s1.push(10);
for(i=0;i<5;i++)
if(!s1.stack_empty())
cout<<s1.pop()<<endl;
getch();
}



7

//===========================================================================
// Name : exp3.cpp
// Title : Stack4
// Description : Hello World in C++, Ansi-style
//===========================================================================
#include <iostream.h>
#include <conio.h>
#define MAX 10
class stk
{ public:
int a[MAX];
int top;
};
class stack
{ stk s;
public:
stack(int x=-1)
{ s.top=x;
}
void push(int x)
{ (s.top)++;
s.a[s.top]=x;
}
int pop()
{ int x=s.a[s.top];
(s.top)--;
return(x);
}
int stack_full()
{ if(s.top==MAX-1)
return(1);
else
return(0);
}
int stack_empty()
{ if(s.top==-1)
return(1);
else
return(0);
}
};
void main()
{ clrscr();
stack s1;
for(int i=0;i<5;i++)
if(!s1.stack_full())
s1.push(10);
for(i=0;i<5;i++)
if(!s1.stack_empty())
cout<<s1.pop()<<endl;
getch();
}




8

//===========================================================================
// Name : exp3.cpp
// Title : Stack5
// Description : Hello World in C++, Ansi-style
//===========================================================================

#include <iostream.h>
#include <conio.h>
#define MAX 10
class stk
{ public:
int a[MAX];
int top;
};
class stack
{ stk s;
public:
stack(int x=-1)
{ s.top=x;
}
void push(int x)
{ (s.top)++;
s.a[s.top]=x;
}
int pop()
{ int x=s.a[s.top];
(s.top)--;
return(x);
}
int stack_full()
{ if(s.top==MAX-1)
return(1);
else
return(0);
}
int stack_empty()
{ if(s.top==-1)
return(1);
else
return(0);
}
};
void main()
{
clrscr();
stack s1;
for(int i=0;i<5;i++)
if(!s1.stack_full())
s1.push(10);
for(i=0;i<5;i++)
if(!s1.stack_empty())
cout<<s1.pop()<<endl;
getch();
}



9

//===========================================================================
// Name : exp3.cpp
// Title : Stack5
// Description : Hello World in C++, Ansi-style
//===========================================================================
#include <iostream.h>
#include <conio.h>
class node
{ public:
int data;
node *next;
// public:
// friend class stack;
};
class stack
{ node *top;
public:
stack()
{ top=NULL;
}
~stack()
{ node *temp=top;
while(temp!=NULL)
{ top=top->next;
delete temp;
}
}
void push(int x)
{ node *ptr;
ptr=new node;
ptr->data=x;
ptr->next=NULL;
if(top==NULL)
top=ptr;
else
{
ptr->next=top;
top=ptr;
}
}
int pop()
{ int x;
node *temp;
temp=top;
x=top->data;
top=top->next;
delete temp;
return(x);
}
int stack_full() {}
int stack_empty()
{ if(top==NULL)
return(1);
else
return(0);
}
};
10

void main()
{
clrscr();
stack s1;
for(int i=0;i<5;i++)
s1.push(10);
for(i=0;i<5;i++)
if(!s1.stack_empty())
cout<<s1.pop()<<endl;
getch();
}














































11

//===========================================================================
=
// Name : exp4.cpp
// Title : Operatator Overloading.
// Description : Hello World in C++, Ansi-style
//===========================================================================
=

#include <iostream>
#include <math.h>
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)
12

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











13

//===========================================================================
// 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;
int age;
public:
void get_e()
{
cout<<"Enter name and age : <engg.> ";
cin>>name>>age;
}
};
class database : public doctor,public engineer
{
public:
void display();

};
void database :: display()
{
cout<<name<<"\t"<<age<<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;
}
14

//===========================================================================
// 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()
{
15

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

Вам также может понравиться