结构体:计算输入日期是该年的第几天
题目如下
分析
- 首先我们要明白当为闰年的时候 2月份的天数是29天 所以我们需要定义一个函数 来判断这个年份是否为闰年
- 之后 我们要解决的是 输入的问题 那么如何从含有这种分隔符的 输入中 摘取我们的年月日 整数型数据呢 下面我们用到了
scanf("%d-%d-%d",&p.year,&p.month,&p.day)
- 是不是超级的简单
- 解决这个问题之后那之后的岂不是手到擒来
代码如下
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
int is_run(int x)
{
if ( (x%4==0 && x%100!=0) || x%400==0 )
return 1;
else
return 0;
}
struct Orderdate
{
int year;
int month;
int day;
}p;
int main()
{
int th;
int month_day[12]={31,28,31,30,31,30,31,31,30,31,30,31};
while(scanf("%d-%d-%d",&p.year,&p.month,&p.day)!=EOF)
{
th=p.day;
for(int i=0;i<p.month-1;i++)
th+=month_day[i];
if(is_run(p.year) && p.month>2)
printf("%d\n",th+1);
else
printf("%d\n",th);
}
}
测试如下