這篇文章主要介紹了C++實(shí)現(xiàn)簡(jiǎn)易萬(wàn)年歷的方法,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)公司制作網(wǎng)站網(wǎng)頁(yè)找三站合一網(wǎng)站制作公司,專注于網(wǎng)頁(yè)設(shè)計(jì),成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè),網(wǎng)站設(shè)計(jì),企業(yè)網(wǎng)站搭建,網(wǎng)站開發(fā),建網(wǎng)站業(yè)務(wù),680元做網(wǎng)站,已為上千多家服務(wù),創(chuàng)新互聯(lián)公司網(wǎng)站建設(shè)將一如既往的為我們的客戶提供最優(yōu)質(zhì)的網(wǎng)站建設(shè)、網(wǎng)絡(luò)營(yíng)銷推廣服務(wù)!
具體內(nèi)容如下
代碼如下:
/* *文件名稱:萬(wàn)年歷.cpp *作 者:chenghan *完成日期:2019/1/10 *版 本 號(hào):1.0 *問(wèn)題描述:制作一個(gè)簡(jiǎn)單的萬(wàn)年歷 */ #include<iostream> #include <string> using namespace std; //判斷一年是否為閏年,是返回true 否返回false bool isleapyear(int year); //兔子圖案 void Rabbit(); //封裝時(shí)間類 私有數(shù)據(jù)成員包括年月日 class Date { private: int year, month, day; //私有數(shù)據(jù)成員 public: Date(){} //無(wú)參的構(gòu)造函數(shù) Date(int year, int month, int day); //有參的構(gòu)造函數(shù) void Disp_Date(); //顯示星期數(shù) void set(); //用戶輸入時(shí)間 int week(); //判斷星期的函數(shù) void show(); //顯示日歷的函數(shù) }; //主函數(shù) int main() { Date t; //創(chuàng)建一個(gè)Date類對(duì)象 string N="yes"; Rabbit(); while(N=="yes"){ t.set(); //調(diào)用設(shè)置時(shí)間函數(shù) t.Disp_Date(); //顯示星期 t.show(); //展示日歷畫面 cout<<"\n是否繼續(xù)查詢,是(yes)否(no)\n"; cin>>N; } return 0; } //判斷一年是否為閏年,是返回true 否返回false bool isleapyear(int year) { if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) return true; else return false; } //兔子圖案 void Rabbit() { cout<<endl; cout<<" ┏━┓ ┏━┓"<<endl; cout<<"作者:chenghan ★│┃ ┃│┃"<<endl; cout<<" ┃│┗灬┛│┃"<<endl; cout<<"版本:1.0 ┃ ┃"<<endl; cout<<" ┃ ^ ^ ┃"<<endl; cout<<"時(shí)間:2019/1/10 ﹌ ˇ ﹌ "<<endl; cout<<" ┗○━━━○┛"<<endl; cout<<"---------------------------------\n"; cout<<"歡 來(lái) 萬(wàn) 歷"<<endl; cout<<" 迎 到 年 !"<<endl; cout<<"---------------------------------------------------\n"; } //有參的構(gòu)造函數(shù) Date::Date(int year, int month, int day) //有參的構(gòu)造函數(shù) { this->year = year; this->month = month; this->day = day; } //顯示星期數(shù) void Date::Disp_Date(){ cout << year << "年" << month << "月" << day << "日 星期" ; switch(this->week()){ case 0: cout<<"日\(chéng)n"; break; case 1: cout<<"一\n"; break; case 2: cout<<"二\n"; break; case 3: cout<<"三\n"; break; case 4: cout<<"四\n"; break; case 5: cout<<"五\n"; break; case 6: cout<<"六\n"; break; } } //用戶設(shè)置時(shí)間 void Date::set() { cout<<"請(qǐng)輸入您所想要查找的年、月、日:"; cin>>year>>month>>day; } //判斷星期的函數(shù) int Date::week(){ int C,y,d,M; if(this->month==1||this->month==2){ C = (this->year-1)/100; y = (this->year-1)%100; M = this->month+12; d = this->day; } else{ C = this->year/100; //C世紀(jì)數(shù)減一 y = this->year%100; //y年份后兩位 d = this->day; //d是日 M = this->month; } int W = C/4 - 2*C + y + y/4 + 13 * (M+1) / 5 + d - 1; //判斷星期的蔡勒公式 if (W < 0) /* 如果w是負(fù)數(shù),則計(jì)算余數(shù)方式不同 */ { W = 7 - (-W) % 7; return W; //返回值1~6對(duì)應(yīng)星期一到六 0對(duì)應(yīng)七 } else return W%7; } //顯示日歷的函數(shù) void Date::show(){ Date temp; temp.year = this->year; temp.month = this->month; temp.day = 1; int count = temp.week(); cout<<"---------------------------------------------------"<<endl; //粗制濫造的界面 cout<<"---------------------"<<this->year<<"年"<<this->month<<"月"<<"---------------------\n"; cout<<"日 一 二 三 四 五 六\n"; for(int i=0;i<count;i++){ cout<<" \t"; } if(temp.month == 1 ||temp.month == 3 ||temp.month == 5 || temp.month ==7 || temp.month ==8 || temp.month ==10 ||temp.month == 12){ for(int j=1;j<32;j++){ if(j<10)cout<<" "<<j<<"\t"; else cout<<j<<"\t"; if(count==6){ cout<<"\n"; count = 0; } else count++; } } else if(temp.month == 4 ||temp.month == 6 ||temp.month == 9 ||temp.month == 11){ for(int j=1;j<31;j++){ if(j<10)cout<<" "<<j<<"\t"; else cout<<j<<"\t"; if(count==6){ cout<<"\n"; count = 0; } else count++; } } else if(temp.month==2){ if(isleapyear(this->year)){ for(int j=1;j<30;j++){ if(j<10)cout<<" "<<j<<"\t"; else cout<<j<<"\t"; if(count==6){ cout<<"\n"; count = 0; } else count++; } } else{ for(int j=1;j<29;j++){ if(j<10)cout<<" "<<j<<"\t"; else cout<<j<<"\t"; if(count==6){ cout<<"\n"; count = 0; } else count++; } } } cout<<"\n---------------------------------------------------"; }
運(yùn)行結(jié)果:
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“C++實(shí)現(xiàn)簡(jiǎn)易萬(wàn)年歷的方法”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
網(wǎng)站名稱:C++實(shí)現(xiàn)簡(jiǎn)易萬(wàn)年歷的方法
本文地址:http://muchs.cn/article32/jchspc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、虛擬主機(jī)、網(wǎng)站營(yíng)銷、網(wǎng)站制作、面包屑導(dǎo)航、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)