添加复用
This commit is contained in:
parent
d79408ee5b
commit
17ad4b5f81
@ -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
68
base/reusing/studentc.cpp
Normal 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
48
base/reusing/studentc.h
Normal 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
|
42
base/reusing/uses_stuc.cpp
Normal file
42
base/reusing/uses_stuc.cpp
Normal 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
11
base/test5.cpp
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user