码迷,mamicode.com
首页 > 其他好文 > 详细

Vector求期末成绩

时间:2014-06-01 13:53:42      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:vector求期末成绩

#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求期末成绩,布布扣,bubuko.com

Vector求期末成绩

标签:vector求期末成绩

原文地址:http://blog.csdn.net/u013197002/article/details/27957537

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!