只有一个源文件main函数
代码有点多,有好几处可以简短,但是你知道的我比较懒!
这个代码最主要的是
int w;
w = (day + 2 * month2 + 3 * (month2 + 1) / 5 + year2 + year2 / 4 - year2 / 100 + year2 / 400) % 7;/*基姆拉尔森计算公式*/ 这个公式
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>int isleap(int year);/*判断闰年*/
int validdate(int, int, int);/*判断日期是否正确*/
int tianshu(int year, int mouth, int day);/*天数总和*/
int whatday();/*计算当年第几天*/
int date_difference();/*日期差*/
int year_difference(int, int, int, int, int, int);/*年份天数差*/
int month_difference(int, int, int, int, int);/*月份天数差*/
int day_difference(int, int);/*日天数差*/
int yeardays(int);/*该年有多少天*/
int calculation_date();/*推算日期*/
int datemuch(int, int, int, int, int, int);/*判断哪个日期大*/int main(int argc, char* argv[])
{
while (1)
{
printf("----------------日期计算器-----------------\n");
printf("\t\t1.计算当年第几天\n");
printf("\t\t2.计算两个日期差\n");
printf("\t\t3.推算N天后日期\n");
printf("\t\t0.退出\n");
printf("请选择(0~~3选项):");
int option;/*接收选项*/
scanf("%d", &option);
switch (option)
{
case 1://printf("计算当年第几天");
whatday();
break;
case 2://printf("计算两个日期差");
date_difference();
break;
case 3://printf("推算N天后日期");
calculation_date();
break;
case 0:printf("谢谢使用!"); return 0;
default:printf("输入的选项有误,请重新输入!\n");
}
}
}int isleap(int year)/*判断闰年*/
{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
return 1;
}
return 0;
}int validdate(int year, int month, int day)/*判断日期是否正确*/
{
if (month < 1 || month>12) {
printf("您输入的月份有误,应在(1~~12)之间!\n");
return 0;
}
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: if (day < 1 || day>31) {
printf("您输入的日有误,应在 1~~~31 之间!\n");
return 0;
}break;
case 4:
case 6:
case 9:
case 11:if (day < 1 || day>30) {
printf("您输入的日有误,应在 1~~~30 之间!\n");
return 0;
}break;
case 2:if (isleap(year) && (day < 1 || day>29)) {
printf("%d年是闰年,您输入的日有误,应在 1~~~29之间\n", year);
return 0;
}
if (!isleap(year) && (day < 1 || day>28)) {
printf("%d年是平年,您输入的日有误,应在 1~~~28之间\n", year);
return 0;
}break;
}
return 1;
}int tianshu(int year, int month, int day) /*天数总和*/
{
int num[11] = { 31,28,31,30,31,30,31,31,30,31,30 };
int days = day;
int i;
for (i = 0; i < month - 1; i++) {
days += num[i];
}
if (isleap(year)) {
days += 1;
}
return days;
}int whatday()/*计算当年第几天*/
{
int year, month, day, days;
printf("请输入年月日(xxxx-xx-xx,例:2022-3-3):");
scanf("%d-%d-%d", &year, &month, &day);
if (!validdate(year, month, day))
{
printf("您输入的日期有误!\n");
return 0;
}
days = tianshu(year, month, day);
printf("%d-%d-%d是%d年的第%d天。\n", year, month, day, year, days);
printf("距离%d年结束还有%d天。\n", year, yeardays(year) - days);
}int date_difference()/*日期差*/
{
int year1, month1, day1, year2, month2, day2;
do {
printf("请输入第一个日期xxxx-xx-xx(例:2022-3-3):");
scanf("%d-%d-%d", &year1, &month1, &day1);
} while (!validdate(year1, month1, day1));/*判断日期是否正确*/ do {
printf("请输入第二个日期xxxx-xx-xx(例:2022-4-3):");
scanf("%d-%d-%d", &year2, &month2, &day2);
} while (!validdate(year2, month2, day2));/*判断日期是否正确*/ datemuch(year1, month1, day1, year2, month2, day2);
}int year_difference(int year1, int month1, int day1, int year2, int month2, int day2)/*年份天数差*/
{
int days = 0, start, end, startdays, enddays;
if (year1 > year2)
{
start = year2;
end = year1;
startdays = tianshu(year2, month2, day2);
enddays = tianshu(year1, month1, day1);
}
else
{
start = year1;
end = year2;
startdays = tianshu(year1, month1, day1);
enddays = tianshu(year2, month2, day2);
}
for (int i = start; i <= end; i++)
{
if (i == start)
{
days += yeardays(start) - startdays;
}
else if (i == end)
{
days += enddays;
}
else
{
days += yeardays(i);
}
}
return days;
}int month_difference(int year1, int month1, int month2, int day1, int day2)/*月份天数差*/
{
int days = tianshu(year1, month1, day1) - tianshu(year1, month2, day2);
return days > 0 ? days : -days;
}int day_difference(int day1, int day2)/*日天数差*/
{
return (day1 - day2 > 0 ? day1 - day2 : day2 - day1);
}int yeardays(int year)/*该年有多少天*/
{
return isleap(year) ? 366 : 365;
}int datemuch(int year1, int month1, int day1, int year2, int month2, int day2)/*判断哪个日期大*/
{
if (year1 == year2)
{
if (month1 == month2)
{
if (day1 == day2)
{
printf("%d年%d月%d日和%d年%d月%d日是同一个日期,相差0天!\n", year1, month1, day1, year2, month2, day2);
}
else
{
printf("%d年%d月%d日和%d年%d月%d日相差%d天!\n", year1, month1, day1, year2, month2, day2, day_difference(day1, day2));
}
}
else
{
printf("%d年%d月%d日和%d年%d月%d日相差%d天!\n", year1, month1, day1, year2, month2, day2, month_difference(year1, month1, month2, day1, day2));
}
}
else
{
printf("%d年%d月%d日和%d年%d月%d日相差%d天!\n", year1, month1, day1, year2, month2, day2, year_difference(year1, month1, day1, year2, month2, day2));
}
}int calculation_date()/*推算N天后日期*/
{
int year1, month1, day1, days, year, month, day;
int num[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
do {
printf("请输入要推算的日期(xxxx-xx-xx,例:2022-3-3):");
scanf("%d-%d-%d", &year1, &month1, &day1);
} while (!validdate(year1, month1, day1));
printf("请输入推算的天数(负数向前计算):");
scanf("%d", &days); if (days == 0)
{
year = year1;
month = month1;
day = day1;
}
else if (days > 0)
{
year = year1;
month = month1;
day = day1; if (isleap(year))
{
num[2] = 29;
} for (int i = 1; i <= days; i++)
{
day += 1;
if (day > num[month])
{
day = day - num[month];
month++;
if (month > 12)
{
year++;
month = 1;
if (isleap(year))
{
num[2] = 29;
}
else
{
num[2] = 28;
}
}
}
}
}
else
{
year = year1;
month = month1;
day = day1; if (isleap(year))
{
num[2] = 29;
} days = -days;
for (int i = 1; i <= days; i++)
{
day -= 1;
if (day < 1)
{
month--;
if (month < 1)
{
year--;
month = 12;
if (isleap(year))
{
num[2] = 29;
}
else
{
num[2] = 28;
}
}
day = num[month];
}
} int year2, month2;
year2 = year;
month2 = month;
if ((month2 == 1) || (month2 == 2))
{
month2 += 12;
year2--;
} int w;
w = (day + 2 * month2 + 3 * (month2 + 1) / 5 + year2 + year2 / 4 - year2 / 100 + year2 / 400) % 7;/*基姆拉尔森计算公式*/
switch (w)
{
case 0:printf("%d年%d月%d日%d天前的日期是%d年%d月%d日星期一\n", year1, month1, day1, days, year, month, day); break;
case 1:printf("%d年%d月%d日%d天前的日期是%d年%d月%d日星期二\n", year1, month1, day1, days, year, month, day); break;
case 2:printf("%d年%d月%d日%d天前的日期是%d年%d月%d日星期三\n", year1, month1, day1, days, year, month, day); break;
case 3:printf("%d年%d月%d日%d天前的日期是%d年%d月%d日星期四\n", year1, month1, day1, days, year, month, day); break;
case 4:printf("%d年%d月%d日%d天前的日期是%d年%d月%d日星期五\n", year1, month1, day1, days, year, month, day); break;
case 5:printf("%d年%d月%d日%d天前的日期是%d年%d月%d日星期六\n", year1, month1, day1, days, year, month, day); break;
case 6:printf("%d年%d月%d日%d天前的日期是%d年%d月%d日星期日\n", year1, month1, day1, days, year, month, day); break;
}
return 0;
} int year2, month2;
year2 = year;
month2 = month;
if ((month2 == 1) || (month2 == 2))
{
month2 += 12;
year2--;
} int w;
w = (day + 2 * month2 + 3 * (month2 + 1) / 5 + year2 + year2 / 4 - year2 / 100 + year2 / 400) % 7;/*基姆拉尔森计算公式*/
switch (w)
{
case 0:printf("%d年%d月%d日%d天后的日期是%d年%d月%d日星期一\n", year1, month1, day1, days, year, month, day); break;
case 1:printf("%d年%d月%d日%d天后的日期是%d年%d月%d日星期二\n", year1, month1, day1, days, year, month, day); break;
case 2:printf("%d年%d月%d日%d天后的日期是%d年%d月%d日星期三\n", year1, month1, day1, days, year, month, day); break;
case 3:printf("%d年%d月%d日%d天后的日期是%d年%d月%d日星期四\n", year1, month1, day1, days, year, month, day); break;
case 4:printf("%d年%d月%d日%d天后的日期是%d年%d月%d日星期五\n", year1, month1, day1, days, year, month, day); break;
case 5:printf("%d年%d月%d日%d天后的日期是%d年%d月%d日星期六\n", year1, month1, day1, days, year, month, day); break;
case 6:printf("%d年%d月%d日%d天后的日期是%d年%d月%d日星期日\n", year1, month1, day1, days, year, month, day); break;
}
}