Skip to content

Commit f2419f1

Browse files
author
lulongfei
committed
update 14 chapter context
1 parent 059d785 commit f2419f1

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed

chapter14/context/14.1studentc.cpp

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include "14.1studentc.h"
2+
using std::ostream;
3+
using std::endl;
4+
using std::istream;
5+
using std::string;
6+
7+
double Student::Average() const
8+
{
9+
if (scores.size() > 0)
10+
return scores.sum() / scores.size();
11+
else
12+
return 0;
13+
}
14+
15+
16+
const string & Student::Name() const
17+
{
18+
return name;
19+
}
20+
21+
22+
double & Student::operator[](int i)
23+
{
24+
return scores[i];
25+
}
26+
27+
28+
double Student::operator[](int i) const
29+
{
30+
return scores[i];
31+
}
32+
33+
34+
ostream & Student::arr_out(ostream & os) const
35+
{
36+
int i;
37+
int lim = scores.size();
38+
if (lim > 0)
39+
{
40+
for(i = 0; i < lim; i++)
41+
{
42+
os << scores[i] << " ";
43+
if ( i % 5 == 4 )
44+
os << endl;
45+
}
46+
if(i % 5 != 0)
47+
os << endl;
48+
}
49+
else
50+
os << " empty array";
51+
return os;
52+
}
53+
54+
55+
istream & operator>>(istream & is, Student & stu)
56+
{
57+
is >> stu.name;
58+
return is;
59+
}
60+
61+
62+
istream & getline(istream & is, Student & stu)
63+
{
64+
getline(is, stu.name);
65+
return is;
66+
}
67+
68+
69+
ostream & operator<<(ostream & os, const Student & stu)
70+
{
71+
os << "Scores for " << stu.name << ":\n";
72+
stu.arr_out(os);
73+
return os;
74+
}

chapter14/context/14.1studentc.h

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef STUDENTC_H_
2+
#define STUDENTC_H_
3+
4+
#include <iostream>
5+
#include <string>
6+
#include <valarray>
7+
8+
9+
class Student
10+
{
11+
private:
12+
typedef std::valarray<double> ArrayDb;
13+
std::string name;
14+
ArrayDb scores;
15+
std::ostream & arr_out(std::ostream & os) const;
16+
public:
17+
Student(): name("Null Student"), scores(){}
18+
explicit Student(const std::string & s)
19+
:name(s), scores(){}
20+
explicit Student(int n): name("Nully"), scores(n){}
21+
Student(const std::string & s, int n)
22+
:name(s), scores(n){}
23+
Student(const std::string & s, const ArrayDb & a)
24+
:name(s), scores(a){}
25+
Student(const char * str, const double * pd, int n)
26+
:name(str), scores(pd, n) {}
27+
28+
~Student() {}
29+
30+
double Average() const;
31+
const std::string & Name() const;
32+
double & operator[] (int i);
33+
double operator[](int i) const;
34+
35+
friend std::istream & operator>>(std::istream & is, Student & stu);
36+
friend std::istream & getline(std::istream & is, Student & stu);
37+
friend std::ostream & operator<<(std::ostream & os, const Student & stu);
38+
39+
};
40+
41+
#endif

chapter14/context/14.1use_stuc.cpp

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include "14.1studentc.h"
2+
#include <iostream>
3+
4+
5+
using std::cin;
6+
using std::cout;
7+
using std::endl;
8+
9+
void set(Student & sa, int n);
10+
const int pupils = 3;
11+
const int quizzes = 5;
12+
13+
int main()
14+
{
15+
Student ada[pupils] = {Student(quizzes), Student(quizzes), Student(quizzes)};
16+
int i;
17+
for(i = 0; i < pupils; i++)
18+
{
19+
set(ada[i], quizzes);
20+
}
21+
22+
cout << "\nStudent List: \n";
23+
for(i = 0; i < pupils; ++i)
24+
{
25+
cout << ada[i].Name() << endl;
26+
}
27+
28+
cout << "\nResult: ";
29+
30+
for(i = 0; i < pupils; ++i)
31+
{
32+
cout << endl << ada[i];
33+
cout << "average: " << ada[i].Average() << endl;
34+
}
35+
36+
cout << "Done.\n";
37+
return 0;
38+
}
39+
40+
41+
void set(Student & sa, int n)
42+
{
43+
cout << "Please enter the student's name: ";
44+
getline(cin, sa);
45+
cout << "Please enter " << n << " quiz scores: \n";
46+
for(int i = 0; i < n; i++)
47+
{
48+
cin >> sa[i];
49+
}
50+
while (cin.get() != '\n')
51+
continue;
52+
}
53+
54+

0 commit comments

Comments
 (0)