首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在C++中用sscanf赋值

在C++中用sscanf赋值
EN

Stack Overflow用户
提问于 2018-11-03 12:31:35
回答 1查看 250关注 0票数 0

有人能指出,如果存在,我的代码中的错误,因为这个问题花了我相当多的时间来解决,而且我对C++编程非常陌生。

因此,2016-07-08/01:00,是检查字符串是否有效,因此有效的日期格式是ex:bool valid_str2016-7-8/01:00

在带有字符串的Date构造函数中,如果格式无效,则将其赋值给所有0。如果有效,然后将其赋值给变量,这只是一段大代码,但我自己的想法是,我搞砸了sscanf,从而得到了RunTime Error (im使用Linux)。我用对了吗?

代码语言:javascript
复制
bool valid_str(const std::string& str)
{
    int len = str.size();
    if (len != 16)
        return false;
    if (str[4] != '-')
        return false;
    if (str[7] != '-')
        return false;
    if (str[10] != '/')
        return false;
    if (str[13] != ':')
        return false;

    for (int i = 0; i < 4; i++) {
        if (str[i] < '0' || str[i] > '9')
            return false;
    }

    for (int i = 5; i < 7; i++) {
        if (str[i] < '0' || str[i] > '9')
            return false;
    }

    for (int i = 8; i < 10; i++) {
        if (str[i] < '0' || str[i] > '9')
            return false;
    }

    for (int i = 11; i < 13; i++) {
        if (str[i] < '0' || str[i] > '9')
            return false;
    }

    for (int i = 14; i < 16; i++) {
        if (str[i] < '0' || str[i] > '9')
            return false;
    }

    return true;
}

//Date Constructor

Date::Date(const std::string& dateString)
{
    std::string str;
    if (valid_str(dateString) == false) {
        this->m_year = 0;
        this->m_month = 0;
        this->m_day = 0;
        this->m_hour = 0;
        this->m_minute = 0;
    }
    else {
        sscanf(dateString.c_str(), "%4d-%2d-%2d/%2d:%2d",
            &m_year,
            &m_month,
            &m_day,
            &m_hour,
            &m_minute);
    }
}  

(编辑)完整代码:

代码语言:javascript
复制
#include "Date.hpp" 
//#include <initializer_list> 
//#include <string> 
#include <iomanip>
#include <stdio.h>

//using namespace std;
//bool valid_str(std::string &);

 Date::Date(){ } //default constructor 

 /**
  * @brief constructor with arguments
  */
Date::Date(int t_year, int t_month, int t_day, int t_hour, int t_minute) {
m_year = t_year; 
m_month = t_month; 
m_day = t_day; 
m_hour = t_hour; 
m_minute = t_minute; 
}

bool valid_str(const std::string& str) { 
    int len = str.size(); 
    if(len != 16) 
    return false; 
    if(str[4] != '-') 
    return false; 
    if(str[7] != '-') 
    return false; 
    if(str[10] != '/') 
    return false; 
    if(str[13] != ':') 
    return false; 

    for(int i = 0 ; i < 4 ; i++) { 
        if(str[i] < '0' || str[i] > '9') 
        return false; 
        } 

        for(int i = 5 ; i < 7 ; i++) { 
            if(str[i] < '0' || str[i] > '9') 
            return false; 
            } 

            for(int i = 8 ; i < 10 ; i++) { 
                if(str[i] < '0' || str[i] > '9') 
                return false;
                 } 

                for(int i = 11 ; i < 13 ; i++) { 
                    if(str[i] < '0' || str[i] > '9') 
                    return false; 
                    } 

                    for(int i = 14 ; i < 16 ; i++) { 
                        if(str[i] < '0' || str[i] > '9') 
                        return false; 
                        } 

                        return true; 
                        }


 /**
  * @brief constructor with a string
  */
Date::Date(const std::string &dateString)
{ 
    std::string str;
     if(valid_str(dateString) == false) {
          this->m_year = 0; 
          this->m_month = 0; 
          this->m_day = 0; 
          this->m_hour = 0; 
          this->m_minute = 0; 
          } else{ 
              sscanf(dateString.c_str(), "%4d-%2d-%2d/%2d:%2d",
        &m_year,
        &m_month,
        &m_day,
        &m_hour,
        &m_minute);

                  }
                }

int Date::getYear(void) const
{
    return m_year;
}

void Date::setYear(const int t_year){
    m_year = t_year;
}

int Date::getMonth(void) const{
    return m_month;
}

void Date::setMonth(const int t_month){
    m_month = t_month;
}

int Date::getDay(void) const{
    return m_day;
}

