[求助]用if 和switch语句不能解决问题。能不能把程序简单化。
c语言入门经典(美。霍顿)习题:输入日月年,输出日月年。输出格式为:例如输入12,12,2003,输入12th,December,2003.大家看看有我这么编程的么。
有好多错啊,有没有更好的算法。
#include<stdio.h>
int main(void)
{
int year;
char month,date;
scanf("%c%c%d",&date,&month,&year);
if(date=='1')
date='1st';
if(date=='2')
date='2nd';
if(date=='3')
date='3rd';
if(date=='4')
date='4th';
if(date=='5')
date='5th';
if(date=='6')
date='6th';
if(date=='7')
date='7th';
if(date=='8')
date='8th';
if(date=='9')
date='9th';
if(date=='10')
date='10th';
if(date=='11')
date='11st';
if(date=='12')
date='12nd';
if(date=='13')
date='13rd';
if(date=='14')
date='14th';
if(date=='15')
date='15th';
if(date=='16')
date='16th';
if(date=='17')
date='17th';
if(date=='18')
date='18th';
if(date=='19')
date='19th';
if(date=='20')
date='20th';
if(date=='21')
date='21st';
if(date=='22')
date='22nd';
if(date=='23')
date='23rd';
if(date=='24')
date='24th';
if(date=='25')
date='25th';
if(date=='26')
date='26th';
if(date=='27')
date='27th';
if(date=='28')
date='28th';
if(date=='29')
date='29th';
if(date=='30')
date='30th';
if(date=='31')
date='31st';
switch(month)
{case'1':
month='January';
break;
case'2':
month='February';
break;
case'3':
month='March';
break;
case'4':
month='April';
break;
case'5':
month='May';
break;
case'6':
month='June';
break;
case'7':
month='July';
break;
case'8':
month='August';
break;
case'9':
month='September';
break;
case'10':
month='October';
break;
case'11':
month='November';
break;
case'12':
month='December';
break;
default:
printf("input erro");
break;
}
printf("%c%c%d",date,month,year);
return 0;
}