编写程序exp4_1.c定义一个表示日期的结构体
1个回答
展开全部
#include <stdio.h>int dayCountArray[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};struct Date{ int year, month, day;}; int IsLeapYear(struct Date date){ if (0==date.year%400 || (0!= date.year%100 && 0==date.year%4)) { return 1; } return 0;} int Dayth(struct Date date){ int sum = 0; for (int i = 1; i < date.month; ++i) { sum += dayCountArray[i]; } sum += date.day; if (!IsLeapYear(date) && date.month>2) { --sum; } return sum;} int Judge(struct Date date){ if (date.year<0 || date.month < 1 || date.month > 12 || date.day < 0 || date.day > dayCountArray[date.month] || (2==date.month && !IsLeapYear(date) && date.day > 28)) { return 0; } return 1;} int main(int argc, const char * argv[]) { struct Date date; for (int i = 0; i < 10; ++i) { printf("please input date!\n"); scanf("%d %d %d", &date.year, &date.month, &date.day); if (Judge(date)) { printf("dayth : %d\n", Dayth(date)); }else{ printf("Error input!\n"); } } return 0;}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询