c语言两日期相差天数
#include<stdio.h>#include<math.h>voidmain(){ints=0,m1,d1,y1,y2,m2,d2,i,a[12]={31,28,3...
#include <stdio.h>
#include<math.h>
void main()
{
int s=0,m1,d1,y1,y2,m2,d2,i,a[12]={31,28,31,30,31,30,31,31,30,31,30,31},b[12]={31,29,31,30,31,30,31,31,30,31,30,31};
scanf("%d %d %d",&y1,&m1,&d1);
scanf("%d %d %d",&y2,&m2,&d2);
if(y1==y2)
{
if(y1%100!=0&&y1%4==0||y1%400==0)
{
while(m1<m2)
{s=b[m1-1]+s;
m1=m1+1;
}
}
else
{
while(m1<m2)
{s=a[m1-1]+s;
m1=m1+1;
}
}
printf("%d days\n",s+d2-d1);
}
else
{
while(y1<y2)
{
if(m1<=2)
{
if(y1%100!=0&&y1%4==0||y1%400==0)
{s=366+s;}
else
{s=365+s;}
y1=y1+1;
}
else
{
y1=y1+1;
if(y1%100!=0&&y1%4==0||y1%400==0)
{s=366+s;}
else
{s=365+s;}
}
}
if(m1<=m2)
{
if(y2%100!=0&&y2%4==0||y2%400==0)
{for(i=0;m1<m2;i++)
{s=b[m1-1]+s;
m1=m1+1;
}
}
else
{for(i=0;m1<m2;i++)
{s=a[m1-1]+s;
m1=m1+1;
}
}
printf("%d days\n",s+d2-d1);
}
else
{
if(y2%100!=0&&y2%4==0||y2%400==0)
{
while(m1>m2)
{s=b[m1-1]+s;
m1=m1-1;
}
}
else
{
while(m1>m2)
{s=a[m1-1]+s;
m1=m1-1;
}
}
printf("%d days\n",s+d2-d1);
}
}
}
求高手指点错误所在
大体说下我的思路:
1.判断是否是同一年。
2.不同年,先计算相差的年数,再计算月份,最后计算天数 展开
#include<math.h>
void main()
{
int s=0,m1,d1,y1,y2,m2,d2,i,a[12]={31,28,31,30,31,30,31,31,30,31,30,31},b[12]={31,29,31,30,31,30,31,31,30,31,30,31};
scanf("%d %d %d",&y1,&m1,&d1);
scanf("%d %d %d",&y2,&m2,&d2);
if(y1==y2)
{
if(y1%100!=0&&y1%4==0||y1%400==0)
{
while(m1<m2)
{s=b[m1-1]+s;
m1=m1+1;
}
}
else
{
while(m1<m2)
{s=a[m1-1]+s;
m1=m1+1;
}
}
printf("%d days\n",s+d2-d1);
}
else
{
while(y1<y2)
{
if(m1<=2)
{
if(y1%100!=0&&y1%4==0||y1%400==0)
{s=366+s;}
else
{s=365+s;}
y1=y1+1;
}
else
{
y1=y1+1;
if(y1%100!=0&&y1%4==0||y1%400==0)
{s=366+s;}
else
{s=365+s;}
}
}
if(m1<=m2)
{
if(y2%100!=0&&y2%4==0||y2%400==0)
{for(i=0;m1<m2;i++)
{s=b[m1-1]+s;
m1=m1+1;
}
}
else
{for(i=0;m1<m2;i++)
{s=a[m1-1]+s;
m1=m1+1;
}
}
printf("%d days\n",s+d2-d1);
}
else
{
if(y2%100!=0&&y2%4==0||y2%400==0)
{
while(m1>m2)
{s=b[m1-1]+s;
m1=m1-1;
}
}
else
{
while(m1>m2)
{s=a[m1-1]+s;
m1=m1-1;
}
}
printf("%d days\n",s+d2-d1);
}
}
}
求高手指点错误所在
大体说下我的思路:
1.判断是否是同一年。
2.不同年,先计算相差的年数,再计算月份,最后计算天数 展开
2个回答
展开全部
#include <stdio.h>
#include<math.h>
int main()
{
int s=0,m1,d1,y1,y2,m2,d2,i,a[12]={31,28,31,30,31,30,31,31,30,31,30,31},b[12]={31,29,31,30,31,30,31,31,30,31,30,31};
scanf("%d %d %d",&y1,&m1,&d1);
scanf("%d %d %d",&y2,&m2,&d2);
if(y1==y2)
{
if((y1%100!=0&&y1%4==0)||y1%400==0)
{
while(m1<m2)
{s=b[m1-1]+s;
m1=m1+1;
}
}
else
{
while(m1<m2)
{s=a[m1-1]+s;
m1=m1+1;
}
}
printf("%d days\n",s+d2-d1);
}
else
{
while(y1<y2)
{
if(m1<=2)
{
if((y1%100!=0&&y1%4==0)||y1%400==0)
{s=366+s;}
else
{s=365+s;}
y1=y1+1;
}
else
{
y1=y1+1;
if((y1%100!=0&&y1%4==0)||y1%400==0)
{s=366+s;}
else
{s=365+s;}
}
}
if(m1<=m2)
{
if((y2%100!=0&&y2%4==0)||y2%400==0)
{for(i=0;m1<m2;i++)
{s=b[m1-1]+s;
m1=m1+1;
}
}
else
{for(i=0;m1<m2;i++)
{s=a[m1-1]+s;
m1=m1+1;
}
}
printf("%d days\n",s+d2-d1);
}
else
{
if((y2%100!=0&&y2%4==0)||y2%400==0)
{
while(m1>m2)
{s=b[m1-1]+s;
m1=m1-1;
}
}
else
{
while(m1>m2)
{s=a[m1-1]+s;
m1=m1-1;
}
}
printf("%d days\n",s+d2-d1);
}
}
return 0;
}
你试试看!
#include<math.h>
int main()
{
int s=0,m1,d1,y1,y2,m2,d2,i,a[12]={31,28,31,30,31,30,31,31,30,31,30,31},b[12]={31,29,31,30,31,30,31,31,30,31,30,31};
scanf("%d %d %d",&y1,&m1,&d1);
scanf("%d %d %d",&y2,&m2,&d2);
if(y1==y2)
{
if((y1%100!=0&&y1%4==0)||y1%400==0)
{
while(m1<m2)
{s=b[m1-1]+s;
m1=m1+1;
}
}
else
{
while(m1<m2)
{s=a[m1-1]+s;
m1=m1+1;
}
}
printf("%d days\n",s+d2-d1);
}
else
{
while(y1<y2)
{
if(m1<=2)
{
if((y1%100!=0&&y1%4==0)||y1%400==0)
{s=366+s;}
else
{s=365+s;}
y1=y1+1;
}
else
{
y1=y1+1;
if((y1%100!=0&&y1%4==0)||y1%400==0)
{s=366+s;}
else
{s=365+s;}
}
}
if(m1<=m2)
{
if((y2%100!=0&&y2%4==0)||y2%400==0)
{for(i=0;m1<m2;i++)
{s=b[m1-1]+s;
m1=m1+1;
}
}
else
{for(i=0;m1<m2;i++)
{s=a[m1-1]+s;
m1=m1+1;
}
}
printf("%d days\n",s+d2-d1);
}
else
{
if((y2%100!=0&&y2%4==0)||y2%400==0)
{
while(m1>m2)
{s=b[m1-1]+s;
m1=m1-1;
}
}
else
{
while(m1>m2)
{s=a[m1-1]+s;
m1=m1-1;
}
}
printf("%d days\n",s+d2-d1);
}
}
return 0;
}
你试试看!
追问
还是吧对。。你改哪了?
追答
if后面多加了一个括号!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询