Showing posts with label 141353-OOP LAB MANUL. Show all posts
Showing posts with label 141353-OOP LAB MANUL. Show all posts

Wednesday, 12 October 2011

OPERATIONS ON COMPLEX NUMBERS USING FILES AS STORAGE


OPERATIONS ON COMPLEX NUMBERS USING FILES AS STORAGE

AIM:
To write a C++ program to perform addition of complex numbers using files.

ALGORITHM:

Step 1: Start the program.
Step 2: Create a class called COMPLEX and create its members: real and imaginary.
Step 3: Generate the random numbers by using rand() function.
Step 4: Write the randomly generated numbers in a file called “complex1.txt”.
Step 5: Add the two complex numbers.
Step 6: Store the Resultant complex number in another file called “result.txt”
Step 7: Display the resultant value.
Step 8: Stop the program.

PROGRAM:

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
#include<time.h>
class complex
{
public:
int real;
int imag;
complex(int r, int i)
{
real = r;
imag = i;
}
complex()
{
real = imag = 0;
}
void display(void);
};
void complex::display(void)
{
cout<<real<<((imag<0)?"-i":"+i")<<imag<<"\n";
}
void main()
{
clrscr();
ofstream ocom("complex1.txt");
float real,imag;
time_t ti;
srand((unsigned) time(&ti));
real = rand()%100;
imag = rand()%100;
ocom<<"("<<real<<((imag<0)?"-i":"+i")<<imag<<")"<<"+";
real = rand()%100;
imag = rand()%100;
ocom<<"("<<real<<((imag<0)?"-i":"+i")<<imag<<")"<<"\n";
ocom.close();
ifstream icom("complex1.txt");
char no,t,ch,op;
icom>>no;
icom>>real;
icom>>ch;
icom>>no;
icom>>imag;
imag=(ch=='+')?imag:-imag;
icom>>no;
icom>>op;
complex a(real,imag);
icom>>no;
icom>>real;
icom>>ch;
icom>>no;
icom>>imag;
imag=(ch=='+')?imag:-imag;
icom>>no;
icom>>op;
complex b(real,imag);
complex c;
switch(op)
{
case '+':
c.real = a.real+b.real;
c.imag = a.imag+b.imag;
break;
case '-':
c.real = a.real-b.real;
c.imag = a.imag-b.imag;
break;
case '*':
c.real = (a.real*b.real)-(a.imag*b.imag);
c.imag = (a.real*b.imag)+(a.imag*b.real);
break;
case '/':
float qt;
qt = b.real*b.real+b.imag*b.imag;
c.real = (a.real*b.real+a.imag*b.imag)/qt;
c.imag = (a.imag*b.real-a.real*b.imag)/qt;
break;
default:
cout<<"\n Invalid";
}
cout<<"\n complex 1:";
a.display();
cout<<"\n complex 2:";
b.display();
cout<<"\n Resultant complex:";
c.display();
ofstream out("result.txt");
out<<"("<<c.real<<((c.imag<0)?"-i":"+i")<<c.imag<<")";
out.close();
}

OUTPUT:
complex1.txt:
(0+i72)+(39+i71)
result.txt
(39+i143)

RESULT:
Thus the program for addition of complex numbers using files was executed.

DYNAMIC POLYMORPHISM AND RTTI


DYNAMIC POLYMORPHISM AND RTTI
AIM:
To write a C++ program to demonstrate a simple test application for dynamic polymorphism.

ALGORITHM:

Step 1: Start the program.
Step 2: Create a class called Point in order to locate the position of the shapes.
Step 3: Create a class called Shape and use virtual function to draw the corresponding shape.
Step 4: Create classes for shapes like Square, Rectangle, Triangle, Circle, Ellipses.
Step 5: Call all the shapes using objects, in main function.
Step 6: Shapes will be drawn with the given values.
Step 7: Shapes can also be drawn by using their address and display their location.
Step 8: Stop the program.

