كود:
#include<iostream>
#include<fstream>
#include <cstring>
using namespace std;
class Vehicle {
public:
Vehicle(); // constructor
Vehicle( const Vehicle& );
char* Name;
int NumOfSeat;
int EngSize;
int NumOfWh;
char *getName ();
void setVehicle( char* name, int seats, int engsize, int wheels);
};
class VehicleList{
private:
Vehicle Vecarray[20];
int count;
public:
VehicleList(); // constructor
~VehicleList(); // destructor
// gets the first element in the list
Vehicle *getFirst() const;
// gets the last element in the list
Vehicle *getLast() const;
// sets the first element in the list
bool setFirst( Vehicle *input_first );
// sets the last element in the list
bool setLast (Vehicle *input_last );
// adds a vehicle in SORTED order (by name)
bool addVehicle ( Vehicle* );
// finds a vehicle by name
Vehicle *findVehicle( char* input_name );
// deletes the first vehicle matching that name
bool deleteVehicle( char* input_name );
// prints all vehicle's information in order
void print() const;
// prints all vehicle's information in reverse order
void printReverse() const;
// funtciton that sort an array with bubble sort algorithm
void sort( int Vecarray[], int count )
{
int hold;
for ( int pass = 1; pass < count-1; pass++ )
for ( int j = 0; j < count-1; j++ )
if ( Vecarray[j] > Vecarray[j+1]) {
hold = Vecarray[j];
Vecarray[j] = Vecarray[j+1];
Vecarray[j+1] = hold;
}
}
};
int main()
{
char ch;
VehicleList vehicle;
Vehicle v;
char *Name = new char[150];
int NumOfSeat;
int EngSize;
int NumOfWh;
int i;
do{
cout << "\n\n";
cout << "\t1.Add vehicle.\n";
cout << "\t2.delete vehicle.\n";
cout << "\t3.find vehicle.\n";
cout << "\t4.print vehicle.\n";
cout << "\t5.printReverse vehicle.\n";
cout << "\t6.Exit.\n";
cout << "\t?";
cin >> ch;
switch (ch) {
case '1':
cout << "\nENTER THE VEHICLE NAME: ";
cin >> v.Name;
cout <<"\t THE NUMOFSEAT IS: ";
cin >> v.NumOfSeat;
cout << "\t THE ENGSIZE IS: ";
cin >> v.EngSize;
cout << endl << "\t THE NUMOFWH IS: ";
cin >> v.NumOfWh;
vehicle.addVehicle( &v );
break;
case '2':
cout << "\nENTER THE VEHICLE NAME: ";
cin >> Name;
if(!vehicle.deleteVehicle( Name ))
cout<<"THERE IS NO VEHICLE";
else
cout<<"THE VEHICLE WILL BE DELETED:\n";
break;
case '3':
cout << "\nENTER THE VEHICLE NAME: ";
cin >> Name;
v = *(vehicle.findVehicle(Name));
cout << endl <<"\t THE VEHICLE NAME IS: "<< v.Name;
cout << endl <<"\t THE NUMOFSEAT IS: "<< v.NumOfSeat;
cout << endl << "\t THE ENGSIZE IS: " << v.EngSize;
cout << endl << "\t THE NUMOFWH IS: " << v.NumOfWh;
break;
case '4':
vehicle.print();
break;
case '5':
vehicle.printReverse();
break;
}
} while( ch!='6' );
return 0;
}
//constructor
Vehicle::Vehicle()
{
Name = new char[150];
for ( int i = 0 ;i < 150;i++ )
Name = NULL;
NumOfSeat = 0;
EngSize = 0;
NumOfWh = 0; // initialize variables
}
// copy constructor
Vehicle::Vehicle( const Vehicle&v )
{
for ( int i = 0 ;i < 150;i++ )
Name[i] = v.Name[i];
NumOfSeat = v.NumOfSeat;
EngSize = v.EngSize;
NumOfWh = v.NumOfWh;
}
void Vehicle::setVehicle( char* name, int seats, int engsize, int wheels)
{
int j;
if (((int)strlen (name) > 150))
j = 150;
else
j = ( int )strlen( name );
for ( int i= 0;i < j;i++ )
Name[i] = name[i];
if(seats > 0 && seats < 100)
NumOfSeat = seats;
if( engsize > 0 && engsize < 10000)
EngSize = engsize;
if( wheels > 0 && wheels < 20 )
NumOfWh = wheels;
}
char *Vehicle::getName()
{
return Name;
}
// TODO: Messed up
int getNumOfVh()
{
}
// constructor
VehicleList::VehicleList()
{
count = 0;
int swap;
Vehicle v;
ifstream file;
file.open( "vehicle.txt" );
while ( ! file.eof() ){
file.getline( Vecarray[count].Name,150,'\n' );
file >> Vecarray[count].NumOfSeat;
file.seekg( 4,ios::cur );
count++;
}
sort( Vecarray[],count );
file.close();
}
VehicleList::VehicleList( const Vehicle& v )
{
for( int i = 0;i < v.count;i++ )
Vecarray[i]=v.Vecarray[i];
count = v.count;
}
VehicleList::~VehicleList()
{
delete [] Vecarray;
}
// to get first element from list
Vehicle *getFirst() const
{
return &Vecarray[ 0 ];
}
// to get last element from list
Vehicle *getLast() const
{
return &Vecarray[ count ];
}
// to set first elemnet from the list
bool setFirst( Vehicle *input_first )
{
Vecarray[0] = *input_first;
return true;
}
// to set last elemnet from the list
bool setLast (Vehilce *input_last )
{
Vecarray[count] = *input_last;
return true;
}
bool addVehicle ( Vehicle* n)
{
Vecarray[count-1] = n;
sort( Vecarray[]);
return true;
}
// search to any vehicle
Vehicle *findVehicle( char* input_name )
{
bool found = false;
int swap;
for(int i = 0;i < count-1;i++){
swap = strcmp( Vecarray[i].Name,Name );
if ( swap == 0 ){
found = true;
return Vecarray[i];
}
}
if( found == false )
cout << " THE RECORD NOT FOUND" << endl;
}
bool deleteVehicle( char* input_name )
{
bool deleted = false;
int swap;
for( int i = 0;i < count;i++) {
swap = strcmp( Vecarray[i].input_name,input_name ) ;
if (swap == 0 ){
for(int j = i;j < count;j++)
Vecarray[j] = Vecarray[j+1];
count--;
deleted = true;
break;
}
}
if(deleted == false )
cout<< " THE RECORD NOT FOUND"<<endl;
return deleted;
}
// print all vehicles
void Vehicle::print() const
{
for ( int i = 0 ; i < count-1 ; i++){
cout << endl << endl <<"THE PATIENT NUMBER "
<< i << " IS: " << endl;
cout << endl << "\t THE VEHICLE NAME IS: "<< Vecarray[i].Name;
cout << endl << "\t THE NUMOFSEAT IS: "<< Vecarray[i].NumOfSeat << endl;
cout << endl << "\t THE ENGSIZE IS: " << Vecarray[i].EngSize;
cout << endl << "\t THE NUMOFWH IS: " << Vecarray[i].NumOfWh;
}
}
// to print all information of vehicle's in reverse order
void printReverse() const
{
for ( int i = 20; i > 0; i-- )
cout << Vecarray[ i ] << endl;
}