一个练习的小项目,由于正在学习c++,有很多函数和类还不会使用,所以这个日历是用我会的知识去完成的,所以会存在很多地方效率不高,希望看官谅解,这个日历主要的目的是让我体会什么是基于对象的程序设计方法,而改变过去C语言那样基于过程的设计方式,这时我写的第二版,第一版基本就是把C语言的函数,放在类中,然后调用方法,后咨询老师认识的设计的缺陷做了这么一版勉强过的去的日历
当然还有一个明显的缺点就是我忘了设置错误情况的事情了,如果跳转至指定日期是,如果格式错误会怎么样。这个我懒得去修改了,如果你有兴趣的话你可以帮我完成,谢谢。
首先显示一下文件结构
#pragma once
#include
#include "Date.h"
#include "Display.h"
#include
struct Events
{
std::string event;
int year, month, day;
};
class Canlendar
{
public:
Canlendar();
~Canlendar() {}
void showCanlendar();
void adjustCanlendar(char move);
void gotoAppointime(int year,int month,int day);
void addingEvents(const Events &eve);
void showEvents(int day);
int existenceEvent()const;
Date& getDate();
private:
int displayData[6][7];//存放日历表
Date date;
Console view;
std::vector<Events> event;
};
#include "Canlendar.h"
Canlendar::Canlendar()
:date()
,view()
{
struct tm t; //tm结构指针
time_t now; //声明time_t类型变量
time(&now); //获取系统日期和时间
localtime_s(&t, &now); //获取当地日期和时间
int year = t.tm_year + 1900;
int month = t.tm_mon + 1;
int day = t.tm_mday;
date.init_date(year, month, day);
memset(displayData, 0, sizeof(displayData));
}
void Canlendar::showCanlendar()
{
int ar[6][7] = { 0 };
int row = 0;
int d = 1;
int w;
int maxday = date.getDays_ofmonth();
for (; d <= maxday; d++)
{
w = date.calculateDate(d);
if (w == 0)
{
w = 7;
}
ar[row][w - 1] = d;
if (w == 7)
{
row++;
}
}
int year = date.get_year();
int month = date.get_month();
int day = date.get_day();
view.color(11);
printf("日期:%d-%d-%d \n", year, month,day);
view.color(78);
char s1[] = "星期一"; char s2[] = "星期二"; char s3[] = "星期三"; char s4[] = "星期四"; char s5[] = "星期五"; char s6[] = "星期六"; char s7[] = "星期七";
printf("%5s %5s %5s %5s %5s %5s %5s \n", s1, s2, s3, s4, s5, s6, s7);
char space[] = " ";
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 7; j++)
{
if (ar[i][j] == 0)
{
view.color(7);
printf("%5s ", space);
}
else
{
if (date.isToday(year, month, ar[i][j]))
{
view.color(13);
printf("%5d ", ar[i][j]);
}
else
{
view.color(11);
printf("%5d ", ar[i][j]);
}
}
if (event.size() != 0)
{
bool haveEvent = false;
for (int k = 0; k < event.size(); k++)
{
if (event[k].year == year && event[k].month == month && event[k].day == ar[i][j])
{
haveEvent = true;
view.color(4);
std::cout << "*";
break;
}
}
if(haveEvent==false)std::cout << " ";
}
else std::cout << " ";
}
std::cout << std::endl;
}
view.color(11);
std::cout << "Enter >> " ;
view.color(7);
}
void Canlendar::adjustCanlendar(char move)
{
date.controlDate(move);
system("cls");
this->showCanlendar();
}
void Canlendar::gotoAppointime(int year, int month, int day)
{
date.changeDate(year, month, day);
system("cls");
this->showCanlendar();
}
void Canlendar::addingEvents(const Events &eve)
{
event.push_back(eve);
}
int Canlendar::existenceEvent()const
{
int year = date.get_year();
int month = date.get_month();
int day = date.get_day();
int Event = 0;
for (int i = 0; i < event.size(); i++)
{
if (event[i].year == year && event[i].month == month)
{
Event += 1;
}
}
return Event;
}
void Canlendar::showEvents(int day)
{
int year = date.get_year();
int month = date.get_month();
for (int i = 0; i < event.size(); i++)
{
if (event[i].year == year && event[i].month == month && event[i].day==day)
{
std::cout << year<<"-"<<month<<"-"<<event[i].day<<": "<<event[i].event << std::endl;
}
}
}
Date& Canlendar::getDate()
{
return date;
}
#pragma once
#include
#include
class Date
{
public:
Date();
~Date() {}
void init_date(int year, int month, int day);
int getDays_ofmonth()const;
int calculateDate(int day)const;
int get_year()const;
int get_month()const;
int get_day()const;
bool isToday(int year, int month, int day)const;
void controlDate(char ch);
void changeDate(int year, int month, int day);
private:
bool is_leap_year()const;
int year;
int month;
int day;
int nowYear, nowMonth, nowDay;
int allmonth[13];
};
#include "Date.h"
Date::Date()
{
year = month = day;
int tmp[13] = { 29, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
memcpy(allmonth, tmp, sizeof(allmonth));
}
void Date::init_date(int year,int month,int day)
{
this->year =nowYear= year;
this->month =nowMonth= month;
this->day = nowDay=day;
}
bool Date::is_leap_year()const
{
return((year % 400 == 0 || year % 4 == 0 && year % 100 != 0));
}
int Date::getDays_ofmonth()const
{
if (is_leap_year() && month == 2)
{
return allmonth[0];
}
else return allmonth[month];
}
int Date::calculateDate(int day)const
{
int y = year;
int m = month;
int d = day;
if (m == 1 || m == 2)
{
m += 12;
y--;
}
return (d + 1 + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;
}
int Date::get_year()const
{
return year;
}
int Date::get_month()const
{
return month;
}
int Date::get_day()const
{
return day;
}
bool Date::isToday(int year, int month, int day)const
{
return (year == nowYear && month == nowMonth && day == nowDay);
}
void Date::controlDate(char ch)
{
switch (ch)
{
case 'w':
month = (month + 1) % 12;
if (month == 0)
{
month = 12;
}
if (month == 1)
{
year += 1;
}break;
case 's':
month = (month - 1) % 12;
if (month == 0)
{
month = 12;
}
if (month == 1)
{
year -= 1;
}break;
case ' ':
year = nowYear;
month = nowMonth;
day = nowDay;
break;
default:break;
}
}
void Date::changeDate(int year, int month, int day)
{
this->year = year;
this->month = month;
this->day = day;
}
#pragma once
#include
//class Display
//{
//public:
// void color(short x);//自定义函根据参数改变颜色
//};
class Console
{
public:
Console();
~Console();
void color(int color);
void reset();
private:
HANDLE m_hConsole;
WORD m_attributes;
int m_color;
};
#include "Display.h"
//void Display::color(short x) //自定义函根据参数改变颜色
//{
// if (x >= 0)//参数在0-15的范围颜色
// SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x); //只有一个参数,改变字体颜色
//}
Console::Console()
{
m_hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
m_attributes = 0;
m_color = 0;
if (m_hConsole != NULL)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(m_hConsole, &csbi);
m_attributes = csbi.wAttributes;
}
}
void Console::color(int color)
{
if (m_color != color && m_hConsole != NULL)
{
m_color = color;
SetConsoleTextAttribute(m_hConsole, color);
}
}
void Console::reset()
{
if (m_hConsole != NULL)
{
SetConsoleTextAttribute(m_hConsole, m_attributes);
}
}
Console::~Console()
{
if (m_hConsole != NULL)
{
SetConsoleTextAttribute(m_hConsole, m_attributes);
}
}
#include "Canlendar.h"
#include
using namespace std;
int main()
{
Canlendar r;
r.showCanlendar();
while (1)
{
if (_kbhit())
{
char ch = _getch();
switch (ch)
{
case ' ':
case 's':
case 'w': r.adjustCanlendar(ch); break;
case 'e':
int year, month, day;
cout << "请输入日期xxxx xx xx" << endl;
cin >> year >> month >> day;
r.gotoAppointime(year, month, day); break;
case 'n': exit(0); break;
case 'i':
{
Events eve;
eve.year = r.getDate().get_year();
eve.month = r.getDate().get_month();
cout << "请选择在哪一天添加事件" << std::endl;
cin >> eve.day;
cout << "请输入事件:" << endl;
cin.ignore(1);
std::getline(std::cin,eve.event);
r.addingEvents(eve);
cout << "添加完成" << std::endl;; break;
}
case 'o':
{
int event = r.existenceEvent();
cout << "本月含有" << event << "个事件" << endl;
int num = 0;
cout << "请输入查看事件的日期" << endl;
cin >> num;
r.showEvents(num);
break;
}
default:break;
}
}
}
return 0;
}