C++万年历

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C++万年历

Mi ronin   2022-06-18 我要评论

引入

本文是通过面向对象对日历进行实现;

主要会有以下几个模块:模型 视图 控制(也算是浅浅的实现了一下MCV)

文件结构:

在这里我们使用的这几个文件分别对应它们要实现的功能

ModelDate(模型) 获取数据并处理返回数据

ViewDate(视图) 将获得的数据进行组织,并最终向用户输出,可以直观的看到web界面

controller(控制器) 控制着我们不同功能的转换

Controller.h

#ifndef CONTROLLER_H
#define CONTROLLER_H

class ViewDate;
class ModelDate;
class Controller
{
private:
	//需要将视图与模式连接起来
	ViewDate* pview; //视图指针
	ModelDate* pmodel;//模式指针  上述两个指针并不产生对象
public:
	Controller();
	~Controller();

	void SetView(ViewDate* pv);//设计视图将外部视图的地址设置给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() //控制对象  将视图对象和模式对象关联	
		:pview{ nullptr }, pmodel{nullptr}
		{}
	Controller::~Controller()
	{

	}
	void Controller::SetView(ViewDate* pv)
	{
		pview = pv;
	}
	void Controller::SetModel(ModelDate* pm)
	{
		pmodel = pm;
	}
	void Controller::Run() //策略形式来运行
	{
		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);
		//年 月 日 当前这个月的星期 总的天数	
	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(" =_= =_= =_= =_=   现在时间   =_= =_= =_= =_= ");
		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("当前的时间: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 //模式端 是对数据进行处理
{
private:
	ViewDate* pview;
	//指向视图的指针
private:
	int year;
	int month;
	int mday;//这一个月里的第几天
	int curweek;//这一年这一月这一天是第几周
	int oneweek; //这一年这一月1号是第几天
	int mdays;//这一月总的天数
	void SetWM();
public:  //私有属性通过公有方法给出数据
	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;//根据年月获得这个月的总天数
	int GetWeek(int d = 1) const;//判断具体哪一天是周几
public:
	ModelDate();
	~ModelDate();
	void Now();//现在的日期
	void NextYear();
	void PrevYear();
	void NextMonth();
	void PrevMonth();
};

#endif // !MODELDATH_H

ModelDate.cpp

#include<time.h>
#include"ModelDate.h"
#include"ViewDate.h"

	//获取数据的函数
	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//根据年月获得这个月的总天数
	{
		//为了防止在这个表中的数据发生初始化我们采取静态
		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];
	}

	//泰勒公式:根据年月日如何得到星期几
	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; //对象视图与外部视图进行关联
	}
	ModelDate::ModelDate()
		:pview{nullptr} //将视图指针设为空
	{
		Now();
	}
	ModelDate::~ModelDate()
	{

	}

	void ModelDate::SetWM()
	{
		curweek = GetWeek(mday);
		oneweek = GetWeek(1);
		mdays = GetYM_Day();
	}
	void ModelDate::Now()//现在的日期
	{
		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);//事件一改变就发送数据
	}
	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;
}

各功能测试结果

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们