下面是一个万年历的c++程序,不过你自己可以用c来写的。显示系统当前日期的话,只需要调用系统函数gettimeofday()就行
#include
#include
using namespace std;
class Date
{
private:
int year;
int month;
int day;
public:
Date(int y = 1, int m = 1, int d = 1): year(y), month(m), day(d){ }
void SetYear(int y){ year = y; }
void SetMonth(int m){ month = m;}
void SetDay(int d){ day = d; }
int GetYear() const{ return year; }
int GetMonth() const{return month;}
int GetDay() const{ return day; }
static bool IsLeapyear(int y);
static int GetYearDays(int y);
static int GetMonthDays(const Date &d);
static int DateToNum(const Date &d);
static int Week(const Date &d);
};
ostream &operator<<(ostream &out, const Date &d);
istream &operator>>(istream &in, Date &d);
bool Date::IsLeapyear(int y)
{
if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) return true;
else return false;
}
int Date::GetYearDays(int y)
{
if (IsLeapyear(y)) return 366;
else return 365;
}
int Date::GetMonthDays(const Date &d)
{
int n;
switch (d.GetMonth())
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
n = 31;
break;
case 4:
case 6:
case 9:
case 11:
n = 30;
break;
case 2:
if (IsLeapyear(d.GetYear())) n = 29;
else n = 28;
}
return n;
}
int Date::DateToNum(const Date &d)
{
int y, n = 0;
for (y = 1900; y < d.GetYear(); y++)
n += GetYearDays(y);
for (int m = 1; m < d.GetMonth(); m++)
n += GetMonthDays(Date(y, m, 1));
n += d.GetDay();
return n;
}
ostream &operator<<(ostream &out, const Date &d)
{
int i,j,n,m;
m=1;
out<
out<
}
for(j=0;j<7-d.Week(d);j++)
{
out<
}
out<
i=0;
while(n<=(d.GetMonthDays(d)))
{
i++;
out<
if(i%7==0)
out<
out<
}
int Date::Week(const Date &d)
{
int w;
w=(Date::DateToNum(d)-1+1)%7;
return w;
}
int main(void)
{
int s;
int year,month,day;
char m[][5]={"一","二","三","四","五","六","七","八","九","十","十一","十二"};
Date d;
cout <<"输入年份:";
day=0;
month=0;
cin >> year;
cout<
{
month++;
d = Date(year,month,1);
cout<<" ==================================================================== "<
return 0;
}
#include
void main()
{
int i,j,temp,temp_i,day,year;
long int yeardays;
long int sumday(int year); /*所有天数求和 ,不包括今天的 */
int leapyear(int year); /*闰年的定义*/
int month[]={0,31,28,31,30,31,30,31,31,30,31,30,31,29};/*每月天数的判断*/
int everyday[6][14];
printf ("please enter a year:\n");
scanf("%d",&year);
yeardays=sumday(year);
for(temp=1;temp<=6;temp++)
{ getchar();
for(i=0;i<6;i++)
for(j=0;j<14;j++)
everyday[i][j]=0; /*天数初始化*/
switch(temp)
{
case 1:printf("\t 一月 %d\t\t 二月 %d\n",year,year);break;
case 2:printf("\t 三月 %d\t\t 四月 %d\n",year,year);break;
case 3:printf("\t 五月 %d\t\t 六月 %d\n",year,year);break;
case 4:printf("\t 七月 %d\t\t 八月 %d\n",year,year);break;
case 5:printf("\t 九月 %d\t\t 十月 %d\n",year,year);break;
case 6:printf("\t 十一月 %d\t\t 十二月 %d\n",year,year);break;
} /*控制两栏输出*/
printf("日 一 二 三 四 五 六 日 一 二 三 四 五 六 \n");
i=0;j=0;
for(temp_i=0;temp_i<(yeardays%7);temp_i++)
{
everyday[0][j++]=0;
}
day=1;
while(day<=month[2*temp-1]) /*奇数输出*/
{
if(j%7==0)
{
j=0;i++;
}
everyday[i][j++]=day;
day++;
yeardays++;
}
i=0;j=7;
for(temp_i=0;temp_i<(yeardays%7);temp_i++)
{
everyday[0][j++]=0;
}
day=1;
if(leapyear(year)&&temp==1)
while(day<=month[13])
{
if(j%14==0)
{
j=7;i++;
}
everyday[i][j++]=day;
day++;
yeardays++;
}
else
while(day<=month[2*temp]) /*偶数输出*/
{
if (j%14==0)
{
j=7;i++;
}
everyday[i][j++]=day;
day++;
yeardays++;
}
for (i=0;i<6;i++)
{
for(j=0;j<14;j++)
{
if(everyday[i][j]!=0)
printf("%-4d",everyday[i][j]);
else
printf(" ");
if(j==6)
printf(" ");
}
printf("\n");
}
}
}
int leapyear(int year) /*闰年的判断*/
{
if((year%4==0&&year%100!=0)||year%400==0)
return(1);
else
return (0);
}
long int sumday(int year) /*每年天数的判断*/
{
int yearstart=1,leapsum=0;
while(yearstart
if(leapyear(yearstart)!=0)
leapsum++;
yearstart++;
}
return 366*leapsum+365*(year-leapsum); /*不包括今天的*/
}
。。。fopen("系统盘里的时间文件。。。呵呵","r")