/****************组织程序.cpp*******************/
#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <string>
#include <vector>
#include "Student_info.h"
#include "grade.h"
using std::cin; using std::cout;
using std::endl; using std::string;
using std::setprecision; using std::streamsize;
using std::vector; using std::sort;
using std::domain_error; using std::istream;
using std::max;
// compute a student's overall grade from midterm and final exam grades and homework grade
int main()
{
vector<Student_info> students;
Student_info record;
string::size_type maxlen = 0;
//read and store all the records, and find the length of the longest name
while ( read(cin, record) ) {
maxlen = max(maxlen, record.name.size());
students.push_back(record);
}
//alphabetize the records
sort(students.begin(), students.end(), compare);
for (vector<Student_info>::size_type i = 0;
i != students.size(); ++i) {
//write the name, padded on the right to maxlen+1 characters
cout << students.name
<< string(maxlen + 1 - students.name.size(), ' ');
//compute and write the grade
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;
}
system("PAUSE");
return 0;
}
/*
Duck 4.75 4.75 5.0 4.9 1.0 3.8
yannzi 3.9 3.8 4.8 4.9 4.2
Agirl 2.0 2.0 5.0 5.0 5.0
*/
// source file for the median function
#include <algorithm>
#include <stdexcept>
#include <vector>
#include "median.h"
using std::domain_error; using std::sort; using std::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_median_h
#define GUARD_median_h
//median.h-final version
#include <vector>
double median(std::vector<double>);
#endif
//source file for the grade function
#include <stdexcept>
#include <vector>
#include "grade.h"
#include "median.h"
#include "Student_info.h"
using std::domain_error; using std::vector;
double grade(double midterm, double final, double homework)
{
return 0.2 * midterm + 0.4 * final + 0.4 * homework;
}
double grade(const Student_info& s)
{
return grade(s.midterm, s.final, s.homework);
}
double grade(double midterm, double final, const vector<double>& hw)
{
if (hw.size() == 0)
throw domain_error("student hax done no homework");
return grade(midterm, final, median(hw));
}
#ifndef GUARD_grade_h
#define GUARD_grade_h
//grade.h
#include <vector>
#include "Student_info.h"
double grade(double, double, const std::vector<double>&);
double grade(double, double, double);
double grade(const Student_info&);
#endif
#ifndef GUARD_Student_info_h
#define GUARD_Student_info_h
//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
文章评论(0条评论)
登录后参与讨论