Searching method

Hi

I wanted to create a C++ program.Using constructor and proper methods design a class graphics which stores shapes, area, back color, forecolor. Use this class in the main program to input any N shapes and perform the following options and print the list in
a) Sort according to area.
b) Search according to accepted shape.

This is a Program for a student.

please assist me.

High!

First of all to tune your C++ the introduction samples below will suffice:
http://www.cplusplus.com/doc/tutorial/classes/
Basically the C++ does not tell the class except
for its type name that is the C string when runing a program, though it looks from an editorial point of view as such. Namely it is the proper way to develop a method of classes because
of the C/C++ environment.
Also you may use "new" operator for a limited rows of dynamic figures having inserted a type id of your own into a class declaration. Otherwise they cannot be found amongst a resesrved memory of the program as such leading to the unwanted dismantling
after all.
Hi Again,

I have completed first part i.e sorting. please find the code below.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class graphics
{
public:
char shape[20];
int area;
int dim;
char fc[20],bc[20];
graphics()
{
strcpy(shape,"");
strcpy(fc,"");
strcpy(bc,"");
area=0;
dim=0;
}
~graphics()
{}

void getdata()
{
cout<<"Enter the shape \n";
cin>>shape;
cout<<"Enter the area \n";
cin>>area;
cout<<"Enter the dimensions \n";
cin>>dim;
cout<<"Enter the forecolor \n";
cin>>fc;
cout<<"Enter the backcolor \n";
cin>>bc;
}
void putdata()
{
cout<<"Shape:"<<shape<<"\n ";
cout<<"Area:"<<area<<" \n";
cout<<"Dimensions:"<<dim<<" \n";
cout<<"Forecolor:"<<fc<<"\n ";
cout<<"Backcolor:"<<bc<<"\n";
}
};

void main()
{
graphics grp[10];
graphics temp;
int i,j,n;
clrscr();
cout<<"Enter how many shapes do you want:";
cin>>n;
for(i=1;i<=n;i++)
grp[i].getdata();
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
{
if(grp[i].area>grp[j].area)
{
temp=grp[i];
grp[i]=grp[j];
grp[j]=temp;
}
}
clrscr();
for(i=1;i<=n;i++)
grp[i].putdata();
getch();
}


Now i wanted to search a shape adding to above program.

Please assist me
You're using a c-style array to store your graphics items. So you could use the c qsort function. (search online for details on how to use it)

If you want to use c++, then I'd suggest using an stl container. You could use std::list to store your graphics items. A benefit of this is that you're then not limited to a maximum of 10 graphics items as you are with your hardcoded array (graphics grp[10];)

Once you have all your items stored in your std::list (or vector or whatever container you choose), you can then use either std::sort algorithm, or the sort member function.

eg: using a list:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// a functor class which compares the area member of graphics objects
struct compare_area 
{
    bool operator(const graphics &a, const graphics &b)
    {
        return a.area < b.area;
    }
}

// in your main function...
std::list<graphics> lst;
// ask user for num items, read them into temp
lst.push_back(temp); // adds temp to list
// now sort the list
lst.sort(compare_area()); // passes an anonymous compare_area functor to the sort algorithm 

Read up on std::list and std::list::sort
Hi

It would be great if i got the entire program, because i am not good in C++.

I guess this is for sorting. I want for searching also.
Sorry mate, I can't give you the entire program... for 2 reasons: 1. I haven't written it, and 2: it's for your homework - you have to do it to learn!

But to help you in your learning... use this site! :) There are examples on most pages which can help you learn.

std::list is a doubly linked list:
http://www.cplusplus.com/reference/stl/list/

to add items to your list, call push_back:
http://www.cplusplus.com/reference/stl/list/push_back/

how to sort your list once you've populated it:
http://www.cplusplus.com/reference/stl/list/sort/

to search for values in a sorted range (which is what your list becomes once you've sorted it), use the algorithm std::equal_range:
http://www.cplusplus.com/reference/algorithm/equal_range/

note: equal range will return a pair of iterators delimiting a range of values which match your search criteria. If you have only 1 item whose value matches, the 2 iterators in the returned pair will be the same.

to print the items matching your search criteria, use std::for_each with a function/functor that prints the members of your graphics as you wish:
http://www.cplusplus.com/reference/algorithm/for_each/

note: you will have to sort twice - once by area, and again by shape. Once it's sorted by shape you can then call equal_range and for_each to print the items which match the searched for shape.

If you don't want to sort by shape, you could just combine two algorithms, for_each and find_if.
http://www.cplusplus.com/reference/algorithm/find_if/

something like this will get you each graphics item which matches a shape:

1
2
3
4
5
std::for_each(std::find_if(graphics_list.begin(),
                           graphics_list.end(),
                           shape_is("SQUARE")),
              graphics_list.end(),
              print_graphics_item());


shape_is is a functor whose constructor takes a parameter (the shape you want to search for) and has:

bool operator()(const graphics& item)

which checks to see if the item's shape matches the one you passed to the constructor

print_graphics_item is a functor which has:

void operator()(const graphics& item)

which prints your item

There is a forum post on here relating to functors:
http://www.cplusplus.com/forum/general/12793/

If you want more information about the stl, a very general overview can be found on wikipedia:
http://en.wikipedia.org/wiki/C%2B%2B_Standard_Library
Last edited on
Topic archived. No new replies allowed.