首页 > 代码库 > Vector求期末成绩
Vector求期末成绩
#include<iostream> #include <string> #include <stdexcept> #include <iomanip> #include <algorithm> #include <vector> using namespace std; //求排序后家庭作业成绩中的中间成绩 double median(vector<double> vec) { typedef vector<double>::size_type vec_size; vec_size size = vec.size(); if (size == 0) throw domain_error("没有做一门家庭作业!---在求中间值函数中"); sort(vec.begin(),vec.end()); vec_size mid = size / 2; return size % 2 == 0 ? (vec[mid - 1] + vec[mid]) / 2 : vec[mid]; } //期末成绩 double grade(double midterm, double final, double homework) { return 0.2 * midterm + 0.4 * final + 0.4 * homework; } //期末成绩 double grade(double midterm, double final, const vector<double>& homework) { if( homework.size() == 0) throw domain_error("没有做过一门家庭作业!"); return grade(midterm, final, median(homework)); } //读取家庭作业成绩 istream& read_hw(istream& in,vector<double>& hw { if(in) { hw.clear(); double x; while (in>>x) hw.push_back(x); in.clear(); } return in; } int main() { cout<<"请输入您的姓名:"; string name; cin>>name; double midterm, final; cout<<"请输入期中、期末成绩:"; cin>>midterm>>final; cout<<"请输入家庭作业成绩用EOF结束:"; vector<double> homework; read_hw(cin,homework); try { double final_grade = grade(midterm, final, homework); streamsize prec = cout.precision(); cout<<"你最终的期末成绩:"<<setprecision(5)<<final_grade<<setprecision(prec)<<endl; } catch(domain_error) { cout<<"你必须输入你的家庭作业成绩!"<<endl; } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。