C语言编程问题

【题目内容】
合同和其他法律文档中经常使用下列日期格式:
Dated this day of ,20 .
编写程序,用这种格式来显示日期。用户以月/日/年的格式录入日期,然后计算机显示出“法定”格式的日期。
【样式输入】
Enterdate (mm/dd/yy):7/19/14

【样式输出】
Datedthis 19th day of July,2014.

【注意】
考虑两个问题:1、如何为日添加“th”或“st”,“nd”,“rd”;2、如何用单词而不是数字来显示月份。

解法如下:

#include<stdio.h>
int main() {
int d, m, y1, y2;
char *months[12] = { "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December", };
printf("Enter date (mm/dd/yy): ");
if (scanf("%d/%d/%c%c", &m, &d, &y1, &y2) != 4) {
printf("Illegal input.\n");
return 0;
}
printf("Dated this ");
switch (d % 10) {
case 1:
if (d / 10 == 1)
goto DEFAULT;
printf("%dst. ", d);
break;
case 2:
if (d / 10 == 1)
goto DEFAULT;
printf("%dnd. ", d);
break;
case 3:
if (d / 10 == 1)
goto DEFAULT;
printf("%drd. ", d);
break;
default:
DEFAULT: printf("%dth. ", d);
break;
}
printf("day of %s, 20%c%c", months[m - 1], y1, y2);
return 0;
}

“注意”部分的问题在代码中已体现。

追问

能把代码复制过来吗?我懒得自己打。。谢谢啦!!

追答

代码已经包含在上次的回答中了。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-04-16
#include<stdio.h>
int main()
{
int d, m, y;
printf("Enter date (mm/dd/yy):");
char *months[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
if ((scanf("%d/%d/%d", &m, &d, &y)!=3)||y<0||y>99||d<0||m<0||m>12)
{
printf("Input error!\n");
return 0;
}
int a[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
if((y+1900)%4==0)
{
a[2]++;
}
if(d>a[m])
{
printf("Input error!\n");
return 0;
}
printf("Dated this ");
switch (d % 10)
{
if(d<20&&d>10)
{
goto DEFAULT;
}
case 1:
if (d / 10 == 1)
goto DEFAULT;
printf("%dst ", d);
break;
case 2:
if (d / 10 == 1)
goto DEFAULT;
printf("%dnd ", d);
break;
case 3:
if (d / 10 == 1)
goto DEFAULT;
printf("%drd ", d);
break;
default:
DEFAULT:
printf("%dth ", d);
break;
}
printf("day of %s, 20%.2d.\n", months[m - 1], y);
return 0;
}
相似回答