C/C++
Программирование на C++
Опишите структуру Abiturient (ФИО; серия, номер паспорта; балл ЕГЭ по математике; балл ЕГЭ по русскому языку; балл ЕГЭ по обществознанию, направление подготовки). Организуйте поиск данных об абитуриенте по фамилии
#include <algorithm>
#include <iostream>
#include <string>
#include <set>
#include <sstream>
using namespace std;
string line(istream& inp, const char* msg) {
cout << msg;
string str;
getline(inp, str);
return str;
}
string word(istream& inp, const char* msg) {
cout << msg;
string str;
inp >> str;
inp.ignore(inp.rdbuf()->in_avail());
return str;
}
short grade(istream& inp, const char* msg, short max) {
short value = 0;
do {
cout << msg;
inp >> value;
inp.ignore(inp.rdbuf()->in_avail());
} while (value > max);
return value;
}
struct Abiturient {
short math;
short russian;
short social;
string name;
string passport;
string direction;
Abiturient() : math(0), russian(0), social(0) { }
int sum()const {
return math + russian + social;
}
string surname()const {
istringstream iss(name);
string word;
iss >> word;
return word;
}
private:
friend bool operator<(const Abiturient& a, const Abiturient& b) {
return a.name < b.name;
}
friend istream& operator>>(istream& inp, Abiturient& ab) {
ab.name = line(inp, "Ф. И. О.: ");
ab.passport = line(inp, "Серия и номер паспорта: ");
ab.direction = line(inp, "Направление подготовки: ");
ab.math = grade(inp, "Математика: ", 5);
ab.russian = grade(inp, "Русский язык: ", 5);
ab.social = grade(inp, "Обществознание: ", 5);
return inp;
}
friend ostream& operator<<(ostream& out, const Abiturient& ab) {
return out
<< "Ф. И. О.: " << ab.name << '\n'
<< "Серия и номер паспорта: " << ab.passport << '\n'
<< "Направление подготовки: " << ab.direction << '\n'
<< "Математика: " << ab.math << '\n'
<< "Русский язык: " << ab.russian << '\n'
<< "Обществознание: " << ab.social << '\n'
<< "Сумма баллов: " << ab.sum() << '\n';
}
};
int main() {
system("chcp 1251 > nul");
const auto length = 3;
set<Abiturient> list;
cout << "Введите данные на " << length << " абитуриентов: \n\n";
for (auto i = 0; i < length; ++i) {
Abiturient ab;
cin >> ab;
list.emplace(ab);
puts("");
}
auto surname = word(cin, "Фамилия: "); // Не Ф. И. О., а только фамилия
puts("");
auto target = [surname](const Abiturient& a) {
return a.surname() == surname;
};
auto pos = find_if(list.begin(), list.end(), target);
if (pos == list.end()) puts("Не найдено!");
else {
cout << *pos << '\n';
while ((pos = find_if(++pos, list.end(), target)) != list.end()) {
cout << *pos << '\n';
}
}
system("pause > nul");
}
#include <iostream>
#include <string>
#include <set>
#include <sstream>
using namespace std;
string line(istream& inp, const char* msg) {
cout << msg;
string str;
getline(inp, str);
return str;
}
string word(istream& inp, const char* msg) {
cout << msg;
string str;
inp >> str;
inp.ignore(inp.rdbuf()->in_avail());
return str;
}
short grade(istream& inp, const char* msg, short max) {
short value = 0;
do {
cout << msg;
inp >> value;
inp.ignore(inp.rdbuf()->in_avail());
} while (value > max);
return value;
}
struct Abiturient {
short math;
short russian;
short social;
string name;
string passport;
string direction;
Abiturient() : math(0), russian(0), social(0) { }
int sum()const {
return math + russian + social;
}
string surname()const {
istringstream iss(name);
string word;
iss >> word;
return word;
}
private:
friend bool operator<(const Abiturient& a, const Abiturient& b) {
return a.name < b.name;
}
friend istream& operator>>(istream& inp, Abiturient& ab) {
ab.name = line(inp, "Ф. И. О.: ");
ab.passport = line(inp, "Серия и номер паспорта: ");
ab.direction = line(inp, "Направление подготовки: ");
ab.math = grade(inp, "Математика: ", 5);
ab.russian = grade(inp, "Русский язык: ", 5);
ab.social = grade(inp, "Обществознание: ", 5);
return inp;
}
friend ostream& operator<<(ostream& out, const Abiturient& ab) {
return out
<< "Ф. И. О.: " << ab.name << '\n'
<< "Серия и номер паспорта: " << ab.passport << '\n'
<< "Направление подготовки: " << ab.direction << '\n'
<< "Математика: " << ab.math << '\n'
<< "Русский язык: " << ab.russian << '\n'
<< "Обществознание: " << ab.social << '\n'
<< "Сумма баллов: " << ab.sum() << '\n';
}
};
int main() {
system("chcp 1251 > nul");
const auto length = 3;
set<Abiturient> list;
cout << "Введите данные на " << length << " абитуриентов: \n\n";
for (auto i = 0; i < length; ++i) {
Abiturient ab;
cin >> ab;
list.emplace(ab);
puts("");
}
auto surname = word(cin, "Фамилия: "); // Не Ф. И. О., а только фамилия
puts("");
auto target = [surname](const Abiturient& a) {
return a.surname() == surname;
};
auto pos = find_if(list.begin(), list.end(), target);
if (pos == list.end()) puts("Не найдено!");
else {
cout << *pos << '\n';
while ((pos = find_if(++pos, list.end(), target)) != list.end()) {
cout << *pos << '\n';
}
}
system("pause > nul");
}
Похожие вопросы
- Программирование на C++
- Программирование на C++
- Помоги написать лабу по программированию на c++
- Помогите решить задачу по программированию на C++
- Программирование на C. Помогите бездарю
- На каком языке программирования написан C (Си)?
- Программирование на c++
- Программирование на C++
- Помогите с программированием на C++
- Задание по программированию язык C/СИ