用C语言编修已知1980年1月1日是星期二,输入任意一个日期计算出该天是星期几

2024-11-26 06:30:27
推荐回答(4个)
回答1:

#include
#include
int NOLEAP_MON[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int LEAP_MON[13] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool isLeap(int year) {
return (year%400==0) || (year%4==0 &&year%100!=0);
}
int countDays(int year, int mon, int day)
{
int i, total = 0, j;
if (year< 1980) {
total = 0;
for(i=1979; i>year; i--) {
total += 365;
if(isLeap(i)) total ++;
}
if(isLeap(year)) {
for(i=12; i>mon; i--) total += LEAP_MON[i];
total += LEAP_MON[mon] - day +1;
} else {
for(i=12; i>mon; i--) total += NOLEAP_MON[i];
total += NOLEAP_MON[mon] - day +1;
}
total = - total;
} else {
total = 0;
for(i=1980; i total += 365;
if(isLeap(i)) total ++;
}
if(isLeap(year)) {
for(i=1; i } else {
for(i=1; i }
total += day -1;
}
return total;
}
void countWeek(int year, int mon, int day)
{
char WEEK_DAY[7][10] = {"一", "二", "三"茄裤, "四", "五", "六", "日"};
int diff, week;
diff = countDays(year, mon, day);
week = 2 + diff % 7;
if(week> 7) week -= 7;
else if (week <=0) week += 7;
printf("%d年%d月%d日是星期裂纳毁%s\n", year, mon, day, WEEK_DAY[week-1]);
}
int main()
{
int n, y, m, d, days, week;
char str[1001], *p;

printf("输入年月日(用空肆备格隔开): ");
scanf("%d%d%d", &y, &m, &d);
countWeek(y, m, d);
system("pause");
return 0;
}

回答2:

计算机日期计算是拦友以一个基准值进行的(好像是1970年1月1号),算出你输入的日期平年和闰年距离1970年有慧含多少个,然简碧槐后得到天数再除以7.

回答3:

循环

回答4:

看着就不想编 唉 太堕落了