添加复用

This commit is contained in:
朱毅骏 2021-08-30 18:39:01 +08:00
parent d79408ee5b
commit 17ad4b5f81
5 changed files with 173 additions and 1 deletions

View File

@ -53,4 +53,7 @@ add_executable(useStock20 class/useStock20.cpp)
add_executable(namessp namespacetest/namessp.cpp)
add_executable(string new/string.cpp)
add_executable(placenew new/placenew.cpp)
add_executable(usett extend/usett.cpp)
add_executable(usett extend/usett.cpp)
add_executable(test5_1 test5.cpp)
add_executable(studentc reusing/studentc.cpp)
add_executable(uses_stuc reusing/uses_stuc.cpp)

68
base/reusing/studentc.cpp Normal file
View File

@ -0,0 +1,68 @@
//
// Created by zhuyijun on 2021/8/30.
//
#include "studentc.h"
using std::ostream;
using std::endl;
using std::istream;
using std::string;
/**
*
* @return double
*/
double Student::Average() const {
if (scores.size() > 0) {
return scores.sum() / scores.size();
}
return 0;
}
const std::string & Student::Name() const {
return name;
}
double &Student::operator[](int i) {
return scores[i];
}
double Student::operator[](int i) const {
return scores[i];
}
ostream &Student::arr_out(std::ostream &os) const {
int i;
int lim = scores.size();
if (lim > 0) {
for (i = 0; i < lim; ++i) {
os << scores[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 >> stu.name;
return is;
}
istream & getline(istream & is,Student & stu){
getline(is,stu.name);
return is;
}
ostream & operator<<(ostream & os,const Student & stu){
os <<"Scores for "<<stu.name <<":\n";
stu.arr_out(os);
return os;
}

48
base/reusing/studentc.h Normal file
View File

@ -0,0 +1,48 @@
//
// Created by zhuyijun on 2021/8/30.
//
#ifndef BASE_STUDENTC_H
#define BASE_STUDENTC_H
#include <iostream>
#include <string>
#include <valarray>
class Student {
private:
//使用typedef 对std::valarray<double>进行简化表达
typedef std::valarray<double> ArrayDb;
std::string name;
ArrayDb scores;
std::ostream & arr_out(std::ostream & os) const;
public:
Student() :name("Null Student"),scores() {}
/**
* C++explicit关键字只能用于修饰只有一个参数的类构造函数,
* , ,
* implicit, ,
* implicit()
* @param s
*/
explicit Student(const std::string & s):name(s),scores(){}
explicit Student(int n):name("Nully"),scores(n){}
Student(const std::string & s, int n) :name(s),scores(n){}
Student(const std::string & s,const ArrayDb & a):name(s),scores(a){}
Student(const char * str,const double * pd,int n) :name(str),scores(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_STUDENTC_H

View File

@ -0,0 +1,42 @@
//
// Created by zhuyijun on 2021/8/30.
//
#include <iostream>
#include "studentc.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;
}
}

11
base/test5.cpp Normal file
View File

@ -0,0 +1,11 @@
//
// Created by zhuyijun on 2021/8/4.
//
#include <iostream>
using namespace std;
int main() {
int a = 010 & 001;
cout << a;
return 0;
}