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

📄 main.cpp

📁 Accelerated C++ 课后练习题 本人自己完成、可供参考
💻 CPP
字号:
#include<algorithm>
#include<iomanip>
#include<ios>
#include<iostream>
#include<stdexcept>
#include<string>
#include<vector>
#include"grade.h"
#include"Student_info.h"

using namespace std;


int main()
{
    vector<Student_info> students;
    Student_info record;
    string::size_type maxlen = 0;       // the length of the longest name

    // read and store all the students data.
    // Invariant:  students contains all the student records read so far
    //   maxlen contains the length of the longest name in students
    while (read(cin, record)) 
	{
        // find length of longest name
        maxlen =_MAX(maxlen, record.name.size());
        students.push_back(record);
    }

    // alphabetize the student records
    sort(students.begin(), students.end(), compare);

    // write the names and grades
    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[i].name<< string(maxlen + 1 - students[i].name.size(), ' ');

        // compute and write the grade
        try 
		{
            double final_grade = grade(students[i]);
            streamsize prec = cout.precision();
            cout << setprecision(3) << final_grade<< setprecision(prec);
        } 
		catch (domain_error e) 
		{
            cout << e.what();
        }
        cout << endl;
    }
    return 0;
}




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

double grade(const Student_info& s)
{
	return grade(s.midterm,s.final,s.homework);
}
		



bool compare(const Student_info& x,const Student_info& y)
{
	return x.name<y.name;
}
istream& read(istream& is,Student_info& s)
{
	//read an store th student's name and midterm and final exam grades
	is>>s.name>>s.midterm>>s.final;
	read_hw(is,s.homework);			//read and store all the students homework grades
	return is;
}
//read homework grades from an input stream into a 'vector'
istream& read_hw(istream& in,vector<double>& hw)
{
	if(in)
	{
		//get rid of previous contents
		hw.clear();

		//read homework grades
		double x;
		while(in>>x)
			hw.push_back(x);
		
		//clear the stream so that input will work for the next student
		in.clear();
	}
	return in;
}

⌨️ 快捷键说明

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