用C语言编修已知1980年1月1日是星期二,输入任意一个日期计算出该天是星期几
4个回答
展开全部
#include <stdio.h>
#include <stdlib.h>
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<year; i++) {
total += 365;
if(isLeap(i)) total ++;
}
if(isLeap(year)) {
for(i=1; i<mon; i++) total += LEAP_MON[i];
} else {
for(i=1; i<mon; i++) total += NOLEAP_MON[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;
}
#include <stdlib.h>
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<year; i++) {
total += 365;
if(isLeap(i)) total ++;
}
if(isLeap(year)) {
for(i=1; i<mon; i++) total += LEAP_MON[i];
} else {
for(i=1; i<mon; i++) total += NOLEAP_MON[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;
}
展开全部
计算机日期计算是以一个基准值进行的(好像是1970年1月1号),算出你输入的日期平年和闰年距离1970年有多少个,然后得到天数再除以7.
更多追问追答
追问
我要变编程
追答
给我100块钱,我给你编一个
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
看着就不想编 唉 太堕落了
追问
不行啊 你要救命的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询