Accelerated C++ 4 organizing programs and data
来源:互联网 发布:淘宝代购需要什么证明 编辑:程序博客网 时间:2024/05/17 03:42
Student_info.h
#ifndef GUARD_Student_info#define GUARD_Student_info// Student_info.h header file#include <iostream>#include <string>#include <vector>struct Student_info{ std::string name; double midterm, final; std::vector<double> homework;};bool compare(const Student_info&, const Student_info&);std::istream& read_hw(std::istream&, std::vector<double>&);std::istream& read(std::istream&, Student_info&);#endif
Student_info.cpp
// source file for Student_info-related functions#include <vector>#include <iostream>#include "Student_info.h"using std::cout; using std::vector; using std::istream;bool compare(const Student_info& x, const Student_info& y) { return x.name < y.name;}istream& read_hw(istream& in, vector<double>& hw) { // read and store the homework grades cout << "Enter all your homework grades, " "followd by end-of-file: "; if (in) { // get rid of previous contents hw.clear(); // read homework grades double x; while (in >> x) hw.push_back(x); // clear the stream so that input will work for the next student in.clear(); } return in;}istream& read(istream& is, Student_info& s) { // read and store the student's name and midterm and final exam grades cout << "Please enter your first name, midterm and final exam grades: "; is >> s.name >> s.midterm >> s.final; read_hw(is, s.homework); return is;}
grade.h
#ifndef GUARD_grade_h#define GUARD_grade_h// grade.h#include <vector>double median(std::vector<double>);double grade(double, double, double);double grade(double, double, const std::vector<double>&);double grade(const Student_info&);#endif
grade.cpp
// source file for the grade-ralated functons#include <vector>#include <stdexcept>#include "Student_info.h"#include "median.h"using std::vector; using std::domain_error; using std::sort;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 - 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>& hw) { if (hw.size() == 0) throw domain_error("student has done no homework"); return grade(midterm, final, median(hw));}double grade(const Student_info& s) { return grade(s.midterm, s.final, s.homework);}
main.cpp
#include <algorithm>#include <iomanip> // setprecision#include <ios> // streamsize #include <iostream>#include <stdexcept>#include <string>#include "Student_info.h"#include "grade.h"using std::vector; using std::sort; using std::string; using std::endl;using std::domain_error; using std::cin; using std::cout; using std::max;using std::streamsize; using std::setprecision;int main(){ vector<Student_info> students; Student_info record; string::size_type maxlen = 0; // read and store all the students' data // Invariant: students contains all the student records read so far // maxlen contains the length of the longest name in students while (read(cin, record)) { // find length of longest name maxlen = max(maxlen, record.name.size()); students.push_back(record); } // alphabetize the student records sort(students.begin(), students.end(), compare); // write the names and grades for (vector<Student_info>::size_type i = 0; i != students.size(); ++i){ // write the name, padded on the right to maxlen + 1 characters cout << students[i].name << string(maxlen + 1 - students[i].name.size(), ' '); // compute and write the grade try { double final_grade = grade(students[i]); streamsize prec = cout.precision(); cout << setprecision(3) << final_grade << setprecision(prec); } catch (domain_error e) { cout << e.what(); } cout << endl; } return 0;}
阅读全文
0 0
- Accelerated C++ 4 organizing programs and data
- Accelerated C++<4-4>
- Accelerated C++<4-1>
- Accelerated C++<4-2>
- Accelerated C++<4-3>
- Accelerated C++<4-5>
- Accelerated C++<4-6>
- Accelerated C++<4-7>
- Graduate Programs in Big Data Analytics and Data Science
- gcc教程 Compiling C and C++ Programs
- PERL and C Programs for Validating and Checking
- Debugging C and C++ programs with gdb (and ddd)
- Casting in C++: Bringing Safety and Smartness to Your Programs
- Accelerated rendering and compositing
- Programs as Data: Function Pointers
- OpenGL ES2.0 Programming Guide - Chapter 4: Shaders and Programs
- 读《Accelerated C++》有感
- 【Accelerated C++】读书笔记(一)
- Linux LVM硬盘管理及LVM扩容
- AOP
- WEB项目再次部署到服务器流程
- 中医经络、睡眠
- C#实现邮件发送的功能
- Accelerated C++ 4 organizing programs and data
- 支付宝支付为什么出现填写卡号
- CodeForces
- xcode真机调试无法运行或者安装包无法运行
- Qt中update()和repaint()的区别
- EditText想要让默认输入类型为数字,并且可以切换到其他输入类型
- BaseAdapter 基础知识
- JAVA学习之JAVAC使用详解
- volatile关键字解析