【问题标题】:Enter date data type from user输入用户的日期数据类型
【发布时间】:2014-11-07 06:14:38
【问题描述】:

我遇到了一个问题,我希望用户输入日期。用户输入的日期可以是任何格式,例如。 dd-mm-yydd-mm-yyyydd-month-yy。我只能想到两种方法。

1.使用结构。但没有指定日期格式。

2.使用sscanf()sprintf()函数复制为字符串,然后使用sscanf()函数获取单个值。

请纠正我。

【问题讨论】:

    标签: c date input


    【解决方案1】:

    嗯,实现这一目标的最简单 [也可能是最安全] 的方法是

    1. 使用fgets()将输入作为字符串
    2. 使用strtok_r() 标记字符串,使用.-/ 作为分隔符,并且可能使用strlen() 来区分ddmmyyyy
    3. 使用strtol() ans store 将字符串转换为相应的整数。

    不用说,事后别忘了验证数据。

    【讨论】:

      【解决方案2】:

      使用 strtok() 如下所示,您可以使用问题中提到的任何格式获取输入的日期。

       int main()
          {
             char *p; 
             char a[20];
             printf("ENter date:\n");
             scanf("%s",a);
      
             p = strtok(a,"-");
             while( p  != NULL)
             {   
                printf("%s ",p);/* I am printing here you can do what even you want to */
                p = strtok(NULL,"-");
             }   
          }
      

      【讨论】:

      • 谢谢,你能解释一下最后一行 p=strtok(NULL,"-");
      • strtok() 保持内部状态,因此一旦您将正确的字符串值传递给此函数,稍后只需传递 NULL 和分隔符
      【解决方案3】:

      我将在这里成为魔鬼的拥护者,并建议编写一个简单的解析器。

      让我们看看格式,看看我们将接受什么:

      • 一天(01 到 31)
      • 一个月(01 到 12 或“一月”到“十二月”)
      • 一年(70-99 和 00-16 或 1970 至 2016)

      下面是我刚刚拍的东西作为例子

      #include <ctype.h>   // isalpha, isdigit
      #include <string.h>  // strncmp
      
      struct DATE {
          int day, month, year;
      };
      
      // return 1 on success, else 0 on failure
      int readdate(DATE *date, char *text) {
          int toklen = 0;
          char *tokval;
          if (isdigit(*text)) {      // expect a number for the day
              date->day = *text++ - '0';
              if (isdigit(*text))    // accept another number
                  date->day = date->day * 10 + *text++ - '0';
              if (date->day < 1 || date->day > 31)
                  return 0;
          } else return 0;
          if (*text == '-')          // expect a hyphen
              text++;
          else
              return 0;
          if (isdigit(*text)) {      // accept a number for the month...
              date->month = *text++ - '0';
              if (isdigit(*text))    // accept another number
                  date->month = date->month * 10 + *text++ - '0';
              if (date->month == 0 || date->month > 12)
                  return 0;
          } else if (isalpha(*text)) {
              tokval = text;         // accept a word
              do {
                  toklen++;
                  text++:
              } while (isalpha(*text));
              /* expect that the word is a name of a month */
              if (!strncmp("january", tokval, toklen))
                  date->month = 1;
              else if (!strncmp("february", tokval, toklen))
                  date->month = 2;
              else if (!strncmp("march", tokval, toklen))
                  date->month = 3;
              else if (!strncmp("april", tokval, toklen))
                  date->month = 4;
              else if (!strncmp("may", tokval, toklen))
                  date->month = 5;
              else if (!strncmp("june", tokval, toklen))
                  date->month = 6;
              else if (!strncmp("july", tokval, toklen))
                  date->month = 7;
              else if (!strcmp("august", tokval, toklen))
                  date->month = 8;
              else if (!strcmp("september", tokval, toklen))
                  date->month = 9;
              else if (!strcmp("october", tokval, toklen))
                  date->month = 10;
              else if (!strcmp("november", tokval, toklen))
                  date->month = 11;
              else if (!strcmp("december", tokval, toklen))
                  date->month = 12;
              else
                  return 0;
          } else return 0;
          if (*text == '-')        // expect a hyphen
              text++;
          else
              return 0;
          if (isdigit(*text)) {           // expect a number
              date->year = *text++ - '0';
              if (isdigit(*text)) {       // expect another number
                  date->year = date->year * 10 + *text++ - '0';
                  if (isdigit(*text)) {   // accept another number
                      date->year = date->year * 10 + *text++ - '0';
                      if (isdigit(*text)) // expect another number
                          date->year = date->year * 10 + *text++ - '0';
                      else
                          return 0;
                  }
              } else return 0;
              /* ensure the year fits a valid range */
              if (date->year >= 70 && date->year <= 99)
                  date->year += 1900;
              else if (date->year < 70)
                  date->year += 2000;
              if (date->year < 1970 || date->year > 2070)
                  return 0;
          } else return 0;
      /*
       * you can do some optional checking on the date here to verify if the
       * day of the month is valid here
       */
          return 1;
      }
      

      当您开始处理多种字符串格式时,从长远来看,编写一个简单的解析器通常更容易且更易于管理。但这只是我的 2 美分。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-17
        • 2013-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-29
        • 2018-12-06
        • 1970-01-01
        相关资源
        最近更新 更多