Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a class that has three unsigned members representing a year, month,
- * and day. Write a constructor that takes a string representing a date. Your
- * constructor should handle a variety of date formats, such as January 1,
- * 1990. 1/1/1900. Jan 1, 1900, and so on.
- */
- #include <iostream>
- #include <vector>
- #include <sstream>
- #include <string>
- using std::cin; using std::cout; using std::endl; using std::vector;
- using std::string; using std::istringstream; using std::ostream;
- class Date {
- friend void mdy(const string &str, Date &obj);
- friend void mmdy(const string &str, Date &obj);
- friend void slashmdy(const string &str, Date &obj);
- public:
- Date() : year(0), month(0), day(0) {}
- Date(const string &str);
- void printdata(ostream &os) { cout << "Month: " << month << "\n"
- << "Day: " << day << "\n"
- << "Year: " << year << endl;}
- private:
- unsigned year, month, day;
- vector<const string> monthabbr = {"Jan", "Feb", "Mar", "Apr", "May",
- "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
- vector<const string> month = {"January", "February", "March", "April",
- "May", "June", "July", "August", "September",
- "October", "November", "December"};
- };
- int main(void)
- {
- /*cout << "Creating first: ";
- Date first;
- cout << endl;*/
- cout << "Creating second: ";
- Date second("Jan 1, 1900");
- second.printdata(cout);
- cout << "Creating third: ";
- Date third("5/12/1900");
- third.printdata(cout);
- /*
- cout << "Creating fourth: ";
- Date fourth("1/1,/1900");*/
- cout << endl;
- //first.printdata(cout);
- //second.printdata(cout);
- return 0;
- }
- // Jan 31, 2014
- void mdy(const string &str, Date &obj)
- {
- }
- // January 2, 2018
- void mmdy(const string &str, Date &obj)
- {
- }
- // 1/1/1900 | m/d/y
- // 11/31/1900
- void slashmdy(const string &str, Date &obj)
- {
- unsigned pos = 0;
- obj.month = 0, obj.day = 0, obj.year = 0;
- string::const_iterator current;
- // Create three istringstreams and convert it to int
- for (string::const_iterator beg = str.begin();
- beg != str.end();
- ++beg, ++pos) {
- // get month
- if (*beg == '/' && pos > 0 && pos < 3) {
- string strmonth(str.begin(), beg);
- istringstream convert(strmonth);
- current = beg;
- if (!(convert >> obj.month))
- obj.month = 0;
- }
- // get day and year
- if (*beg == '/' && pos > 2 && pos < 6) {
- string strday(current + 1, beg);
- istringstream convert(strday);
- current = beg;
- if (!(convert >> obj.day))
- obj.day = 0;
- string stryear(current + 1, str.end());
- istringstream convertyr(stryear);
- if (!(convertyr >> obj.year))
- obj.year = 0;
- }
- }
- // now check if date is correctly, no need to check the year
- if (!(obj.month >= 1 && obj.month <= 12))
- obj.month = 0;
- if (!(obj.day >= 1 && obj.day <= 31))
- obj.day = 0;
- }
- /* Types of initialization:
- 1: "January 1, 1990" // call mmdy()
- 2: "1/1/1900" // call slashmdy
- 3: "Jan 1, 1900"*/ // call mdy()
- /* Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec */
- Date::Date (const string &str)
- {
- unsigned slash = 0, comma = 0;
- for (string::const_iterator beg = str.begin();
- beg != str.end();
- ++beg) {
- switch (*beg) {
- case '/': ++slash; break;
- case ',': ++comma; break;
- default: break;
- }
- }
- if (slash == 2 && !comma) { //-- seems ok
- slashmdy(str, *this);
- } else if (comma == 1 && !slash) {
- cout << "One comma!" << endl;
- unsigned count = 0;
- for (string::const_iterator beg = str.begin();
- beg != str.end();
- ++beg, ++count)
- if (*beg == ' ')
- break;
- if (count == 3)
- cout << "mdy();" << endl; //mdy(str, *this);
- else
- cout << "mmdy();"<< endl;//mmdy(str, *this);
- } else {
- // "default" initialize, didn't want use delegated constructors
- month = 0, day = 0, year = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement