条件判断
题目:编写一个程序,提示用户输入3个整数值,分别代表年,月,日。例如用户输入了2003,12,31,程序就以2003 december 31st的格式输出该日期。
必须在日期的后面加上th,nd,st和rd。例如1st,2nd,3rd,4th,11th,12th,13th,14th,21st,22nd,23rd,24th.
下面是我写的1-5个月的程序,测试了一下,基本可以实现,但是写的太繁琐,希望前辈们可以指点。刚学条件判断这章。
#include <stdio.h>
int main(void)
{
int nian=0;
int yue=0;
int ri=0;
printf("\n 请输入年月日: \n");
scanf("%d %d %d",&nian,&yue,&ri);
if(yue==1)
{
if(ri==1||ri==11||ri==21||ri==31)
printf("\n 日期为:%d january %dst \n",nian,ri);
else
if(ri==2||ri==12||ri==22)
printf("\n 日期为:%d january %dnd \n",nian,ri);
else
if(ri==3||ri==13||ri==23)
printf("\n 日期为:%d january %drd \n",nian,ri);
else
printf("\n 日期为:%d january %dth \n",nian,ri);
}
if(yue==2)
{
if(ri==1||ri==11||ri==21||ri==31)
printf("\n 日期为:%d february %dst \n",nian,ri);
else
if(ri==2||ri==12||ri==22)
printf("\n 日期为:%d february %dnd \n",nian,ri);
else
if(ri==3||ri==13||ri==23)
printf("\n 日期为:%d february %drd \n",nian,ri);
else
printf("\n 日期为:%d february %dth \n",nian,ri);
}
if(yue==3)
{
if(ri==1||ri==11||ri==21||ri==31)
printf("\n 日期为:%d march %dst \n",nian,ri);
else
if(ri==2||ri==12||ri==22)
printf("\n 日期为:%d march %dnd \n",nian,ri);
else
if(ri==3||ri==13||ri==23)
printf("\n 日期为:%d march %drd \n",nian,ri);
else
printf("\n 日期为:%d march %dth \n",nian,ri);
}
if(yue==4)
{
if(ri==1||ri==11||ri==21||ri==31)
printf("\n 日期为:%d april %dst \n",nian,ri);
else
if(ri==2||ri==12||ri==22)
printf("\n 日期为:%d april %dnd \n",nian,ri);
else
if(ri==3||ri==13||ri==23)
printf("\n 日期为:%d april %drd \n",nian,ri);
else
printf("\n 日期为:%d april %dth \n",nian,ri);
}
if(yue==5)
{
if(ri==1||ri==11||ri==21||ri==31)
printf("\n 日期为:%d may %dst \n",nian,ri);
else
if(ri==2||ri==12||ri==22)
printf("\n 日期为:%d may %dnd \n",nian,ri);
else
if(ri==3||ri==13||ri==23)
printf("\n 日期为:%d may %drd \n",nian,ri);
else
printf("\n 日期为:%d may %dth \n",nian,ri);
}
return 0;
}