excel函数排名公式,Accelerated C++ Chapter4.1 用函数来计算学生成绩

 2023-09-23 阅读 21 评论 0

摘要:最近在自学C++,在看斯坦福推荐的一本C++教材(没有核实是不是真的斯坦福推荐的,也是某乎上看到的,然后就买了书跟着敲代码) Learner:JC 书名:Accelerated C++: Practical Programming by Example 作

最近在自学C++,在看斯坦福推荐的一本C++教材(没有核实是不是真的斯坦福推荐的,也是某乎上看到的,然后就买了书跟着敲代码)

Learner:JC

书名:Accelerated C++: Practical Programming by Example

作者:Andrew Koenig

代码中的注释主要来源书中,部分是我自己的理解也在上面,说得冗杂请见谅,毕竟初学者。

 

第四章的任务主要用于根据学生的期中midterm,期末成绩final,以及平时作业成绩homework,将平时作业成绩hw当作c++数据类型中的vector<double>来处理,编写函数来生成homework的中位数(涉及到0-index),再结合公式输出该学生的最终成绩。

// acc_ch4_Organizing programs_and_data2
// Accelerated C++ Chapter 4 Organizing programs and data#include <iostream>
#include <stdexcept>
#include <vector>
#include <algorithm>
#include <string>
#include <ios>
#include <iomanip>// using std::cin;
// using std::cout;
// using std::endl;
// using std::setprecision;
// using std::sort;
// using std::streamsize;
// using std::string;
// using std::vector;
using namespace std;// compute a student's overall grade from midterm and final exam grades and homework grade
// 根据学生的midterm,final还有homework生成成绩的grade
double grade(double midterm, double final, double homework)
{return 0.2 * midterm + 0.4 * (final + homework);
}
// parameter是形参,argument是实参// cout << "Your final grade is " << setprecision(3)
//      //  << 0.2 * midterm + 0.4 * final + 0.4 * sum/count 有grade函数,所以我们不需要详细的写出这一部分,而只需要grade函数
//      << grade(midterm, final, sum / count) // sum/count 这种是表达式expression
//      << setprecision(prec) << endl;// 4.1.1 查找中值的函数median(就是中位数)
// 回顾:如果有奇数个元素,那么中位数就是最中间的一个元素,如果有偶数个元素那么中位数就是最中间的两个元素的平均数
// 现在这个函数只能处理vector<double>类型的函数,在后面的章节我们可以定义一个通用的函数来解决更多类型变量的函数// compute the median of a vector<double>
// note that calling this function copies the entire argument vectordouble median(vector<double> vec)
{typedef vector<double>::size_type vec_sz;vec_sz size = vec.size();                                                    // xxxx.size() 这个size函数是vec的的成员函数if (size == 0) throw domain_error("median of an empty vector"); // domain_error is an object in <stdexcept> librarysort(vec.begin(), vec.end());vec_sz mid = size / 2;return size % 2 == 0 ? (vec[mid] + vec[mid - 1]) / 2 // 0-index,所以一共有0,1,2,3,...,mid-1,mid,mid+1,...size-1个元素,也就是有size个元素: vec[mid];                     // 因为是0-index,所以size/2得到的证书vec[mid]刚好是vec的中位数。
}// compute a student's overall grade from midterm and final exam grades
// and vector of homework grades
// main difference here is that this grade function uses <vector>,homework instead of double homeworkdouble 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> homework;
vector<double>& hw = homework; // hw is a synonym for homework
// this is not const reference so whatever do to hw will do the same to homework
// in another sense, not read-only, function can modify this variable - homework/hw;// 4.1.3 Reading homework grades
// 读取家庭作业成绩// 因为没有函数可以返回一个一个以上的值,所以我们把想输出的一个变量in作为形参parameter,这样就能输出in
// read homework from an input stream into a vector<double>
istream& read_hw(istream& in, vector<double>& hw)
{if (in){// get tid of previous content (like cleaning the variable space)hw.clear();// read homework gradesdouble x;while (in >> x)hw.push_back(x);// clear the stream so that input will work for the next studentin.clear();}return in;
}int main()
{// ask for and read the student's namecout << "Please enter your first name: ";string name;cin >> name;cout << "Hello, " << name << "!" << endl;// ask for and read the midterm and final grades (not reading homework yet)cout << "Please enter your midterm and final grades: ";double midterm, final; cin >> midterm >> final;// ask for the homework gradescout << "Enter all your homework grades: ";read_hw(cin, homework); //read_hw fucntion will change homework into a vector<double>// computer and generate the final grade, if possibletry {double final_grade = grade(midterm, final, homework);streamsize prec = cout.precision();cout << "Your 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;}return 0;
}

参考Accelerated C++书籍,代码仅用于学习,不作商业用途

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/87088.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息