📄 validdate.cpp
字号:
//
// ValidDate.cpp
//
// Input date, use regular expression to test the date is valid or invalid
//
// L.F.
// 2006-04.03
//
#include <string>
#include <iostream>
#include <boost/regex.hpp>
#if defined(BOOST_MSVC) || (defined(__BORLANDC__) && (__BORLANDC__ == 0x550))
// problem with std::getline under MSVC6sp3
std::istream& getline( std::istream& is, std::string& s )
{
s.erase();
char c = is.get();
while ( c != '\n' )
{
s.append( 1, c );
c = is.get();
}
return is;
}
#endif
std::string validDate = std::string(
"^(?:"
"([0-9]{4}-(?:(?:0?[1,3-9]|1[0-2])-(?:29|30)|((?:0?[13578]|1[02])-31)))"
"|"
"([0-9]{4}-(?:0?[1-9]|1[0-2])-(?:0?[1-9]|1\\d|2[0-8]))"
"|"
"((?:(\\d\\d(?:0[48]|[2468][048]|[13579][26]))|(?:0[48]00|[2468][048]00|[13579][26]00))-0?2-29)"
")$"
);
boost::regex e( validDate, boost::regbase::normal );
int main( int argc )
{
std::string lineText;
do {
if ( argc == 1 )
{
std::cout << "PLEASE INPUT THE DATE <\"QUIT\" EXIT THE PROGRAM>" << std::endl;
getline( std::cin, lineText );
if ( lineText == "QUIT" )
break;
}
std::cout << "\t=========================================================" << std::endl;
std::cout << std::endl;
if ( boost::regex_match( lineText, e ) )
std::cout << "\tVALID DATE" << std::endl;
else
std::cout << "\tINVALID DATE" << std::endl;
std::cout << "\tTHE DATE YOU INPUT IS : " << lineText << std::endl;
std::cout << "\tExpression is :" << validDate << std::endl;
std::cout << std::endl;
std::cout << "\t=========================================================" << std::endl;
} while ( argc == 1 );
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -