24 某年龄段内的学生
作者: Turbo时间限制: 1S章节: 类与对象
问题描述 :
实验目的:组合对象、静态成员的使用。
实验内容:
设计一个Date类,包含:
1、私有实例属性年月日。
2、私有静态属性maxYear和minYear,
3、公有静态方法设置maxYear和minYear的值,如:
static void SetMaxYear(int maxY)
{
maxYear = maxY;
}
设计一个Student类,数据成员包含int类型的学号、string类型的姓名、Date类型的生日。
设计一个StudentList类,包含:
1、私有属性:Student对象数组,可声明大小为100,即保存不超过100个元素
2、私有属性:数组中实际保存的元素个数
3、公有方法:AddStudent(Student stu),将传入的形参stu增加到数组中
4、公有方法:DeleteStudent(int num),根据传入的学号num从数组中删除学生
5、公有方法:StudentList GetStudent(int year1, int year2),其中:
形参:包括两个int型值year1和year2,并且year1<=year2
功能:查找Student对象数组中所有出生年在year1和year2之间(含year1和year2)的学生
返回值:返回符合条件的学生,用一个StudentList对象返回
6、公有方法:Print(),输出Student对象数组中所有学生的信息,包括学号、姓名、生日(年月日)。如果数组中有多个元素(学生),则一个元素输出一行。如果数组中没有元素,则输出“No result.”
以上三个类只规定了必要的几个方法,其它的方法请自行设计,包括构造函数。
下面解释Date类中静态成员maxYear和minYear的作用:
由于学生入学年龄有限制,比如,规定大学生必须在13岁到45岁之间。因此,如果现在是2020年,则入学学生中,maxYear为2020-13=2007,minYear为2020-45=1975。
程序运行时,首先由用户输入maxYear和minYear,并存储到Date类中。
当创建Student对象时,如果发现学生的出生年份大于maxYear,则设置该学生的出生年为maxYear;如果发现学生的出生年份小于minYear,则设置该学生的出生年为minYear。
输入学生生日时,不需要检查日期是否合法。
main函数可参考如下代码:
int main()
{
int num, birthYear, birthMonth, birthDay;
int maxYear, minYear, year1, year2;
int op;
string name;
StudentList sl;
Student stu;
cin >> maxYear >> minYear;
Date::SetMaxYear(maxYear);
Date::SetMinYear(minYear);
while (cin >> op)
{
switch (op)
{
case 1:
cin >> num >> name >> birthYear >> birthMonth >> birthDay;
stu = Student(num, name, Date(birthYear, birthMonth, birthDay));
sl.AddStudent(stu);
break;
case 2:
cin >> num;
sl.DeleteStudent(num);
break;
case 3:
cin >> year1 >> year2;
StudentList temp = sl.GetStudent(year1, year2);
temp.Print();
break;
}
}
return 0;
}
输入说明 :
首先输入两个整数maxYear和minYear。并保证maxYear>=minYear。
然后输入多组测试数据,每组测试数据包含两行:
第一行输入一个操作的种类:
1:增加一个学生
2:删除一个学生
3:输入两个年份,输出出生日期在这两年(包含这两年)之间的所有学生的信息。
第二行输入所需要的参数:
对于第1个操作,第二行输入学生的信息,包括学号、姓名(中间无空格)、出生年、出生月、出生日。
对于第2个操作,第二行输入一个编号num
对于第3个操作,第二行输入两个year:year1和year2,并保证year1<=year2。
所有输入数据之间以一个空格分隔。无多余空格或空行,两组测试数据之间也无空行。
在输入时,将保证不会向学生列表中添加一个学号(num)已经存在的学生。
输出说明 :
仅第3个操作有输出,因此,测试数据中必然包含第3个操作。
第3个操作输出符合条件的所有学生的信息,包括学号、姓名、出生年、出生月、出生日。
每个学生的输出占一行,输出的数据项之间以一个空格分隔。
如果没有符合条件的学生,则输出一行“No result.”,不包括引号。
如果有多个学生,输出学生的顺序为学生加入的顺序。
输出中无多余空格,也无多余空行。
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Date
{
public:
static int maxYear, minYear;
int year, month, day;
static void SetMaxYear(int y)
{
maxYear = y;
}
static void SetMinYear(int y)
{
minYear = y;
}
Date(int y, int m, int d)
{
year = (y > maxYear) ? maxYear : (y < minYear ? minYear : y);
month = m;
day = d;
}
};
int Date::maxYear = 0;
int Date::minYear = 0;
class Student
{
public:
int num;
string name;
Date birth;
Student(int n, string na, Date b) :num(n), name(na), birth(b) {}
bool operator==(const Student& s)
{
return num == s.num;
}
};
class StudentList
{
public:
vector<Student> students;
void AddStudent(Student s)
{
students.push_back(s);
}
void DeleteStudent(int num)
{
for (vector<Student>::iterator it = students.begin(); it != students.end(); it++)
{
if (it->num == num)
{
students.erase(it);
break;
}
}
}
StudentList GetStudent(int year1, int year2)
{
StudentList result;
for (Student s : students)
{
if (s.birth.year >= year1 && s.birth.year <= year2)
{
result.students.push_back(s);
}
}
return result;
}
void Print()
{
if (students.empty())
{
cout << "No result." << endl;
}
else
{
for (Student s : students)
{
cout << s.num << " " << s.name << " " << s.birth.year << " " << s.birth.month << " " << s.birth.day << endl;
}
}
}
};
int main()
{
int num, birthYear, birthMonth, birthDay;
int maxYear, minYear, year1, year2;
int op;
string name;
StudentList sl;
Student stu(0, "", Date(0, 0, 0));
cin >> maxYear >> minYear;
Date::SetMaxYear(maxYear);
Date::SetMinYear(minYear);
while (cin >> op)
{
switch (op)
{
case 1:
cin >> num >> name >> birthYear >> birthMonth >> birthDay;
stu = Student(num, name, Date(birthYear, birthMonth, birthDay));
sl.AddStudent(stu);
break;
case 2:
cin >> num;
sl.DeleteStudent(num);
break;
case 3:
cin >> year1 >> year2;
StudentList temp = sl.GetStudent(year1, year2);
temp.Print();
break;
}
}
return 0;
}