在C ++中解析字符串,接受单数字和双数字日期格式

时间:2017-05-04 17:00:20

标签: c++

当从用户输入作为日期时,我希望能够提取数字,无论格式是mm / dd / yy还是m / d / yy。我只能弄清楚如何做一个或另一个,但不能两者兼顾。我怎么能这样做?我是来自python的C ++的新手。这是我的代码。

#include <iostream>
#include <string>
using namespace std;



bool isMagicDate(string year, string month, string day)

{       
int int_month = stoi(month);
int int_day  = stoi(day);
int int_year = stoi(year);

if (int_month * int_day == int_year)
    return true;

else return false;
}   


int main()
{
string date;

cout << "enter a date in the format mm/dd/yy: " << endl;
cin >> date;


string month = date.substr(0,2);
string day = date.substr(3,2);
string year = date.substr(6,2);

cout << "the month is " << month  << endl 
     << "the day is " << day  << endl
     << "the year is " << year << endl;

cout << "the date you entered is " << date << endl;

bool magic = isMagicDate(year, month, day);

cout << "Is the date magic? " << magic << endl;


return 0;
}

4 个答案:

答案 0 :(得分:1)

如果您的输入具有完全mm/dd/yym/d/yy格式。您可以使用字符串.find()进行简单检查,如下所示:

if(date.find("/") == 2)
{
    // do what you need if first "/" is at position 2
}
else
{
    // do what you need if the first "/" is at position 1
}

答案 1 :(得分:1)

您可以使用std::istringstream来解析日期字符串,例如:

#include <iostream>
#include <string>
#include <sstream>

bool isMagicDate(int year, int month, int day)
{       
    return ((month * day) == year);
}   

int main()
{
    std::string date;

    std::cout << "enter a date in the format mm/dd/yy: " << std::endl;
    std::cin >> date;

    int month, day, year;
    char slash1, slash2;

    std::istringstream iss(date);
    if ((iss >> month >> slash1 >> day >> slash1 >> year) &&
        (slash1 == '/') && (slash2 == '/'))
    {
        std::cout << "the date you entered is " << date << std::endl;

        std::cout << "the month is " << month << std::endl 
             << "the day is " << day  << std::endl
             << "the year is " << year << std::endl;

        bool magic = isMagicDate(year, month, day);

        std::cout << "Is the date magic? " << magic << std::endl;
    }
    else
        std::cout << "invalid date entered!" << std::endl;

    return 0;
}

或者,在C ++ 11及更高版本中,您可以改为使用std::get_time I / O操纵器:

#include <iostream>
#include <string>
#include <iomanip>

int main()
{
    std::cout << "enter a date in the format mm/dd/yy: " << std::endl;

    std::tm t = {};
    if (std:cin >> std::get_time(&t, "%m/%d/%y"))
    {
        std::cout << "the date you entered is " << std::put_time(&t, "%c") << std::endl;

        std::cout << "the month is " << tm.tm_mon+1 << std::endl 
             << "the day is " << tm.tm_mday << std::endl
             << "the year is " << tm.tm_year << std::endl;

        // use tm as needed ...
    }
    else
        std::cout << "invalid date entered!" << std::endl;

    return 0;
}

答案 2 :(得分:1)

将您的日期分为月,日和日以'/'作为分隔符的年份

// func to split the date
vector<string> split_date(const string &s, char delim) {
    stringstream ss(s);
    string item;
    vector<string> tokens;
    while (getline(ss, item, delim)) {
        tokens.push_back(item);
    }
    return tokens;
}

注意 此解决方案不会跳过空标记。

现在您可以轻松提取月份,日期和时间的值。来自split_date返回的向量的年份。

// splitting
string date = "04/05/2017"; // mm/dd/yy or m/d/yy

vector<string> splitted_date = split_date(date, '/');
if(splitted_date.size() == 3) {
    string month = splitted_date[0];
    string day = splitted_date[1];
    string year = splitted_date[2];
}

答案 3 :(得分:0)

这是一个使用结构的解决方案,可以为您提供另一个想法。

#include <stdio.h>
#include <conio.h>  // for clrscr() function
#include <iostream>
#include <string>

using namespace std;

struct Date
{
    string month;
    string day;
    string year;
};

void setDate(Date &d);
void printDate(Date &d);
bool isMagicDate(Date &d);

 int main(int argc, char* argv[])
{
    Date mydate;

    setDate(mydate);

    cout << "The Month is: " << mydate.month << endl;
    cout << "The Day is: " << mydate.day << endl;
    cout << "The Year is: " << mydate.year << endl;

    cout << "The date you entered is: ";

    printDate(mydate);

    bool magic = isMagicDate(mydate);

    cout << "Is the date magic? " << isMagicDate(mydate) << endl;


    getch();
    return 0;
}


void setDate(Date &d)
{
    string date;
    printf("Enter date mm/dd/yy: ");

    getline(cin,date,'/');
    d.month = date;

    getline(cin,date,'/');
    d.day = date;

    getline(cin,date,'\n');
    d.year = date;

}


bool isMagicDate(Date &d)
{
    return stoi(d.month) * stoi(d.day) == stoi(d.year);
}


void printDate(Date &d)
{
    cout << d.month << "/" << d.day << "/" << d.year << endl;
}