求助啊 求助 如何动态显示时间 就是一秒一秒的动态时间
#include<stdio.h>#include<string.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
typedef struct
{
//农历年份
int lunar_year;
//农历月份
int lunar_month;
//农历日期
int lunar_day;
//本月天数
int current_mday;
//本年闰月
int leap_month;
//闰月标记
int leap_flag;
}Lunar_info;
typedef struct
{
//阳历年份
int era_year;
//阳历月份
int era_month;
//阳历日期
int era_day;
}Chinese_era;
int s_year;
int s_month;
int s_day;
/*1900-2050年的农历数据
数据格式说明:
5位十六进制数字 例:04bd8
1位:是否为闰月(30天) 0:不是 1:是
2、3、4位:转换二进制为:0100 1011 1101(1为30天,0为29天)
04bd8表示为(13个月):29,30,29,29,30,29,30,30,30(闰月),30,30,29,30;
5位:如果有闰月,则为月份,没有则为0, 此为8月*/
int lunar_info[]=
{
0x04bd8,0x04ae0,0x0a570,0x054d5,0x0d260,0x0d950,0x16554,0x056a0,0x09ad0,0x055d2,
0x04ae0,0x0a5b6,0x0a4d0,0x0d250,0x1d255,0x0b540,0x0d6a0,0x0ada2,0x095b0,0x14977,
0x04970,0x0a4b0,0x0b4b5,0x06a50,0x06d40,0x1ab54,0x02b60,0x09570,0x052f2,0x04970,
0x06566,0x0d4a0,0x0ea50,0x06e95,0x05ad0,0x02b60,0x186e3,0x092e0,0x1c8d7,0x0c950,
0x0d4a0,0x1d8a6,0x0b550,0x056a0,0x1a5b4,0x025d0,0x092d0,0x0d2b2,0x0a950,0x0b557,
0x06ca0,0x0b550,0x15355,0x04da0,0x0a5b0,0x14573,0x052b0,0x0a9a8,0x0e950,0x06aa0,
0x0aea6,0x0ab50,0x04b60,0x0aae4,0x0a570,0x05260,0x0f263,0x0d950,0x05b57,0x056a0,
0x096d0,0x04dd5,0x04ad0,0x0a4d0,0x0d4d4,0x0d250,0x0d558,0x0b540,0x0b6a0,0x195a6,
0x095b0,0x049b0,0x0a974,0x0a4b0,0x0b27a,0x06a50,0x06d40,0x0af46,0x0ab60,0x09570,
0x04af5,0x04970,0x064b0,0x074a3,0x0ea50,0x06b58,0x055c0,0x0ab60,0x096d5,0x092e0,
0x0c960,0x0d954,0x0d4a0,0x0da50,0x07552,0x056a0,0x0abb7,0x025d0,0x092d0,0x0cab5,
0x0a950,0x0b4a0,0x0baa4,0x0ad50,0x055d9,0x04ba0,0x0a5b0,0x15176,0x052b0,0x0a930,
0x07954,0x06aa0,0x0ad50,0x05b52,0x04b60,0x0a6e6,0x0a4e0,0x0d260,0x0ea65,0x0d530,
0x05aa0,0x076a3,0x096d0,0x04bd7,0x04ad0,0x0a4d0,0x1d0b6,0x0d250,0x0d520,0x0dd45,
0x0b5a0,0x056d0,0x055b2,0x049b0,0x0a577,0x0a4b0,0x0aa50,0x1b255,0x06d20,0x0ada0,
0x14b63
};
/*天干*/
char Gan[10][3] = {"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"};
/*地支*/
char Zhi[12][3] = {"子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"};
/*生肖*/
char Animals[12][3] = {"鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"};
/*24节气
char solar_term[24][5] =
{
"小寒","大寒","立春","雨水","惊蛰","春分",
"清明","谷雨","立夏","小满","芒种","夏至",
"小暑","大暑","立秋","处暑","白露","秋分",
"寒露","霜降","立冬","小雪","大雪","冬至"
};*/
/*农历日*/
char chinese_day[30][5] =
{
"初一","初二","初三","初四","初五","初六","初七",
"初八","初九","初十","十一","十二","十三","十四",
"十五","十六","十七","十八","十九","廿十","廿一",
"廿二","廿三","廿四","廿五","廿六","廿七","廿八",
"廿九","卅十"
};
/*农历月*/
char chinese_month[12][5] = {"正","二","三","四","五","六","七","八","九","十","冬","腊"};
/******************************************************************************************************
** 函数名称:TIME
** 功能描述:取得系统当前时间
** 如:TIME() 返回系统当前时间
** 输 入:
** 输 出: 系统当前时间 成功
** Error:
*******************************************************************************************************/
void TIME()
{
static char *week[]={"日","一","二","三","四","五","六"};
time_t t;
struct tm *tp;
t=time(NULL);
tp=localtime(&t);
printf("%d年%02d月%02d日",tp->tm_year+1900,tp->tm_mon+1,tp->tm_mday);
printf(" %02d:%02d:%02d ",tp->tm_hour,tp->tm_min,tp->tm_sec);
printf("星期%s\t ",week[tp->tm_wday]);
}
/******************************************************************************************************
** 函数名称:buffer
** 功能描述:延迟刷新时间
** 如:buffer(2) 返回2秒后执行下一操作
** 输 入: n 秒
** 输 出: 对应秒数 成功
** Error: 输入错误 失败
*******************************************************************************************************/
void buffer(int n)
{
time_t start,end;
start=time(NULL);
end=time(NULL);
while(end-start<n)
end=time(NULL);
}
/*******************************************************************************************************
** 函数名称:leap
** 功能描述:返回阳历某年的天数,闰年返回366天,平年返回365天
** 如:leap(2000) 返回366天
** 输 入: year 公历年
** 输 出: 对应天数 成功
** Error: 输入错误 失败
********************************************************************************************************/
int leap(int year)
{
if(year%4==0&&year%100!=0||year%400==0) return 366;
else return 365;
}
/*******************************************************************************************************
** 函数名称:day
** 功能描述:返回阳历当月的天数
** 如:day(1) 返回31天
** 输 入: month 公历月
** 输 出: 对应天数 成功
** Error: 输入错误 失败
********************************************************************************************************/
int day(int month)
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) return 31;
if(month==4||month==6||month==9||month==11) return 30;
if(month==2&&leap(s_year)==366) return 29;
else return 28;
}
/*******************************************************************************************************
** 函数名称:get_solar_total
** 功能描述:返回1900年01月01日到M年M月的天数
** 如:get_solar_total(2000,01) 返回36555天
** 输 入: s_year,s_month
** 输 出: M年M月-1900-01-01 成功
** Error: 输入错误 失败
********************************************************************************************************/
int get_solar_total(int solar_year, int solar_month)
{
int total;
int temp_num;
total=0;
for (temp_num=1900;temp_num<solar_year;temp_num++)
{
total+=leap(temp_num);
}
for(temp_num=1;temp_num<solar_month;temp_num++)
{
total+=day(temp_num);
}
return total;
}
/*******************************************************************************************************
** 函数名称:get_week
** 功能描述:返回M年M月的1号为星期几,以1900.01.01为基准1为星期日
** 如:get_week(2000,01) 返回星期六
** 输 入: s_year,s_month
** 输 出: M年M月的一号是星期几 成功
** Error: 输入错误 失败
********************************************************************************************************/
int get_week(int solar_year, int solar_month)
{
int week;
int total;
/* 1900.01.01为星期一*/
week=2;
total=get_solar_total(solar_year, solar_month);
total%=7;
week=week+total;
week%=7;
if (week==0)
week=7;
return week;
}
/******************************************************************************************************
** 函数名称:get_leap_month
** 功能描述:确定是否存在农历的闰月
** 如:get_leap_month(1982) 返回存在闰4月
** 输 入: lunar_year
** 输 出: 是否存在闰月 成功
** Error: 输入错误 失败
*******************************************************************************************************/
int get_leap_month(int lunar_year)
{
return lunar_info[lunar_year-1900]&0xf;
}
/******************************************************************************************************
** 函数名称:get_leap_month_day
** 功能描述:若存在闰月,返回闰月的天数,30?29
** 如:get_leap_month_day(1982) 返回30天
** 输 入: lunar_year
** 输 出: 存在的是大月还是小月 成功
** Error: 输入错误 失败
*******************************************************************************************************/
int get_leap_month_day(int lunar_year)
{
if (get_leap_month(lunar_year))
return ((lunar_info[lunar_year-1900]&0x10000)?30:29);
else
return(0);
}
/******************************************************************************************************
** 函数名称:get_lunar_month_total
** 功能描述:确定农历当月天数,30?29
** 如:get_lunar_month_total(1982,4) 返回30天
** 输 入: lunar_year
** 输 出: 存在的是大月还是小月 成功
** Error: 输入错误 失败
*******************************************************************************************************/
int get_lunar_month_total(int lunar_year, int lunar_month)
{
return ((lunar_info[lunar_year-1900]&(0x10000>>lunar_month))?30:29);
}
/******************************************************************************************************
** 函数名称:get_lunar_year_total
** 功能描述:农历当年总天数,354?355
** 如:get_lunar_year_total(1982) 返回355天
** 输 入: lunar_year
** 输 出: 一年的总天数 成功
** Error: 输入错误 失败
*******************************************************************************************************/
int get_lunar_year_total(int lunar_year)
{
/*348 = 29*12 */
int sum=348;
int i;
for(i=0x8000;i>0x8; i>>=1)
sum+=(lunar_info[lunar_year-1900]&i)?1:0;
return(sum+get_leap_month_day(lunar_year));
}
/******************************************************************************************************
** 函数名称:get_chinese_era
** 功能描述:确定天干地支
** 如:get_chinese_era(2007-1864,str) 返回str='丁亥'
** 输 入: s-year-1864,str
** 输 出: 干+支 成功
** Error: 输入错误 失败
*******************************************************************************************************/
void get_chinese_era(int num, char *str)
{
strcpy(str, Gan[num%10]);
strcat(str, Zhi[num%12]);
}
/*24节气算法
void sTerm()
{}*/
/*返回农历信息*/
int get_lunar_info(int solar_year, int solar_month, Lunar_info *l_info,Chinese_era *china_era)
{
int i;
int total;
int temp_num;
/*阳历1900年01月31日为农历1900年01月01日*/
total=get_solar_total(solar_year,solar_month)-30;
china_era->era_day=total+40;
/*将13和14月作为下一年的1月和2月,便于确定Y年M月D日是星期几
蔡勒公式求星期几:W=[C/4]-2C+y+[y/4]+[13×(M+1)/5]+d-1*/
china_era->era_month=14;
for(i=1900;i<2051&&total>=0;i++)
{
/*temp_num记录从1900到2050年闰年和平年的天数*/
temp_num=get_lunar_year_total(i);
total-=temp_num;
/*累计月*/
china_era->era_month+=12;
}
l_info->lunar_year=i;
if(total<0)
{
total+=temp_num;
/*减年*/
l_info->lunar_year--;
china_era->era_month-=12;
}
/*1864年为农历甲子年*/
china_era->era_year=l_info->lunar_year-1864;
/*返回是否存在闰月*/
l_info->leap_month=get_leap_month(l_info->lunar_year);
l_info->leap_flag=0;
for(i=1;i<13 &&total>0;i++)
{
if (l_info->leap_month>0&&i==(l_info->leap_month+1)&&l_info->leap_flag==0)
{
--i;
temp_num=get_leap_month_day(l_info->lunar_year);
/*存在闰月*/
l_info->leap_flag=1;
}
else
temp_num=get_lunar_month_total(l_info->lunar_year, i);
if (l_info->leap_flag==1&&i==(l_info->leap_month+1))
l_info->leap_flag=0;
total-=temp_num;
if (l_info->leap_flag==0)
china_era->era_month++;
}
if(total==0&&l_info->leap_month>0&&i==l_info->leap_month+1)
if(l_info->leap_flag==1)
l_info->leap_flag=0;
else
{
l_info->leap_flag=1;
--i;
--china_era->era_month;
}
if(total<0)
{
total+=temp_num;
--i;
--china_era->era_month;
}
l_info->lunar_month=i;
l_info->lunar_day=total+1;
if (l_info->leap_flag==0)
l_info->current_mday=get_lunar_month_total(l_info->lunar_year,l_info->lunar_month);
else
l_info->current_mday=get_leap_month_day(l_info->lunar_year);
return 0;
}
/*输出农历日期信息*/
int out_chinese(Lunar_info *l_info,char *chinese_str)
{
/*初一不打印*/
if (l_info->lunar_day!=1)
{
strcpy(chinese_str, chinese_day[l_info->lunar_day-1]);
if (l_info->lunar_day==15&&l_info->lunar_month==1)
strcpy(chinese_str, "元宵节");
if(l_info->lunar_day==5&&l_info->lunar_month==5)
strcpy(chinese_str, "端午节");
if(l_info->lunar_day==7&&l_info->lunar_month==7)
strcpy(chinese_str, "情人节");
if(l_info->lunar_day==15&&l_info->lunar_month==7)
strcpy(chinese_str, "中元节");
if (l_info->lunar_day==15&&l_info->lunar_month==8)
strcpy(chinese_str, "中秋节");
if (l_info->lunar_day==9&&l_info->lunar_month==9)
strcpy(chinese_str, "重阳节");
if(l_info->lunar_month==12)
{
if(l_info->lunar_day==30)
strcpy(chinese_str, "除夕");
if(l_info->current_mday<30&&l_info->lunar_day==29)
strcpy(chinese_str, "除夕");
if (l_info->lunar_day==8)
strcpy(chinese_str, "腊八节");
if (l_info->lunar_day==23)
strcpy(chinese_str, "小年");
}
}
else
{
if (l_info->leap_flag==1)
{
//如果存在闰月就将汉字闰复制到数组里,再连接农历月份
strcpy(chinese_str, "闰");
strcat(chinese_str,chinese_month[l_info->lunar_month-1]);
strcat(chinese_str, "月");
}
else
{
if (l_info->lunar_month==1)
strcpy(chinese_str, "春节");
else
{
strcpy(chinese_str,chinese_month[l_info->lunar_month-1]);
strcat(chinese_str, "月");
if(get_lunar_month_total(l_info->lunar_year,l_info->lunar_month)==30)
strcat(chinese_str,"大");
else
strcat(chinese_str,"小");
}
}
}
return 0;
}
/*界面输出*/
void sub_menue()
{
int week,s_month_total,space_num,c_num;
int i,j;
char tmp_str[9];
Chinese_era c_ear;
Lunar_info l_info;
/*每个月一号为星期几*/
week=get_week(s_year,s_month);
/*返回当月的天数*/
s_month_total=day(s_month);
//将已开辟内存空间tmp_str的首sizeof(tmp_str)个字节的值设为值0,作用在于可以方便的清空结构类型的数组或变量。
memset(&c_ear,0,sizeof(c_ear));
memset(&l_info,0,sizeof(l_info));
/*返回农历信息*/
get_lunar_info(s_year,s_month,&l_info,&c_ear);
memset(tmp_str,0,sizeof(tmp_str));
/*确定干支*/
get_chinese_era(s_year-1864,tmp_str);
system("cls");
system ("color 4f");
system ("cls");
printf("\t ╭───────────────────────────╮\n");
printf("\t1、↑年| 万年历 |\n");
printf("\t |───────────────────────────|\n");
printf("\t2、↓年| 阳历:%d年%02d月 农历:%s年 【%s】 |\n",
s_year,s_month, tmp_str, Animals[(s_year-1864)%12]);
printf("\t |───────────────────────────|\n");
printf("\t3、↑月| 今天是:");
TIME();
printf("|\n");
printf("\t |───────────────────────────|\n");
printf("\t4、↓月|星期日 星期一 星期二 星期三 星期四 星期五 星期六|\n");
printf("\t |───────────────────────────|\n");
printf("\t5、Exit| ");
space_num=week;
for (i=1; i<space_num;i++)
{
printf(" ");
}
c_num=0;
for (i=1; i<=s_month_total;i++)
{
s_day=i;
printf("%02d",s_day);
week+=1;
if((week>7)||(i==s_month_total))
{
week=1;
printf("\n |");
if (i<7)
{
for (j=1;j<space_num;j++)
{
printf(" ");
}
week=space_num;
}
else
week=1;
while(1)
{
week+=1;
memset(tmp_str,0,sizeof(tmp_str));
out_chinese(&l_info,tmp_str);
printf("% 6s", tmp_str);
if (l_info.lunar_day<l_info.current_mday)
l_info.lunar_day=l_info.lunar_day+1;
else
{
if ((l_info.lunar_month==l_info.leap_month)&&l_info.leap_flag==0)
{
/*存在闰月,返回天数*/
l_info.current_mday=get_leap_month_day(l_info.lunar_year);
l_info.leap_flag=1;
}
else
{
l_info.lunar_month=l_info.lunar_month+1;
if (l_info.lunar_month>12)
{
l_info.lunar_month=1;
/*月数大于12进年*/
l_info.lunar_year=l_info.lunar_year+1;
}
/*返回闰月的天数*/
l_info.current_mday=get_lunar_month_total(l_info.lunar_year, l_info.lunar_month);
l_info.leap_flag=0;
}
l_info.lunar_day=1;
}
c_num++;
if ((week>7)||(c_num==s_month_total))
{
week=1;
printf("\n | ");
break;
}
else
printf(" ");
}
}
else
printf(" ");
}
printf("\n");
printf("\t ╰───────────────────────────╯\n");
}
main()
{
char tmp_str[9];
int curr_year,curr_month;
time_t t;
struct tm *tp;
t=time(NULL);
tp=localtime(&t);
lable:;
s_year=tp->tm_year+1900;
s_month=tp->tm_mon+1;
sub_menue();
while (1)
{
printf("\t\t 你可以输入年份(1900-2050),也可以输入(1-5)!\n请输入:");
/*将已开辟内存空间tmp_str的首sizeof(tmp_str)个字节的值设为值0,作用在于可以方便的清空结构类型的数组或变量。*/
memset(tmp_str,0,sizeof(tmp_str));
gets(tmp_str);
/*atoi将字符串转换成整型数,用curr_month,curr_year接收输入的值*/
curr_month=curr_year=atoi(tmp_str);
/*上一年*/
if(curr_year==1&&strlen(tmp_str)==1)
{
s_year--;
if(s_year<1900)
s_year=2050;
}
/*下一年*/
else if(curr_year==2&&strlen(tmp_str)==1)
{
s_year++;
if(s_year>2050)
s_year=1900;
}
/*上一月*/
else if(curr_month==3&&strlen(tmp_str)==1)
{
s_month--;
if (s_month<1)
{
s_year-=1;
s_month=12;
if(s_year<1900)
s_year=2050;
}
}
/*下一月*/
else if(curr_month==4&&strlen(tmp_str)==1)
{
s_month++;
if (s_month>12)
{
s_year+=1;
s_month=1;
if(s_year>2050)
s_year=1900;
}
}
/*退出*/
else if(curr_year==5&&strlen(tmp_str)==1)
{
system("cls");
printf("\n\n\n\n\n\n\n\n\t\t\t 谢 谢 使 用, 再 见 !!!\n\t\t\t ");
buffer(1);
exit(0);
}
else
{
s_year=atoi(tmp_str);
if (strlen(tmp_str)>4||(s_year<1900)||(s_year>2050))
{
printf("\t\t\t Error:年份输入错误,重新输入!!!\n");
buffer(1);
goto lable;
}
printf("请输入月份(1-12):");
memset(tmp_str,0,sizeof(tmp_str));
gets(tmp_str);
s_month=atoi(tmp_str);
if (strlen(tmp_str)>2||(s_month<1)||(s_month>12))
{
printf("\t\t\t Error:月份输入错误,重新输入!!!\n");
buffer(1);
goto lable;
}
}
sub_menue();
}
}