C++中的代码重用(一)

//statement.h
#ifndef _STATEMENT_H_
#define _STATEMENT_H_
#include<iostream>
#include<string>
#include<valarray>//包含size(),max(),min(),sum(),operator[](int i)
//定义包含关系
using namespace std;
class student//合并valarray和string类
{
private:
	typedef valarray<double> Arraydb;
	string name;
	Arraydb score;
	ostream& arr_out(ostream &os) const;
public:
	student() :name("null"), score(){}
	explicit student(int i) :name("null"), score(i){}
	student(const char *c, const double *pt, int n) :name(c), score(pt, n){}
	explicit student(const string &str):name(str), score(){}
	student(const string &str, int n,const double &d) :name(str), score(n,d){}
	student(const student &stu);
	~student(){}
	const string& get_name()const;
	void show(ostream &os)const;
	const double operator[](int i) const{ return score[i]; }
	double& operator[](int i){ return score[i]; }//以便于输入数据
	double average() const;
	friend istream& getline(istream &is, student &stu);
	friend istream & operator >>(istream &is, student &stu);
	friend ostream & operator<<(ostream &os, student &stu);
};

#endif //_STATEMENT_H_

//define.cpp
/**
*Copyright U+00A9 2018 XXXXXX. All Right Reserved.
*Filename:
*Author:XXXXXXX
*Date:2018.12.01
*Version:VS 2013
*Description:
**/

//#include<iostream>
//#include<cstdlib>
#include"statement.h"

using namespace std;
ostream& student::arr_out(ostream&os) const
{
	int i;
	int lim = score.size();
	if (lim > 0)
	{
		for (i = 0; i < lim; i++)
		{
			os << score[i] << " ";
			if (i % 5 == 4)
				os << endl;
		}
		if (i % 5 != 0)
			os << endl;
	}
	else
		os << "empty array!";
	return os;
}

student::student(const student &stu)
{
	name = stu.name;
	score = stu.score;
}
const string& student::get_name()const
{
	return name;
}

double student::average() const
{
	if (score.size() > 0)
		return (double)score.sum() / score.size();
	else
		return 0;
}

void student::show(ostream &os)const
{
	os << "Scores for " << name << ":\n";
	arr_out(os);
	os << "Average is " << average() << endl;
}

istream& getline(istream &is, student &stu)
{
	getline(is, stu.name);
	return is;
}

istream & operator >>(istream &is, student &stu)
{
	is >> stu.name;
	return is;
}

ostream & operator<<(ostream &os, student &stu)
{
	os << "Scores for " << stu.name << ":\n";
	stu.arr_out(os);
	return os;
}

//main.cpp
/**
*Copyright U+00A9 2018 XXXX. All Right Reserved.
*Filename:
*Author:XXXXXX
*Date:2018.12.01
*Version:VS 2013
*Description:
**/

#include<iostream>
#include<cstdlib>
#include"windows.h"
#include"statement.h"
using namespace std;
void set(student &stu, int n);
const int pupils=3;
const int quizzes = 5;
int main()
{
	student stu[pupils] = {student(quizzes),student(quizzes),student(quizzes)};
	cout << "Set infomation for student.\n";
	for (int j = 0; j < pupils; j++)
		set(stu[j], quizzes);
	cout << "The follow content is information for student: ";
	for (int j = 0; j < pupils; j++)
		cout << stu[j].get_name() << ",";
	cout << ".\n";
	for (int t = 0; t < pupils; t++)
	{
		stu[t].show(cout);
	}
  system("pause");
  return 0;
}

void set(student &stu, int n)
{
	cout << "please input Student'name: ";
	getline(cin,stu);
	cout << "please input " << n << " quizzes'score: ";
	for (int i = 0; i < n; i++)
		cin >> stu[i];
	while (cin.get() != '\n')
		continue;
}

程序运行如下

猜你喜欢

转载自blog.csdn.net/weixin_43871369/article/details/84994045