Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 扫描一个日期的两种不同类型的输入_C_Scanf_Date Parsing - Fatal编程技术网

C 扫描一个日期的两种不同类型的输入

C 扫描一个日期的两种不同类型的输入,c,scanf,date-parsing,C,Scanf,Date Parsing,我有一个完美的工作代码,可以告诉我当我输入某个日期时,它是一周中的哪一天,但是我被困在如何输入的问题上。我必须能够以以下形式接受这两种输入: 年月日~~~~~~~~~~~~~~~~示例:2014年4月3日 或 年月日示例:2014年3月4日 我在如何使我的程序能够接受这两种不同类型的输入,并且无论使用哪种输入,都能吐出正确的工作日方面遇到了问题 #include <stdio.h> int main(){ int inputyear, daynumb, days_passed_sin

我有一个完美的工作代码,可以告诉我当我输入某个日期时,它是一周中的哪一天,但是我被困在如何输入的问题上。我必须能够以以下形式接受这两种输入:

年月日~~~~~~~~~~~~~~~~示例:2014年4月3日

年月日示例: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;

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

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... */

month=mapmonthnamebuffer部分的意思是什么?这是您自定义的月份名称到月份编号的映射。你支持本地化吗?不要忘了处理不可能映射的情况。对于如何使输入工作,我仍然有点困惑,您能否澄清如何操作?选择要读取的输入。文件可以是您打开的文件,甚至可以是stdin。
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... */