Custom Search

Tuesday, November 4, 2008

Defining a class and creating a vector of the class, then sorting and searching the vector

#include <vector>

#include<string>

#include <cmath>

#include <fstream>

#include <cstdlib>

#include <iostream>

#include <iomanip>

#include <string>

using namespace std;


 

const long SIZE=6;

const double PI=3.14159;


 

class tankcar {

//********the following attributes representthe object********************

//**********they can only be acessed by the methods***********************


 

private:

    string id;

    double radius;

    double length;

    double weight;

    char type;


 

//******************here begin the method prototypes***********************

//******************the methods define the interface**************

//******************between the "outside world" and *****************

//******************an object's attributes*********************************

public:

    tankcar(); //constructor has same name as class

//overloaded constructor

tankcar(string initial_id, double initial_radius,double initial_length,double initial_weight,

            char initial_type);


 

~tankcar(); //destructor


 

//readers - report current values of class attributes

    double read_radius();

double read_length();

double read_weight();

char read_type();

    double read_volume();

    double read_girth();

    double read_area();

    string read_id();

//writers - update the values of attributes

    void write_radius(double);

    void write_length(double);

    void write_weight(double);

    void write_type(char);

    void write_id(string);

};

//************************end class definition**************************


 

//************************begin class method definitions****************

//*** The :: is called a scope resolution operator ********************

//*** The scope resolution operator notifies the compiler where to look for

//*** the declarations of variables not declared in the method blocks**


 

tankcar::tankcar() //constructor has same name as class

{cout<<"+";

     id ="";

radius=0.0;

length=0.0;

weight=0.0;

type='z';

}


 

//overloaded constructor

    tankcar::tankcar(string initial_id, double initial_radius,double initial_length,

            double initial_weight,char initial_type)

    { cout<<"\noutput for this example,note an initialized tankcar has been "<<

        "created\n";

     id = initial_id;

radius=initial_radius;

length=initial_length;

weight=initial_weight;

type=initial_type;

    }


 

//destructor for this application does nothing

//only one destructor allowed


 

tankcar::~tankcar() //destructor

    {cout<<"-";

    }


 

//readers - report current values of class attributes

double tankcar::read_radius()

        {return radius;}

double tankcar::read_length()

        {return length;}

double tankcar::read_weight()

        {return weight;}

char tankcar::read_type()

        {return type;}

double tankcar::read_volume()

        {return PI*pow(radius,2.0)*length;}

double tankcar::read_girth()

        {return 2.0*PI*radius;};

double tankcar::read_area()

        {return 2.0*PI*radius*(radius+length);}

    string tankcar::read_id()

        {return id;}

//writers - update the values of attributes

void tankcar::write_radius(double new_radius)

        {radius=new_radius;}

void tankcar::write_length(double new_length)

{length=new_length;}

void tankcar::write_weight(double new_weight)

        {weight=new_weight;}

void tankcar::write_type(char new_type)

        {type=new_type;}

    void tankcar::write_id(string new_id)

        {id = new_id;}

//***********end of methods for class account***********************


 

//********start general function prototypes for application*******


 

void get_cars(vector <tankcar> &c);    //if the function updates the vector

                    //the vector must be passed by explicit reference

void print_cars(vector <tankcar> c);

void sort_cars_by_id(vector <tankcar> &c);

void use_linear_search(vector <tankcar> c);

void use_binary_search(vector <tankcar> c);

int linear(vector <tankcar> c,string search);

int binary(vector <tankcar> c,string search);


 

int main()

{

vector <tankcar> cars;


 

get_cars(cars);

print_cars(cars);


 

use_linear_search(cars);


 

sort_cars_by_id(cars);

print_cars(cars);


 

use_binary_search(cars);


 

return 0;

}


 

void get_cars(vector<tankcar> &c)

{tankcar next_tanker;

double rad, len,w;

char t;

string id, file_name;

ifstream in;

cout<<"enter name of tank car data file"<<endl;

getline(cin, file_name);

in.open(file_name.c_str());

if(in.fail())

{cout<<"cannot open "<<file_name<<endl;

exit(1);

}


 

in>>id>> rad >>len >>w>>t; //priming read for test of eof

while (!in.eof())

{ next_tanker.write_id(id);

next_tanker.write_radius(rad);

next_tanker.write_length(len);

next_tanker.write_weight(w);

next_tanker.write_type(t);

c.push_back(next_tanker); // add a copy of the next_tanker to the end of the vector

in>>id>> rad >>len >>w>>t; //all subsequent data read here and tested for eof

                //at the top of the loop-read first then test for eof


 

}

in.close();

}


 

void print_cars(vector<tankcar> c)

{int i;

cout<<

"\n\n"<<setw(4)<<"id"<<setw(8)<<"radius"<<setw(12)<<"length"<<setw(10)<<"weight"<<setw(8)<<"type";

cout<<setw(15)<<"volume"<<setw(10)<<"girth"<<setw(12)<<"area"<<"\n\n";


 

cout<<fixed<<setprecision(2);


 

for(i=0; i < c.size(); ++i)

{ cout<< setw(4)<<c[i].read_id()

     <<setw(8)<<c[i].read_radius()<<setw(12)

     <<c[i].read_length()<<setw(10)

     << c[i].read_weight()<<setw(8)<< c[i].read_type()

     <<setw(15)<< c[i].read_volume()<<setw(10)<< c[i].read_girth()

     <<setw(12)<< c[i].read_area()<< endl;

}

}


 

