| Код | #include <iostream> #include <iomanip> #include <string> #include <vector> #include <ctime>
using namespace std;
struct TFlight { unsigned number; string type; string destination; time_t time; }; //----------------------------------------------// const char* const GetTime(time_t theTime) { tm* t = gmtime(&theTime);
static char str[16]; strftime(str, sizeof(str), "%d.%m.%y %H:%M", t); return str; } //----------------------------------------------// TFlight GetFlight() { static unsigned number = 1;
const char* CType[] = {"boing", "airbus", "sukhoi", "tu"}; const char* CDest[] = {"Novosibirsk", "Irkutsk", "Krasnoyarsk", "Chita"}; TFlight flight;
flight.number = number++; flight.type = CType[rand() % (sizeof(CType)/sizeof(*CType))]; flight.destination = CDest[rand() % (sizeof(CDest)/sizeof(*CDest))]; flight.time = time(NULL) + rand() % 100 * 1000; return flight; } //----------------------------------------------// void Sort(vector<TFlight>& flight) { for (unsigned i = 0; i < flight.size()-1; ++i) { unsigned min = i; for (unsigned j = i+1; j < flight.size(); ++j) { if (flight[j].time < flight[min].time) { min = j; } } TFlight buff = flight[i]; flight[i] = flight[min]; flight[min] = buff; } } //----------------------------------------------//
int main() { srand(time(NULL));
vector<TFlight> flight; for (unsigned i = 0; i < 10; ++i) { flight.push_back(GetFlight()); } Sort(flight); for (unsigned i = 0; i < flight.size(); ++i) { cout << setw(4) << flight[i].number << setw(10) << flight[i].type << setw(15) << flight[i].destination << setw(17) << GetTime(flight[i].time) << endl; } return 0; }
|
http://codepad.org/Uq9n9BiQ
Бинарник с исходником ниже. |