您的位置:首页 > 其它

一个简单的日历实现程序--calendar

2013-04-04 14:27 633 查看
calendar.h

#ifndef _CALENDAR_H_
#define _CALENDAR_H_
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
//##################################################
#define MONTH 12
#define WEEK 7
//##################################################
const char* MonthName[MONTH+1]={"\0","January","Febrary","March","April","May","June","July","August","September","October","November","December"};//enum of month
const char* WeekName[WEEK]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};//enum of week
typedef struct tm _ctime;//struct defined in time.h
typedef struct month_t{
int mn;//which month
int beg;//Which weekday this month begins
int days;//how many days in this week
}mt;
//##################################################
class calendar{
private:
int year;
mt mont[MONTH+1];
bool if_leap_year()const;
inline void map_date_week(const int&,const int&,const int&,_ctime* dt);
void format_print(const int&,const int&)const;
void ini_calendar(const int&,_ctime*);
void show_month(const int&)const;
public:
calendar();
calendar(const int&);
~calendar(){};
void show_calendar()const;
};
#endif

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

calendar.cpp

#include "calendar.h"

calendar::calendar(){
time_t t = time(NULL);
_ctime* loc_t=localtime(&t);
year = loc_t->tm_year;
year+=1900;
ini_calendar(year,loc_t);
}

void calendar::ini_calendar(const int& Year, _ctime* loc_t){
loc_t->tm_year= Year-1900;
bool if_leap=if_leap_year();

for(int i=1;i<=MONTH;i++){
loc_t->tm_mon=i-1;
loc_t->tm_mday=1;
mktime(loc_t);
mont[i].mn=i;
mont[i].beg=loc_t->tm_wday;
switch(i){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:mont[i].days=31;break;
case 2: mont[i].days=if_leap?29:28;break;
case 4:
case 6:
case 9:
case 11:mont[i].days=30;break;
}
}
}

calendar::calendar(const int& q_year):year(q_year){
_ctime* loc_t = (_ctime*)malloc(sizeof(_ctime));
ini_calendar(year,loc_t);
if(loc_t){
free(loc_t);
loc_t = NULL;
}
}

bool calendar::if_leap_year()const{
if(year%100==0){
if(year%400==0) return true;
else return false;
}
else{
if(year%4==0) return true;
else return false;
}
}

void calendar::map_date_week(const int& m_year,const int& m_month,const int& m_date,_ctime* dt){
dt->tm_year = m_year-1900;
dt->tm_mon = m_month-1;
dt->tm_mday = m_date;
mktime(dt);
}

void calendar::format_print(const int& beg,const int& dt)const{
if(beg==0) printf("%3d",dt);
else{
for(int i=1;i<beg;i++) printf("\t ");
printf("\t%3d",dt);
}
}

void calendar::show_calendar()const{
printf("*******************||%d||********************\n",year);
for(int i=1;i<=MONTH;i++) show_month(i);
}

void calendar::show_month(const int& i)const{
printf("**************||%s %2d||*****************\n",MonthName[i],i);
for(int j=0;j<WEEK;j++){
if(j==0) printf("%s",WeekName[j]);
else printf("\t%s",WeekName[j]);
}
printf("\n=======================================================\n");
format_print(mont[i].beg,1);
for(int j=2;j<=mont[i].days;j++){
int tmp_mode=((j-1)+mont[i].beg)%7;
if(tmp_mode) printf("\t%3d",j);
else printf("\n%3d",j);
}
printf("\n=======================================================\n");
}

int main(int argc,char** argv){
calendar* my_cal;
if(argc==1) my_cal= new calendar();
else if(argc==2){
int year = atoi(argv[1]);
my_cal=new calendar(year);
}
else{
perror("error arguments");
exit(-1);
}
my_cal->show_calendar();
return 0;
}

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: