-2

例子:

输入(dd mm yyyy):11 11 2011

输出:2011 年 11 月 11 日

这是正确的,但月份和年份之间有一个额外的数字

我不知道如何删除它。请帮忙!

#include <stdio.h>
int getDay(int day)
{
  switch(day)
  {
    case 1: printf("%dst ", day); break;
    case 2: printf("%dnd ", day); break;
    case 3: printf("%drd ", day); break;
    default: printf("%dth ", day); break;
  }
}
int getMonth(int month)
{
  switch(month)
  {  
    case 1: printf("January "); break;
    case 2: printf("February "); 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("Invalid "); break;
  }
}

int main()
{
  int day, month, year;
  printf("Enter date in dd mm yyyy format: ");
  scanf("%d %d %d", &day, &month, &year);
  day = getDay(day);
  month = getMonth(month);
  year = year;
  printf("%d", day);
  printf("%d", month);
  printf("%d", year);
  fflush(stdin);
  getchar();
}
4

5 回答 5

1

为什么你的getMonth函数(应该称为 IMO printMonth)既有副作用(它打印一些东西)又给出了一些有用(但未指定)的值(即缺少return)?你应该学会启用所有警告,改进你的代码直到编译器没有给出警告,并使用调试器。在 Linux 上,这意味着编译gcc -Wall -g source.c -o myprog然后调试gdb myprog

于 2012-10-22T12:38:18.407 回答
1

Your getMonth() and getDay() functions both should return an int according to the headers, you're not returning anything from the functions, so what's stored in these lines:

day = getDay(day);
month = getMonth(month); 

Is anyone's guess, then you're printing those values here:

printf("%d", day);   
printf("%d", month); 

If you just want to display "11th November 2011" have both your getDay and getMonth functions return void, don't re-assign day and month, and restructure your output:

getDay(day);         //print day and "th" or "st" or "rd"
getMonth(month);     //print month
printf("%d", year);  //print year
于 2012-10-22T12:42:37.237 回答
0

该代码中有很多错误和未定义的行为。我建议做类似的事情:

#include <stdio.h>

typedef enum
{
  DAY_FORMAT_ST,
  DAY_FORMAT_ND,
  DAY_FORMAT_RD,
  DAY_FORMAT_TH,
  DAY_FORMAT_N

} day_format_t;

#define MONTHS_N  12


const char* get_day_str   (unsigned int day);
const char* get_month_str (unsigned int month);



int main()
{
  int day;
  int month;
  int year;

  printf("Enter date in dd mm yyyy format: ");
  scanf("%d %d %d", &day, &month, &year);
  getchar(); // clear trailing line feed from stdin


  printf("%d%s ", day, get_day_str(day));
  printf("%s ", get_month_str(month));
  printf("%d", year);
  getchar();
  return 0;
}



const char* get_day_str (unsigned int day)
{
  static const char* DAY_FORMAT [DAY_FORMAT_N] =
  {
    "st",
    "nd",
    "rd",
    "th"
  };

  day_format_t  day_index = day - 1; // compensate for 0-index arrays
  const char*   result;

  if (day_index < DAY_FORMAT_TH)
  {
    result = DAY_FORMAT[day_index];
  }
  else
  {
    result = DAY_FORMAT[DAY_FORMAT_TH];
  }

  return result;
}

const char* get_month_str (unsigned int month)
{
  static const char* MONTH [MONTHS_N] =
  {
    "January",
    "February",    
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
  };

  return MONTH [month-1];
}

请注意,代码需要额外的错误处理来处理奇怪的用户输入,并且这种错误处理最好在 main() 中而不是在函数中。

于 2012-10-22T13:24:58.460 回答
0
#include <stdio.h>
int getDay(int day)
{
  switch(day)
  {
    case 1: printf("%dst ", day); break;
    case 2: printf("%dnd ", day); break;
    case 3: printf("%drd ", day); break;
    default: printf("%dth ", day); break;
  }
}
int getMonth(int month)
{
  switch(month)
  {  
    case 1: printf("January "); break;
    case 2: printf("February "); 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("Invalid "); break;
  }
}

int main()
{
  int day, month, year;
  printf("Enter date in dd mm yyyy format: ");
  scanf("%d %d %d", &day, &month, &year);
  getDay(day);
  getMonth(month);
  printf("%d", year);
  fflush(stdin);
  getchar();
}
于 2012-10-22T12:51:05.620 回答
0

you were printing day and month twice here is redefined code

 #include <stdio.h>
 void getDay(int day)
 {
   switch(day)
 {
      case 1: printf("%dst ", day); break;
       case 2: printf("%dnd ", day); break;
       case 3: printf("%drd ", day); break;
       default: printf("%dth ", day); break;
 }
}
void getMonth(int month)
{
 switch(month)
{
  case 1: printf("January "); break;
   case 2: printf("February "); 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("Invalid "); break;
}
}

int main()
{
   int day, month, year;
   printf("Enter date in dd mm yyyy format: ");
    scanf("%d %d %d", &day, &month, &year);
      getDay(day);
    getMonth(month);
    year;
   // printf("%d", day);
  // printf("%d", month);
   printf("%d", year);
   fflush(stdin);
   getchar();
}

this is one way....

于 2012-10-22T12:46:45.177 回答