C++设计一个电子时钟类,包含年月日,时分秒等属性,由时间类和日期类组合而成,实现并测试这个类。正确显示,年/月/日,时:分:秒。
时间要自动跑么?
class Time //时间类
{
private:
int nHour; //小时
int nMin; //分钟
int nSec; //秒
public:
Time() {nHour = 0;nMin = 0;nSec = 0;} //默认无参构造函数,将属性都初始化为0
Time(int h,int m,int s) {nHour = h;nMin = m;nSec = s;} //带参构造函数,在定义类对象时可以直接赋予初始值
void SetTime(int h,int m,int s) {nHour = h;nMin = m;nSec = s;} //设置时间,可随时修改类属性值
void print() {cout<<nHour<<":"<<nMin<<":"<<nSec;} //打印函数,按照 时:分:秒 格式输出
};
class Date //日期类
{
private:
int nYear; //年份
int nMonth; //月份
int nDay; //日期
public:
Date() {nYear = 0;nMonth = 0;nDay = 0;} //默认无参构造函数
Date(int y,int m,int d) {nYear = y;nMonth = m;nDay = d;} //带参构造函数
void SetDate(int y,int m,int d) {nYear = y;nMonth = m;nDay = d;} //设置日期
void print() {cout<<nYear<<"/"<<nMonth<<"/"<<nDay;} //按照 年/月/日 格式输出日期
};
class Clock //时钟类
{
private:
Date date; //日期对象
Time time; //时间对象
public:
Clock() {} //默认无参构造函数
Clock(int year,int month,int day,int hour,int min,int sec) //带参构造函数
{
date.SetDate(year,month,day); //分别将参数传递给成员类对象
time.SetTime(hour,min,sec);
}
void SetDateTime(int year,int month,int day,int hour,int min,int sec) //设置日期时间函数
{
date.SetDate(year,month,day);
time.SetTime(hour,min,sec);
}
void print() //打印日期时间
{
date.print();
cout<<" ";
time.print();
cout<<endl;
}
};
int main()
{
Clock clock; //创建一个时钟类对象
SYSTEMTIME st;
GetLocalTime(&st); //获取操作系统中的当前时间
clock.SetDateTime(st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond); //将当前时间传递给时钟类对象
clock.print(); //显示当前的日期时间信息
return 0;
}
#include <Windows.h>
#include <ctime>
#include <iostream>
using namespace std;
typedef void(*BACKCALLFUNC)(void *); //定时器回调函数申明
class Time
{
private:
int nHour;
int nMin;
int nSec;
public:
Time() {nHour = 0;nMin = 0;nSec = 0;}
Time(int h,int m,int s) {nHour = h;nMin = m;nSec = s;}
void SetTime(int h,int m,int s) {nHour = h;nMin = m;nSec = s;}
void print() {cout<<nHour<<":"<<nMin<<":"<<nSec;}
};
class Date
{
private:
int nYear;
int nMonth;
int nDay;
public:
Date() {nYear = 0;nMonth = 0;nDay = 0;}
Date(int y,int m,int d) {nYear = y;nMonth = m;nDay = d;}
void SetDate(int y,int m,int d) {nYear = y;nMonth = m;nDay = d;}
void print() {cout<<nYear<<"/"<<nMonth<<"/"<<nDay;}
};
class Clock
{
private:
Date date;
Time time;
public:
Clock() {}
Clock(int year,int month,int day,int hour,int min,int sec)
{
date.SetDate(year,month,day);
time.SetTime(hour,min,sec);
}
void SetDateTime(int year,int month,int day,int hour,int min,int sec)
{
date.SetDate(year,month,day);
time.SetTime(hour,min,sec);
}
void print()
{
date.print();
cout<<" ";
time.print();
cout<<endl;
}
};
void backcallfunc(void *arg) //定时器回调函数
{
system("cls");
Clock clock;
SYSTEMTIME st;
GetLocalTime(&st);
clock.SetDateTime(st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
clock.print();
}
void settimer(unsigned int id, int msec, BACKCALLFUNC backcallfunc)//实现定时器
{
if (msec < 0) //判断时间段是否满足 >=0
{
return;
}
clock_t start, finish;
start = clock();//计时函数
double totaltime = 0;//定义时间变量
while (1)
{
finish = clock();
totaltime = (double)(finish - start);
if (totaltime > msec)
{
backcallfunc(&totaltime);
break;
}
}
}
int main()
{
//不断循环执行定时器,每秒执行一次
while(1)
settimer(1, 1000, backcallfunc);
return 0;
}