1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iomanip>
using namespace std;
class Student_info {
private:
string name;
double midterm, final;
vector<double> homework;
public:
istream& read(istream&);
double grade() const;
string get_name() const {return name;}
vector<double> get_homework() const {return homework;}
bool valid() const {return !homework.empty();}
};
istream& Student_info::read(istream& in){
in >> name >> midterm >> final;
read_hw(in, homework);
return in;
}
void read_hw(istream& in, vector<double>& records){
if(in){
records.clear();
double x;
while (in>>x){
records.push_back(x);
}
in.clear();
}
}
double Student_info::grade() const{
return ::grade(midterm, final, homework);
}
double grade(double mid, double fin, vector<double> records){
try {
return 0.4*mid + 0.4*fin + 0.2*get_median(records);
} catch (domain_error e){
cout<<e.what();
}
}
double get_median(vector<double>& records){
if (records.size() == 0)
throw domain_error("there are no homework being submitted.");
vector<double>::size_type mid = records.size()/2;
sort(records.begin(), records.end());
return records.size()%2 == 0? records[mid+1] : records[mid];
}
bool compare(const Student_info& x, const Student_info& y){
return x.get_name() < y.get_name();
}
bool did_all_hw(const Student_info& s){
return (find(s.get_homework().begin(), s.get_homework().end(), 0) == s.get_homework().end());
}
int main(){
vector<Student_info> students;
Student_info record;
string::size_type maxlen = 0;
vector<Student_info> did, didnt;
while(record.read(cin)){
if (did_all_hw(record))
did.push_back(record);
else
didnt.push_back(record);
maxlen = max(maxlen, record.get_name().size());
students.push_back(record);
}
if (did.empty()){
cout<<"No student did all the homework"<<endl;
}
if (didnt.empty()){
cout<<"All the student did all the homework"<<endl;
}
sort(students.begin(), students.end(), compare);
for (vector<string>::size_type i = 0; i != students.size(); ++i){
cout << students[i].get_name() << string(maxlen + 1 - students[i].get_name().size(), ' ');
try{
double final_grade = students[i].grade();
streamsize prec = cout.precision();
cout<<setprecision(3)<<final_grade<<setprecision(prec);
}
catch (domain_error e){
cout<<e.what();
}
cout<<endl;
}
return 0;
}
|