Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. //Name: 周政耀
  2. //ID Number: B10515009
  3. //Problem: Design Month Class
  4. #include <iostream>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. string MONTH_TABLE[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  10.  
  11. class Month{
  12. private:
  13.     int month_;
  14. public:
  15.     Month();
  16.     Month(int);
  17.     Month(char, char, char);
  18.     void inputInt();
  19.     void inputFirstThreeLetters();
  20.     void outputInt();
  21.     void outputFirstThreeLetters();
  22.     Month nextMonth();
  23. };
  24.  
  25. Month::Month(){
  26.     //default
  27.     this->month_ = 1;
  28. }
  29.  
  30. Month::Month(int month){
  31.     // initializing and use number to represent a month
  32.     this->month_ = month;
  33. }
  34.  
  35. Month::Month(char firstLetter, char secondLetter, char thirdLetter){
  36.     // initializing and use the first three letter to represent a month
  37.     string month = "";
  38.     month += firstLetter;
  39.     month += secondLetter;
  40.     month += thirdLetter;
  41.     for(int i=0;i<12;++i){
  42.         if(month == MONTH_TABLE[i]){
  43.             this->month_ = i+1;
  44.             break;
  45.         }
  46.     }
  47. }
  48.  
  49. void Month::inputInt(){
  50.     // reads the month as an integer
  51.     int month;
  52.     cin >> month;
  53.     this->month_ = month;
  54. }
  55.  
  56. void Month::inputFirstThreeLetters(){
  57.     // reads the month as the first three letters in the name of the month
  58.     char tempLetter;
  59.     string month = "";
  60.     for(int i=0;i<3;++i){
  61.         cin >> tempLetter;
  62.         month += tempLetter;
  63.     }
  64.     for(int i=0;i<12;++i){
  65.         if(month == MONTH_TABLE[i]){
  66.             this->month_ = i+1;
  67.             break;
  68.         }
  69.     }
  70. }
  71. void Month::outputInt(){
  72.     // output the month as an integer
  73.     cout << this->month_;
  74. }
  75.  
  76. void Month::outputFirstThreeLetters(){
  77.     // outputs the month as the first three letters in the name of the month
  78.     cout << MONTH_TABLE[this->month_-1];
  79. }
  80.  
  81. Month Month::nextMonth(){
  82.     //returns the next month as a value of type Month
  83.     return Month(this->month_+1!=13?this->month_+1:1);
  84. }
  85.  
  86. //input-main1.cpp
  87. int main(void)
  88. {
  89.     Month month1, month2(2), month3('M', 'a', 'r'), month4, month5, month6;
  90.     month4 = month3.nextMonth();
  91.     month5.inputInt();
  92.     month6.inputFirstThreeLetters();
  93.    
  94.     cout << "Month1 = ";
  95.     month1.outputInt();
  96.     cout << ' ';
  97.     month1.outputFirstThreeLetters();
  98.     cout << endl;
  99.    
  100.     cout << "Month2 = ";
  101.     month2.outputInt();
  102.     cout << ' ';
  103.     month2.outputFirstThreeLetters();
  104.     cout << endl;
  105.    
  106.     cout << "Month3 = ";
  107.     month3.outputInt();
  108.     cout << ' ';
  109.     month3.outputFirstThreeLetters();
  110.     cout << endl;
  111.    
  112.     cout << "Month4 = ";
  113.     month4.outputInt();
  114.     cout << ' ';
  115.     month4.outputFirstThreeLetters();
  116.     cout << endl;
  117.    
  118.     cout << "Month5 = ";
  119.     month5.outputInt();
  120.     cout << ' ';
  121.     month5.outputFirstThreeLetters();
  122.     cout << endl;
  123.    
  124.     cout << "Month6 = ";
  125.     month6.outputInt();
  126.     cout << ' ';
  127.     month6.outputFirstThreeLetters();
  128.     cout << endl;
  129.     return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement