Accelerated C++ exercises 4-0

#include<algorithm>  
#include<iostream>  
#include<ios>  
#include<iomanip>  
#include<string>  
#include<vector>  
using std::cin;  
using std::cout;  
using std::endl;  
using std::sort;  
using std::streamsize;  
using std::string;  
using std::vector;  
using std::setprecision;  
using std::domain_error;
using std::istream;
double grade(double midterm,double final,double homework)
{
	return 0.2*midterm+0.4*final+0.4*homework;
}
double median(vector<double> vec)
{
	typedef vector<double>::size_type vec_sz;  
    vec_sz size=vec.size();  
    if(size==0)  
    {  
        cout<<endl<<"You must enter your grades."  
            "Place try again."<<endl;  
        return 1;  
    }  
    sort(vec.begin(),vec.end());  
  
    vec_sz mid=size/2;  
    return size%2==0 ? (vec[mid]+vec[mid-1])/2:vec[mid];  
}
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));
}
istream& read_hw(istream& in,vector<double>& hw)
{
	if(in)
	{
		hw.clear();
		double x;
		while(cin>>x)
			hw.push_back(x);
		in.clear();
	}
	return in;
}

int main()  
{  
    cout<<"Place enter your first name:";  
    string name;  
    cin>>name;  
    cout<<"Hello ,"<<name<<"!"<<endl;  
  
    cout<<"Place enter your midterm and final exam grades:";  
    double midterm,final;  
    cin>>midterm>>final;  
  
    cout<<"Place enter all your homework grades,"  
        "fellowed by end-of-file:";  
    vector<double> homework;  
	read_hw(cin,homework);
	try{
		double final_garde=grade(midterm,final,homework);
		streamsize prec=cout.precision();
		cout<<"Your final grade is  :"<<setprecision(3)
			<<final_garde<<setprecision(prec)<<endl;
	}
	catch(domain_error){
		cout<<endl<<"You must enter your grades."  
            "Place try again."<<endl;  
        return 1; 
	}
    system("pause");  
    return 0;     
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值