#include <stdio.h> #include <stdbool.h> /* 功能:根据当天日期输出明天的日期 时间:2018-06-05 //此程序无输入错误检测 */ //定义一个日期类型 形如2018-06-05 struct date{ int year; int month; int day; }; //判断某年是否为闰年 bool isLeap(struct date d); //返回某月的总天数 int numberOfDays(struct date d); int main(int argc,char const *argv[]) { struct date today, tomorrow; printf("输入今天的日期:(year mm dd)"); scanf("%i %i %i",&today.year,&today.month,&today.day); //如果当天不是本月的最后一天 if(today.day != numberOfDays(today) ){ tomorrow.day = today.day + 1; tomorrow.month = today.month; tomorrow.year = today.year; }else if( today.month == 12 ){ //如果当天是今年的最后一天 tomorrow.day = 1; tomorrow.month = 1; tomorrow.year = today.year + 1; }else{ tomorrow.day = 1; tomorrow.month = today.month + 1; tomorrow.year = today.year; } printf("明天的日期是: %i-%i-%i",tomorrow.year,tomorrow.month,tomorrow.day); return 0; } int numberOfDays(struct date d) { int days; //每个月份的天数 const int daysPerMonth[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; if( 2 == d.month && isLeap(d) ){ days = 29; }else{ days = daysPerMonth[d.month]; } return days; } bool isLeap(struct date d) { bool leap = false; if( (d.year % 4 == 0 && d.year % 100 != 0) || d.year % 400 == 0 ){ leap = true; } return leap; }
转载请标明出处:C语言结构体小练习
文章来源: C语言结构体小练习