Имеются сведения о результатах зимней сессии группы студентов, в которой учится 30
человек. Информация вводится построчно.
Пример входной строки:
21) Соколов экзамены : 4 3 4 4 3 ; зачеты : + + + + + – + +
Здесь первые две цифры 21) – двухзначное число, являющееся
идентификационным номером первокурсника, далее результаты
5 экзаменов (оценки от 2 до 5), затем результаты зачетов («+»
означает, что зачет сдан, «–» не сдан).
Оценки за экзамены и результаты сдачи зачетов разделяются во
входной строке одним пробелом.
Отличником считается студент, у которого сданы все зачеты и по
всем экзаменам получена оценка 5.
У неуспевающего студента не сдан хотя бы один зачет или хотя
бы один экзамен.
Составить программу, которая подсчитывает количество отличников и количество неуспевающих студентов.
C/C++
Помогите решить на C++
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <cctype>
using namespace std;
struct Student {
int id;
vector<int> exams;
vector<char> tests;
string name;
Student() : id(0) {}
bool parse(const string& query) {
auto i = 0U;
auto n = query.length();
int id = 0;
while ((query[i] > 0 && isdigit(query[i])) && i < n) {
id *= 10;
id += query[i] - 48;
++i;
}
string name;
while ((query[i] != ' ') && i < n) ++i;
while ((query[++i] != ' ') && i < n) name += query[i];
while ((query[i] < 0 || !isdigit(query[i])) && i < n) ++i;
vector<int> exams;
while ((query[i] > 0 && isdigit(query[i]) || isspace(query[i])) && i < n) {
if (isdigit(query[i])) exams.push_back(query[i] - 48);
++i;
}
while ((query[i] != '+' && query[i] != '-') && i < n) ++i;
vector<char> tests;
while ((query[i] == '+' || query[i] == '-' || isspace(query[i])) && i < n) {
if (query[i] == '+' || query[i] == '-') tests.push_back(query[i]);
++i;
}
if (id == 0 || name.empty() || exams.size() < 2 && exams.size() > 5 || tests.empty()) return false;
this->id = id;
this->name = name;
this->exams = exams;
this->tests = tests;
return true;
}
bool excellent()const {
return exams.size() == count(exams.begin(), exams.end(), 5)
&& tests.size() == count(tests.begin(), tests.end(), '+');
}
bool underperforming()const {
return find(exams.begin(), exams.end(), 2) != exams.end()
|| find(tests.begin(), tests.end(), '-') != tests.end();
}
};
class Group {
public:
Group() : n(0) {}
Group(const size_t n) : n(n) {}
void add(const Student& student) {
table.push_back(student);
}
int excellent()const {
auto n = 0;
for (auto rec : table) if (rec.excellent()) ++n;
return n;
}
int underperforming()const {
auto n = 0;
for (auto rec : table) if (rec.underperforming()) ++n;
return n;
}
size_t size()const {
return table.size();
}
private:
size_t n;
vector<Student> table;
};
int main() {
system("chcp 1251 > nul");
const auto n = 30U;
Group group(n);
while (group.size() != n) {
Student student;
string query;
getline(cin, query);
if (student.parse(query)) group.add(student);
}
cout << "Отличников: " << group.excellent() << '\n';
cout << "Неуспевающих: " << group.underperforming() << '\n';
system("pause > nul");
}
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <cctype>
using namespace std;
struct Student {
int id;
vector<int> exams;
vector<char> tests;
string name;
Student() : id(0) {}
bool parse(const string& query) {
auto i = 0U;
auto n = query.length();
int id = 0;
while ((query[i] > 0 && isdigit(query[i])) && i < n) {
id *= 10;
id += query[i] - 48;
++i;
}
string name;
while ((query[i] != ' ') && i < n) ++i;
while ((query[++i] != ' ') && i < n) name += query[i];
while ((query[i] < 0 || !isdigit(query[i])) && i < n) ++i;
vector<int> exams;
while ((query[i] > 0 && isdigit(query[i]) || isspace(query[i])) && i < n) {
if (isdigit(query[i])) exams.push_back(query[i] - 48);
++i;
}
while ((query[i] != '+' && query[i] != '-') && i < n) ++i;
vector<char> tests;
while ((query[i] == '+' || query[i] == '-' || isspace(query[i])) && i < n) {
if (query[i] == '+' || query[i] == '-') tests.push_back(query[i]);
++i;
}
if (id == 0 || name.empty() || exams.size() < 2 && exams.size() > 5 || tests.empty()) return false;
this->id = id;
this->name = name;
this->exams = exams;
this->tests = tests;
return true;
}
bool excellent()const {
return exams.size() == count(exams.begin(), exams.end(), 5)
&& tests.size() == count(tests.begin(), tests.end(), '+');
}
bool underperforming()const {
return find(exams.begin(), exams.end(), 2) != exams.end()
|| find(tests.begin(), tests.end(), '-') != tests.end();
}
};
class Group {
public:
Group() : n(0) {}
Group(const size_t n) : n(n) {}
void add(const Student& student) {
table.push_back(student);
}
int excellent()const {
auto n = 0;
for (auto rec : table) if (rec.excellent()) ++n;
return n;
}
int underperforming()const {
auto n = 0;
for (auto rec : table) if (rec.underperforming()) ++n;
return n;
}
size_t size()const {
return table.size();
}
private:
size_t n;
vector<Student> table;
};
int main() {
system("chcp 1251 > nul");
const auto n = 30U;
Group group(n);
while (group.size() != n) {
Student student;
string query;
getline(cin, query);
if (student.parse(query)) group.add(student);
}
cout << "Отличников: " << group.excellent() << '\n';
cout << "Неуспевающих: " << group.underperforming() << '\n';
system("pause > nul");
}
Похожие вопросы
- Помогите решить лабораторную c++
- Помогите решить задачу c++
- Помогите решить задачу, c++, функции
- Помогите решить задачу C++
- Помогите решить задачу C++
- Помогите решить ошибку c++, Смотри внутри.
- Помогите решить на c++.
- Помогите решить задачки C++
- Помогите решить задачку c++.
- Помогите решить задачу C++