2017-05-04 14 views
0

ユーザから日付を入力するとき、数値がmm/dd/yyかm/d/yyのいずれであるかを抽出できます。 。私はどちらかをどうやって行うのかしか分かりませんが、両方はできません。これについてどうすればいいですか?私はPythonから来ているC++には新しいです。ここに私のコードです。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; 
} 
+0

findとsubstrを使用する区切り文字関数を使用できます。そうすれば、両方の日付形式を扱うことができます。例が必要な場合はお知らせください。しかしここにはありますhttp://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c – geekonedge

答えて

1

また
#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以降では、あなたができるあなたはそうのような文字列.find()との簡単なチェックを行うことができます代わりに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; 
} 
+1

おそらく'ignore'が何であるかを検証します。 –

+0

@BoundaryImposition:done –

+0

ありがとうございましたこれは非常に役に立ちました –

1

あなたの入力は正確にmm/dd/yyまたはm/d/yy形式になります。あなたが例えば、日付文字列を解析するstd::istringstreamを使用することができます

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

は、月にとのデート&年のあなたの日付を分割このソリューションは、空のトークンをスキップしていないことを区切り

// 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; 
} 

として210。

&年の値を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]; 
} 
+0

これも有効なアプローチです。後でintにlexcastするのを忘れないでください。 –

+0

そして、 'splitted_date [index]'で値にアクセスする前に 'splitted_date.size()'が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; 
} 
関連する問題