C语言结构体编程,输入一年月日星期几,计算N天后是哪年哪月哪日星期几,考虑平闰年及各种输入错误情况。 c语言??输入任一年、月、日,计算该年月日为星期几

c\u8bed\u8a00 \u8f93\u5165\u67d0\u5e74\u67d0\u6708\u67d0\u65e5\u661f\u671f\u51e0\uff0c\u8ba1\u7b972000\u5929\u540e\u662f\u54ea\u5e74\u54ea\u6708\u54ea\u65e5\u661f\u671f\u51e0

\u95ee\u9898\u662f\u8fd9\u4e24\u5343\u5929\u4e4b\u95f4\u6709\u51e0\u4e2a\u95f0\u5e74\u5462\uff1f\u662f\u8fdb\u884c\u5224\u65ad\u5462\uff1f\u8fd8\u662f\u9760\u9ed8\u8ba4\u7684\u5462\uff1f\u8bf7\u63d0\u793a\uff1f

\u4e0d\u5fc5\u7528 switch. \u4ee5\u524d\u7f16\u8fc7\u4e00\u4e2a. \u73b0\u5728\u627e\u51fa\u6765\u914d\u4e86\u4e2a\u4e3b\u7a0b\u5e8f. \u4f60\u8bd5\u8bd5\u5427.

---\u8f93\u5165\u5e74,\u6708,\u65e5:2000,3,1
\u7b54: 2000\u5e743\u67081\u65e5\u662f\u661f\u671f\u4e09.

---\u8f93\u5165\u5e74,\u6708,\u65e5:2008,4,20
\u7b54: 2008\u5e744\u670820\u65e5\u662f\u661f\u671f\u65e5.

---\u8f93\u5165\u5e74,\u6708,\u65e5:0

\u5e74\u6708\u65e5\u6570\u5b57\u7528\u9017\u53f7\u9694\u5f00. \u60f3\u7ed3\u675f\u7684\u65f6\u5019\u8f93\u5165\u4e00\u4e2a0\u5c31\u53ef\u4ee5\u4e86.

int Ymd2Wday(int year, int month, int days)
{
static int mdays[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 };
int i, y = year - 1;
for (i=0; i<month; ++i) days += mdays[i];
if (month>2) { // Increment date if this is a leap year after February
if (((year%400)==0) || ((year&3)==0 && (year%100))) ++days;
}
return (y+y/4-y/100+y/400+days)%7;
}

int main()
{
int y,m,d,w;
static char* wday[]={ "\u65e5","\u4e00","\u4e8c","\u4e09","\u56db","\u4e94","\u516d" };
printf("---\u8f93\u5165\u5e74,\u6708,\u65e5:");
while (scanf("%d,%d,%d", &y, &m, &d)==3) {
w = Ymd2Wday(y,m,d);
printf("\u7b54: %d\u5e74%d\u6708%d\u65e5\u662f\u661f\u671f%s.\n\n---\u8f93\u5165\u5e74,\u6708,\u65e5:", y, m, d, wday[w]);
}
return 0;
}

#include "stdio.h"

typedef struct Date
{
int year;
int month;
int day;
}Date;

char *Week[] = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int IsLeapYear(int year);
void ListDate(int weekday, Date now, int diff);

/* 知道今天星期几以及年月日,编写C程序,要求能算出任意天后是星期几以及年月日 */
void main()
{
int weekday;
Date now;
int diff;

printf("请输入今天是星期几以及日期");
printf("星期几(0..6,星期日..星期六):");
scanf("%d", &weekday);
printf("日期(yyyy-mm-dd): ");
scanf("%d-%d-%d", &now.year, &now.month, &now.day);
printf("相隔天数:");
scanf("%d", &diff);

ListDate(weekday, now, diff);
}

/* 判断闰年 */
int IsLeapYear(int year)
{
return ((year%400==0) || (year%4==0 && year%100!=0)) ? 1 : 0;
}

/* 已知今天是星期几(weekday,0..6,Sunday..Saturday)以及日期(now),输出diff天后是星期几以及日期 */
/* diff > 0 指diff天后,diff < 0 指diff天前 */
void ListDate(int weekday, Date now, int diff)
{
int w1;
Date d1;

/* 计算diff天后是星期几 */
w1 = weekday + diff;
while(w1 < 0)
{
w1 += 7;
}
w1 = w1 % 7;
printf("%d 天后:\n", diff);
printf("%s\n", Week[w1]);

/* 计算diff天的日期 */
d1.year = now.year;
d1.month = now.month;
d1.day = now.day + diff;

if(d1.day > 0)
{
while(d1.day > days[d1.month-1])
{
d1.day -= days[d1.month-1] + (d1.month==2 && IsLeapYear(d1.year));
d1.month++;
if(d1.month > 12)
{
d1.month = 1;
d1.year++;
}
}
}
else if(d1.day < 0)
{
while(d1.day <= 0)
{
d1.month--;
if(d1.month <= 0)
{
d1.month = 12;
d1.year--;
}
d1.day += days[d1.month-1] + (d1.month==2 && IsLeapYear(d1.year));
}
}
printf("%d-%d-%d\n", d1.year, d1.month, d1.day);
}

扩展阅读:一节课就可以学会c++视频 ... 学编程一年大概多少钱 ... 一节课教你学会c++视频 ... 小学生c++趣味编程视频 ... c++免费视频教程 ... 为什么不建议孩子学编程 ... c++入门程序代码 ... 学习c++的视频 ... c语言struct结构体数组 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网