习题代码求前辈指点
习题要求输入3个整数值,分别代表月,日,年。例如输入了12,31,2003 程序就以 31st December 2003 的格式输出该日期。必须在日期的后面加上th,nd,st和rd。例如1st,2nd
我的思路就是先用if语句检测月,日,年的数值范围,因为一年月份最多12个,每月天数最多31....
然后就是想用枚举。例如存储12个月份,然后根据输入输出12个月份中的一个....
下面是我的代码,错误很多。希望前辈指点。我后面用switch语句另写了一个答案,达到题目要求了。但还想试试其他写法,也能达到题目的要求
#include <stdio.h>
int main(void)
{
unsigned int month = 0u;
unsigned int data = 0u;
unsigned int years = 0u;
printf("输入三个整数: ");
scanf("%u %u %u\n",&month,&data,&years);
if(month >= 1 && month <= 12)
{if(data <=31 && data >= 1)
{if(years <= 1900 && years >= 2099)
enum day {1st,2nd,3rd,4th,5th,6th,7th,8th,9th,10th,11th,12th,
13th,14th,15th,16th,17th,18th,19th,20th,21st,22nd,23rd,
24th,25th,26th,27th,28th,29th,30th,31st};
enum day today = 1st+(data-1);
enum a_month {January,FebMarch,MarApril,AprMay,MayJune,JunJuly,JulAugust,AugSeptember,
SepOctober,OctNovember,NovDecember,December};
enum a_month A_month= January+(month-1);
printf("%u %u %u\n",today,A_month,years);
else
printf("请输入1900到2099之间的整数");
}
else
printf("请输入1到31之间的整数");
}
else
printf("请输入1到12之间的整数");
上面这个代码错误很多,求前辈指点。下面是我用switch语句写的另外一个答案代码
#include <stdio.h>
int main(void)
{
unsigned int month = 0u;
unsigned int day = 0u;
unsigned int years = 0u;
printf("输入三个整数:");
scanf("%u %u %u",&month,&day,&years);
switch(day)
{
case 1:
printf("1st");
break;
case 2:
printf("2nd");
break;
case 3:
printf("3rd");
break;
case 4:
printf("4th");
break;
case 5:
printf("5th");
break;
case 6:
printf("6th");
break;
case 7:
printf("7th");
break;
case 8:
printf("8th");
break;
case 9:
printf("9th");
break;
case 10:
printf("10th");
break;
case 11:
printf("11th");
break;
case 12:
printf("12th");
break;
case 13:
printf("13th");
break;
case 14:
printf("14th");
break;
case 15:
printf("15th");
break;
case 16:
printf("16th");
break;
case 17:
printf("17th");
break;
case 18:
printf("18th");
break;
case 19:
printf("19th");
break;
case 20:
printf("20th");
break;
case 21:
printf("21st");
break;
case 22:
printf("22nd");
break;
case 23:
printf("23rd");
break;
case 24:
printf("24th");
break;
case 25:
printf("25th");
break;
case 26:
printf("26th");
break;
case 27:
printf("27th");
break;
case 28:
printf("28th");
break;
case 29:
printf("29th");
break;
case 30:
printf("30th");
break;
case 31:
printf("31st");
break;
default:
printf("请输入1到31之间的整数");
break;
}
switch(month)
{
case 1:
printf(" January");
break;
case 2:
printf(" Februar");
break;
case 3:
printf(" March");
break;
case 4:
printf(" April");
break;
case 5:
printf(" May");
break;
case 6:
printf(" June");
break;
case 7:
printf(" July");
break;
case 8:printf(" August");
break;
case 9:
printf(" September");
break;
case 10:
printf(" October");
break;
case 11:
printf(" November");
break;
case 12:
printf(" December");
break;
default:
printf("请输入1到12之间的整数");
break;
}
if(years <=2099 && years >= 1900)
printf(" %u\n",years);
else
printf("请输入1900到2099之间的整数");
return 0;
}