我想使程序获取一个日期(例如3月9号)
之后我把要倒计时的时间(例如6月20号)输入使程序自动获取还有多少天
我看了很多的资料还是不会希望老师可以帮我
谢谢
#include <stdio.h>
struct date
{
int year;
int month;
int day;
};
int main(void)
{
int isPrime(int year);
int dateDiff(struct date mindate,struct date maxdate);
struct date mindate,maxdate;
int days;
printf("please input the one date:");
scanf("%i-%i-%i",&mindate.year,&mindate.month,&mindate.day);
printf("please input other day:");
scanf("%i-%i-%i",&maxdate.year,&maxdate.month,&maxdate.day);
days=dateDiff(mindate,maxdate);
printf("the day is:%d\n",days);
return 0;
}
int isPrime(int year)
{
if ((year%4==0&&year%100!=0)||(year%400==0))
{
return 1;
}
else
{
return 0;
}
}
int dateDiff(struct date mindate,struct date maxdate)
{
int days=0, flag=1;
const int primeMonth[12]={31,29,31,30,31,30,31,31,30,31,30,31};
const int notPrimeMonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
struct date tmp;
if((mindate.year>maxdate.year)||(mindate.year==maxdate.year&&mindate.month>maxdate.month)||(mindate.year==maxdate.year&&mindate.month==maxdate.month&&mindate.day>maxdate.day))
{
tmp=mindate;
mindate=maxdate;
maxdate=tmp;
}
int maxmonth,minmonth;
if (maxdate.month<mindate.month)
{
maxmonth=mindate.month;
minmonth=maxdate.month;
flag=-1;
}
else
{
maxmonth=maxdate.month;
minmonth=mindate.month;
flag=1;
}
for(int j=mindate.year;j<maxdate.year;++j)
{
if (isPrime(j)==1)
{
days+=366;
}
else
days+=365;
}
int day;
if(isPrime(maxdate.year)==1)
{
for(int i=minmonth;i<maxmonth;i++)
{
day=primeMonth[i-1]*flag;
days=days+day;
}
days=days+maxdate.day-mindate.day;
}
else
{
for (int i=minmonth;i<maxmonth;i++)
{
day=notPrimeMonth[i-1]*flag;
days=days+day;
}
days=days+maxdate.day-mindate.day;
}
return days;
}
李木子 发表于2017-03-10 09:12
#include <stdio.h>
struct date
{
int year;
int month;
int day;
};
int main(void)
{
int isPrime(int year);
int dateDiff(struct date mindate,struct date maxdate);
struct date mindate,maxdate;
int days;
printf("please input the one date:");
scanf("%i-%i-%i",&mindate.year,&mindate.month,&mindate.day);
printf("please input other day:");
scanf("%i-%i-%i",&maxdate.year,&maxdate.month,&maxdate.day);
days=dateDiff(mindate,maxdate);
printf("the day is:%d\n",days);
return 0;
}
int isPrime(int year)
{
if ((year%4==0&&year%100!=0)||(year%400==0))
{
return 1;
}
else
{
return 0;
}
}
int dateDiff(struct date mindate,struct date maxdate)
{
int days=0, flag=1;
const int primeMonth[12]={31,29,31,30,31,30,31,31,30,31,30,31};
const int notPrimeMonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
struct date tmp;
if((mindate.year>maxdate.year)||(mindate.year==maxdate.year&&mindate.month>maxdate.month)||(mindate.year==maxdate.year&&mindate.month==maxdate.month&&mindate.day>maxdate.day))
{
tmp=mindate;
mindate=maxdate;
maxdate=tmp;
}
int maxmonth,minmonth;
if (maxdate.month<mindate.month)
{
maxmonth=mindate.month;
minmonth=maxdate.month;
flag=-1;
}
else
{
maxmonth=maxdate.month;
minmonth=mindate.month;
flag=1;
}
for(int j=mindate.year;j<maxdate.year;++j)
{
if (isPrime(j)==1)
{
days+=366;
}
else
days+=365;
}
int day;
if(isPrime(maxdate.year)==1)
{
for(int i=minmonth;i<maxmonth;i++)
{
day=primeMonth[i-1]*flag;
days=days+day;
}
days=days+maxdate.day-mindate.day;
}
else
{
for (int i=minmonth;i<maxmonth;i++)
{
day=notPrimeMonth[i-1]*flag;
days=days+day;
}
days=days+maxdate.day-mindate.day;
}
return days;
}
在生成时候发生错误:
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 C4996 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 中考倒计时 c:\users\qq229\desktop\中考倒计时\中考倒计时\中考倒计时\中考倒计时.cpp 20
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 C4996 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 中考倒计时 c:\users\qq229\desktop\中考倒计时\中考倒计时\中考倒计时\中考倒计时.cpp 22