添加复用

This commit is contained in:
朱毅骏 2021-08-30 19:22:36 +08:00
parent 17ad4b5f81
commit 76810761bf
3 changed files with 148 additions and 0 deletions

67
base/reusing/studenti.cpp Normal file
View File

@ -0,0 +1,67 @@
//
// Created by zhuyijun on 2021/8/30.
//
#include "studenti.h"
using std::ostream;
using std::endl;
using std::istream;
using std::string;
/**
*
* @return double
*/
double Student::Average() const {
if (ArrayDb::size() > 0) {
return ArrayDb::sum() / ArrayDb::size();
}
return 0;
}
const std::string & Student::Name() const {
return (const string &) *this;
}
double &Student::operator[](int i) {
return ArrayDb::operator[](i);
}
double Student::operator[](int i) const {
return ArrayDb::operator[](i);
}
ostream &Student::arr_out(std::ostream &os) const {
int i;
int lim = ArrayDb::size();
if (lim > 0) {
for (i = 0; i < lim; ++i) {
os << ArrayDb::operator[](i) << " ";
if (i % 5 == 4) {
os << endl;
}
}
if (i % 5 != 0) {
os << endl;
}
} else {
os << " empty array ";
}
return os;
}
istream & operator>>(istream & is,Student & stu){
is >> (string &) stu;
return is;
}
istream & getline(istream & is,Student & stu){
getline(is,(string &) stu);
return is;
}
ostream & operator<<(ostream & os,const Student & stu){
os <<"Scores for "<<(const string &) stu <<":\n";
stu.arr_out(os);
return os;
}

39
base/reusing/studenti.h Normal file
View File

@ -0,0 +1,39 @@
//
// Created by zhuyijun on 2021/8/30.
//
#ifndef BASE_STUDENTI_H
#define BASE_STUDENTI_H
#include <iostream>
#include <valarray>
#include <string>
/**
*
*/
class Student: private std::string,private std::valarray<double>{
private:
typedef std::valarray<double> ArrayDb;
std::ostream & arr_out(std::ostream & os) const;
public:
Student() :std::string("Null Student"),ArrayDb(){}
explicit Student(const std::string & s) :std::string(s),ArrayDb(){}
explicit Student(int n):std::string("Nully"),ArrayDb(n){}
Student(const std::string & s,int n) :std::string(s),ArrayDb(n){}
Student(const std::string & s,const ArrayDb & a) : std::string(s),ArrayDb(a){}
Student(const char * str,const double * pd,int n):std::string(str),ArrayDb(pd,n){}
~Student(){};
double Average() const;
const std::string & Name() const;
double & operator[](int i);
double operator[](int i) const;
//友元
friend std::istream & operator>>(std::istream & is,Student & stu);
friend std::istream & getline(std::istream & is,Student & stu);
friend std::ostream & operator<<(std::ostream & os,const Student & stu);
};
#endif //BASE_STUDENTI_H

View File

@ -0,0 +1,42 @@
//
// Created by zhuyijun on 2021/8/30.
//
#include <iostream>
#include "studenti.h"
using namespace std;
void set(Student & sa,int n);
const int pupils = 3;
const int quizzes = 5;
int main() {
Student ada[pupils] = {
Student{quizzes},Student{quizzes},Student{quizzes}
};
for (int i = 0; i < pupils; ++i) {
set(ada[i],quizzes);
}
cout <<"\nStudent List:\n";
for (int i = 0; i < pupils; ++i) {
cout <<ada[i].Name() << endl;
}
cout <<"\nResult:";
for (int i = 0; i < pupils; ++i) {
cout << endl <<ada[i];
cout <<"average: "<<ada[i].Average() << endl;
}
cout << "Done.\n";
return 0;
}
void set(Student & sa,int n){
cout <<"Please enter the student's name: ";
getline(cin,sa);
cout <<"Please enter " << n <<" quiz scores : \n";
for (int i = 0; i < n; ++i) {
cin >> sa[i];
}
while (cin.get() != '\n'){
continue;
}
}