期末的实践
#include <iostream>
#include <string.h>
using namespace std;
#define MAX 100
class CDate // 定义日期类
{
private:
int year; // 年
int month; // 月
int day; // 日
public:
CDate(int y=0,int m=0,int d=0);
bool operator < (CDate d);
//利用bool返回ture或者false
//重载"<"运算符
friend istream & operator >> (istream &in,CDate &d);
friend ostream & operator<<(ostream &out,CDate &d);
friend bool CheckValid(CDate d);//检查天数是否正确
friend bool LeapYear(int year);//判断是否是闰年
//友元函数访问私有成员
void SetDate(int y,int m,int d);
};
CDate::CDate(int y,int m,int d):year(y),month(m),day(d) {}
//初始化表实现数据成员的初始化
void CDate::SetDate(int y,int m,int d)//构造函数
{
year=y;
month=m;
day=d;
}
istream &operator>>(istream &in,CDate &d)
{
char ch1,ch2;
cout<<"请输入日期(如例子:2008-08-08):";
while(1)
{
cin>>d.year>>ch1>>d.month>>ch2>>d.day;
if (ch1=='-' && ch2=='-')
if (CheckValid(d)) break;
cerr<<"时间格式或取值不正确! 请重新输入\n";
//cerr:输出到标准错误的ostream对象,常用于程序错误信息;不支持缓冲
}
return cin;//错误后重新输入
}
// 重载输出运算符<<
ostream &operator<<(ostream &out,CDate &d)
{
out<<d.year<<"年"<<d.month<<"月"<<d.day<<"日";
return out;
}
// 判断日期d1<d2
bool CDate::operator < (CDate d)
{
if (year<d.year) return true;
if (year>d.year) return false;
if (month<d.month) return true;
if (month>d.month) return false;
if (day<d.day) return true;
return false;
}
// 检查是否为闰年
bool LeapYear(int year)
{
if (year%4==0 && year%100 || year%400==0)
return true;
return false;
}
// 检查日期合法性
bool CheckValid(CDate d)
{
int n;
if (d.month<1 || d.month>12) return false;
if (d.day<1) return false;
n=31;
switch(d.month)//switch开关语句剔除特殊情况
{
case 2:
if (LeapYear(d.year))
n=29;
else
n=28;
break;
case 4:
case 6:
case 9:
case 11:
n=30;
break;
}
if (d.day>n) return false;
return true;
}
class CProblem//定义题型变量
{
private:
char *type; // 题类
bool und; // 是否理解
CDate date; // 录入日期
public:
static int num; // 理解统计数
//静态局部变量num
CProblem();
void InputData();
//定义友元函数来访问私有成员
friend void Sort(); // 排序
friend void FindType(); // 按题类查询
friend void Statistic(); // 按是否理解统计
friend void Display(); // 显示全部信息
} pro[MAX];
int CProblem::num=0;//将num初始化为0
CProblem::CProblem() {}
// 输入信息
void CProblem::InputData()
{
int p;
char s[66];//为题型定义一个字符数组
cout<<"请输入题目类型(NO."<<num<<"):\n";
cout<<"题目类型:";
cin>>s;//输入题型
type=new char[strlen(s)+1];//申请动态储存空间
strcpy(type,s);//将s这个字符串复制到type
cout<<"是否理解(1-是,0-否):";
cin>>p;
if (p) und=true;
else und=false;
cin>>date;
cout<<"信息已录入"<<endl;
}
// 冒泡排序
void Sort()
{
int i,j,p,num;
char *pt;
bool pu;
CDate pd;
num=CProblem::num;
for(i=1; i<num; i++)
{
p=i;
for(j=i+1; j<=num; j++)
if (pro[j].date<pro[p].date) p=j;//找到当前未排序元素中时间最前的对象的下标
if (p==i) continue;//跳出
//交换stu[i]和stu[p]
pt=pro[i].type;
pro[i].type=pro[p].type;
pro[p].type=pt;
pu=pro[i].und;
pro[i].und=pro[p].und;
pro[p].und=pu;
pd=pro[i].date;
pro[i].date=pro[p].date;
pro[p].date=pd;
}
cout<<"按时间排序如下:"<<endl;
for(i=1; i<=num; i++)
{
cout<<pro[i].date<<pro[i].type<<endl;
}
}
// 按题型查询
void FindType()
{
char type[41];
int i,num;
cout<<"请输入题型:";
cin>>type;
num=CProblem::num;
for(i=1; i<=num; i++)
if (strcmp(pro[i].type,type)==0) break;//利用strcmp进行字符串比较
if (i>num)
{
cout<<"查无此题!"<<endl<<endl;
return;
}
//如果查到了,显示题目信息
cout<<"题型:"<<pro[i].type<<endl;
cout<<"是否理解:";
if (pro[i].und)
cout<<"是"<<endl;
else
cout<<"否"<<endl;
cout<<"日期:"<<pro[i].date<<endl;
cout<<endl;
}
// 按理解与否统计
void Statistic()
{
int i,num,s1,s0;//定义理解数与不懂数
num=CProblem::num;
s1=0;//将理解数初始
s0=0;//将不懂数初始
for(i=1; i<=num; i++)
if (pro[i].und==1)
s1++;
else
s0++;
cout<<"理解题数:"<<s1<<endl;
cout<<"不懂题数:"<<s0<<endl;
cout<<"加油学习吧!"<<endl;
}
// 显示全部信息
void Display()
{
int i,num;
num=CProblem::num;
for(i=1; i<=num; i++)
{
cout<<pro[i].type<<"\t";
if (pro[i].und)
cout<<"是";
else
cout<<"否";
cout<<"\t"<<pro[i].date<<endl;
}
cout<<endl;
}
int main()
{
char *menu[]= { ""," 1********按题型输入******",
" 2********按时间排序******",
" 3********按题型查询******",
" 4********按理解统计******",
" 5********显示全部信息****",
" 6********退出************" };
int i,p;
bool end;
end=false;
while(!end)
{
for(i=1; i<7; i++)
cout<<" "<<menu[i]<<endl;
cin>>p;
switch(p)
{
case 1: // 输入信息
CProblem::num++;
pro[CProblem::num].InputData();
break;
case 2: // 排序
Sort();
break;
case 3: // 按题型查询
FindType();
break;
case 4: // 按是否理解统计
Statistic();
break;
case 5: // 显示全部信息
Display();
break;
case 6: // 退出
end=true;
break;
}
}
return 0;
}