|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
/**************************
这是一个将年月日换算成第几天的小程序
****************************/
#include<stdio.h>
void main()
{
int year,month,day,leap,sum,d;
printf("是否开始查询,退出请按0。继续查询请按1\n");
scanf("%d",&d);
while(d)
{
printf("请按照年.月.日格式输入\n");
if (scanf("%d.%d.%d",&year,&month,&day)) //这里出了问题,我为了检查用户输入格式是否正确利用scanf的返回值,但是一
旦用户输入错误,会变无限循环。中间没有问题,经过多次测试。最后面也添加
了else。看了一个小 时也没办法在功能(检查用户输入格式是否正确)保证的情况下写好
{
if(year%4==0)
{
if(year%100==0)
{
if(year%400==0)
{
leap=1;
}
else
{
leap=0;
}
}
else{
leap=1;
}
}
else{
leap=0;
}
switch(month)
{
case 1:
if(day<32&&day>0)
{
if(leap==1)
{
sum=day;
printf("该年份是润年\n%d年%d月%d日是%d年的第%d天\n",year,month,day,year,sum);
}
else
{
sum=day;
printf("该年份不是润年\n%d年%d月%d日是%d年的第%d天\n",year,month,day,year,sum);
}
}
else{
printf("本月不存在所输入日期\n");
}
break;
case 2:
if(leap==1)
{
if(day<30&&day>0)
{
sum=day+31;
printf("该年份是润年\n%d年%d月%d日是%d年的第%d天\n",year,month,day,year,sum);
}
else{
printf("本月不存在所输入日期\n");
}
}
else
{
if(day<29&&day>0)
{
sum=day+31;
printf("该年份不是润年\n%d年%d月%d日是%d年的第%d天\n",year,month,day,year,sum);
}
else{
printf("本月不存在所输入日期\n");
}
}
break;
case 3:
if(day<32&&day>0)
{
if(leap==1)
{
sum=day+31+29;
printf("该年份是润年\n%d年%d月%d日是%d年的第%d天\n",year,month,day,year,sum);
}
else
{
sum=day+31+28;
printf("该年份不是润年\n%d年%d月%d日是%d年的第%d天\n",year,month,day,year,sum);
}
}
else{
printf("本月不存在所输入日期\n");
}
break;
case 4:
if(day<31&&day>0)
{
if(leap==1)
{
sum=day+31+29+31;
printf("该年份是润年\n%d年%d月%d日是%d年的第%d天\n",year,month,day,year,sum);
}
else
{
sum=day+31+28+31;
printf("该年份不是润年\n%d年%d月%d日是%d年的第%d天\n",year,month,day,year,sum);
}
}
else{
printf("本月不存在所输入日期\n");
}
break;
case 5:
if(day<32&&day>0)
{
if(leap==1)
{
sum=day+31+29+31+30;
printf("该年份是润年\n%d年%d月%d日是%d年的第%d天\n",year,month,day,year,sum);
}
else
{
sum=day+31+28+31+30;
printf("该年份不是润年\n%d年%d月%d日是%d年的第%d天\n",year,month,day,year,sum);
}
}
else{
printf("本月不存在所输入日期\n");
}
break;
case 6:
if(day<31&&day>0)
{
if(leap==1)
{
sum=day+31+29+31+30+31;
printf("该年份是润年\n%d年%d月%d日是%d年的第%d天\n",year,month,day,year,sum);
}
else
{
sum=day+31+28+31+30+31;
printf("该年份不是润年\n%d年%d月%d日是%d年的第%d天\n",year,month,day,year,sum);
}
}
else{
printf("本月不存在所输入日期\n");
}
break;
case 7:
if(day<32&&day>0)
{
if(leap==1)
{
sum=day+31+29+31+30+31+30;
printf("该年份是润年\n%d年%d月%d日是%d年的第%d天\n",year,month,day,year,sum);
}
else
{
sum=day+31+28+31+30+31+30;
printf("该年份不是润年\n%d年%d月%d日是%d年的第%d天\n",year,month,day,year,sum);
}
}
else{
printf("本月不存在所输入日期\n");
}
break;
case 8:
if(day<32&&day>0)
{
if(leap==1)
{
sum=day+31+29+31+30+31+30+31;
printf("该年份是润年\n%d年%d月%d日是%d年的第%d天\n",year,month,day,year,sum);
}
else
{
sum=day+31+28+31+30+31+30+31;
printf("该年份不是润年\n%d年%d月%d日是%d年的第%d天\n",year,month,day,year,sum);
}
}
else{
printf("本月不存在所输入日期\n");
}
break;
case 9:
if(day<31&&day>0)
{
if(leap==1)
{
sum=day+31+29+31+30+31+30+31+31;
printf("该年份是润年\n%d年%d月%d日是%d年的第%d天\n",year,month,day,year,sum);
}
else
{
sum=day+31+28+31+30+31+30+31+31;
printf("该年份不是润年\n%d年%d月%d日是%d年的第%d天\n",year,month,day,year,sum);
}
}
else{
printf("本月不存在所输入日期\n");
}
break;
case 10:
if(day<32&&day>0)
{
if(leap==1)
{
sum=day+31+29+31+30+31+30+31+31+30;
printf("该年份是润年\n%d年%d月%d日是%d年的第%d天\n",year,month,day,year,sum);
}
else
{
sum=day+31+28+31+30+31+30+31+31+30;
printf("该年份不是润年\n%d年%d月%d日是%d年的第%d天\n",year,month,day,year,sum);
}
}
else{
printf("本月不存在所输入日期\n");
}
break;
case 11:
if(day<31&&day>0)
{
if(leap==1)
{
sum=day+31+29+31+30+31+30+31+31+30+31;
printf("该年份是润年\n%d年%d月%d日是%d年的第%d天\n",year,month,day,year,sum);
}
else
{
sum=day+31+28+31+30+31+30+31+31+30+31;
printf("该年份不是润年\n%d年%d月%d日是%d年的第%d天\n",year,month,day,year,sum);
}
}
else{
printf("本月不存在所输入日期\n");
}
break;
case 12:
if(day<32&&day>0)
{
if(leap==1)
{
sum=day+31+29+31+30+31+30+31+31+30+31+30;
printf("该年份是润年\n%d年%d月%d日是%d年的第%d天\n",year,month,day,year,sum);
}
else
{
sum=day+31+28+31+30+31+30+31+31+30+31+30;
printf("该年份不是润年\n%d年%d月%d日是%d年的第%d天\n",year,month,day,year,sum);
}
}
else{
printf("本月不存在所输入日期\n");
}
break;
default : printf("不存在所输入月份\n");
}
}
else
{
printf("格式输入有误\n");
}
printf("是否继续查询,退出请按0。继续查询请按1\n");
scanf("%d",&d);
}
} |
|