完整题干:
设计一个程序,用于人事管理,要求可以对人员的基本情况进行修改。设计一个“人员”类,包括有编号、性别、参加工作日期、出生日期,现在的日期等数据类型,其中“参加工作日期、出生日期、现在的日期”为另一个“日期”类内嵌子对象,用成员函数实现对人员信息的录入和显示,用函数Age()计算并输出人员的工龄和年龄,另设计一个函数实现对人员的排序(按年龄由大到小进行排序)。输入的数据应保存至磁盘文件中。
个人答案:
#include<string>
#include<iostream>
#include<fstream>
using namespace std;
class Date
{
public:
void shuchu1();
void shuru1()
{
cin >> year1 >> month1 >> day1; //输入工作日期
cin >> year2 >> month2 >> day2; //输入出生日期
cin >> year3 >> month3 >> day3; //输出现在的日期
}
int y1()
{
return(year1);
} //返回工作日期,出生日期和现在日期的年份,月份和天数,方便计算年龄与工龄
int y2()
{
return(year2);
}
int y3()
{
return(year3);
}
int m1()
{
return(month1);
}
int m2()
{
return(month2);
}
int m3()
{
return(month3);
}
int d1()
{
return(day1);
}
int d2()
{
return(day2);
}
int d3()
{
return(day3);
}
private:
int year1, month1, day1;
int year2, month2, day2;
int year3, month3, day3;
};
class Person :public Date
{
public:
void Age(); //计算工龄和年龄的函数
void shuchu2()
{
ofstream outfile("D:/text/f1.txt", ios::app);
outfile << "该员工的姓名与性别分别是:" << xm << " " << xb << endl;
}
void shuru2()
{
cin >> xm >> xb; //输入姓名与性别
}
string xinmin()
{
return(xm); //返回姓名
}
private:
string xm, xb;
};
void Person::Age()
{
ofstream outfile("D:/text/f1.txt", ios::app);
int year = 0;//这三个是最后输出的年龄,精确到日
int month = 0;
int day = 0;
int monthday1[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };//平年的数组
int monthday2[12] = { 31,29,31,30,31,30,31,31,30,31,30,31 };//闰年的数组
if (m3() > m1())
{
if (d3() < d1())
{
if (((y1() % 4 == 0) && (y1() % 100 != 0)) || ((y1() % 400 == 0)))//判断闰年(四年一闰 百年不闰 四百又闰)
{
year = y3() - y1();
month = m3() - m1() - 1;
day = monthday2[m1() - 1] - d1() + d3() + 1;
outfile << "该员工的工龄与年龄为:" << year << "年" << month << "个月 " << day << "天" << " " << y3() - y2() << "岁" << endl;
}
else
{
year = y3() - y1();
month = m3() - m1() - 1;
day = monthday1[m1() - 1] - d1() + d3() + 1;
outfile << "该员工的工龄与年龄为:" << year << "年" << month << "个月 " << day << "天" << " " << y3() - y2() << "岁" << endl;
}
}
else
{
year = y3() - y1();
month = m3() - m1();
day = d3() - d1() + 1;
outfile << "该员工的工龄与年龄为:" << year << "年" << month << "个月 " << day << "天" << " " << y3() - y2() << "岁" << endl;
}
}
else
{
if (d3() < d1())
{
if (((y1() % 4 == 0) && (y1() % 100 != 0)) || ((y1() % 400 == 0)))//判断闰年(四年一闰 百年不闰 四百又闰)
{
year = y3() - y1() - 1;
month = 12 - m1() + m3() - 1;
day = monthday2[m1() - 1] - d1() + d3() + 1;
outfile << "该员工的工龄与年龄为:" << year << "年" << month << "个月 " << day << "天" << " " << y3() - y2() << "岁" << endl;
}
else
{
year = y3() - y1() - 1;
month = 12 - m1() + m3() - 1;
day = monthday1[m1() - 1] - d1() + d3() + 1;
outfile << "该员工的工龄与年龄为:" << year << "年" << month << "个月 " << day << "天" << " " << y3() - y2() << "岁" << endl;
}
}
else
{
year = y3() - y1() - 1;
month = 12 - m1() + m3();
day = d3() - d1() + 1;
outfile << "该员工的工龄与年龄为:" << year << "年" << month << "个月 " << day << "天" << " " << y3() - y2() << "岁" << endl;
}
}
}
void Date::shuchu1()
{
ofstream outfile("D:/text/f1.txt", ios::app);
outfile << "该员工的工作日期为:" << year1 << "年" << month1 << "月" << day1 << "日" << endl;
outfile << "出生日期为:" << year2 << "年" << month2 << "月" << day2 << "日" << endl;
outfile << "现在的日期为:" << year3 << "年" << month3 << "月" << day3 << "日" << endl;
}
int main()
{
ofstream outfile("D:/text/f1.txt", ios::app);
Person yg; //给Person类设立对象
int a[3];
string c[3]; //同理
int b;
for (int i = 0; i < 3; i++) //三个员工
{
cout << "请输入第" << i + 1 << "位员工的姓名与性别:";
yg.shuru2();
cout << "请输入第" << i + 1 << "位员工的工作日期,出生日期和现在的日期:" << endl;
yg.shuru1();
yg.shuchu2();
yg.shuchu1();
yg.Age();
b = yg.y3() - yg.y2();
a[i] = b; //将第i位的员工年龄赋值给a[i]
c[i] = yg.xinmin(); //将第i为的员工的姓名赋值给c[i]
cout << endl;
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3 - i - 1; j++)
{
if (a[j] < a[j + 1])
{
int d;
string k;
d = a[j];
a[j] = a[j + 1];
a[j + 1] = d; //冒泡排序法 按年龄从大到小
k = c[j];
c[j] = c[j + 1];
c[j + 1] = k;
}
}
}
outfile << "按年龄从大到小排序为:" << endl;
for (int g = 0; g < 3; g++)
{
outfile << c[g] << " " << a[g] << "岁" << endl;
}
}