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
|
#include <iostream>
#include <ios>
#include <iomanip>
#include <list>
#include <cstdlib>
using namespace std;
//Data structure for holding students name and final grade
struct Student_info
{
string name;
double final;
};
//calculate and return the average from list of homework grades
double average(list<double>& hw)
{
double sum = 0;
list<double>::size_type size = hw.size();
list<double>::iterator i = hw.begin();
for(i; i != hw.end(); i++){
sum += *i;
}
return (sum/size);
}
//calculate and return the final grade of a student final exam weights 40%, midterm 20%, homeworks 40%
double grade(list<double>& hw, double midterm, double final)
{
return final*0.4+midterm*0.2+average(hw);
}
//read homework grades from the input and store them in a list
istream& read_hw(istream& in, list<double>& hw)
{
if(in){
hw.clear();
double x;
while(in >> x){
hw.push_back(x);
}
in.clear();
}
return in;
}
//read students name, midterm, final and homework grades
istream& read(istream& in, Student_info& s)
{
list<double> hw;
double midterm, final;
in >> s.name >> midterm >> final;
read_hw(in, hw);
s.final = grade(hw,midterm,final);
return in;
}
//print out stored data
void print_records(list<Student_info>& stud)
{
streamsize prec = cout.precision();
list<Student_info>::iterator i = stud.begin();
for(i; i != stud.end(); i++){
cout << (*i).name << " " << setprecision(3) << (*i).final << endl << setprecision(prec);
}
}
int main()
{
Student_info s;
list<Student_info> stud;
while(read(cin,s)){
stud.push_back(s);
}
print_records(stud);
return 0;
}
|