6、日历记事本
要求:带有日程提醒功能的日历。
显示信息:用户可以向前查询前一个月的日期,也可以向后查询下一个月的日期。
定时提醒:用户可以针对某一天来添加,删除和编辑这一天的日程提醒信息,当系统时间和提醒时间相吻合时,给出具有提示信息的对话框。
查询信息:用户可以查询到某个月的所有的提示信息。
#include<iostream.h>
#include<string>
#include<fstream.h>
#include<time.h>
#include<windows.h>
#include<stdlib.h>
void turn();
class nb
{
public:
int num;
char sth[50];
friend ostream & operator<<(ostream &out,nb &s);
friend istream & operator>>(istream &in, nb &s);
};
ostream & operator<<(ostream &out,nb &s)
{
out<<s.num<<" "<<s.sth<<'\n';
return out;
}
istream & operator>>(istream &in,nb &s)
{
in>>s.num>>s.sth;
return in;
}
int j=0;
int k=0;
int x;
/************************************************
*** 输出是星期几的函数。 ***
************************************************/
void WEEK(int year,int month,int day)
{
int d=0,i,t=1,week;
int a[12]={31,29,31,30,31,30,31,31,30,31,30,31};//闰年。
int b[12]={31,28,31,30,31,30,31,31,30,31,30,31};//平年。
if(year%4==0&&year%100!=0||year%400==0)//判断是否是闰年。
{
for(i=1;i<month;i++)
d=d+a[i-1];
}
else
{
for(i=1;i<month;i++)
d=d+b[i-1];
}
d=d+day;
week=(year-1)*365+(year-1)/4-(year-1)/100+(year-1)/400+d;//判断这一天是星期几的公式。
week=week%7;//判断这一天是星期几的公式。当week=0,表示星期日。
switch(week)//下面是输出星期。
{
case 1:printf("\t\t%d 年%d 月%d 日 是 星 期: 一 ",year,month,day);break;
case 2:printf("\t\t%d 年%d 月%d 日 是 星 期: 二 ",year,month,day);break;
case 3:printf("\t\t%d 年%d 月%d 日 是 星 期: 三 ",year,month,day);break;
case 4:printf("\t\t%d 年%d 月%d 日 是 星 期: 四 ",year,month,day);break;
case 5:printf("\t\t%d 年%d 月%d 日 是 星 期: 五 ",year,month,day);break;
case 6:printf("\t\t%d 年%d 月%d 日 是 星 期: 六 ",year,month,day);break;
case 0:printf("\t\t%d 年%d 月%d 日 是 星 期: 日 ",year,month,day);break;
}
}
void pri()//菜单显示功能
{
printf("\n\n 请按数字键选择你所需要的功能\n");
printf("\n\t输入 :1 日历查询\n");
printf("\n\t输入 :2 记事\n");
printf("\n\t输入 :3 备忘查找\n");
printf("\n\t输入 :0 退出\n");
}
void DAY(int year,int month)//导入日期并输出日历的函数
{
int week;
int t=1;
int d=0,i;
int a[12]={31,29,31,30,31,30,31,31,30,31,30,31};
int b[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if(year%4==0&&year%100!=0||year%400==0)//if和for是计算d的值,d表示到今天为止总共的天数。
{
for(i=1;i<month;i++)
d=d+a[i-1];
}
else
{
for(i=1;i<month;i++)
d=d+b[i-1];
}
week=(year-1)*365+(year-1)/4-(year-1)/100+(year-1)/400+d;//判断这个月的第一天是星期几
week=week%7;
printf("\n\n\t这是%d年的%d月的日历",year,month);
printf("\n\t日\t一\t二\t三\t四\t五\t六\t\n");
for(i=0;i<=week;i++)//输出日历第一行的格式
{
if(week==6)break;
else cout<<"\t";
t++;
}
printf("\t");
if(year%4==0&&year%100!=0||year%400==0)//输出日历
{
for(i=1;i<=a[month-1];i++)
{
printf("%d\t",i);
if(t%7==0)
printf("\n\t");
t++;
}
}
else
{
for(i=1;i<=b[month-1];i++)
{
printf("%d\t",i);
if(t%7==0)
printf("\n\t");
t++;
}
}
}
void search()//这是查找某一天的函数
{
int year,month,day;
printf("\n\t请 输 入 你 所 想 要 查 找 的 日 期:\n");
printf("\n格 式 为 ****-**-** 例 如(2011-1-1)\n");
scanf("%d-%d-%d",&year,&month,&day);
if(month<=12&&month>=1&&day<=31&&day>=1)
{
system("cls");
WEEK(year,month,day);
DAY(year,month);
cout<<endl;
turn();
}
if(month>12||month<1||day>31)
{
printf("格式有误请重新输入!");
search();
}
}
//******************* 记事部分 **********************************
void record() //记事功能开启
{
ofstream ofile;
ofile.open("1111.txt",ios::app);
nb s;
printf("输入时间和事件吧\n");
cin>>s;
ofile<<s;
ofile.close();
turn();
}
//******************************************************************
void recordlook()
{
cout<<"输入要查询的时间,如20140312"<<endl;
int Time;
int num1=0;
cin>>Time;
ifstream ifile;
ifile.open("1111.txt");
nb s;
ifile>>s;
cout<<"事件查询结果:"<<endl;;
while(!ifile.eof())
{
if(s.num==Time)
{
cout<<s;
num1++;
}
ifile>>s;
}
if(num1==0)
cout<<"不存在!可能输入有误";
ifile.close();
cout<<endl<<endl;
turn();
}
void allrecord()
{
ifstream ifile;
ifile.open("1111.txt");
nb s;
ifile>>s;
cout<<"所有的事件:"<<endl;;
while(!ifile.eof())
{
cout<<s;
ifile>>s;
}
ifile.close();
cout<<endl<<endl;
recordlook();
}
void check(int x)//菜单输入的检验
{
if(x>3)
{
printf("\t\t输入有误,请重新输入!\n");
fflush(stdin);
cin>>x;
check(x);
}
}
void choice(int x) //菜单功能转入
{
switch(x)
{
case 0:
exit(0);
case 1:
search();
break;
case 2:
system("cls");
fflush(stdin);
record();
break;
case 3:
system("cls");
fflush(stdin);
allrecord();
break;
}
}
void turn() //返回
{
printf("输入0返回主菜单!!\n");
while(1)
{
cin>>x;
if(x==0)
{
pri();
fflush(stdin);
cin>>x;
check(x);
choice(x);
}
}
}
int main()
{
int year,month,day;
int a[12]={31,29,31,30,31,30,31,31,30,31,30,31};
int b[12]={31,28,31,30,31,30,31,31,30,31,30,31};
time_t today;
struct tm *p;
time(&today);
p=localtime(&today); //转换成本地时间的日期
year=p->tm_year+1900;
month=p->tm_mon+1;
day=p->tm_mday;
WEEK(year,month,day);
DAY(year,month);
pri();
cin>>x;
check(x);
choice(x);
return 0;
}