#include<iostream> #include<string> #include<vector> #include<stdexcept>//domain_error #include<algorithm>//sort #include<iomanip>//setprecision #include<istream> using std::cout; using std::endl; using std::cin; using std::string; using std::vector; using std::domain_error; using std::istream; using std::streamsize; using std::setprecision; //计算一个vector<double>类型的变量的中值 //值得注意的是,调用函数时整个vector参数都会被复制 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]; } //根据学生的期中考试、期末考试以及家庭作业成绩来计算总成绩 double grade(double midterm, double final, double homework) { return 0.2*midterm + 0.4*final + 0.4*homework; } //根据学生的期中考试、期末考试以及家庭作业成绩来计算总成绩 //这个函数不用复制他的参数,原因是median已经为我们完成了这个工作 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)); } //从输入流中将家庭作业的成绩读入到一个vector<double>中 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 << "Please enter your first name :\n"; string name; cin >> name; cout << "Hello , " << name << " !" << endl; //请求并读入期中和期末考试成绩 cout << "Please enter you midterm and final exam grades :\n"; double midterm, final; cin >> midterm >> final; //请求用户输入家庭作业成绩 cout << "Please enter all your homework grades : " "followed by end-of file(ctrl+z) "; vector<double>homework; //读入家庭作业成绩 read_hw(cin, homework); //假如可以的话,计算生成总成绩 try{ double final_grade = grade(midterm, final, homework); streamsize prec = cout.precision(); cout << "You final grade is " << setprecision(3) << final_grade << setprecision(prec) << endl; } catch (domain_error){ cout << endl << "You must enter your grades ." << "Please try again ." << endl; return 1; } system("pause"); return 0; }
这段程序的
//从输入流中将家庭作业的成绩读入到一个vector<double>中 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; }
在vs2013上运行时提示有错误
错误 1 error C2280: “std::basic_istream<char,std::char_traits<char>>::basic_istream(const std::basic_istream<char,std::char_traits<char>> &)”: 尝试引用已删除的函数 d:\vs2013_code\accelerated c++_4\accelerated c++_4\p60.cpp 62 1 Accelerated C++_4
2 IntelliSense: 无法引用 函数 “std::basic_istream<_Elem, _Traits>::basic_istream(const std::basic_istream<_Elem, _Traits>::_Myt &) [其中 _Elem=char, _Traits=std::char_traits<char>]” (已声明 所在行数:77,所属文件:”D:\vs2013\VC\include\istream”) — 它是已删除的函数 d:\vs2013_code\Accelerated C++_4\Accelerated C++_4\P60.cpp 62 9 Accelerated C++_4
刚刚开始学c++,求帮忙解答
解决方案
10
30
istream& read_hw(istream& in, vector<double>& hw)
{
{
return in;
}