扫描2种不同类型的输入以获取日期

时间:2014-03-30 15:50:07

标签: c scanf date-parsing

我有一个完美的代码,能够告诉我输入特定日期的星期几,但是我对如何接受输入感到困惑。我必须能够以下列形式接受这两个输入:

mm / dd / yyyy ~~~~~~~~~~~~~~~示例:03/04/2014

月dd,yyyy ~~~~~~~~~~~~例如:2014年3月4日

无论使用哪种输入,我都无法让我的程序能够接收这些不同类型的输入并吐出正确的工作日。

#include <stdio.h>
int main(){
int inputyear, daynumb, days_passed_since_anchor, inputmonth, days_in_month,totdays_for_7,rmd_for_7;

days_in_month=days_passed_in_months(inputmonth, inputyear);
//call  function to get days in all the previous months in the current year, if we are in march then there were 59 days before this month if its a non leap year.

      days_passed_since_anchor = (inputyear-1905) * (365.25);

      daynumb = days_in_month + inputday;

      totdays_for_7 = days_passed_since_anchor + daynumb;

      rmd_for_7=totdays_for_7%7;
      if(rmd_for_7==2){
          printf("Monday \n");
        }
      if(rmd_for_7==3){
          printf("Tuesday \n");
        }
      if(rmd_for_7==4){
          printf("Wednesday \n");
        }
      if(rmd_for_7==5){
          printf("Thursday \n");
        }
      if(rmd_for_7==6){
          printf("Friday \n");
        }
      if(rmd_for_7==0){
          printf("Saturday \n");
        }
      if(rmd_for_7==1){
          printf("Sunday \n");
        }
      return 0;

}

1 个答案:

答案 0 :(得分:2)

mm/dd/yyyy~~~~~~~~~~~~~~~Example: 03/04/2014
Month dd, yyyy~~~~~~~~~~~~Example: March 04, 2014 

请注意,一种格式以数字开头,另一种格式以名字开头。所以,试试这个:

char buffer[32];
if(3 != fscanf(file, "%d/%d/%d", &month, &day, &year))
  if(3 != fscanf(file, "%31s %d, %d", buffer, &day, &year))
    abort()
  else
    month = mapmonthname(buffer);
/* Now do fun things... */