Advertisement
Guest User

class Date

a guest
Dec 25th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.74 KB | None | 0 0
  1. /* Write a class that has three unsigned members representing a year, month,
  2.  * and day. Write a constructor that takes a string representing a date. Your
  3.  * constructor should handle a variety of date formats, such as January 1,
  4.  * 1990. 1/1/1900. Jan 1, 1900, and so on.
  5.  */
  6. #include <iostream>
  7. #include <vector>
  8. #include <sstream>
  9. #include <string>
  10. using std::cin; using std::cout; using std::endl; using std::vector;
  11. using std::string; using std::istringstream; using std::ostream;
  12.  
  13. class Date {
  14. friend void mdy(const string &str, Date &obj);
  15. friend void mmdy(const string &str, Date &obj);
  16. friend void slashmdy(const string &str, Date &obj);
  17. public:
  18.     Date() : year(0), month(0), day(0) {}
  19.     Date(const string &str);
  20.     void printdata(ostream &os) { cout << "Month: " << month << "\n"
  21.                     << "Day: " << day << "\n"
  22.                     << "Year: " << year << endl;}
  23. private:
  24.     unsigned year, month, day;
  25.     vector<const string> monthabbr = {"Jan", "Feb", "Mar", "Apr", "May",
  26.             "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  27.     vector<const string> month = {"January", "February", "March", "April",
  28.             "May", "June", "July", "August", "September",
  29.             "October", "November", "December"};
  30. };
  31.  
  32. int main(void)
  33. {
  34.     /*cout << "Creating first: ";
  35.     Date first;
  36.     cout << endl;*/
  37.     cout << "Creating second: ";
  38.     Date second("Jan 1, 1900");
  39.     second.printdata(cout);
  40.     cout << "Creating third: ";
  41.     Date third("5/12/1900");
  42.     third.printdata(cout);
  43.     /*
  44.     cout << "Creating fourth: ";
  45.     Date fourth("1/1,/1900");*/
  46.     cout << endl;
  47.     //first.printdata(cout);
  48.     //second.printdata(cout);
  49.     return 0;
  50. }
  51. // Jan 31, 2014
  52. void mdy(const string &str, Date &obj)
  53. {
  54.  
  55. }
  56.  
  57. // January 2, 2018
  58. void mmdy(const string &str, Date &obj)
  59. {
  60.  
  61. }
  62. // 1/1/1900 | m/d/y
  63. // 11/31/1900
  64. void slashmdy(const string &str, Date &obj)
  65. {
  66.     unsigned pos = 0;
  67.     obj.month = 0, obj.day = 0, obj.year = 0;
  68.     string::const_iterator current;
  69.     // Create three istringstreams and convert it to int
  70.     for (string::const_iterator beg = str.begin();
  71.         beg != str.end();
  72.         ++beg, ++pos) {
  73.         // get month
  74.         if (*beg == '/' && pos > 0 && pos < 3) {
  75.             string  strmonth(str.begin(), beg);
  76.             istringstream convert(strmonth);
  77.             current = beg;
  78.             if (!(convert >> obj.month))
  79.                 obj.month = 0;
  80.         }
  81.         // get day and year
  82.         if (*beg == '/' && pos > 2 && pos < 6) {
  83.             string strday(current + 1, beg);
  84.             istringstream convert(strday);
  85.             current = beg;
  86.             if (!(convert >> obj.day))
  87.                 obj.day = 0;
  88.             string stryear(current + 1, str.end());
  89.             istringstream convertyr(stryear);
  90.             if (!(convertyr >> obj.year))
  91.                 obj.year = 0;
  92.         }
  93.     }
  94.     // now check if date is correctly, no need to check the year
  95.     if (!(obj.month >= 1 && obj.month <= 12))
  96.         obj.month = 0;
  97.     if (!(obj.day >= 1 && obj.day <= 31))
  98.         obj.day = 0;
  99. }
  100.  
  101. /* Types of initialization:
  102.    1: "January 1, 1990" // call mmdy()
  103.    2: "1/1/1900" // call slashmdy
  104.    3: "Jan 1, 1900"*/ // call mdy()
  105.    /* Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec */
  106. Date::Date (const string &str)
  107. {
  108.     unsigned slash = 0, comma = 0;
  109.     for (string::const_iterator beg = str.begin();
  110.         beg != str.end();
  111.         ++beg) {
  112.         switch (*beg) {
  113.             case '/': ++slash; break;
  114.             case ',': ++comma; break;
  115.             default: break;
  116.         }
  117.     }
  118.    
  119.     if (slash == 2 && !comma) { //-- seems ok
  120.         slashmdy(str, *this);
  121.     } else if (comma == 1 && !slash) {
  122.         cout << "One comma!" << endl;
  123.         unsigned count = 0;
  124.         for (string::const_iterator beg = str.begin();
  125.             beg != str.end();
  126.             ++beg, ++count)
  127.             if (*beg == ' ')
  128.                 break;
  129.             if (count == 3)
  130.                 cout << "mdy();" << endl; //mdy(str, *this);
  131.             else
  132.                 cout << "mmdy();"<< endl;//mmdy(str, *this);
  133.     } else {
  134.         // "default" initialize, didn't want use delegated constructors
  135.         month = 0, day = 0, year = 0;
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement