include include include include using namespace std include

#include
#include
#include
#include
using namespace std;
#include

struct vehicle
{
int year;
string make;
string model;
};

class BinSearch{

string fileName;
vector veh;

public:

BinSearch(string& fn){ fileName.assign(fn);

}

void binSearchRec(int year);
void binRecSearch(int year,int first,int last);
void binSearchIter(int year);

void readFile();
void print();
void sort();

};

void BinSearch::readFile()
{
vehicle v;

std::fstream ifs;
ifs.open(this->fileName.c_str());

if(!ifs.is_open()){
   cout<<\"Error in opening file\"<    return;
}

string str;

while(!ifs.eof()){

  
   ifs>>str;

   if(str!=\"|\")
   {
       v.year=atoi(str.c_str());
       ifs>>v.make;
       ifs>>v.model;
       this->veh.push_back(v);
   }
}

ifs.close();


}

void BinSearch::sort()
{

int size=this->veh.size();

vehicle tmp;
for(int i=0;i {
   for(int j=0;j    {
       if(this->veh[j].year > this->veh[j+1].year)
       {
           tmp.year=this->veh[j].year;
           tmp.make=this->veh[j].make;
           tmp.model=this->veh[j].model;

           this->veh[j].year=this->veh[j+1].year;
           this->veh[j].make=this->veh[j+1].make;
           this->veh[j].model=this->veh[j+1].model;
          
          
           this->veh[j+1].year=tmp.year;
           this->veh[j+1].make=tmp.make;
           this->veh[j+1].model=tmp.model;
       }
   }
}

}
void BinSearch::print(){

for(vector::iterator iter=this->veh.begin();iter!=this->veh.end();iter++)
{
   cout<<\"Year:\"<year<    cout<<\"Make:\"<make<    cout<<\"Model:\"<model<    cout<<\"*******************************\"< }


}

void BinSearch::binSearchIter(int year){

int size=this->veh.size();
int first=0;
int last=size;
int middle=(first+last)/2;

cout<<\"Middle:\"<

while(first<=last){

   if(year>this->veh[middle].year)
   {
       first=middle;
   }
   else if(yearveh[middle].year)
   {
       last=middle;
   }
   else if(year==this->veh[middle].year)
   {
       cout<<\"Year found\"<        cout<<\"Year:\"<        cout<<\"Make:\"<veh[middle].make<        cout<<\"Model:\"<veh[middle].model<       return;  
   }
   middle=(first+last)/2;
}

if(first>last) cout<<\"Not found\"<   
}

void BinSearch::binSearchRec(int year)
{

int first=0;
int last=this->veh.size();

binRecSearch(year,first,last);
}

void BinSearch::binRecSearch(int year,int first,int last)
{

int middle=(first+last)/2;

if(this->veh[middle].year==year) {
       cout<<\"Year found\"<        cout<<\"Year:\"<        cout<<\"Make:\"<veh[middle].make<        cout<<\"Model:\"<veh[middle].model<    }
else if(year < this->veh[middle].year)
{
   binRecSearch(year,first,middle);
}
else if(year > this->veh[middle].year)
   binRecSearch(year,middle,last);
else
{
   cout<<\"Element not found\"<    return;
}
}

int main(void)
{

string fn(\"vehicle.txt\");
BinSearch bin(fn);

bin.readFile();
//bin.print();
bin.sort();
/*cout<<\"====================================\"< cout<<\"After Sort:\"< cout<<\"====================================\"< bin.print();
*/
bin.print();
bin.binSearchIter(2012);
bin.binSearchRec(2012);
return 0;
}

Can someone help to comments on each line of code on understanding way?

Solution

#include<iostream> //Library file
#include
#include<fstream> //Library file
using namespace std; // name space definition
#include<iomanip> //Library file
struct vehicle // structure declaration
{
int year;
string make;
string model;
};
class BinSearch{ // Binary search class declaration
string fileName;
vector veh;
public:
BinSearch(string& fn){ fileName.assign(fn); //constructor
}
void binSearchRec(int year); // Recursive binary search prototype
void binRecSearch(int year,int first,int last); //Record search prototype
void binSearchIter(int year); // Iterative binary search prototype
void readFile(); // Read file function
void print(); // Print function
void sort(); //Sort function

};
void BinSearch::readFile() // Accessing of function readFile()
{
vehicle v;
std::fstream ifs;   
ifs.open(this->fileName.c_str()); // File opened to read

if(!ifs.is_open()){ // checks for Error in file opening
cout<<\"Error in opening file\"< return;
}
string str;   
while(!ifs.eof()){ // checks for end of file
  
ifs>>str; // Extracts data from file
if(str!=\"|\") // Checks for the presence of operator “|” in file
{
v.year=atoi(str.c_str()); // function Converts the string to integer
ifs>>v.make; // Extracts data from file
ifs>>v.model; // Extracts data from file
this->veh.push_back(v); //resolves ambiguity
}
}
ifs.close();

}
void BinSearch::sort() //Accessing the function sort to sort the file
{
int size=this->veh.size(); // using this pointer to pass data
vehicle tmp;
for(int i=0;i {
for(int j=0;j   
if(this->veh[j].year > this->veh[j+1].year) //checking for the year of vehicle before sorting
{
tmp.year=this->veh[j].year; //using this pointer to sort the data
tmp.make=this->veh[j].make;
tmp.model=this->veh[j].model;
this->veh[j].year=this->veh[j+1].year;
this->veh[j].make=this->veh[j+1].make;
this->veh[j].model=this->veh[j+1].model;
  
  
this->veh[j+1].year=tmp.year;
this->veh[j+1].make=tmp.make;
this->veh[j+1].model=tmp.model;
}
}
}
}
void BinSearch::print(){ // Accessing the print function
for(vector::iterator iter=this->veh.begin();iter!=this->veh.end();iter++) // vector is used when file size varies and to get file size
{
cout<<\"Year:\"<year< cout<<\"Make:\"<make< cout<<\"Model:\"<model< cout<<\"*******************************\"< } // print the vehicle details

}
void BinSearch::binSearchIter(int year){ // Access the iterative binary search function
int size=this->veh.size();
int first=0;
int last=size;
int middle=(first+last)/2;
cout<<\"Middle:\"<
while(first<=last){
if(year>this->veh[middle].year) //Search by year
{
first=middle;
}
else if(yearveh[middle].year)
{
last=middle;
}
else if(year==this->veh[middle].year)
{
cout<<\"Year found\"< cout<<\"Year:\"< cout<<\"Make:\"<veh[middle].make< cout<<\"Model:\"<veh[middle].model< //Print model and make of year searched
return;
}
middle=(first+last)/2;
}
if(first>last) cout<<\"Not found\"< // Print if record not found
}
void BinSearch::binSearchRec(int year) // Access the recursive binary search function
{
int first=0;
int last=this->veh.size();
binRecSearch(year,first,last);
}
void BinSearch::binRecSearch(int year,int first,int last)
{ // Access the recursive binary search function

int middle=(first+last)/2;
if(this->veh[middle].year==year) { // search by year of vehicla
cout<<\"Year found\"< cout<<\"Year:\"< // print if vehicle found cout<<\"Make:\"<veh[middle].make< cout<<\"Model:\"<veh[middle].model< }
else if(year < this->veh[middle].year)
{
binRecSearch(year,first,middle);
}
else if(year > this->veh[middle].year)
binRecSearch(year,middle,last);
else
{
cout<<\"Element not found\"< return; //print if vehicle not found
}
}

int main(void) //main function
{
string fn(\"vehicle.txt\"); // access the text file
BinSearch bin(fn); //call binary search function
bin.readFile(); //call function to read file
//bin.print(); //call the function to print
bin.sort(); // call the function to sort details
/*cout<<\"====================================\"< cout<<\"After Sort:\"< cout<<\"====================================\"< bin.print();
*/
bin.print(); // call the function to print sorted file
bin.binSearchIter(2012); //call the function to search iteratively by year
bin.binSearchRec(2012); //call the function to search recursively by year
return 0;
}

#include #include #include #include using namespace std; #include struct vehicle { int year; string make; string model; }; class BinSearch{ string fileName; vec
#include #include #include #include using namespace std; #include struct vehicle { int year; string make; string model; }; class BinSearch{ string fileName; vec
#include #include #include #include using namespace std; #include struct vehicle { int year; string make; string model; }; class BinSearch{ string fileName; vec
#include #include #include #include using namespace std; #include struct vehicle { int year; string make; string model; }; class BinSearch{ string fileName; vec
#include #include #include #include using namespace std; #include struct vehicle { int year; string make; string model; }; class BinSearch{ string fileName; vec
#include #include #include #include using namespace std; #include struct vehicle { int year; string make; string model; }; class BinSearch{ string fileName; vec

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site