PROGRAM:
#include<iostream.h>
#include<conio.h>
class Point
{
public:
            int x; int y;
            Point(){}
            Point(int tempX, int tempY)
            {
            x = tempX;
            y = tempY;
            }
            int GetX()
            {
            return x;
            }
            int GetY()
            {
            return y;
            }
            friend ostream & operator <<(ostream &tempout, Point &tempPoint)
            {
            tempout<<"("<<tempPoint.GetX()<<tempPoint.GetY()<<")";
            return tempout;
            }
};
class Shape
{
 Point position;

 public:
            Shape(){}
            virtual void draw()
            {
             cout<<"shape is drawm";
             }
};
class Square:public Shape
{
 Point leftbottom;
 int length;
 public:
  Square(){}
  Square(Point tleftbottom, int tlength)
  {
   leftbottom = tleftbottom;
   length = tlength;
  }
  void draw()
  {
   cout<<"Square is drawn at"<<leftbottom<<"and with length as :"<<length<<"\n";
  }
 };
class Rectangles : public Shape
{
 Point leftbottom, lefttop, rightbottom, righttop;
 public:
  Rectangles(){}
  Rectangles(Point tleftbottom, Point tlefttop, Point trightbottom, Point trighttop)
  {
   leftbottom = tleftbottom;
   lefttop = tlefttop;
   rightbottom = trightbottom;
   righttop = trighttop;
  }
  void draw()
  {
   cout<<"Rectangle is drawn at ("<<leftbottom<<", "<<rightbottom<<")"<<"and"<<"("<<lefttop<<","<<righttop<<")\n";
  }
};
class Triangle : public Shape
{
 Point avertex, bvertex, cvertex;
 public:
 Triangle(){}
 Triangle(Point tavertex, Point tbvertex, Point tcvertex)

 {
  avertex = tavertex;
  bvertex = tbvertex;
  cvertex = tcvertex;
  }
  void draw()
  {
   cout<<"Triangle is drawn at"<<avertex<<" "<<bvertex<<" "<<cvertex<<"\n";
   }
};
class Circle: public Shape
{
 Point center; int radius;
 public:
 Circle(){}
 Circle(Point tcenter, int tradius)
 {
  center = tcenter;
  radius = tradius;
 }
 void draw()
 {
  cout<<"Circle is drawn at"<<" " <<center<<" " <<"and the radius is: "<<radius<<"\n";
 }
};
class Ellipses: public Shape
{
 Point center;
 int radius; int angle;
 public:
 Ellipses(){}
 Ellipses(Point tcenter, int tradius, int tangle)
 {
  center = tcenter;
  radius = tradius;
  angle = tangle;
 }
 void draw()
 {
  cout<<"Ellipse is drawn at"<<center<<"and the radius is"<<radius<<"with an angle"<<angle<<"\n";
 }
};
void main()
{
clrscr();
cout<<"\n";

Point p1(10,20);
Point p2(3,2);
Square sq(p1,5);
sq.draw();
Rectangles rect(p1,p2,p1,p2);
rect.draw();
Circle c(p1,50);
c.draw();
Ellipses e(p2,34,23);
e.draw();
Triangle t(p1,p2,p1);
t.draw();
Shape *s;
s=&sq;
s->draw();
s=&rect;
s->draw();
s=&t;
s->draw();
getch();
}

OUTPUT:

Square is drawn at(1020)and with length as :5
Rectangle is drawn at ((1020), (1020))and((32),(32))
Circle is drawn at (1020) and the radius is: 50
Ellipse is drawn at(32)and the radius is34with an angle23
Triangle is drawn at(1020) (32) (1020)
Square is drawn at(1020)and with length as :5
Rectangle is drawn at ((1020), (1020))and((32),(32))
Triangle is drawn at(1020) (32) (1020)

RESULT:
Thus the C++ program to demonstrate a simple test application for dynamic polymorphism was executed.

MINIMUM SPANNING TREE


MINIMUM SPANNING TREE
AIM:
To find the minimum cost spanning tree in a graph using Prims algorithm.
ALGORITHM:
Step 1 : Start the process.
Step 2: Create two classes as POINT and ARC.
Step 3: Define a Graph class which represents the collection of Point and Arc objects.
Step 4: Write a method to find a minimum cost spanning tree in a graph.
Step 5: Enter the Number of nodes, source node, destination node, weight for the corresponding edge.
Step6: Repeat step5 until all vertices, edges and weight are defined.
Step 7: The edge does not form a cycle in the graph, for Minimum Spanning Tree.
Step 8: Thus set of edges is connected and form a Minimum Spanning Tree.
Step 9: Display the adjacency matrix for the graph and the minimum total path of all the edges. 
Step 10: Stop the process.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#define MAX 50
#define TRUE 1
#define FALSE 0
#define MAXINT 250

