程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 面向對象小練習

面向對象小練習

編輯:C++入門知識

晚上寫日記,發現已經好幾十篇都沒寫了
因為日記是按天數排列的,為了保證排列不斷開
就要手動把日期天氣星期篇數神馬的一一加上
編了一個小程序
順便實踐一下面向對象的編程思想
實現了年月日按日遞增的功能(閏年已考慮在內)

[cpp]
#define LOCAL 
#include<iostream> 
 
class YEAR//year類  

public: 
       YEAR():year(0),leap(false){} 
       void setyear(int y){year=y;leap=leaptest(y);} 
       int getyear(){return year;} 
       bool isleap(){return leap;} 
       void add(bool b){if(b){++year;leap=leaptest(year);}} 
private: 
       bool leaptest(int y) 
       {return ((y%400==0)||(y%100!=0&&y%4==0));} 
       int year; 
       bool leap; 
}; 
 
class MONTH//month類  

public: 
       MONTH():month(0) 
       { 
             int t[12]={31,28,31,30,31,30,31,31,30,31,30,31}; 
             for(int i=0;i<12;i++) 
                monthBox[i]=t[i];                  
       } 
       void setmonth(int m){month=m;} 
       int getmonth(){return month;} 
       bool add(bool b){if(b){month%=12;++month;return month==1;}return false;} 
       int contain(bool leap) 
       { 
           if(leap&&month==2) 
                    return 29; 
           else 
                    return monthBox[month-1];        
       } 
private: 
        int monthBox[12]; 
        int month; 
}; 
 
class DAY//day類  

public: 
       DAY():day(0){} 
       void setday(int d){day=d;} 
       int getday(){return day;} 
       bool add(int contain){day%=contain;++day;return day==1;} 
private: 
        int day; 
}; 
 
class WEEK//week類  

public: 
       WEEK():week(0) 
       { 
              std::string t[7]={"星期一","星期二","星期三","星期四","星期五","星期六","星期日"}; 
              for(int i=0;i<7;i++) 
                   weekBox[i]=t[i]; 
                              
       } 
       void setweek(int w){week=w;week--;} 
       std::string getweek(){return weekBox[week];} 
       void add(){(++week)%=7;} 
private: 
        std::string weekBox[7]; 
        int week;   
}; 
 
class DATE//date類  

public: 
       DATE():cnt(0){} 
       void setdate(int y,int m,int d,int w,int c) 
       { 
            year.setyear(y); 
            month.setmonth(m); 
            day.setday(d); 
            week.setweek(w); 
            cnt=c;    
       } 
       void add() 
       { 
            year.add(month.add(day.add(month.contain(year.isleap())))); 
            week.add(); 
            cnt++; 
       } 
       void printdate() 
       { 
                 std::cout<<year.getyear()<<"年"<<month.getmonth()<<"月"<<day.getday()<<"號"<<"   晴   "<<week.getweek()<<"   "<<cnt<<std::endl; 
       }        
private: 
        YEAR year; 
        MONTH month; 
        DAY day; 
        WEEK week; 
        int cnt; 
}; 
 
int main() 

#ifdef LOCAL 
       freopen("input.txt","r",stdin); 
       freopen("output.txt","w",stdout); 
#endif 
        DATE date; 
        date.setdate(2011,11,30,4,538); 
        int n; 
        std::cin>>n; 
        while(n--) 
        { 
              date.printdate(); 
              date.add();          
        } 
        return 0; 


摘自 Dreamer Thinker Doer

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved