File tree 3 files changed +169
-0
lines changed
3 files changed +169
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 << " \n Student List: \n " ;
23
+ for (i = 0 ; i < pupils; ++i)
24
+ {
25
+ cout << ada[i].Name () << endl;
26
+ }
27
+
28
+ cout << " \n Result: " ;
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
+
You can’t perform that action at this time.
0 commit comments