C/C++
Помогите с задачей на c++
Определить комбинированный (структурный) тип для представления анкеты студента, состоящей из его фамилии, дня рождения и пола. «День рождения» состоит из полей: «число», «месяц», «год». Ввести информацию по 25 студентам из группы. Вывести фамилию самого старшего мальчика из группы
struct myRecord {
string Family;
struct { int day; int month, int year; } Date;
int Sex;
}
string Family;
struct { int day; int month, int year; } Date;
int Sex;
}
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
void flush(istream& inp = cin) {
inp.ignore(numeric_limits<streamsize>::max(), '\n');
}
struct Date {
int day;
int month;
int year;
Date() : day(1), month(1), year(1900) {}
Date(const int d, const int m, const int y) : day(d), month(m), year(y) {}
string sdate()const {
string s = day < 10 ? "0" : "";
s += to_string(day) + "." + (month < 10 ? "0" : "");
s += to_string(month) + "." + to_string(year);
return s;
}
private:
friend bool operator<(const Date& a, const Date& b) {
if (a.year < b.year) return true;
if (a.year == b.year) {
if (a.month < b.month) return true;
if (a.month == b.month) return a.day < b.day;
}
return false;
}
friend ostream& operator<<(ostream& out, const Date& d) {
return out << d.sdate();
}
};
struct Student {
char sex;
Date date;
string name;
private:
friend bool operator<(const Student& a, const Student& b) {
return a.date < b.date;
}
friend ostream& operator<<(ostream& out, const Student& s) {
return out << s.name << ' ' << s.date << ' ' << s.sex;
}
};
Date input_date(const string& msg = "") {
Date date;
if (!msg.empty()) cout << msg << '\n';
cout << "Число [1-31]: ";
cin >> date.day;
flush();
cout << "Месяц [1-12]: ";
cin >> date.month;
flush();
cout << "Год рождения: ";
cin >> date.year;
flush();
return date;
}
Student input_student() {
Student student;
cout << "Фамилия студента: ";
cin >> student.name;
flush();
student.date = input_date("Введите дату рождения:");
cout << "Укажите пол [м, ж]: ";
student.sex = getchar();
flush();
return student;
}
int main() {
system("chcp 1251 > nul");
const auto length = 25U; // Стоит ли ради этого вводить данные для 25 человек? Хватило бы и трёх...
Student group[length];
for (auto i = 0U; i < length; ++i) group[i] = input_student();
auto young = *max_element(begin(group), end(group));
cout << "\nСамый молодой (-ая): " << young << '\n';
system("pause > nul");
}
#include <iostream>
#include <string>
using namespace std;
void flush(istream& inp = cin) {
inp.ignore(numeric_limits<streamsize>::max(), '\n');
}
struct Date {
int day;
int month;
int year;
Date() : day(1), month(1), year(1900) {}
Date(const int d, const int m, const int y) : day(d), month(m), year(y) {}
string sdate()const {
string s = day < 10 ? "0" : "";
s += to_string(day) + "." + (month < 10 ? "0" : "");
s += to_string(month) + "." + to_string(year);
return s;
}
private:
friend bool operator<(const Date& a, const Date& b) {
if (a.year < b.year) return true;
if (a.year == b.year) {
if (a.month < b.month) return true;
if (a.month == b.month) return a.day < b.day;
}
return false;
}
friend ostream& operator<<(ostream& out, const Date& d) {
return out << d.sdate();
}
};
struct Student {
char sex;
Date date;
string name;
private:
friend bool operator<(const Student& a, const Student& b) {
return a.date < b.date;
}
friend ostream& operator<<(ostream& out, const Student& s) {
return out << s.name << ' ' << s.date << ' ' << s.sex;
}
};
Date input_date(const string& msg = "") {
Date date;
if (!msg.empty()) cout << msg << '\n';
cout << "Число [1-31]: ";
cin >> date.day;
flush();
cout << "Месяц [1-12]: ";
cin >> date.month;
flush();
cout << "Год рождения: ";
cin >> date.year;
flush();
return date;
}
Student input_student() {
Student student;
cout << "Фамилия студента: ";
cin >> student.name;
flush();
student.date = input_date("Введите дату рождения:");
cout << "Укажите пол [м, ж]: ";
student.sex = getchar();
flush();
return student;
}
int main() {
system("chcp 1251 > nul");
const auto length = 25U; // Стоит ли ради этого вводить данные для 25 человек? Хватило бы и трёх...
Student group[length];
for (auto i = 0U; i < length; ++i) group[i] = input_student();
auto young = *max_element(begin(group), end(group));
cout << "\nСамый молодой (-ая): " << young << '\n';
system("pause > nul");
}
Похожие вопросы
- ПОМОГИТЕ С ЗАДАЧЕЙ НА C++
- Помогите решить задачу по C++!
- Помогите решить задачу на C++.
- Помогите с задачей по C++
- Помогите решить задачу на C++
- Помогите решить задачу на c++
- Помогите решить задачу на C++
- Помогите с задачей по C++
- Помогите с задачей в C++ пожалуйста. Какое условие правильно написать в Z чтобы выводилось сообщение "error!" ?
- Помогите решить задачу на C++