21、某年齡段內的學生
問題描述 :
實驗目的:組合物件、靜態成員的使用。
實驗內容:
設計一個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.”,不包括引號。
如果有多個學生,輸出學生的順序為學生加入的順序。
輸出中無多餘空格,也無多餘空行。
輸入範例 :
2007 1990
1
1 Rose 2002 1 1
1
2 Kevin 1992 1 1
1
3 John 2012 1 1
3
1992 2010
2
2
1
2 Tom 1987 1 1
3
1987 2010
輸出範例 :
1 Rose 2002 1 1
2 Kevin 1992 1 1
3 John 2007 1 1
1 Rose 2002 1 1
3 John 2007 1 1
2 Tom 1990 1 1
解題程式碼:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<algorithm>
#include<set>
#include<vector>
#include<queue>
using namespace std;
class Date {
private:
int year;
int month;
int day;
static int maxYear;
static int minYear;
public:
static void SetMaxYear(int maxY);
static void SetMinYear(int minY);
Date(int birthYear,int birthMonth,int birthDay);
Date();
friend class StudentList;
};
int Date::maxYear = 0;
int Date::minYear = 0;
Date::Date(int birthYear, int birthMonth, int birthDay)
{
year= birthYear;
month = birthMonth;
day = birthDay;
}
Date::Date()
{
;
}
void Date::SetMaxYear(int maxY)
{
Date::maxYear = maxY;
}
void Date::SetMinYear(int minY)
{
Date::minYear = minY;
}
class Student {
private:
int num;
string name;
Date birthday;
public:
Student(int num, string name, Date birthday);
Student();
friend class StudentList;
};
Student::Student()
{
;
}
Student::Student(int num, string name, Date birthday)
{
Student::num = num;
Student::name = name;
Student::birthday = birthday;
}
class StudentList {
private:
Student people[100];
int t=0;
public:
void AddStudent(Student stu);
void DeleteStudent(int num);
StudentList GetStudent(int year1, int year2);
void Print();
};
void StudentList::AddStudent(Student stu)
{
if (stu.birthday.year > Date::maxYear)
stu.birthday.year = Date::maxYear;
if (stu.birthday.year < Date::minYear)
stu.birthday.year = Date::minYear;
people[t] = stu;
t++;
}
void StudentList::DeleteStudent(int num)
{
int i;
for (i = 0; i < t; i++)
if (people[i].num == num)
break;
if (i != t)
{
for (; i < t - 1; i++)
people[i] = people[i + 1];
t--;
}
return;
}
StudentList StudentList::GetStudent(int year1, int year2)
{
StudentList ans;
int i;
for (i = 0; i < t; i++)
if (people[i].birthday.year <= year2 && people[i].birthday.year >= year1)
ans.people[ans.t++] = people[i];
return ans;
}
void StudentList::Print()
{
int i;
for (i = 0; i < t; i++)
cout << people[i].num << " " << people[i].name <<" "<< people[i].birthday.year << " " << people[i].birthday.month << " " << people[i].birthday.day << endl;
if (t == 0)
cout << "No result." << endl;
return;
}
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;
}