void Date::setDay(const int t_day){
    m_day = t_day;
}

  int Date::getHour(void) const{
      return m_hour;
  }

    void Date::setHour(const int t_hour){
        m_hour = t_hour;
    }

      int Date::getMinute(void) const{
          return m_minute;
      }

      void Date::setMinute(const int t_minute){
        m_minute = t_minute;
      }

      int leapYear(int y){
       if (y % 400 == 0)
        return 1;
        if (y %4 == 0 && y % 100 == 0){
        return 1;
        }else 
        return 0;
    }

     bool Date::isValid(const Date &t_date){
        int x[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        x[1] += leapYear(t_date.m_year);

        if(t_date.m_year < 1000 || t_date.m_year > 9999){
            return false;
        }else if (t_date.m_month < 1 || t_date.m_month > 12){
            return false;
        }else if(t_date.m_day < 1 || t_date.m_day > x[t_date.m_month - 1]){
            return false;
        }else if(t_date.m_hour > 24 || t_date.m_hour < 0){
            return false;
        }else if(t_date.m_minute > 59 || t_date.m_minute < 0){
            return false;
        }else return true;
            }

/**
  *  @brief overload the assign operator
  */
Date& Date::operator=(const Date &t_date){
        this->m_year = t_date.m_year;
        this->m_month = t_date.m_month;
        this->m_day = t_date.m_day;
        this->m_hour = t_date.m_hour;
        this->m_minute = t_date.m_minute;
}
/**
  * @brief check whether the CurrentDate is  greater than the t_date
  */
bool Date::operator>(const Date &t_date) const{
    if(this->m_year > t_date.m_year){
        return true;
    }else if(this->m_month > t_date.m_month){
        return true;
    }else if(this->m_day > t_date.m_day){
        return true;
    }else if(this->m_hour > t_date.m_hour){
        return true;
    }else if(this->m_minute > t_date.m_minute){
        return true;
    }else return false;
}

/**
  * @brief check whether the CurrentDate is  less than the t_date
  */
bool Date::operator<(const Date &t_date) const{
    if(this->m_year < t_date.m_year){
        return true;
    }else if(this->m_month < t_date.m_month){
        return true;
    }else if(this->m_day < t_date.m_day){
        return true;
    }else if(this->m_hour < t_date.m_hour){
        return true;
    }else if(this->m_minute < t_date.m_minute){
        return true;
    }else return false;
}


 /**
  * @brief check whether the CurrentDate is  greater or equal than the t_date
  */
  bool Date::operator>=(const Date &t_date) const{
      if (*this > t_date || *this == t_date)
        return true;
    else
        return false;
  }


  /**
  * @brief check whether the CurrentDate is  less than or equal to the t_date
  */
  bool Date::operator<=(const Date &t_date) const{
       if (*this < t_date || *this == t_date)
        return true;
    else
        return false;
  }

/**
  * @brief check whether the CurrentDate is equal to the t_date
  */
bool Date::operator==(const Date &t_date) const{
    if (this->m_year == t_date.m_year)
        return true;
    if (this->m_month == t_date.m_month)
        return true;
    if (this->m_day == t_date.m_day)
        return true;
    if (this->m_hour == t_date.m_hour)
        return true;
    if (this->m_minute == t_date.m_minute)
        return true;
    return false;
}


Date Date::stringToDate(const std::string &t_dateString){
    int y = 0, m = 0, d = 0, h = 0, mm = 0;
    sscanf(t_dateString.c_str(), "%d-%d-%d/%d:%d", y, m, d, h, mm);
    return Date(y, m, d, h, mm);
}

std::string Date::dateToString(const Date &t_date){
    char c[30];
    sscanf(c, "%4d-%2d-%2d/%2d:%2d", t_date.m_year, t_date.m_month, t_date.m_day, t_date.m_hour, t_date.m_minute);
    return std::string(c);
}
EN

回答 1

Stack Overflow用户

发布于 2018-11-03 16:26:09

作为参考,它在Windows 10上编译和执行,在MS编译器版本19.15.26732.1 x64上没有问题

代码语言:javascript
复制
uint32_t m_year = 0;
uint32_t m_month = 0;
uint32_t m_day = 0;
uint32_t m_hour = 0;
uint32_t m_minute = 0;

constexpr char datestring[] = "2016-07-08/01:00";
constexpr char invalid[] = "2016-7-8/01:00";
constexpr char format[] = "%4u-%2u-%2u/%2u:%2u";

void getdate(const char date[])
{
    int32_t result = sscanf(date, format, 
        &m_year,
        &m_month,
        &m_day,
        &m_hour,
        &m_minute);

    _ASSERT(result == 5);
    _ASSERT(m_year == 2016);
    _ASSERT(m_month == 7);
    _ASSERT(m_day == 8);
    _ASSERT(m_hour == 1);
    _ASSERT(m_minute == 0);
}

int wmain()
{
    getdate(datestring);
    getdate(invalid);

    std::string_view sw{datestring};
    getdate(sw.data());
    std::string str{datestring};
    getdate(str.c_str());
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53131337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档