C語言結構體小練習
阿新 • • 發佈:2018-12-31
#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;
}