Наконец-то дошли руки.
| Код | #include <iostream> #include <algorithm> #include <vector> #include <string>
struct date { int day; int month; int year; bool operator <= (const date& obj) const { return ( (day <= obj.day) && (month <= obj.month) && (year <= obj.year) ); } bool operator >= (const date& obj) const { return !(*this <= obj); } date (int d = 0, int m = 0, int y = 0):day(d), month(m), year(y) {} date(const date& obj):day(obj.day),month(obj.month),year(obj.year){} };
class request { private: std::string m_destination; std::string m_name; friend std::ostream& operator << (std::ostream& stream, const request& obj); friend std::istream& operator >> (std::istream& stream, request& obj); public: date m_fdate; int m_flight; request(const std::string& destination, int flight, const std::string name, const date& fdate):m_destination(destination),m_flight(flight),m_name(name),m_fdate(fdate){} request():m_destination("N/A"), m_flight(-1), m_name("N/A"), m_fdate(0,0,0){} };
std::ostream& operator << (std::ostream& stream, const request& obj) { stream << "Name: " << obj.m_name << "\nDestination: " << obj.m_destination << "\nFlight: " << obj.m_flight << std::endl; stream << "Date: " << obj.m_fdate.day << ' ' << obj.m_fdate.month << ' ' << obj.m_fdate.year; return stream; }
std::istream& operator >> (std::istream& stream, request& obj) { std::cout << "Enter name: "; stream >> obj.m_name; std::cout << "Enter destination: "; stream >> obj.m_destination; std::cout << "Enter flight: "; stream >> obj.m_flight; std::cout << "Enter day: "; stream >> obj.m_fdate.day; std::cout << "Enter month: "; stream >> obj.m_fdate.month; std::cout << "Enter year: "; stream >> obj.m_fdate.year; return stream; }
std::istream& operator >> (std::istream& stream, date& obj) { std::cout << "Enter day: "; stream >> obj.day; std::cout << "Enter month: "; stream >> obj.month; std::cout << "Enter year: "; stream >> obj.year; return stream; }
class find_flight { private: int m_flight; date m_start_date; date m_end_date; public: bool operator() (const request& obj) const { if ( (obj.m_fdate >= m_start_date) && (obj.m_fdate <= m_end_date) && (obj.m_flight == m_flight) ) return true; return false; } find_flight(int flight, const date& start_date, const date& end_date):m_flight(flight), m_start_date(start_date), m_end_date(end_date){} };
template <typename PRED, typename InIter, typename OutIter> void copy_if(InIter FIRST, InIter LAST, OutIter OUT, PRED Cond) { for ( ; FIRST != LAST; ++FIRST) if ( Cond(*FIRST) ) OUT = *FIRST; }
int main(int argc, char* argv[]) { std::vector<request> arr; char choice = '\0'; while ( (choice != 'n') && (choice != 'N') ) { request buf; std::cin >> buf; arr.push_back( buf ); std::cout << "One more request to add? y/n"; std::cin >> choice; } //Показать все рейсы std::copy( arr.begin(), arr.end(), std::ostream_iterator<request>(std::cout, "\n") ); //Сбор данных о искомых рейсах date start_date; date end_date; int flight = 0; std::cout << "Enter start date\n"; std::cin >> start_date; std::cout << "Enter end date\n"; std::cin >> end_date; std::cout << "Enter flight: "; std::cin >> flight; //Поиск рейса по критерию и вывод на экран copy_if( arr.begin(), arr.end(), std::ostream_iterator<request>(std::cout, "\n"), find_flight(flight, start_date, end_date) ); std::cin.sync(); std::cin.get(); return 0; }
|
для тех кто боится std::
| Код | #include <iostream> #include <algorithm> #include <vector> #include <string>
using std::ostream; using std::istream; using std::cout; using std::cin; using std::endl; using std::string; using std::vector; using std::ostream_iterator;
struct date { int day; int month; int year; bool operator <= (const date& obj) const { return ( (day <= obj.day) && (month <= obj.month) && (year <= obj.year) ); } bool operator >= (const date& obj) const { return !(*this <= obj); } date (int d = 0, int m = 0, int y = 0):day(d), month(m), year(y) {} date(const date& obj):day(obj.day),month(obj.month),year(obj.year){} };
class request { private: string m_destination; string m_name; friend ostream& operator << (ostream& stream, const request& obj); friend istream& operator >> (istream& stream, request& obj); public: int m_flight; date m_fdate; request(const string& destination, int flight, const string name, const date& fdate):m_destination(destination),m_flight(flight),m_name(name),m_fdate(fdate){} request():m_destination("N/A"), m_flight(-1), m_name("N/A"), m_fdate(0,0,0){} };
ostream& operator << (ostream& stream, const request& obj) { stream << "Name: " << obj.m_name << "\nDestination: " << obj.m_destination << "\nFlight: " << obj.m_flight << endl; stream << "Date: " << obj.m_fdate.day << ' ' << obj.m_fdate.month << ' ' << obj.m_fdate.year; return stream; }
istream& operator >> (istream& stream, request& obj) { cout << "Enter name: "; stream >> obj.m_name; cout << "Enter destination: "; stream >> obj.m_destination; cout << "Enter flight: "; stream >> obj.m_flight; cout << "Enter day: "; stream >> obj.m_fdate.day; cout << "Enter month: "; stream >> obj.m_fdate.month; cout << "Enter year: "; stream >> obj.m_fdate.year; return stream; }
istream& operator >> (istream& stream, date& obj) { cout << "Enter day: "; stream >> obj.day; cout << "Enter month: "; stream >> obj.month; cout << "Enter year: "; stream >> obj.year; return stream; }
class find_flight { private: int m_flight; date m_start_date; date m_end_date; public: bool operator() (const request& obj) const { if ( (obj.m_fdate >= m_start_date) && (obj.m_fdate <= m_end_date) && (obj.m_flight == m_flight) ) return true; return false; } find_flight(int flight, const date& start_date, const date& end_date):m_flight(flight), m_start_date(start_date), m_end_date(end_date){} };
template <typename PRED, typename InIter, typename OutIter> void copy_if(InIter FIRST, InIter LAST, OutIter OUT, PRED Cond) { for ( ; FIRST != LAST; ++FIRST) if ( Cond(*FIRST) ) OUT = *FIRST; }
int main(int argc, char* argv[]) { vector<request> arr; char choice = '\0'; while ( (choice != 'n') && (choice != 'N') ) { request buf; cin >> buf; arr.push_back( buf ); cout << "One more request to add? y/n"; cin >> choice; } //Показать все рейсы copy( arr.begin(), arr.end(), ostream_iterator<request>(cout, "\n") ); //Сбор данных о искомых рейсах date start_date; date end_date; int flight = 0; cout << "Enter start date\n"; cin >> start_date; cout << "Enter end date\n"; cin >> end_date; cout << "Enter flight: "; cin >> flight; //Поиск рейса по критерию и вывод на экран copy_if( arr.begin(), arr.end(), ostream_iterator<request>(cout, "\n"), find_flight(flight, start_date, end_date) ); cin.sync(); cin.get(); return 0; }
|
Я для простоты разинкапсулировал два поля ... времени особо не было, чтобы подправить, так как вспомнил я об этом в самый послдний момент  |