📄 3-5 .cpp
字号:
#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <string>
#include <vector>
using std::cin; using std::sort;
using std::cout; using std::streamsize;
using std::endl; using std::string;
using std::setprecision;
using std::vector;
int main()
{
cout << "Please enter the number of the students:";
int number;
cin >> number;
vector<string> students;
vector<double> grades;
for(int need = 0;need != number;need++) {
//ask for and read the student's name
cout << "Please enter the student's name: ";
string name;
cin >> name;
students.push_back(name);
cout << endl;
//ask for and read the midterm and final grades
cout << "Please enter your midterm and final grades:";
double midterm,final;
cin >> midterm >> final;
//ask for and read the homework grades
cout << "Enter all your ten homework grades:";
vector<double> homework;
double x;
//invariant: homework contains all the homework grades read so far
for(int i = 10;i != 0;i--) {
cin >> x;
homework.push_back(x);
}
//check that the student entered some homework grades
typedef vector<double>::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 the grades
sort(homework.begin(),homework.end());
//compute the median homework grade
vec_sz mid = size / 2;
double median;
median = size % 2 == 0 ? (homework[mid] + homework[mid-1]) / 2 : homework[mid];
//compute ane write the final grade
double grade;
streamsize prec = cout.precision();
setprecision(3);
grade = 0.2 * midterm + 0.4 * final + 0.4 * median ;
setprecision(prec);
grades.push_back(grade);
cout << endl;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -