作者:红杏出墙的爱_408 | 来源:互联网 | 2023-09-25 22:47
设计思路:
数据层:
#pragma once
#include
#include
using namespace std;
class ViewDate;//数据层 --- 进行所有的数据处理class ModelDate
{
private:ViewDate* view; //数据层连接显示层
private:int year;int month;int mday; //今天是几号int curweek; //当天星期int hour;int min;int sec;int oneweek; //本月1号星期int mdays; //本月天数static bool Is_Leap(int y);static int GetYM_Days(int y, int m);static int Get_Week(int y, int m, int d = 1);
public:int GetYear()const; //年int GetMonth()const; //月int GetMday()const; //日int GetCurweek()const; //星期int GetHour()const; //小时int GetMin()const; //分int GetSec()const; //秒int GetMdays()const; //本月天数int GetOneweek()const; //本月1号星期
public:ModelDate();~ModelDate();void SetView(ViewDate* pview);void Now();void NextMonth();void PrevMonth();void NextYear();void PrevYear();
};
#include"Model.h"
#include"View.h"//常用静态方法
bool ModelDate::Is_Leap(int y)
{return (y % 4 == 0 && y % 100 == 0) || (y % 400 == 0);
}int ModelDate::GetYM_Days(int y, int m)
{static const 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 12if (m == 2 && Is_Leap(y)){m = 0;}return days[m];
}int ModelDate::Get_Week(int y, int m, int d)
{int c &#61; y / 100; //世纪-1y %&#61; 100; //年份后两位if (m &#61;&#61; 1 || m &#61;&#61; 2) //月份>&#61;3 <&#61;14{m &#43;&#61; 12; }return (y &#43; y / 4 &#43; c / 4 - 2 * c &#43; 26 * (m &#43; 1) / 10 &#43; d - 1) % 7;
}/
///获取数据层的属性值
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::GetHour()const //小时
{return hour;
}int ModelDate::GetMin()const //分
{return min;
}int ModelDate::GetSec()const //秒
{return sec;
}int ModelDate::GetMdays()const //本月天数
{return mdays;
}int ModelDate::GetOneweek()const
{return oneweek;
}//对数据层数据的操作方法void ModelDate::SetView(ViewDate* pview)
{view &#61; pview;
}ModelDate::ModelDate():year(1),month(1),mday(1),hour(0),min(0),sec(0)
{Now();
}ModelDate::~ModelDate() {}void ModelDate::Now()
{time_t tmp;tm tnode;time(&tmp);localtime_s(&tnode, &tmp);year &#61; tnode.tm_year &#43; 1900;month &#61; tnode.tm_mon &#43; 1;mday &#61; tnode.tm_mday;hour &#61; tnode.tm_hour;min &#61; tnode.tm_min;sec &#61; tnode.tm_sec;curweek &#61; tnode.tm_wday;mdays &#61; GetYM_Days(year, month);oneweek &#61; Get_Week(year, month);view->Event(this, 0); //连接显示层
}void ModelDate::NextMonth()
{if (&#43;&#43;month > 12){year &#43;&#61; 1;month &#61; 1;}mday &#61; 1;mdays &#61; GetYM_Days(year, month);curweek &#61; oneweek &#61; Get_Week(year, month);view->Event(this); //连接显示层
}void ModelDate::PrevMonth()
{if (--month <1){year -&#61; 1;month &#61; 12;}mday &#61; 1;mdays &#61; GetYM_Days(year, month);curweek &#61; oneweek &#61; Get_Week(year, month);view->Event(this); //连接显示层
}void ModelDate::NextYear()
{year &#43;&#61; 1;mday &#61; 1;curweek &#61; oneweek &#61; Get_Week(year, month);mdays &#61; GetYM_Days(year, month);view->Event(this); //连接显示层
}void ModelDate::PrevYear()
{if (--year <1) return;mday &#61; 1;curweek &#61; oneweek &#61; Get_Week(year, month);mdays &#61; GetYM_Days(year, month);view->Event(this); //连接显示层
}
显示层
#pragma once
#include
class ModelDate;//显示层 --- 只负责显示class ViewDate
{
public:int Menu();void PrintDate(int year, int month, int mday, int curweek, int oneweek, int hour, int min, int sec, int mtotal,int sign) const;//留给数据层的接口void Event(ModelDate* pomde); void Event(ModelDate* pomde,int());
};
#include"View.h"
#include"Model.h"int ViewDate::Menu()
{printf("************欢迎使用LoveHoneyYou的日历**********\n");printf("*******************当前日历***********************\n");printf("********** 0. 按0退出 **********\n");printf("********** 1. 获取下一个月的日历 **********\n");printf("********** 2. 获取上一个月的日历 **********\n");printf("********** 3. 获取下一个年的日历 **********\n");printf("********** 4. 获取上一个年的日历 **********\n");printf("********** 5. 获取当前时间的日历 **********\n");int n &#61; 0;printf("请输入你的选择-> ");scanf_s("%d", &n);system("cls");return n;
}void ViewDate::PrintDate(int year, int month, int mday, int curweek, int oneweek, int hour, int min, int sec, int mtotal,int sign) const
{if (sign &#61;&#61; 0){printf("%dyear %dmonth \n", year, month);}else{printf("%dyear %dmonth %dday %dweek %dhour %dmin %dsec\n", year, month, mday, curweek, hour, min, sec);}printf("日 一 二 三 四 五 六\n");int weekday &#61; oneweek;int arr[6][7]{};int count &#61; 1;int j &#61; weekday;int d &#61; mtotal;int tag &#61; 0;for (int i &#61; 0; i <6; i&#43;&#43;){for (; j <7; j&#43;&#43;){arr[i][j] &#61; count&#43;&#43;;if (count > d){tag &#61; 1;break;}}if (tag){break;}j &#61; 0;}int m &#61; weekday;if (m &#61;&#61; 0){}else if (m &#61;&#61; 1){printf(" ");}else if (m &#61;&#61; 2){printf(" ");}else if (m &#61;&#61; 3){printf(" ");}else if (m &#61;&#61; 4){printf(" ");}else if (m &#61;&#61; 5){printf(" ");}else{printf(" ");}int k &#61; weekday;for (int i &#61; 0; i <6; i&#43;&#43;){for (; k <7; k&#43;&#43;){if (arr[i][k] !&#61; 0){printf("%2d ", arr[i][k]);}}k &#61; 0;printf("\n");}
}//int year, int month, int oneweek, int mtotal
void ViewDate::Event(ModelDate* pomde)
{PrintDate(pomde->GetYear(), pomde->GetMonth(), pomde->GetMday(), pomde->GetCurweek(), pomde->GetOneweek(), pomde->GetHour(), pomde->GetMin(), pomde->GetSec(), pomde->GetMdays(), 0);
}//int year, int month, int mday, int curweek, int oneweek, int hour, int min, int sec, int mtotal
void ViewDate::Event(ModelDate* pomde, int())
{PrintDate(pomde->GetYear(), pomde->GetMonth(), pomde->GetMday(), pomde->GetCurweek(), pomde->GetOneweek(), pomde->GetHour(), pomde->GetMin(), pomde->GetSec(), pomde->GetMdays(), 1);
}
控制层
#pragma once
class ViewDate;
class ModelDate;//控制层---连接显示层和数据层class Controller
{
private:ModelDate* model;ViewDate* view;
public:Controller();~Controller();void SetModel(ModelDate* pmodel);void SetView(ViewDate* pview);void Run();void Now();void NextMonth();void PrevMonth();void NextYear();void PrevYear();
};
#include"Controller.h"
#include"Model.h"
#include"View.h"
Controller::Controller() {}
Controller::~Controller() {}void Controller::SetModel(ModelDate* pmodel)
{model &#61; pmodel;
}void Controller::SetView(ViewDate* pview)
{view &#61;pview ;
}void Controller::Run()
{int select &#61; 0;do{select &#61; view->Menu(); //连接显示层switch (select){case 0:break;case 1:NextMonth(); //连接数据层break;case 2:PrevMonth(); //连接数据层break;case 3:NextYear(); //连接数据层break;case 4:PrevYear(); //连接数据层break;case 5:Now(); //连接数据层break;}} while (select !&#61; 0);
}void Controller::Now()
{model->Now();
}void Controller::NextMonth()
{model->NextMonth();
}void Controller::PrevMonth()
{model->PrevMonth();
}void Controller::NextYear()
{model->NextYear();
}void Controller::PrevYear()
{model->PrevYear();
}
客户端
//#pragma
#include"Model.h"
#include"View.h"
#include"Controller.h"class Client
{
private:ModelDate model;ViewDate view;Controller contr;
public:Client(){contr.SetModel(&model);contr.SetView(&view);model.SetView(&view);}void Run(){contr.Run();}
};int main()
{Client client;client.Run();return 0;
}