不好意思啦,代码在下面~
// Manage students and teachers' information.
// features: set the students' scores and select the students
// whose scores are higher than 80 and print.
#include <assert.h>
#include <iostream>
#include <string>
using namespace std;
class Student;
// the teacher's information.
class Teacher {
public:
Teacher(string, const unsigned int); // 1 student at lease.
~Teacher(); // destructor.
void insert_student(Student *); // insert a new student.
void sort_students(); // sort the students' information.
void print_students(); // print all the student's information.
private:
string name_;
unsigned int maxn_; // the max number of the students.
unsigned int stu_num_; // the number of the students.
Student** pStu_; // the pointer point to the pointer of Student.
};
// Teacher's constructor.
Teacher::Teacher(string s, const unsigned int n) {
name_ = s;
maxn_ = n;
stu_num_ = 0;
pStu_ = new Student*[n]; // dynamically generate array.
}
// Teacher's destructor.
Teacher::~Teacher() {
delete[] pStu_;
pStu_ = NULL;
}
// insert a student.
void Teacher::insert_student(Student *p) {
assert(stu_num_ < maxn_);
*(pStu_ + stu_num_) = p; // point to the new student.
stu_num_++; // the number of the students increase.
}
// sort the students' information using bubble sorting algorithms.
void Teacher::sort_students() {
for(int i = 0; i != stu_num_ - 1; ++i) {
for(int j = 0; j != stu_num_ - 1 - i; ++j) {
if (pStu_[j]->score_ > pStu_[j + 1]->score_) {
// swap the two pointer.
Student *p;
p = pStu_[j];
pStu_[j] = pStu_[j + 1];
pStu_[j + 1] = p;
}
}
}
}
// print all the student's information.
void Teacher::print_students() {
for(int i = 0; i != stu_num_; ++i) {
cout << pStu_[i]->name_ << ": " << pStu_[i]->score_ << endl;
}
}
// the student's information.
class Student {
public:
Student(string, double);
friend void Teacher::sort_students();
friend void Teacher::print_students();
friend void select_student(Student*, double);
private:
string name_; // the student's name.
double score_; // the student's score.
};
// Student's constructor.
Student::Student(string n, unsigned double s) {
name_ = n;
score_ = s;
}
// select the students whose scores are higher than d
// and print them.
void select_student(Student* s, double d) {
if (s->score_ >= 80.0) {
cout << s->name_ << ": " << s->score_ << endl;
}
}
// test.
int main() {
Teacher t1("Mr.Wang", 3);
Student s1("student1", 90.2) , s2("student2", 85.3), s3("student3", 45.3);
t1.insert_student(&s1);
t1.insert_student(&s2);
t1.insert_student(&s3);
t1.sort_students();
t1.print_students();
return 0;
}