Accelerated C++学习笔记 Ch3

/* ----------------------------------------------------- *
	Ch3 使用批量数据
	说明:引用书中代码,略有改动
	输入eof:按Ctrl+z,再回车
	(windows,Unix或Linux下按Ctrl+d)
	知识点:
	sort
	vector	.push_back(x); .size();
	typedef vector::size_type vec_sz;
	streamsize
	setprecision
 * ----------------------------------------------------- */

#include 
#include 
#include  // sort
#include  // setprecision
#include  // setprecision

using namespace std;

int main()
{
    cout << "Please enter your name: ";
    string name;
    cin >> name;
    cout<> midterm >> final;

    cout << "Please enter all your homework grades, "
         "followed by end-of-file: ";
    vector homework;
    double x;
    while(cin>>x)
        homework.push_back(x);

    typedef vector::size_type vec_sz;
    vec_sz size=homework.size();

    if(size==0)
    {
        cout << endl << "You must enter your grades."
             "Please try again." << endl;
        return 1;
    }

    sort(homework.begin(),homework.end());

    vec_sz mid=size/2;
    double median;
    median = size % 2 == 0 ? (homework[mid]+homework[mid-1])/2
             :  homework[mid];

    streamsize prec = cout.precision();
    cout << "Your final grade is " << setprecision(3)
         << 0.2 * midterm + 0.4 * final + 0.4 * median
         << setprecision(prec) << endl;

    return 0;
}
/* ----------------------------------------------------- *
Please enter your name: Tom

Please enter your midterm and final exam grades: 99 89
Please enter all your homework grades, followed by end-of-file: 89 78 68 98^Z
Your final grade is 88.8

Process returned 0 (0x0)   execution time : 109.625 s
Press any key to continue.
 * ----------------------------------------------------- */

你可能感兴趣的:(C++)