Given the following definitions:
struct date_rec
{ int day; int month; int year; }; struct date_rec current_date; write a program containing the following functions, complete: (a) Input the value of current_date: void input_date(struct date_rec *current_date) ( b) Increase current_date by 1 day: void increment_date(struct date_rec *current_date) © Display the value of current_date: void output_date(struct date_rec *current_date) consider the actual number of days in each month, and also consider the leap year.
**Input format requirement: "%d%d%d" Prompt message: "Please enter the current date (year, month, day):"
** Output format requirement: "Current date: %d year%d month %d day!" (Add a new date after 1 day)
Code:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int month[12] = {
31,28,31,30,31,30,31,31,30,31,30,31 };
struct date_rec
{
int year;
int month;
int day;
};
void input_date(struct date_rec* current_date)
{
printf("请输入当前日期(年 月 日):");
scanf("%d%d%d", ¤t_date->year, ¤t_date->month, ¤t_date->day);
}
void increment_date(struct date_rec* current_date)
{
if (Isleap(current_date->year)) //判断是否闰年
month[1] = 29;
else
month[1] = 28;
current_date->day++;
if (current_date->day > month[current_date->month - 1]) //天数加一后超出了该月的总天数
{
current_date->day = 1; //进位后日期为下月第一天
current_date->month++; //定位到下个月
}
if (current_date->month > 12) //如果月数超出一年12个月
{
current_date->month = 1; //进位到下一年的一月
current_date->year++;
}
}
void output_date(struct date_rec* current_date)
{
printf("当前日期:%d年%d月%d日!", current_date->year, current_date->month, current_date->day);
}
int Isleap(int year)
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return 1;
else
return 0;
}
int main()
{
struct date_rec current_date;
input_date(¤t_date);
increment_date(¤t_date);
output_date(¤t_date);
}