C/C++
Помогите, пож, задача с++ очень нужно
Вычислить средний балл группы и распечатать список студентов, имеющих средний балл ниже среднего балла группы
#include <iostream>
#include <string>
#include <set>
#include <numeric>
#include <iomanip>
using namespace std;
class Student {
public:
Student()
: average_score_(.0) {}
Student(const string name, const double average_score)
: name_(name), average_score_(average_score) {}
bool less(const double average_score)const {
return average_score_ < average_score;
}
double average_score()const {
return average_score_;
}
private:
string name_;
double average_score_;
friend bool operator<(const Student& a, const Student& b) {
return a.name_ < b.name_;
}
friend istream& operator>>(istream& inp, Student& student) {
cout << "Ф. И. О.: ";
getline(inp, student.name_);
cout << "Средний балл: ";
inp >> student.average_score_;
inp.ignore(numeric_limits<streamsize>::max(), '\n');
return inp;
}
friend ostream& operator<<(ostream& out, const Student& student) {
out << fixed << setprecision(2U);
out << student.name_ << " = " << student.average_score_;
out.unsetf(ios::fixed);
return out;
}
};
class Group {
public:
Group(const size_t quantity)
: quantity_(quantity) {}
void add(const Student& student) {
students_.insert(student);
}
void add(Student&& student) {
students_.emplace(student);
}
double average_score()const {
auto lambda = [](double s, const Student& student) {
return s += student.average_score();
};
return accumulate(students_.begin(), students_.end(), .0, lambda) / students_.size();
}
void less()const {
const auto avg = average_score();
auto id = 0U;
cout << "\tСредний балл ниже среднего: \n\n";
for (const auto& student : students_) {
if (student.less(avg)) cout << ++id << ". " << student << '\n';
}
}
private:
set<Student> students_;
size_t quantity_;
friend istream& operator>>(istream& inp, Group& group) {
Student student;
for (auto i = 0U; i < group.quantity_; ++i) {
inp >> student;
group.add(student);
cout.put('\n');
}
return inp;
}
friend ostream& operator<<(ostream& out, const Group& group) {
auto id = 0U;
for (const auto& student : group.students_) out << ++id << ". " << student << '\n';
return out;
}
};
int main() {
system("chcp 1251 > nul");
Group group(12U);
cin >> group;
system("cls");
cout
<< group << '\n'
<< fixed << setprecision(2U)
<< "Средний балл группы: " << group.average_score() << "\n\n";
cout.unsetf(ios::fixed);
group.less();
system("pause > nul");
}
#include <string>
#include <set>
#include <numeric>
#include <iomanip>
using namespace std;
class Student {
public:
Student()
: average_score_(.0) {}
Student(const string name, const double average_score)
: name_(name), average_score_(average_score) {}
bool less(const double average_score)const {
return average_score_ < average_score;
}
double average_score()const {
return average_score_;
}
private:
string name_;
double average_score_;
friend bool operator<(const Student& a, const Student& b) {
return a.name_ < b.name_;
}
friend istream& operator>>(istream& inp, Student& student) {
cout << "Ф. И. О.: ";
getline(inp, student.name_);
cout << "Средний балл: ";
inp >> student.average_score_;
inp.ignore(numeric_limits<streamsize>::max(), '\n');
return inp;
}
friend ostream& operator<<(ostream& out, const Student& student) {
out << fixed << setprecision(2U);
out << student.name_ << " = " << student.average_score_;
out.unsetf(ios::fixed);
return out;
}
};
class Group {
public:
Group(const size_t quantity)
: quantity_(quantity) {}
void add(const Student& student) {
students_.insert(student);
}
void add(Student&& student) {
students_.emplace(student);
}
double average_score()const {
auto lambda = [](double s, const Student& student) {
return s += student.average_score();
};
return accumulate(students_.begin(), students_.end(), .0, lambda) / students_.size();
}
void less()const {
const auto avg = average_score();
auto id = 0U;
cout << "\tСредний балл ниже среднего: \n\n";
for (const auto& student : students_) {
if (student.less(avg)) cout << ++id << ". " << student << '\n';
}
}
private:
set<Student> students_;
size_t quantity_;
friend istream& operator>>(istream& inp, Group& group) {
Student student;
for (auto i = 0U; i < group.quantity_; ++i) {
inp >> student;
group.add(student);
cout.put('\n');
}
return inp;
}
friend ostream& operator<<(ostream& out, const Group& group) {
auto id = 0U;
for (const auto& student : group.students_) out << ++id << ". " << student << '\n';
return out;
}
};
int main() {
system("chcp 1251 > nul");
Group group(12U);
cin >> group;
system("cls");
cout
<< group << '\n'
<< fixed << setprecision(2U)
<< "Средний балл группы: " << group.average_score() << "\n\n";
cout.unsetf(ios::fixed);
group.less();
system("pause > nul");
}
Похожие вопросы
- Помогите решить задачу по программированию на C++
- СРОЧНО! Помогите с задачей.
- ПОМОГИТЕ С ЗАДАЧЕЙ НА C++
- Помогите решить задачу по C++!
- Помогите решить задачу по программированию
- Помогите решить задачу пожалуйста, в C++
- Помогите решить задачу на c++
- Помогите с задачей на языке СИ
- Помогите решить задачу на С++ (мне не совсем ясен смысл задания)
- Помогите решить задачу на С++, используя статические массивы