|
//file Flights004.cpp //date 12/08/2006 //author aou //csis250.tripod.com // Flights and Flights /////////////////////////////////////////////////////////// //includes /////////////////////////////////////////////////////////// #include <iostream> #include <ctime> using namespace std; const int TEST_COUNT = 5; const int MAX_CHARS = 32; const int MAX_COST = 99; char *testCities[] = {"MCI", "LAX", "NYC", "CHI", "JAX", "SJC", "STL", "TUL", "PIT"}; const int CITIES_COUNT = sizeof(testCities)/sizeof(testCities[3]); class CFlight { private: char orig[MAX_CHARS]; char dest[MAX_CHARS]; int cost; CFlight *next; public: CFlight(void); CFlight(char orig[], char dest[], int cost); CFlight(char ch); friend class CFlights; friend ostream & operator << (ostream & bob, const CFlight &flight); }; class CFlights { private: int count; CFlight *first; CFlight *last; public: CFlights(void); CFlights(int n); CFlights(char ch); bool add(char orig[], char dest[], int cost); CFlight * findFlight(char orig[], char dest[]); CFlights * findFlights(char orig[], char dest[]); CFlight * findCheapestFlight(char orig[], char dest[]); CFlights * findFlightsOrderedByCost(char orig[], char dest[]); friend ostream & operator << (ostream & bob, const CFlights &flights); }; void test_CFlight_constructorDefault(void); void test_CFlight_constructorSecond(void); void test_CFlight_constructorRand(void); void main(void) { srand((unsigned int) time(NULL)); test_CFlight_constructorRand(); //test_CFlight_constructorDefault(); //test_CFlight_constructorSecond(); //cout << CITIES_COUNT << endl; } void test_CFlight_constructorRand(void) { cout << "======================\n"; cout << "test_CFlight_constructorRand \n"; cout << "======================\n"; for (int i=1; i<=TEST_COUNT; i++) { CFlight myFlight('c'); cout << myFlight << endl; CFlight yourFlight('m'); cout << yourFlight << endl; cout << "----------------------\n"; } } CFlight::CFlight(char ch) { //creates a random flight this->cost = rand()%MAX_COST + 1; int intOrig, intDest; intOrig = rand()%CITIES_COUNT; do { intDest = rand()%CITIES_COUNT; } while (intOrig == intDest); strcpy(this->orig, testCities[intOrig]); strcpy(this->dest, testCities[intDest]); this->next = NULL; } void test_CFlight_constructorSecond(void) { cout << "======================\n"; cout << "test_CFlight_constructorSecond \n"; cout << "======================\n"; for (int i=1; i<=TEST_COUNT; i++) { CFlight myFlight("MCI", "LAX", 50); cout << myFlight << endl; cout << sizeof(myFlight) << endl; cout << sizeof(CFlight) << endl; CFlight yourFlight("LAX", "MCI", 90); cout << yourFlight << endl; cout << "----------------------\n"; } } CFlight::CFlight(char orig[], char dest[], int cost) { this->cost = cost; strcpy(this->orig, orig); strcpy(this->dest, dest); this->next = NULL; } void test_CFlight_constructorDefault(void) { cout << "======================\n"; cout << "test_CFlight_constructorDefault \n"; cout << "======================\n"; for (int i=1; i<=TEST_COUNT; i++) { CFlight myFlight; cout << myFlight << endl; CFlight yourFlight; cout << yourFlight << endl; cout << "----------------------\n"; } } CFlight::CFlight(void) { this->cost = -1; strcpy(this->orig, "??"); strcpy(this->dest, "??"); this->next = NULL; } ostream & operator << (ostream & bob, const CFlight &flight) { bob << "flight at " << &flight << "(" << flight.orig << ", " << flight.dest << ", " << flight.cost << ", " << flight.next << ")\n"; return bob; } |