admin管理员组文章数量:1531403
2023年12月28日发(作者:)
#include
#include
#include
typedef struct
{
int year;
int month;
int day;
}date;
int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int leap1(int year);
int calculate(date a,date b);
date print();
void distance();
void week();
void later();
int judge(date d1);
void main()
{
int m;
char ch;
do{
printf("------------------MENU------------------n");
printf("----------------------------------------n");
printf(" ate difference date and date
printf(" ate someday is day of week
printf(" ate from after some day's day
printf("
printf("please input the choice :(1~4)n");
scanf("%d",&m);
system("clear");
switch(m)
{
case 1:
distance();break;
case 2:
week();break;
case 3:
later();break;
case 4:
exit(0);
n");
n");
n");
n");
default:
printf("please input correct numbern");
}
printf("do you want to continue the menu:(Y/N)n");
fflush(stdin);
scanf("%c",&ch);
system("clear");
}while(ch=='y'&&'Y');
}
int year_to_day(int year_begin, int year_end)
{
int sum = 0;
int number = year_begin;
while(number != year_end)
{
sum = sum + 365 + leap1(year_begin);
number++;
}
return sum;
}
int month_to_day(date now)
{
int i, day = 0;
for (i = 0; i < - 1; ++i)
{
day = day + month[i];
}
return ( >= 3) ? day + + leap1() : day + ;
}
int calculate(date begin,date end)
{
return year_to_day(, ) - month_to_day(begin) + month_to_day(end);
}
int leap1(int year)
{
return ((year % 400 == 0) || ((year % 4 == 0)&& (year %100 != 0))) ? 1 : 0;
}
date print()
{
char date1[10];
char year[5], month[3], day[3];
date d1;
memset(date1, 0x0, sizeof(date1));
memset(year, 0x0, sizeof(year));
memset(month, 0x0, sizeof(month));
memset(day, 0x0, sizeof(day));
printf("please input the date,format[20120203]n");
fflush(stdin);
scanf("%s", date1);
date1[9]= '0';
memcpy(year, date1, 4);
memcpy(month, date1+4, 2);
memcpy(day, date1+6, 2);
= atoi(year);
= atoi(month);
= atoi(day);
return d1;
}
void distance()
{
date d1,d2;
int sum;
char ch;
do{
d1=print();
d2=print();
printf ("year = %dmonth = %dday = %dn", , , );
printf ("year = %dmonth = %dday = %dn", , , );
if(judge(d1)&&judge(d2))
{
if(<=)
{
sum=calculate(d1,d2);
}
else
{
sum=calculate(d2,d1);
}
printf("the distance between two dates is %dn",sum);
}
printf("do you want to continue?Y/Nn");
fflush(stdin);
scanf(" %c",&ch);
system("clear");
}while(ch=='y'||ch=='Y');
}
void week()
{
date d1;
int century = 0;
int year = 0;
int weekday = 0;
int month = 0;
char ch;
do
{
system("clear");
d1=print();
if (judge(d1))
{
if ( < 3)
{
month = + 12;
--;
}
else
month = ;
century = /100;
year = % 100;
weekday = year + (year/4)+(century/4)-(2*century)+(26*(month + 1)/10) + -
1;
weekday = (weekday < 0) ? weekday + 7: weekday;
printf ("weekday = %dn", weekday);
switch(weekday%7)
{
case 0:
printf("Sundayn");break;
case 1:
printf("Mondayn");break;
case 2:
printf("Tuesdayn");break;
case 3:
printf("Wednesdayn");break;
case 4:
printf("Thursdayn");break;
case 5:
printf("Fridayn");break;
case 6:
printf("Saturdayn");break;
default:
printf ("Errorn");
break;
}
printf("do you want to continue?Y/Nn");
fflush(stdin);
scanf(" %c",&ch);
}
}while(ch=='y'||ch=='Y');
system("clear");
}
date now_to_later(date now, int gap)
{
date d1;
int sum, sum_bak;
int temp;
d1 = now;
sum = gap;
if(judge(d1)&&(sum>0) && (sum < 3649270))
{
while(sum>365)
{
if(>=3)
{
++;
sum=sum-365-leap1();
}
else
{
sum=sum-365-leap1();
++;
}
}
while(sum > 0)
{
if ( != 2)
{
temp = month[ - 1] - + 1;
}
else
{
temp = month[ - 1] +leap1()- + 1;
}
sum_bak = sum;
sum = sum - temp;
if (sum >= 0)
{
++;
= 1;
if ( > 12)
{
= 1;
++;
}
sum_bak = sum;
}
}
+= sum_bak;
}
return d1;
}
date now_to_fronter(date now, int gap)
{
date d1;
int sum, sum_bak;
int temp;
d1 = now;
sum = gap;
if(judge(d1)&&(sum < 0) && (sum > -3649270))
{
while(sum < -365)
{
if(>=3)
{
sum=sum+365+leap1();
--;
}
else
{
--;
sum=sum + 365 + leap1();
}
}
sum_bak = sum;
while(sum < 0)
{
temp =-;
sum = sum - temp;
if (sum <= 0)
{
--;
if ( < 1)
{
= 12;
--;
}
if ( == 2)
= month[ - 1] + leap1();
else
= month[ - 1];
sum_bak = -sum;
}
}
if (sum_bak < 0)
{
= + sum_bak;
}else
{
if ( == 2)
= month[ - 1] + leap1() - sum_bak;
else
= month[ - 1] - sum_bak;
}
}
return d1;
}
void later()
{
date d1;
int sum, sum_bak;
int temp;
char ch;
do
{
d1=print();
printf("please input the number of days[+to future-to fronter]:n");
fflush(stdin);
scanf("%d",&sum);
if (sum > 0)
d1 = now_to_later(d1, sum);
else
d1 = now_to_fronter(d1, sum);
if(judge(d1))
{
printf("the date is flowing:n");
printf("the year is : %dn",);
printf("the month is : %dn",);
printf("the day is : %dn",);
}
else
{
printf ("errot to calculaten");
}
printf("do you want to continue?Y/Nn");
fflush(stdin);
scanf(" %c",&ch);
system("clear");
}while(ch=='y'||ch=='Y');
}
int judge(date d1)
{
return (( > 0 && <= 9999) &&
( > 0 && <= 12) &&
( > 0 && (
(( == 2) && ( < month[ - 1] + leap1()))||
(( != 2) && ( < month[ - 1]))
}
))) ? 1 : 0;
版权声明:本文标题:计数程序 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1703769889a70980.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论