⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 4-6.cpp

📁 Accelerated C++ 课后练习题 本人自己完成、可供参考
💻 CPP
字号:
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

struct Student_info
{
	string name;
   double grade;
};


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];
}

//compute a student's overall grade from midterm and final exam grades
double grade(double midterm,double final,double homework)
{
	return 0.2*midterm+0.4*final+0.4*homework;
}

//compute a student's overall grade from minterm and final exam grades
//and vector of homework grades
//this funtion does not copy its argument median does so far
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));
}

int main()
{
	struct Student_info record;
	vector<Student_info> students;
	string s;
	double midterm,final,g;
	vector<double> homework;
	while(1)
	{		
		cin>>s;
		if(s=="exit")
			break;
		cout<<"please input midterm,final exam and homework grades"<<endl;
		if(cin>>midterm>>final)
		{
			homework.clear();
			while(cin>>g)
				homework.push_back(g);
			cin.clear();
		}
		else 
		{
			cout<<"The worry input! you have to reinput!"<<endl;
			return 1;
		}
		record.grade=grade(midterm,final,homework);
		record.name=s;
		students.push_back(record);
	}

	vector<struct Student_info>::size_type i=0;
	for(;i<students.size();++i)
		cout<<students[i].name<<students[i].grade<<endl;

	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -