万年历程序里的时间同步问题,求指教?
#include <stdio.h>#include <stdlib.h>
#include <time.h>
void Time(); //获取当前系统时间
void whatisit(); //查询某年某月某日是星期几
void leapyear(); //查询某年是否是闰年
int isleapyear(int); //判断某年是否为闰年
int whatisita(int ,int ); //判断当天是星期几
void printyearday(); //打印某年全年月历
int everymonth(int,int); //查询某月天数
void printmonth(int,int); //打印月历
void printmonthday(); //打印某年某月月历
char *week[]={"星期天","星期一","星期二","星期三","星期四","星期五","星期六"};
void main()
{
int n;
printf(" 欢迎使用万年历系统! \n");
Time();
while(1)
{
printf(" -----------------------------------\n");
printf(" 1. 查询某年某月某日是星期几\n");
printf(" 2. 查询某年是否是闰年 \n");
printf(" 3. 打印某年全年月历 \n");
printf(" 4. 打印某年某月月历 \n");
printf(" 0. 退出 \n");
printf(" -----------------------------------\n");
printf("请选择操作:");
scanf("%d",&n);
switch(n)
{ case 1:whatisit();break;
case 2: leapyear();break;
case 3:printyearday();break;
case 4:printmonthday();break;
case 0:printf("感谢您的使用!\n");exit(0);
default:printf("您的输入错误,请按要求输入!\n");break;
}
}
}
//------------------------
void Time()
{
time_t rawtime;
struct tm * timeinfo; //struct tm 时间结构
time ( &rawtime ); // 获取系统时间
timeinfo = localtime ( &rawtime ); //转换为当地时间
printf( " 当前时间为: %4d-%02d-%02d %02d:%02d:%02d\n", 1900+timeinfo->tm_year,1+timeinfo->tm_mon,timeinfo->tm_mday,
timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec );
}
//-------------------------
void whatisit()
{ int y,m,d,s,sum,n,i,k=1;
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
while(k)
{ printf("请依次输入年 月 日:");
scanf("%d %d %d",&y,&m,&d);
if(m<1||m>12||d<1||d>31){ printf("输入错误,请重新输入!\n");continue;}
s=0;
for(i=1;i<m;i++)
s=s+a[i];
s=s+d;
if(isleapyear(y)&&m>2) s=s+1;
sum=s;
n=(y-1+(y-1)/4-(y-1)/100+(y-1)/400+sum)%7;
printf("这天是:%s\n",week[n]);
printf("是否继续查询(Y/N)?:");
getchar();
if((getchar())=='y'||(getchar())=='Y') k=1;
else k=0;
}
}
//-------------------------
int isleapyear(int year)
{ return((year%4==0&&year%100!=0)||(year%400==0));}
//-------------------------
void leapyear()
{ int y,k=1;
while(k)
{
printf("请输入年份:");
scanf("%d",&y);
if(isleapyear(y)) printf("%d年是闰年!\n",y);
else printf("%d年不是闰年!\n",y);
printf("是否继续查询(Y/N)?:");
getchar();
if((getchar())=='y'||(getchar())=='Y') k=1;
else k=0;
}
}
//----------------------
int whatisita(int y,int m)
{ int n,i,s=0;
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
for(i=1;i<m;i++)
s=s+a[i];
if(isleapyear(y)&&m>2) s=s+1;
s=s+1;
n=(y-1+(y-1)/4-(y-1)/100+(y-1)/400+s)%7;
return n;
}
//----------------------
void printyearday()
{ int y,val,sum,i,k=1;
while(k)
{ printf("请输入要查询的年份:");
scanf("%d",&y);
for(i=1;i<=12;i++)
{ printf("%d年%d月:\n",y,i);
val=whatisita(y,i);
sum=everymonth(y,i);
printmonth(val,sum);
printf("\n");
printf("\n");
}
printf("是否继续查询(Y/N)?:");
getchar();
if((getchar())=='y'||(getchar())=='Y') k=1;
else k=0;
}
}
//------------------------
int everymonth(int year,int month )
{ int s;
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
s=a[month];
if(isleapyear(year)&&month==2) s=s+1;
return s;
}
//--------------------------
void printmonth(int val,int sum)
{ int n=1,i,j=0;
char *x[7]={"星期天","星期一","星期二","星期三","星期四","星期五","星期六"};
for(i=0;i<7;i++)
printf(" %s",x[i]);
printf("\n");
while(j<val) {printf(" ");j++;}
while(n<=sum)
{ if((n+val-1)%7==0)
printf("\n");
printf("%7d",n);
n++;
}
}
//-----------------------
void printmonthday()
{ int y,val,sum,m,k=1;
while(k)
{ printf("请输入要查询的年份 月份:");
scanf("%d %d",&y,&m);
printf("%d年%d月:\n",y,m);
val=whatisita(y,m);
sum=everymonth(y,m);
printmonth(val,sum);
printf("\n");
printf("是否继续查询(Y/N)?:");
getchar();
if((getchar())=='y'||(getchar())=='Y') k=1;
else k=0;
}
}
//----------------------------------------
程序运行时获取的当地时间,怎么才能同步?就是让那个电子时间表是走的。。不是静止的,
而又不影响后面万年历的各种操作。