1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
const int MAX_FLIGHTS = 100;
struct Flight {
string from;
string to;
string seatClass;
double price;
int flightIndex;
};
int readTickets(ifstream &, int &, Flight);
void printTickets(Flight, int);
void printTicketsFromTo(Flight, int);
int findCheapestTicket(Flight, int);
int main()
{
Flight flights[MAX_FLIGHTS];
int nFlights = 0;
int userChoice = 0;
string fromChoice,
toChoice;
ifstream fin;
fin.open("tickets.txt");
if (!fin){
cout << "The file could not open." << endl;
return 1;
}
readTickets (&fin, &nFlights, flights[0]);
cout << "Select the action: " << endl;
cout << "1) Display all flights" << endl;
cout << "2) Show the flights that depart from a given city" << endl;
cout << "3) Find the cheapest ticket" << endl;
cout << "4) Exit the program" << endl;
cin >> userChoice;
while (userChoice != 4)
if (userChoice >= 1 || userChoice <= 3){
switch(userChoice){
case 1 :
printTickets(flights, nFlights);
cout << "Select the action: " << endl;
cout << "1) Display all flights" << endl;
cout << "2) Show the flights that depart from a given city" << endl;
cout << "3) Find the cheapest ticket" << endl;
cout << "4) Exit the program" << endl;
cin >> userChoice;
break;
case 2 :
printTicketsFromTo(flights, nFlights);
cout << "Select the action: " << endl;
cout << "1) Display all flights" << endl;
cout << "2) Show the flights that depart from a given city" << endl;
cout << "3) Find the cheapest ticket" << endl;
cout << "4) Exit the program" << endl;
cin >> userChoice;
break;
case 3 :
findCheapestTicket(flights, nFlights);
cout << "Select the action: " << endl;
cout << "1) Display all flights" << endl;
cout << "2) Show the flights that depart from a given city" << endl;
cout << "3) Find the cheapest ticket" << endl;
cout << "4) Exit the program" << endl;
cin >> userChoice;
break;
default :
cout << "Your decision was not present within the confines of the presented choices." << endl;
cout << "Select the action: " << endl;
cout << "1) Display all flights" << endl;
cout << "2) Show the flights that depart from a given city" << endl;
cout << "3) Find the cheapest ticket" << endl;
cout << "4) Exit the program" << endl;
cin >> userChoice;
}
}
else
cout << "Exiting the program";
return 0;
}
int readTickets(ifstream &fin, int &nFlights, Flight flights[0]){
while(getline(fin, flights[nFlights].from, '\n') &&
getline(fin, flights[nFlights].to, '\n') &&
fin >> flights[nFlights].seatClass >> ws &&
fin >> flights[nFlights].price >> ws) {
flights[nFlights].flightIndex = nFlights;
nFlights++;
}
return nFlights;
}
void printTickets(Flight flights[0], int maxflights){
int currentFlights = 0;
cout << "From To Seat type Price " << endl;
while (currentFlights < maxflights) {
cout << setw(18) << flights[currentFlights].from << flights[currentFlights].to
<< setw(11) << flights[currentFlights].seatClass << setw(6) << flights[currentFlights].price << endl;
currentFlights++;
}
}
void printTicketsFromTo(Flight flights[0], int maxflights){
string fromCity,
toCity;
int currentFlights = 0;
cout << "Enter departure city: ";
cin >> fromCity;
cout << "Enter arrival city: ";
cin >> toCity;
cout << "From To Seat type Price " << endl;
while (currentFlights < maxflights) {
if (fromCity == flights[currentFlights].from && toCity == flights[currentFlights].to) {
cout << setw(18) << flights[currentFlights].from << flights[currentFlights].to
<< setw(11) << flights[currentFlights].seatClass << setw(6) << flights[currentFlights].price << endl;
}
currentFlights++;
}
cout << "-------------------------------------------";
}
void findCheapestTicket(Flight flights[0], int maxflights){
bool swap;
int temp;
do{
swap = false;
for (int i = 0; i < maxflights; i++) {
if (flights[i].price > flights[i + 1].price) {
temp = flights[i].flightIndex;
flights[i].flightIndex = flights[i + 1].flightIndex;
flights[i + 1].flightIndex = temp;
swap = true;
}
}
} while (swap);
}
|