|
|

楼主 |
发表于 2006-3-28 23:42:37
|
显示全部楼层
第一个文件:grade.h
#ifndef GUARD_grade_h
#define GUARD_grade_h
//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
第二个文件:median.h
#ifndef GUARD_median_h
#define GUARD_median_h
//median.h
#include<vector>
double median(std::vector<double>);
#endif
第三个文件:Student_info.h
#ifndef GUARD_Student_info
#define GUARD_Student_info
//Student_info.h
#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
第四个文件:grade.cpp
//grade.cpp
#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 std::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);
}
第五个文件:median.cpp
//median.cpp
#include <algorithm>
#include <stdexcept>
#include <vector>
using namespace std;
double median(std::vector<double> vec)
{
typedef std::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.cpp
//Student_info.cppp
#include "Student_info.h"
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)
{
cout<<"Please enter the student's name,"
"midterm grade and fianl grade:"<<endl;
//读入并存储学生的姓名以及期中、期末考试成绩
is>>s.name>>s.midterm>>s.final;
read_hw(is,s.homework);
return is;
}
//从输入流中将家庭作业的成绩读入到一个vector<double>中
istream& read_hw(istream& in,vector<double>& hw)
{
in.clear();
if(in)
{
//清除原先的内容
hw.clear();
cout<<"Please enter your homework grades:"<<endl;
//读家庭作业成绩
double x;
while(in>>x)
hw.push_back(x);
//清除流以使输入动作对下一个学生有效
in.clear();
}
return in;
}
主文件:main.cpp
#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include "grade.h"
#include "Student_info.h"
using namespace std;
int main()
{
std::vector<Student_info> students;
Student_info record;
string::size_type maxlen = 0;
//读并存储所有学生的数据
//students包含了所有学生记录
//max包含了students中最长的姓名
while (read(cin,record))
{
//找出最长的姓名的长度
maxlen = max(maxlen,record.name.size());
students.push_back(record);
}
//按字母顺序排列记录
sort(students.begin(),students.end(),compare);
//输出姓名和成绩
for (std::vector<Student_info>::size_type i=0;
i!=students.size();++i)
{
//输出姓名,把姓名填充至maxlen+1个字符的长度
cout<<setw(maxlen+1)<<students.name;
//计算并输出成绩
try
{
double final_grade = grade(students);
streamsize prec = cout.precision();
cout<<setprecision(3)<<final_grade
<<setprecision(prec);
}
catch (domain_error e)
{
cout<<e.what();
}
cout<<endl;
}
return 0;
} |
|