//Program : Part B-2
//Using constructors and proper methods design a class graphics which stores //shapes, area, back color, fore colors. Use this class in the main program to input //any 'N' shapes and perform the following options and print the list in the neat //format.
//a)Sort according to Area
//b)Search according to accepted shape
//Name : LDK E-NOTES Reg.No : **********
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<iomanip.h>
#define max 20
class graphics
{
public:
char shape[15],bcolor[15],fcolor[15];
float area;
graphics()
{
strcpy(shape,"\0");
area=0;
strcpy(bcolor,"\0");
strcpy(fcolor,"\0");
}
void getdata();
void printdata();
};
void graphics::getdata()
{
cout<<"Enter the shape : ";
cin>>shape;
cout<<"Enter the area : ";
cin>>area;
cout<<"Enter the background color : ";
cin>>bcolor;
cout<<"Enter the foreground color : ";
cin>>fcolor;
}
void graphics::printdata()
{
cout<<"Shape = "<<shape<<endl;
cout<<"Area = "<<area<<endl;
cout<<"Background color = "<<bcolor<<endl;
cout<<"Foreground color = "<<fcolor<<endl;
}
void main()
{
int i,j,n,s,ch=1;
char sh[15];
graphics g[max],temp;
clrscr();
cout<<"Enter the number of shapes : ";
cin>>n;
for(i=0;i<n;i++)
g[i].getdata();
while(ch<=2)
{
cout<<"1.Sort"<<endl;
cout<<"2.Search"<<endl;
cout<<"3.Exit"<<endl;
cout<<"Enter your choice : ";
cin>>ch;
switch(ch)
{
case 1 : cout<<"\nSorting shapes according to area"<<endl;
for(i=1;i<n;i++)
for(j=0;j<n-i;j++)
if(g[j].area>g[j+1].area)
{
temp=g[j];
g[j]=g[j+1];
g[j+1]=temp;
}
for(i=0;i<n;i++)
{
g[i].printdata();
}
break;
case 2 : cout<<"Enter the shape to be searched : ";
cin>>sh;
s=0;
for(j=0;j<n;j++)
if(strcmp(g[j].shape,sh)==0)
{
s=1;
break;
}
if(s==1)
cout<<"\nSearched shape "<<sh<<" is found"<<endl;
else
cout<<"\nSearched shape "<<sh<<" is not found"<<endl;
break;
case 3 : exit(0);
default : cout<<"\nInvalid choice";
}
getch();
}
}
Output :
Enter the number of shapes : 3
Enter the shape : Circle
Enter the area : 6.0
Enter the background color : White
Enter the foreground color : Black
Enter the shape : Square
Enter the area : 5.5
Enter the background color : Yellow
Enter the foreground color : Green
Enter the shape : Rectangle
Enter the area : 7.5
Enter the background color : Blue
Enter the foreground color : White
1.Sort
2.Search
3.Exit
Enter your choice : 1
Sorting shapes according to area
Shape = Square
Area = 5.5
Background color = Yellow
Foreground color = Green
Shape = Circle
Area = 6
Background color = White
Foreground color = Black
Shape = Rectangle
Area = 7.5
Background color = Blue
Foreground color = White
1.Sort
2.Search
3.Exit
Enter your choice : 2
Enter the shape to be searched : Triangle
Searched shape Triangle is not found
1.Sort
2.Search
3.Exit
Enter your choice : 2
Enter the shape to be searched : Circle
Searched shape Circle is found
No comments:
Post a Comment