#include<iostream>
#include <string>
#include <stdexcept>
#include <iomanip>
#include <algorithm>
#include <vector>
using namespace std;
//求排序后家庭作业成绩中的中间成绩
double median(vector<double> vec)
{
typedef vector<double>::size_type vec_size;
vec_size size = vec.size();
if (size == 0)
throw domain_error("没有做一门家庭作业!---在求中间值函数中");
sort(vec.begin(),vec.end());
vec_size mid = size / 2;
return size % 2 == 0 ? (vec[mid - 1] + vec[mid]) / 2 : vec[mid];
}
//期末成绩
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>& homework)
{
if( homework.size() == 0)
throw domain_error("没有做过一门家庭作业!");
return grade(midterm, final, median(homework));
}
//读取家庭作业成绩
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;
}
int main()
{
cout<<"请输入您的姓名:";
string name;
cin>>name;
double midterm, final;
cout<<"请输入期中、期末成绩:";
cin>>midterm>>final;
cout<<"请输入家庭作业成绩用EOF结束:";
vector<double> homework;
read_hw(cin,homework);
try
{
double final_grade = grade(midterm, final, homework);
streamsize prec = cout.precision();
cout<<"你最终的期末成绩:"<<setprecision(5)<<final_grade<<setprecision(prec)<<endl;
}
catch(domain_error)
{
cout<<"你必须输入你的家庭作业成绩!"<<endl;
}
return 0;
}
Vector求期末成绩
最新推荐文章于 2021-05-26 20:06:13 发布