C++面向?qū)ο髮崿F(xiàn)萬年歷的示例代碼
引入
本文是通過面向?qū)ο髮θ諝v進(jìn)行實現(xiàn);
主要會有以下幾個模塊:模型 視圖 控制(也算是淺淺的實現(xiàn)了一下MCV)
文件結(jié)構(gòu):
在這里我們使用的這幾個文件分別對應(yīng)它們要實現(xiàn)的功能
ModelDate(模型) 獲取數(shù)據(jù)并處理返回數(shù)據(jù)
ViewDate(視圖) 將獲得的數(shù)據(jù)進(jìn)行組織,并最終向用戶輸出,可以直觀的看到web界面
controller(控制器) 控制著我們不同功能的轉(zhuǎn)換
Controller.h
#ifndef CONTROLLER_H #define CONTROLLER_H class ViewDate; class ModelDate; class Controller { private: //需要將視圖與模式連接起來 ViewDate* pview; //視圖指針 ModelDate* pmodel;//模式指針 上述兩個指針并不產(chǎn)生對象 public: Controller(); ~Controller(); void SetView(ViewDate* pv);//設(shè)計視圖將外部視圖的地址設(shè)置給pview void SetModel(ModelDate *pm);//將模式的地址給pmode void Run(); void Now(); void NextYear(); void PrevYear(); void NextMonth(); void PrevMonth(); }; #endif
Controller.cpp
#include<stdio.h> #include"Controller.h" #include "ViewDate.h" #include "ModelDate.h" Controller::Controller() //控制對象 將視圖對象和模式對象關(guān)聯(lián) :pview{ nullptr }, pmodel{nullptr} {} Controller::~Controller() { } void Controller::SetView(ViewDate* pv) { pview = pv; } void Controller::SetModel(ModelDate* pm) { pmodel = pm; } void Controller::Run() //策略形式來運(yùn)行 { int select = 0; do { select = pview->Menum(); switch (select) { case 0:break; case 1: NextMonth(); break; case 2: PrevMonth(); break; case 3: NextYear(); break; case 4: PrevYear(); break; default: printf("select error\n"); break; } } while (select != 0); } void Controller::Now() { pmodel->Now(); } void Controller::NextYear() { pmodel->NextYear(); } void Controller::PrevYear() { pmodel->PrevYear(); } void Controller::NextMonth() { pmodel->NextMonth(); } void Controller::PrevMonth() { pmodel->PrevMonth(); }
ViewDate.h
#ifndef VIEWDATE_H #define VIEWDATE_H class ModelDate; class ViewDate { private: public: ViewDate(); ~ViewDate(); int Menum(); void PrintDate(int y, int m, int md, int ow, int mt); //年 月 日 當(dāng)前這個月的星期 總的天數(shù) void Event(ModelDate* pmodel); }; #endif
ViewDate.cpp
#include<stdio.h> #include"ViewDate.h" #include"ModelDate.h" ViewDate::ViewDate() { } ViewDate::~ViewDate() { } int ViewDate::Menum() { printf("**********************************************"); printf("\n"); printf(" =_= =_= =_= =_= 現(xiàn)在時間 =_= =_= =_= =_= "); printf("\n"); printf(" =_= =_= =_= =_= 0.退出 =_= =_= =_= =_= "); printf("\n"); printf(" =_= =_= =_= =_= 1.下個月日歷 =_= =_= =_= =_= "); printf("\n"); printf(" =_= =_= =_= =_= 2.上個月日歷 =_= =_= =_= =_= "); printf("\n"); printf(" =_= =_= =_= =_= 3.下一年日歷 =_= =_= =_= =_="); printf("\n"); printf(" =_= =_= =_= =_= 4.上一年日歷 =_= =_= =_= =_= "); printf("\n"); printf("**********************************************"); printf("\n"); int select = 0; printf("請輸入你的選擇: "); scanf_s("%d", &select); return select; } void ViewDate::PrintDate(int year, int month, int mday, int oneweek, int mdays) { int n = mdays + oneweek; int d = 1; printf("當(dāng)前的時間:2022年 %d月\n",month-1); printf("獲取的時間:%d年 %d月 \n", year, month); printf(" 日 一 二 三 四 五 六\n"); for (int i = 1; i <= n; ++i) { if (i <= oneweek) { printf("%5c", ' '); } else { printf("%5d", d++); } if (i % 7 == 0) printf("\n"); } printf("\n"); } void ViewDate::Event(ModelDate* pmodel) //事件與模式相連 { PrintDate(pmodel->GetYear(), pmodel->GetMonth(), pmodel->GetMday(), pmodel->GetOneweek(), pmodel->GetMdays()); }
ModelDate.h
#ifndef MODELDATH_H #define MODELDATH_H class ViewDate; class ModelDate //模式端 是對數(shù)據(jù)進(jìn)行處理 { private: ViewDate* pview; //指向視圖的指針 private: int year; int month; int mday;//這一個月里的第幾天 int curweek;//這一年這一月這一天是第幾周 int oneweek; //這一年這一月1號是第幾天 int mdays;//這一月總的天數(shù) void SetWM(); public: //私有屬性通過公有方法給出數(shù)據(jù) void SetView(ViewDate *pv); int GetYear() const; int GetMonth()const; int GetMday() const; int GetCurweek() const; int GetOneweek() const; int GetMdays() const; public: bool Is_leap() const; //判斷是否是閏年 int GetYM_Day() const;//根據(jù)年月獲得這個月的總天數(shù) int GetWeek(int d = 1) const;//判斷具體哪一天是周幾 public: ModelDate(); ~ModelDate(); void Now();//現(xiàn)在的日期 void NextYear(); void PrevYear(); void NextMonth(); void PrevMonth(); }; #endif // !MODELDATH_H
ModelDate.cpp
#include<time.h> #include"ModelDate.h" #include"ViewDate.h" //獲取數(shù)據(jù)的函數(shù) int ModelDate::GetYear() const { return year; } int ModelDate::GetMonth()const { return month; } int ModelDate::GetMday() const { return mday; } int ModelDate::GetCurweek() const { return curweek; } int ModelDate::GetOneweek() const { return oneweek; } int ModelDate::GetMdays() const { return mdays; } bool ModelDate::Is_leap() const //判斷是否是閏年 { return ((year % 4 == 0) && ((year % 100) != 0) || year % 400 == 0); } //用到查表的方法 int ModelDate::GetYM_Day() const//根據(jù)年月獲得這個月的總天數(shù) { //為了防止在這個表中的數(shù)據(jù)發(fā)生初始化我們采取靜態(tài) static int days[]{ 29, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; //0 1 2 3 4 5 6 7 8 9 10 11 12 int m = month; if (2 == m && Is_leap()) { m = 0; } return days[m]; } //泰勒公式:根據(jù)年月日如何得到星期幾 int ModelDate::GetWeek(int d ) const//判斷具體哪一天是周幾 { int c = year / 100; int y = year % 1; int m = month; if (m == 1 || m == 2) { m += 12; y--; } return ((y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1) % 7) > 0 ? (y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1) % 7 : (y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1) % 7 + 7; } void ModelDate::SetView(ViewDate* pv) { pview = pv; //對象視圖與外部視圖進(jìn)行關(guān)聯(lián) } ModelDate::ModelDate() :pview{nullptr} //將視圖指針設(shè)為空 { Now(); } ModelDate::~ModelDate() { } void ModelDate::SetWM() { curweek = GetWeek(mday); oneweek = GetWeek(1); mdays = GetYM_Day(); } void ModelDate::Now()//現(xiàn)在的日期 { time_t tt; tm md; time(&tt); localtime_s(&md, &tt); year = md.tm_year + 1900; month = md.tm_mon + 1; mday = md.tm_mday; } void ModelDate::NextYear()//下一年 { year += 1; SetWM(); pview->Event(this);//事件一改變就發(fā)送數(shù)據(jù) } void ModelDate::PrevYear()//上一年 { if (year > 1) { year -= 1; } SetWM(); pview->Event(this); } void ModelDate::NextMonth()//下一個月份 { if (++month > 12) { month = 1; year += 1; } SetWM(); pview->Event(this); } void ModelDate::PrevMonth()//上一個月份 { if (--month < 1) { if (year > 1) { year -= 1; month = 12; } } SetWM(); pview->Event(this); }
main.cpp
#include<stdio.h> #include<stdlib.h> #include<iostream> #include<string.h> #include"Controller.h" #include"ModelDate.h" #include"ViewDate.h" class Client { public: Client() :contr{}, model{}, view{} { contr.SetModel(&model); contr.SetView(&view); model.SetView(&view); } ~Client(){} void Run() { contr.Run(); } private: Controller contr; ModelDate model; ViewDate view; }; int main() { Client client; client.Run(); return 0; }
各功能測試結(jié)果
以上就是C++面向?qū)ο髮崿F(xiàn)萬年歷的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于C++萬年歷的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C++實現(xiàn)LeetCode(67.二進(jìn)制數(shù)相加)
這篇文章主要介紹了C++實現(xiàn)LeetCode(67.二進(jìn)制數(shù)相加),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07一文讓你不再害怕指針之C指針詳解(經(jīng)典,非常詳細(xì))
這篇文章主要給大家介紹了C指針的相關(guān)資料,文中介紹的很經(jīng)典,非常詳細(xì),文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用C指針具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08C++判斷主機(jī)是否處于聯(lián)網(wǎng)狀態(tài)
這篇文章主要為大家詳細(xì)介紹了C++判斷主機(jī)是否處于聯(lián)網(wǎng)狀態(tài),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06