首页 > 代码库 > Exercise 3.2 Display a date

Exercise 3.2 Display a date

无法让我不汗颜:

Exercise 3-2. Write a program that prompts the user to enter the date as three integer
values for the month, the day in the month, and the year. the program should then output
the date in the form 31st December 2003 when the user enters 12 31 2003, for example.
You will need to work out when th, nd, st, and rd need to be appended to the day value. Don’t forget 1st, 2nd, 3rd,
4th; but 11th, 12th, 13th, 14th; and 21st, 22nd, 23rd, and 24th.

#include<stdio.h>int main(void){    int date = 0;    int month = 0;    int year = 0;        printf("pls enter the date");//如何一块输入日期、时间、?    scanf("%d", &date);//date描述不对,应该是day     scanf("%d", &month);    scanf("%d", &year);        if(date>3 && date<21 || date>23 && date<31)              printf("%dth", date);//没有换行符     else    printf("%d%s", date, (date == 1?"st":(date == 2?"nd": "rd")));//后面的要加"字符串符号" .2.?:格式记错  3. 赋值符号和等于号记错 switch (month):{     case month = 1; //月份直接写反             printf("January");              breakcase month = 2 ;             printf("February");             break;      case month = 3;             printf("March");             break;      case month = 4;             printf("April");             break;      case month = 5;             printf("May");             break;      case month = 6;             printf("June");             break;      case month = 7;             printf("July");             break;      case month = 8;             printf("August");             break;      case month = 9;             printf("September");             break;      case month = 10;             printf("October");             break;      case month = 11;             printf("November");             break;      case month = 12;             printf("December");             break;        }     printf("%d", year);        return 0;}

 

别人家的代码:

 1 // Exercise 3.2 Display a date 2 #include <stdio.h> 3  4 int main(void) 5 { 6   int month = 0; 7   int day = 0; 8   int year = 0; 9   printf("Enter the date as three integer values separated by spaces (month day year): ");10   scanf("%d", &month);11   scanf("%d", &day);12   scanf("%d", &year);13   if(day > 3 && day < 21 || day > 23 && day < 31)14     printf("\n%dth ",day);15   else16     printf("\n%d%s ", day, (day%10 == 1 ? "st": (day%10 == 2 ? "nd" : "rd")));// %s,string17 18   switch(month)19   {20     case 1:21       printf("January ");22       break;23     case 2:24       printf("February ");25       break;26     case 3:27       printf("March ");28       break;29     case 4:30       printf("April ");31       break;32     case 5:33       printf("May");34       break;35     case 6:36       printf("June");37       break;38     case 7:39       printf("July");40       break;41     case 8:42       printf("August");43       break;44     case 9:45       printf("September");46       break;47     case 10:48       printf("October");49       break;50     case 11:51       printf("November");52       break;53     case 12:54       printf("December");55       break;56   }57   printf(" %d\n", year);58   return 0;59 }

 

===================

........

printf("\n%dth ",day);

.......

printf("May");

........

printf(" %d\n", year);

--------------------------------

三个并列的printf

最后的输出却是这种结果31st December 2003

why不是换行?

答:

#include<stdio.h>
int main(void)
{
printf("asl;fkjl");
printf(" ---");
return 0;
}

编译器显示,的确是同层次的printf按照一行输出,除非有换行符、

==================

Exercise 3.2 Display a date