[原创]输入一个日期,求那个日期是那年的第多少天
// 1. 定义一个包括年、月、日的结构体变量,当输入年、// 月、日数据后,计算该日是这一年中的第几天。
#include <stdio.h>
typedef struct Date
{
int year ;
int month;
int day ;
} date;
int months = {{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}} ;
// 判断平年 or 闰年
int isLeap(int m)
{
if (m%4 != 0 || (m%100 == 0 && m%400 != 0))
return 0;
return 1;
}
int isSuccDate(date *pd)
{
// 判断年是否合法
if (pd->year < 0)
{
printf("对不起,公元前不在本程序计算之内\r\n") ;
return 0 ;
}
// 判断月是否合法
if (pd->month < 1 || pd->month > 12)
{
printf("对不起,您的月份输入不正确!\r\n") ;
return 0 ;
}
// 判断日是否合法
if (pd->day < 0 ||
(pd->day > months[ isLeap( pd->year)]))
{
printf("对不起,您的天数输入不正确! \r\n") ;
return 0 ;
}
return 1;
}
// 计算天数函数
int Calculate(date *pd)
{
int i ,
nSum;
for (i = 1, nSum = 0; i < pd->month; ++i)
{
nSum += months ;
}
nSum += pd->day ;
return nSum ;
}
int main(void)
{
date d ;
scanf("%d%d%d", &d.year, &d.month, &d.day) ;
if (! isSuccDate(&d))
{
return 0 ;
}
printf("%d\r\n", Calculate(&d)) ;
return 0 ;
}
我顶你
美男子美男子顶个
心情指数~~#include <stdio.h>
int months = {{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}} ;
typedef struct Date
{
int year;
int month;
int day;
}Day;
int RunNian(int year)
{
if((!(year%4) && year%100) || !(year%400))return 1;
else return 0;
}
int JianCe(Day * date)
{
int rn=0;
if(date->year<=0 || date->year>9999)return 0;
if( RunNian(date->year) ) rn=1;
if(date->month<=0 || date->month>12)return 0;
if(date->day<=0 || date->day>months) return 0;
return 1;
}
Day * ShuRu(int i)
{
Day * d;
d=(Day *)malloc(sizeof(Day));
if(!d)return 0;
if(i==0)printf("请输入您的生日:");
else printf("请输入今天日期:");
scanf("%d-%d-%d",&d->year,&d->month,&d->day);
return d;
}
void ShuChu(Day * date)
{
printf("%4d-%02d-%02d\r\n",date->year,date->month,date->day);
}
int Total(Day ** date)
{
int i,rn,year,month,day,total=0;
for(year=date->year+1;year<date->year;year++)
{
if(RunNian(year))total+=366;
else total+=365;// 也可以建立一个数组
}
if(RunNian(date->year))rn=1;
else rn=0;
for(month=date->month;month<=12;month++)
total+=months;
total-=date->day;
if(RunNian(date->year))rn=1;
else rn=0;
for(month=1;month<date->month;month++)
total+=months;
total+=date->day;
return total;
}
int main()
{
int year,month,day;
Day * date;
int i,t,total;
char * str[]={"体力状态:","情绪状态:","智力状态:"};
char * zt[]={"高潮期","低潮期"};
int zb[]={11,14,16};
int zd[]={23,28,33};
printf("格式为:年-月-日\r\n");
for(i=0;i<2;i++)
{
do
{
t=0;
date=ShuRu(i);
if(!date)return 0;
if(!JianCe(date)){t=1;printf("输入不合法,请重新输入!\r\n");}
}
while(t);
}
for(i=0;i<2;i++)
{
ShuChu(date);
}
printf("总时间为: %d\r\n",total=Total(date));
for(i=0;i<3;i++)
{
t=total%zd;
if(t>zb)printf("%s %s\r\n",str,zt);
else printf("%s %s\r\n",str,zt);
}
return 0;
}
页:
[1]