今天买了袋牛肉干,知道生产日期,知道保质期,计算到今天还有多少天过期,是否可以食用
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>#include<windows.h>
int main()
{
int a[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int last_day = 0;
int sum = 0;
int sum2 = 0;
int year = 0;
int month = 0;
int day = 0;
int Year = 0;
int Month = 0;
int Day = 0;
int h = 0;
int sum1 = 0;
int i = 0;
int t = 0;
printf("请输入生产日期\n");
scanf("%d%d%d",&year,&month,&day);
printf("\n");
printf("请输入保质期(输入月,一月当30天计算)\n");
scanf("%d",&t);
printf("\n");
printf("请输入今天日期\n");
scanf("%d%d%d",&Year,&Month,&Day);
printf("\n");
//生产日期当年的天数
i = month-1;
for(i; i > 0; i--)
{
sum1 = sum1 + a[i];
}
sum1 = sum1 + day;
//今年今天的天数
i = Month-1;
for(i; i > 0; i--)
{
sum2 = sum2 + a[i];
}
sum2 = sum2 + day;
//先当全部是平年
if(sum2 > sum1)
{
sum = sum2 - sum1;
}
else
{
sum = sum1 - sum2;
sum = 365 - sum;
}
if(Year > year)
{
last_day = ((Year - year)-1) * 365;
}
sum = sum + last_day;
//判断闰年
if((Year%4==0 && Year%100!=0)||Year%400==0)
{ if(Month > 2 )
h++;
}
if((year%4==0 && year%100!=0)||year%400==0)
{ if(Month < 2 )
h++;
}
year++;
Year--;
for(year; Year - year >= 0; Year--)
{
if((Year%4==0 && Year%100!=0)||Year%400==0)
{h++;
}
}
sum = sum + h;
printf("好吃的自生产日期后已经过去%d天\n\n",sum);
t = t * 30;
sum = t - sum;
if(sum > 0)
{printf("好吃的还有%d天过期,可以是食用\n\n",sum);
}
else
printf("产品过期,禁止食用\n");
system("pause");
return 0;
}