最近在看《Accelarate C++》这本书,然后抛开书本自己码一遍书上的程序,如果你也准备看这本书,希望可以对你有点帮助,以上。
c文件和h文件、程序包含头文件的定义
主程序 main.c
#include <iostream.h>
#include <ios>
#include <vector.h>
#include <iomanip.h>
#include <string.h>
#include <algorithm>
#include <stdexcept>
#include "grade.h"
#include "Student_info.h"
using std::cin; using std::cout;
using std::string; using std::vector;
using std::endl; using std::sort;
using std::domain_error; using std::max;
using std::setprecision; using std::streamsize;
int main(){
vector<Student_info> students;
Student_info record;
string::size_type maxlen = 0;
//读入数据
while(read(cin, record)){
maxlen = max(maxlen, record.name.size());
students.push_back(record);
}
//按字母顺序排列学生成绩记录
sort(students.begin(), students.end(), compare);
//输出
for(vector<Student_info>::size_type i = 0; i!=students.size(); ++i){
cout<<students[i].name<<string(maxlen+1-students[i].name.size(), ' ');
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;
}
计算中值函数 : median.cpp
#include <algorithm>
#include <stdexcept>
#include <vector.h>
using std::domain_error;
using std::sort;
using std::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];
}
读数据函数 read.cpp
#include "Student_info.h"
using std::istream; using std::vector;
using std::cout; using std::endl;
bool compare(const Student_info& x, const Student_info& y){
return x.name < y.name;
}
istream& read(istream& is, Student_info& s){
// cout<<"please enter name/midterm grade/finalterm grade: "<<endl;
is >>s.name >>s.midterm>>s.final;
// cout<<"please enter homework grades: "<<endl;
read_hw(is, s.homework);
return is;
}
istream& read_hw(istream& is, vector<double>& hw){
if(is){
hw.clear();
double x;
while(is>>x)
hw.push_back(x);
//清楚流以使输入动作对下一个学生有效
is.clear();
}
return is;
}
几个计算成绩函数的grade函数(重载:函数名相同,传参不同)
#include <vector.h>
#include <stdexcept>
#include "grade.h"
#include "median.h"
#include "Student_info.h"
using std::domain_error; using std::vector;
double grade(double midterm, double final, double homework){
return midterm*0.2 + final*0.4 + homework*0.4;
}
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);
}
median.h
#ifndef GUARD_median_h
#define GUARD_median_h
#include <vector.h>
double median(std::vector<double>);
#endif
定义students_info结构体,声明读数据read()函数
#ifndef GUARD_Student_info
#define GUARD_Student_info
#include <iostream.h>
#include <string.h>
#include <vector.h>
//结构体的定义
struct Student_info{
std::string name;
double midterm,final;
std::vector<double> homework;
};
bool compare (const Student_info&,const Student_info&);
std::istream& read(std::istream&, Student_info&);
std::istream& read_hw(std::istream&, std::vector<double>&);
#endif
声明计算成绩的三个函数 grade()
#ifndef GUARD_grade_h
#define GUARD_grade_h
#include <vector.h>
#include "Student_info.h"
double grade(double, double, double);
double grade(double, double, const std::vector<double>&);
double grade(const Student_info&);
#endif
以上是本书中第一个比较大的程序的所有内容,仅供参考。
扫描二维码关注公众号,回复:
2658027 查看本文章