/* Bubble sort with early exit when no swaps*/

void sort_cars_by_id(vector<tankcar> &c)

{

tankcar temp;

int i,j,swapsmade=1;


 

for( i=c.size(); i >=2 && swapsmade ; --i)

{    swapsmade = 0;

    for(j=0; j < i-1; ++j)

    {

        if( c[j].read_id() > c[j+1].read_id())

        { temp = c[j];

         c[j]=c[j+1];

         c[j+1] = temp;

         swapsmade =1;

        }

    }

}

}


 


 

void use_linear_search(vector<tankcar> c)

{int k, more;

string search_id;

do{

    cout<< "enter the id of a tank car whose volume you want to know:";

    cin>> search_id;

    k=linear(c,search_id);

    if (k<0)

        cout<<"tankcar "<<search_id<<" not found\n\n";

    else

        cout<< "the volume of car " << search_id <<" is "<<c[k].read_volume()<<"\n\n";


 

    cout<<"enter 1 to continue this query process; otherwise enter a 0:";

    cin>>more;

} while(more);

}


 

void use_binary_search(vector<tankcar> c)

{int k, more;

string search_id;

do{

    cout<< "enter the id of a tank car whose volume you want to know:";

    cin>> search_id;

    k=binary(c,search_id);

    if (k<0)

        cout<<"tankcar "<<search_id<<" not found\n\n";

    else

        cout<< "the volume of car " << search_id <<" is "<<c[k].read_volume()<<"\n\n";


 

    cout<<"enter 1 to continue this query process; otherwise enter a 0:";

    cin>>more;

}while(more);

}


 


 


 

int linear(vector<tankcar> c,string search)

{

int i;

for( i=0; i<c.size(); ++i)

{if(search == c[i].read_id())

return i;

}

return -1;

}


 


 

int binary(vector<tankcar> c,string search)

{

int mid, lo=0, hi= c.size()-1;


 

while (lo <= hi)

{mid = (lo + hi)/2;

if( search < c[mid].read_id())

hi = mid-1;

else

if (search > c[mid].read_id())

lo = mid +1;

else

return mid;

}

return -1;

}

[pt@cs my_prog1]$

[pt@cs my_prog1]$ c++ search_class_vec.C

[pt@cs my_prog1]$ a.out

+enter name of tank car data file

tank_data.txt

--------


 

id radius length weight type volume girth area


 

1234 12.00 40.00 12000.00 a 18095.56 75.40 3920.70

4321 12.00 35.00 11000.00 a 15833.61 75.40 3543.71

5423 10.00 35.00 10000.00 b 10995.56 62.83 2827.43

3333 13.00 40.00 14000.00 c 21237.15 81.68 4329.11

1123 13.00 40.00 14500.00 c 21237.15 81.68 4329.11

9933 10.00 35.00 10000.00 b 10995.56 62.83 2827.43

8865 14.00 20.00 12000.00 d 12315.03 87.96 2990.79

-------enter the id of a tank car whose volume you want to know:1111

-------tankcar 1111 not found


 

enter 1 to continue this query process; otherwise enter a 0:1

enter the id of a tank car whose volume you want to know:9999

-------tankcar 9999 not found


 

enter 1 to continue this query process; otherwise enter a 0:1

enter the id of a tank car whose volume you want to know:1123

-------the volume of car 1123 is 21237.15


 

enter 1 to continue this query process; otherwise enter a 0:0

-------+-


 

id radius length weight type volume girth area


 

1123 13.00 40.00 14500.00 c 21237.15 81.68 4329.11

1234 12.00 40.00 12000.00 a 18095.56 75.40 3920.70

3333 13.00 40.00 14000.00 c 21237.15 81.68 4329.11

4321 12.00 35.00 11000.00 a 15833.61 75.40 3543.71

5423 10.00 35.00 10000.00 b 10995.56 62.83 2827.43

8865 14.00 20.00 12000.00 d 12315.03 87.96 2990.79

9933 10.00 35.00 10000.00 b 10995.56 62.83 2827.43

-------enter the id of a tank car whose volume you want to know:1111

-------tankcar 1111 not found


 

enter 1 to continue this query process; otherwise enter a 0:1

enter the id of a tank car whose volume you want to know:9999

-------tankcar 9999 not found


 

enter 1 to continue this query process; otherwise enter a 0:1

enter the id of a tank car whose volume you want to know:3333

-------the volume of car 3333 is 21237.15


 

enter 1 to continue this query process; otherwise enter a 0:1

enter the id of a tank car whose volume you want to know:4321

-------the volume of car 4321 is 15833.61


 

enter 1 to continue this query process; otherwise enter a 0:0


 

The file read by the program


 

1234 12 40 12000 a

4321 12 35 11000 a

5423 10 35 10000 b

3333 13 40 14000 c

1123 13 40 14500 c

9933 10 35 10000 b

8865 14 20 12000 d


 


 


 

No comments: