Accelerated C++之计算学生成绩3

层层深入,由浅及深
一个例子,包含多少知识点,自己看哦

第三部分


  • median.h
#ifndef GUARD_median_h
//检查头文件GUARD_median_h是否被定义,
//如果未定义,就会请求预处理程序对在他和#endif之间的所有内容作出适当的处理动作
//名称要唯一
#define GUARD_median_h
//.h中不能包含using声明
#include <vector>

double median(std::vector<double>);

#endif

  • median.cc
#include <algorithm>
#include <iostream>
#include <stdexcept>
#include <vector>
#include "median.h"
using namespace std;

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];

}

  • Student_info.h
#ifndef GUARD_Student_info
#define GUARD_Student_info

#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(istream&, std::vector<double>&);
#endif
  • Student_info.cc
#include "Student_info.h"
#include <iostream>
using namespace std;

bool compare(const Student_info& x, const Student_info& y)
{
    return x.name < y.name;
}

istream& read(istream& is, Student_info &s)
{
    double midterm, final;
    is >> s.name >> midterm >> final;
    read_hw(is, s.homework);
    return is;
}

istream& read_hw(istream& in, vector<double>& hw)
{
    if (in)
    {
        hw.clear();

        double x;
        while (in >> x)
            hw.push_back(x);

        in.clear();
    }

    return in;
}

  • grade.h
#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&);

#endif

  • grade.cc
#include <iostream>
#include <stdexcept>
#include <vector>
#include "grade.h"
#include "median.h"
#include "Student_info.h"
using namespace std;

double grade(double midterm, double final, double homework)
{
    return 0.2 * midterm + 0.4 * final + 0.4 * homework;
}


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);
}


  • main.cc
#include <algorithm>
#include <iomanip>
#include <vector>
#include <iostream>
#include <stdexcept>
#include <string>
#include "grade.h"
#include "Student_info.h"

using namespace std;

int main()
{
    vector<Student_info> students;
    Student_info record;
    string::size_type maxlen = 0;

    while (read(cin, record))
    {
        maxlen = max(maxlen, record.name.size());
        students.push_back(record);
    }

    // 按照顺序排列学生记录
    sort(students.begin(), students.end(), compare);

    for (vector<Student_info>::size_type i = 0; i != students.size(); ++i)
    {

        // 输出姓名,把姓名填充至maxlen+1个字符长度
        cout << students[i].name<< string(maxlen + 1 - students[i].name.size(), ' ');

        try
        {
            double final_grade = grade(students[i]);
            streamsize prec = cout.precision();
            cout << setprecision(3) << final_grade << setprecision(prec);
        } catch (domain_error e)
        {
            cout << e.what();
        }

        cout << endl;
    }

    return 0;
}

未完待续

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值