Acclerated C++ Exercise 5-0(list)

#ifndef GUARD_Student_info 
#define GUARD_Student_info 
 
// `Student_info.h' header file 
#include <iostream> 
#include <string> 
#include <vector> 
 
struct Student_info { 
	std::string name; 
	double midterm, final; 
	std::vector<double> homework; 
}; 
 
bool compare(const Student_info&, const Student_info&); 
std::istream& read(std::istream&, Student_info&); 
std::istream& read_hw(std::istream&, std::vector<double>&); 
#endif
// source file for `Student_info'-related functions   
#include "Student_info.h"   
   
using std::istream;  using std::vector;   
   
bool compare(const Student_info& x, const Student_info& y)   
{   
    return x.name < y.name;   
}   

istream& read(istream& is, Student_info& s)   
{   
    // read and store the student's name and midterm and final exam grades   
    is >> s.name >> s.midterm >> s.final;   
   
    read_hw(is, s.homework);  // read and store all the student's homework grades   
    return is;   
}   
   
// read homework grades from an input stream into a `vector<double>'   
istream& read_hw(istream& in, vector<double>& hw)   
{   
    if (in) {   
        // get rid of previous contents   
        hw.clear();   
   
        // read homework grades   
        double x;   
        while (in >> x)   
            hw.push_back(x);   
   
        // clear the stream so that input will work for the next student   
        in.clear();   
    }   
    return in;   
}  

#ifndef GUARD_median_h 
#define GUARD_median_h 
 #include <vector> 
double median(std::vector<double>);
#endif

// source file for the `median' function   
#include <algorithm>    // to get the declaration of `sort'   
#include <stdexcept>    // to get the declaration of `domain_error'   
#include <vector>       // to get the declaration of `vector'

#include "median.h"  

using std::domain_error;   using std::sort;   using std::vector;   
  
// compute the median of a `vector<double>'   
// note that calling this function copies the entire argument `vector'   
double median(vector<double> vec)   
{   
    typedef vector<double>::size_type vec_sz;   
   
    vec_sz size = vec.size();   
    if (size == 0)   
        throw domain_error("median of an empty vector");   
   
    sort(vec.begin(), vec.end());   
   
    vec_sz mid = size/2;   
   
    return size % 2 == 0 ? (vec[mid] + vec[mid-1]) / 2 : vec[mid];   
}

#ifndef GUARD_grade_h 
#define GUARD_grade_h 
 
#include <vector> 
#include "Student_info.h" 
 
double grade(double, double, double); 
double grade(double, double, const std::vector<double>&); 
double grade(const Student_info&); 
 
bool fgrade(const Student_info&); 
 
#endif

#include <stdexcept>   
#include <vector>   

#include "grade.h"   
#include "median.h"   
#include "Student_info.h"   
   
using std::domain_error;  using std::vector;   
   
   
// compute a student's overall grade from midterm and final exam grades and homework grade   
double grade(double midterm, double final, double homework)   
{   
    return 0.2 * midterm + 0.4 * final + 0.4 * homework;   
}   
   
// compute a student's overall grade from midterm and final exam grades   
// and vector of homework grades.   
// this function does not copy its argument, because `median' does so for us.   
double grade(double midterm, double final, const vector<double>& hw)   
{   
    if (hw.size() == 0)   
        throw domain_error("student has done no homework");   
    return grade(midterm, final, median(hw));   
}   
   
double grade(const Student_info& s)   
{   
    return grade(s.midterm, s.final, s.homework);   
}   
   
// predicate to determine whether a student failed   
bool fgrade(const Student_info& s)   
{   
    return grade(s) < 60;   
}   

#ifndef GUARD_extract_fails_h 
#define GUARD_extract_fails_h 
 #include <vector> 
 #include  <list>
#include "Student_info.h" 
std::list<Student_info> extract_fails( std::list<Student_info>&);
std::vector<Student_info>extract_fails( std::vector<Student_info>&);
#endif

#include "extract_fails.h"
#include "grade.h"

using std::list;
using std::vector;

// version 4: use `list' instead of `vector'   
list<Student_info> extract_fails(list<Student_info>& students)   
{   
    list<Student_info> fail;   
    list<Student_info>::iterator iter = students.begin();   
   
    while (iter != students.end()) {   
        if (fgrade(*iter)) {   
            fail.push_back(*iter);   
            iter = students.erase(iter);   
        } else   
            ++iter;   
    }   
    return fail;   
}

vector<Student_info> extract_fails(vector<Student_info>& students)   
{   
    vector<Student_info> pass, fail;   
   
    for (vector<Student_info>::size_type i = 0;   
         i != students.size(); ++i)   
        if (fgrade(students[i]))   
            fail.push_back(students[i]);   
        else   
            pass.push_back(students[i]);   
   
    students = pass;   
    return fail;   
}   

#include <algorithm>   
#include <list>   
#include <string>   
   
#include "grade.h"   
#include "Student_info.h" 
#include "extract_fails.h"
   
using std::max;   
   
using std::cin;   
using std::cout;   
using std::endl;   
using std::list;   
using std::string;     
   
   
int main()   
{   
	list<Student_info> vs;   
	Student_info s;   
	string::size_type maxlen = 0;   
	while (read(cin, s)) {   
		maxlen = max(maxlen, s.name.size());   
		vs.push_back(s);   
	}   
	vs.sort(compare);   
	list<Student_info> fails = extract_fails(vs);   
	list<Student_info>::iterator i;   
	for (i = fails.begin(); i != fails.end(); ++i)   
		cout << i->name << " " << grade(*i) << endl<<endl;   
	list<Student_info>::iterator j;   
	for (j = vs.begin(); j != vs.end(); ++j)   
		cout << j->name << " " << grade(*j) << endl;   
	system("pause");
    return 0;   
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值