#include <stdio.h> //头文件
//宏定义
#define MON1 31
#define MON2 28
#define MON3 31
#define MON4 30
#define MON5 31
#define MON6 30
#define MON7 31
#define MON8 31
#define MON9 30
#define MON10 31
#define MON11 30
#define MON12 31
int main(int argc, const char *argv[])
{
int year,mon,day;
//宏定义
#define MON1 31
#define MON2 28
#define MON3 31
#define MON4 30
#define MON5 31
#define MON6 30
#define MON7 31
#define MON8 31
#define MON9 30
#define MON10 31
#define MON11 30
#define MON12 31
int main(int argc, const char *argv[])
{
int year,mon,day;
int x = 0, sign = 0;
printf("请输入一个日期:年/月/日\n");
if(scanf("%d/%d/%d", &year, &mon, &day) != 3)
{
puts("intput error");return -1;
}
if(year<1 || mon<1 || mon>12 || day<1 || day>31)
{
puts("date error");printf("line = %d\n", __LINE__);return -1;
}
//一个月30天的月份有:4月 6月 9月 11月
if(mon==4 || mon==6 || mon==9 || mon==11)
{
if(day > 30){
puts("date error");printf("line = %d\n", __LINE__);return -1;
}
}
//润年sign=1 平年sign=0 ,设置标志位
if(year % 4 == 0)
{
sign = 1;
}
//2月俩种可能:28天&29天
if(mon == 2)
{
if(sign==0 && day>28)
{
puts("date error");printf("line = %d\n", __LINE__);return -1;
}
if(sign==1 && day>29){
puts("date error");printf("line = %d\n", __LINE__);return -1;
}
}}
/*
eg: 2018/1/12: 12天
2018/2/12: 1月 + 12天
2018/3/12: 1月 + 2月 + 12天
2018/5/18: 1月 + 2月 + 3月 + 4月 + 18天
*/
switch(mon)
{
case 12:x += MON11;
case 11:x += MON10;
case 10:x += MON9;
case 9:x += MON8;
case 8:x += MON7;
case 7:x += MON6;
case 6:x += MON5;
case 5:x += MON4;
case 4:x += MON3;
case 3:x += MON2 + sign;
case 2:x += MON1;
case 1:x += day;
}
printf("x = %d\n",x);
return 0;