#include<iostream>
#include<string.h>
usingnamespacestd;
#defineMAX100
classCDate // 定义日期类
{
private:
unsignedshortintyear; // 年
unsignedshortintmonth; // 月
unsignedshortintday; // 日
public:
CDate(inty = 0, int m = 0, intd = 0);
booloperator < (CDate d);
friendistream & operator >> (istream&in, CDate &d);
friendostream & operator<<(ostream&out, CDate &d);
friendboolCheckValid(CDate d);
friendboolLeapYear(int year);
voidSetDate(int y, intm, int d);
};
CDate::CDate(inty,intm,intd):year(y), month(m),day(d) {}
// 设置日期
voidCDate::SetDate(inty,intm,intd)
{
year = y;
month = m;
day = d;
}
// 重载输入运算符>>
istream &operator>>(istream &in, CDate &d)
{
charch1, ch2;
cout<< "请输入日期(输入格式:YYYY-MM-DD):";
while(1)
{
cin>> d.year >> ch1 >> d.month>> ch2 >> d.day;
if(ch1 == '-' && ch2 == '-')
if(CheckValid(d)) break;
cerr<< "时间格式或取值不正确! 请重新输入\n";
}
returncin;
}
// 重载输出运算符<<
ostream &operator<<(ostream &out, CDate &d)
{
out<< d.year << "年" << d.month<< "月" << d.day<< "日";
returnout;
}
// 判断日期d1<d2
boolCDate::operator< (CDated)
{
if(year<d.year) returntrue;
if(year>d.year) returnfalse;
if(month<d.month) returntrue;
if(month>d.month) returnfalse;
if(day<d.day) returntrue;
returnfalse;
}
// 检查是否为闰年
bool LeapYear(intyear)
{
if(year% 4 == 0 && year % 100 || year% 400 == 0)
returntrue;
returnfalse;
}
// 检查日期合法性
bool CheckValid(CDated)
{
intn;
if(d.month<1|| d.month>12) returnfalse;
if(d.day<1)returnfalse;
n = 31;
switch(d.month)
{
case2:
if(LeapYear(d.year))
n= 29;
else
n= 28;
break;
case4:
case6:
case9:
case11:
n =30;
break;
}
if(d.day>n)returnfalse;
returntrue;
}
classCStudent
{
private:
char*name; //姓名
boolsex; //性别
CDate date; // 出生日期,类对象作数据成员
public:
staticintnum; // 学生人数
CStudent();
voidInputData();
friendvoidSort(); // 排序
friendvoidFindName(); // 按姓名查询
friendvoidStatistic(); // 按性别统计
friendvoidDisplay(); // 显示全部信息
} stu[MAX];
intCStudent::num= 0;
CStudent::CStudent() {}
// 输入信息
voidCStudent::InputData()
{
intp;
chars[41];
cout<< "请输入学生信息(NO." << num << "):\n";
cout<< "姓名:";
cin>> s;
name = newchar[strlen(s)+ 1];
strcpy(name,s);
cout<< "性别(1-男,0-女):";
cin>> p;
if(p) sex = true;
elsesex = false;
cin>> date;
cout<< endl;
}
// 排序
void Sort()
{
inti, j, p, num;
char*tn;
boolts;
CDate td;
num = CStudent::num;
for(i = 1; i<num; i++)
{
p = i;
for(j = i + 1; j <= num; j++)
if(stu[j].date<stu[p].date) p = j;//找到当前未排序元素中年龄最小的对象的下标
if(p == i) continue;
//下面交换stu[i]和stu[p]
tn =stu[i].name;
stu[i].name= stu[p].name;
stu[p].name= tn;
ts =stu[i].sex;
stu[i].sex= stu[p].sex;
stu[p].sex= ts;
td =stu[i].date;
stu[i].date= stu[p].date;
stu[p].date= td;
}
}
// 按姓名查询
void FindName()
{
charname[41];
inti, num;
cout<< "请输入姓名:";
cin>> name;
num = CStudent::num;
for(i = 1; i <= num; i++)
if(strcmp(stu[i].name, name) == 0) break;
if(i>num)
{
cout<< "查无此人!" << endl << endl;
return;
}
//如果查到了,显示学生信息
cout<< "姓名:" << stu[i].name << endl;
cout<< "性别:";
if(stu[i].sex)
cout<< "男" << endl;
else
cout<< "女" << endl;
cout<< "生日:" << stu[i].date << endl;
cout<< endl;
}
// 按性别统计
void Statistic()
{
inti, num, s1, s0;
num = CStudent::num;
s1 = 0;
s0 = 0;
for(i = 1; i <= num; i++)
if(stu[i].sex == 1)
s1++;
else
s0++;
cout<< "男生人数:" << s1 << endl;
cout<< "女生人数:" << s0 << endl;
cout<< endl;
}
// 显示全部信息
void Display()
{
inti, num;
num = CStudent::num;
for(i = 1; i <= num; i++)
{
cout<< stu[i].name << "\t";
if(stu[i].sex)
cout<< "男";
else
cout<< "女";
cout<< "\t" << stu[i].date <<endl;
}
cout<< endl;
}
int main()
{
char*menu[] = { "", "输入信息", "排序", "按姓名查询", "按性别统计", "显示全部信息", "退出" };
inti, p;
boolend;
end = false;
while(!end)
{
for(i = 1; i<7; i++)
cout<< i << " " << menu[i] << endl;
cin>> p;
switch(p)
{
case1: //输入信息
CStudent::num++;
stu[CStudent::num].InputData();
break;
case2: //排序
Sort();
break;
case3: //按姓名查询
FindName();
break;
case4: //按性别统计人数
Statistic();
break;
case5: //显示全部信息
Display();
break;
case6: //退出
end= true;
break;
}
}
return0;
}
这是在网上找的类似学生信息输入与查询的程序
想要弄成一个窗口,有一个界面。
求教
#include<string.h>
usingnamespacestd;
#defineMAX100
classCDate // 定义日期类
{
private:
unsignedshortintyear; // 年
unsignedshortintmonth; // 月
unsignedshortintday; // 日
public:
CDate(inty = 0, int m = 0, intd = 0);
booloperator < (CDate d);
friendistream & operator >> (istream&in, CDate &d);
friendostream & operator<<(ostream&out, CDate &d);
friendboolCheckValid(CDate d);
friendboolLeapYear(int year);
voidSetDate(int y, intm, int d);
};
CDate::CDate(inty,intm,intd):year(y), month(m),day(d) {}
// 设置日期
voidCDate::SetDate(inty,intm,intd)
{
year = y;
month = m;
day = d;
}
// 重载输入运算符>>
istream &operator>>(istream &in, CDate &d)
{
charch1, ch2;
cout<< "请输入日期(输入格式:YYYY-MM-DD):";
while(1)
{
cin>> d.year >> ch1 >> d.month>> ch2 >> d.day;
if(ch1 == '-' && ch2 == '-')
if(CheckValid(d)) break;
cerr<< "时间格式或取值不正确! 请重新输入\n";
}
returncin;
}
// 重载输出运算符<<
ostream &operator<<(ostream &out, CDate &d)
{
out<< d.year << "年" << d.month<< "月" << d.day<< "日";
returnout;
}
// 判断日期d1<d2
boolCDate::operator< (CDated)
{
if(year<d.year) returntrue;
if(year>d.year) returnfalse;
if(month<d.month) returntrue;
if(month>d.month) returnfalse;
if(day<d.day) returntrue;
returnfalse;
}
// 检查是否为闰年
bool LeapYear(intyear)
{
if(year% 4 == 0 && year % 100 || year% 400 == 0)
returntrue;
returnfalse;
}
// 检查日期合法性
bool CheckValid(CDated)
{
intn;
if(d.month<1|| d.month>12) returnfalse;
if(d.day<1)returnfalse;
n = 31;
switch(d.month)
{
case2:
if(LeapYear(d.year))
n= 29;
else
n= 28;
break;
case4:
case6:
case9:
case11:
n =30;
break;
}
if(d.day>n)returnfalse;
returntrue;
}
classCStudent
{
private:
char*name; //姓名
boolsex; //性别
CDate date; // 出生日期,类对象作数据成员
public:
staticintnum; // 学生人数
CStudent();
voidInputData();
friendvoidSort(); // 排序
friendvoidFindName(); // 按姓名查询
friendvoidStatistic(); // 按性别统计
friendvoidDisplay(); // 显示全部信息
} stu[MAX];
intCStudent::num= 0;
CStudent::CStudent() {}
// 输入信息
voidCStudent::InputData()
{
intp;
chars[41];
cout<< "请输入学生信息(NO." << num << "):\n";
cout<< "姓名:";
cin>> s;
name = newchar[strlen(s)+ 1];
strcpy(name,s);
cout<< "性别(1-男,0-女):";
cin>> p;
if(p) sex = true;
elsesex = false;
cin>> date;
cout<< endl;
}
// 排序
void Sort()
{
inti, j, p, num;
char*tn;
boolts;
CDate td;
num = CStudent::num;
for(i = 1; i<num; i++)
{
p = i;
for(j = i + 1; j <= num; j++)
if(stu[j].date<stu[p].date) p = j;//找到当前未排序元素中年龄最小的对象的下标
if(p == i) continue;
//下面交换stu[i]和stu[p]
tn =stu[i].name;
stu[i].name= stu[p].name;
stu[p].name= tn;
ts =stu[i].sex;
stu[i].sex= stu[p].sex;
stu[p].sex= ts;
td =stu[i].date;
stu[i].date= stu[p].date;
stu[p].date= td;
}
}
// 按姓名查询
void FindName()
{
charname[41];
inti, num;
cout<< "请输入姓名:";
cin>> name;
num = CStudent::num;
for(i = 1; i <= num; i++)
if(strcmp(stu[i].name, name) == 0) break;
if(i>num)
{
cout<< "查无此人!" << endl << endl;
return;
}
//如果查到了,显示学生信息
cout<< "姓名:" << stu[i].name << endl;
cout<< "性别:";
if(stu[i].sex)
cout<< "男" << endl;
else
cout<< "女" << endl;
cout<< "生日:" << stu[i].date << endl;
cout<< endl;
}
// 按性别统计
void Statistic()
{
inti, num, s1, s0;
num = CStudent::num;
s1 = 0;
s0 = 0;
for(i = 1; i <= num; i++)
if(stu[i].sex == 1)
s1++;
else
s0++;
cout<< "男生人数:" << s1 << endl;
cout<< "女生人数:" << s0 << endl;
cout<< endl;
}
// 显示全部信息
void Display()
{
inti, num;
num = CStudent::num;
for(i = 1; i <= num; i++)
{
cout<< stu[i].name << "\t";
if(stu[i].sex)
cout<< "男";
else
cout<< "女";
cout<< "\t" << stu[i].date <<endl;
}
cout<< endl;
}
int main()
{
char*menu[] = { "", "输入信息", "排序", "按姓名查询", "按性别统计", "显示全部信息", "退出" };
inti, p;
boolend;
end = false;
while(!end)
{
for(i = 1; i<7; i++)
cout<< i << " " << menu[i] << endl;
cin>> p;
switch(p)
{
case1: //输入信息
CStudent::num++;
stu[CStudent::num].InputData();
break;
case2: //排序
Sort();
break;
case3: //按姓名查询
FindName();
break;
case4: //按性别统计人数
Statistic();
break;
case5: //显示全部信息
Display();
break;
case6: //退出
end= true;
break;
}
}
return0;
}
这是在网上找的类似学生信息输入与查询的程序
想要弄成一个窗口,有一个界面。
求教