class node
{
public:
int no;
node()
{}


node (int a)
{
no = a;
}
};
class arc
{
public:
int adj;
int weight;
arc()
{}
arc(int a)
{
adj = a;
}
};
class graph
{
public:
node nodes[MAX];
arc arcs[MAX][MAX];
graph(int n)
{
for(int i=1;i<=n;i++)
{
nodes[i].no = 0;
for(int j=1;j<=n;j++)
arcs[i][j].adj = FALSE;
}
}
void join(node n1, node n2, int w)
{
arcs[n1.no][n2.no].adj = w;
arcs[n2.no][n1.no].adj = w;
}

void displayadj(int n)
{
cout<<"\n The adjacency matrix....";
cout<<endl;
for(int i = 1; i<=n; i++)
{
for(int j = 1; j<=n;j++)
cout<<"\t"<<arcs[i][j].adj;
cout<<endl;
}
cout<<endl;
}

void shortpath(int n)
{
int lcost[20];
int clost[20],i,j,k,min;
for(i = 2; i<=n;i++)
{
lcost[i]=arcs[1][i].adj;
clost[i]=1;
}
cout<<"\n Minimum cost spanning tree edges are:\n";
for(i=2;i<=n;++i)
{
min=lcost[2];
k=2;
for(j=3;j<=n;++j)
if(lcost[j]<min)
{
min = lcost[j];
k=j;
}
cout<<"\n"<<k<<"<->"<<clost[k];
lcost[k]=MAXINT;
for(j=2;j<=n;++j)
if((arcs[k][j].adj<lcost[j])&&(lcost[j]<MAXINT))
{
lcost[j]=arcs[k][j].adj;
clost[j]=k;
}
}
}
};
int main()
{
int n;
clrscr();
cout<<"\n Enter total number of nodes...";
cin>>n;
graph g(n);
cout<<"\n Assigning number for each node...";
for(int i = 1;i<=n;i++)
g.nodes[i].no=i;
char ch ='y';
int w;
do
{
node a,b;
cout<<"\n Create path b/w the nodes";
cout<<"\n Enter the source node...";
cin>>a.no;
cout<<"\n Enter the destination node...";
cin >>b.no;
cout<<"\n Enter the weight:";
cin>>w;
g.join(a,b,w);
cout<<"\n Want to continue...[y]es or [n]:";
cin>>ch;
}while(ch=='y');
g.displayadj(n);
g.shortpath(n);
cin>>n;
getch();
return 0;
}

OUTPUT:
 Enter total number of nodes...4
 Assigning number for each node...
 Create path b/w the nodes
 Enter the source node...1
 Enter the destination node...2
 Enter the weight:1
 Want to continue...[y]es or [n]:y
 Create path b/w the nodes
 Enter the source node...1
 Enter the destination node...4
 Enter the weight:2
 Want to continue...[y]es or [n]:y
 Create path b/w the nodes
 Enter the source node...1
 Enter the destination node...3
 Enter the weight:2
 Want to continue...[y]es or [n]:y
 Create path b/w the nodes
 Enter the source node...2
 Enter the destination node...4
 Enter the weight:4
 Want to continue...[y]es or [n]:y
 Create path b/w the nodes
 Enter the source node...2
 Enter the destination node...3
 Enter the weight:5
 Want to continue...[y]es or [n]:y
 Create path b/w the nodes
Enter the source node...3
 Enter the destination node...4
 Enter the weight:3
 Want to continue...[y]es or [n]:n
 The adjacency matrix....
        0       1       2       2
        1       0       5       4
        2       5       0       3
        2       4       3       0
 Minimum cost spanning tree edges are:
2<->1
3<->1
4<->1

RESULT:
Thus the program to find the minimum cost spanning tree in a graph using Prims algorithm was executed.