用C++设计一个Clock时钟类程序

(要求:1.能够人工及自动设置时间,并能显示人工设置时间及返回系统当前时间。2.程序结构采用多文档结构,即类的定义与声明分开。)主要是自动设置时间这一点,是否要从系统处获... (要求:1.能够人工及自动设置时间,并能显示人工设置时间及返回系统当前时间。2.程序结构采用多文档结构,即类的定义与声明分开。)
主要是自动设置时间这一点,是否要从系统处获得时间?还是怎样?希望能讲清楚。谢谢。
回答的好我会追加分的。
你能不能简明一点?
展开
 我来答
KimiaZhu
2006-06-18 · 超过16用户采纳过TA的回答
知道答主
回答量:24
采纳率:0%
帮助的人:0
展开全部
时钟当然要和系统时间同步!自动设置的意思应该就是自动获取系统时间赋给自身吧.
提供给你两个解决方案:
1)用STL里的ctime.h,可以在头文件上包含#include<ctime>,
具体可以查MSDN解决。
2)用windows API解决,可以在头文件里包含#include
<windows.h>,然后用以下函数获取系统时间:
(其中Date是类,GetSystemDate( Date&)可以得到系统时间)
void Date::GetSystemDate( Date& myClockDate)
{
SYSTEMTIME systemTime;
GetSystemTime(&systemTime);
myClockDate.SetMonth( systemTime.wMonth);
myClockDate.SetDay( systemTime.wDay);
myClockDate.SetYear( systemTime.wYear);
myClockDate.SetDayOfWeek( systemTime.wDayOfWeek);
}

顺便把以前写的一个Date给你参考参考吧,
(不太完整和优秀,纯粹是应付作业的,基本没有注释,(不过也是小弟一番心血哦!^-^)另外你的软件平台不一样的话可能要做适当修改才能跑起来,注意这可不是你的Clock哦!你的Clock会用到这里的一小部分功能的.我都粘贴到一个.cpp里了,分离编译你就自己解决了吧,有问题的话可以给我留言~~)

/*************************************************************************************************

问题描述: 写一个Date类,用以保存日期和各种相关计算,
及教材P174-3.21中的Date中要求的功能也已经一并包含进来了
完成日期: 2006-5-1
作者: Kimia
备注: 还不完整的功能和错误处理
调式平台: Windows sever 2003 Enterprise Edition with .NET 2.0
编写环境: Visual Studio 2005 Team Edition
错误报告: KimiaZhu@sina.com
**************************************************************************************************/

#pragma once

#define WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <iostream>
#include <cctype>
#include <string>

using namespace std;
//enum int{JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};

class Date
{
public:
//Constractors
Date( int month = 0, int day = 0, int year = 0);
Date( const Date&);
~Date(){}

class BadDate{};
void GetSystemDate( Date&);
//公有存取器函数
int GetYear() const{ return itsYear;}
int GetMonth() const{ return itsMonth;}
int GetDay() const{ return itsDay;}
int GetDayOfWeek() const{ return itsDayOfWeek;}
void SetYear( int year){ itsYear = year;}
void SetMonth( int month){ itsMonth = month;}
void SetDay( int day){ itsDay = day;}//*/
//void SetDayOfWeek( int dayOfWeek){ itsDayOfWeek = dayOfWeek;}

//检查Date的函数
bool IsRightDate() const;
bool IsLeapYear() const;
bool IsRightYear() const;
bool IsRightMonth() const;
bool IsRightDay() const;

//操作的Date的函数
Date& AddYear( int);
Date& AddMonth( int);
Date& AddDay( int);
Date& MinusYear( int);
Date& MinusMonth( int);
Date& MinusDay( int);//*/
long ToDays() const;
int SetWeek();
void SetDayOfWeek( int dayOfWeek){ itsDayOfWeek = dayOfWeek;}

//重载运算符
bool operator ==(const Date&);
bool operator !=(const Date&);
Date& operator +=(const Date&);
Date& operator -=(const Date&);
Date& operator ++();
Date& operator --();

private:
int itsDay;
int itsMonth;
int itsYear;
int itsDayOfWeek;
};

//成员函数的实现
Date::Date( int month, int day, int year)
: itsMonth( month), itsDay( day), itsYear( year)
{
IsRightDate();
//cout << ToDays() << endl;
//SetWeek();
}

Date::Date(const Date& date)
{
//if( date.IsRightDate() == false) throw BadDate();//*****似乎已经没有必要暸?
itsDay = date.itsDay;
itsMonth = date.itsMonth;
itsYear = date.itsYear;
itsDayOfWeek = date.itsDayOfWeek;
}
void Date::GetSystemDate( Date& systemDate)
{
SYSTEMTIME time;
GetSystemTime(&time);
systemDate.SetMonth( int( time.wMonth));
systemDate.SetDay( time.wDay);
systemDate.SetYear( time.wYear);
systemDate.SetDayOfWeek( time.wDayOfWeek);
}
int Date::SetWeek()
{
long days = ToDays();
int week = days % 7;
SetDayOfWeek(week);
return week;
}
bool Date::IsRightDate() const
{
if( ( IsRightMonth() == true) && ( IsRightDay() == true) && ( IsRightYear() == true) )
return true;
else
{
throw BadDate();
return false;
}
}

bool Date::IsLeapYear() const
{
if ( ( itsYear % 100 != 0 && itsYear % 4 == 0) || ( itsYear % 400 == 0))
return true;
else
return false;
}

inline bool Date::IsRightYear() const
{
if( itsYear != -1)
return true;
else
return false;
}

inline bool Date::IsRightMonth() const
{
if( ( itsMonth >= 0) && ( itsMonth <= 12))
return true;
else
return false;
}

bool Date::IsRightDay() const
{
if( itsMonth == 2 && IsLeapYear() == false)
{
return ( itsDay >= 0 && itsDay <= 28);
}
else if( itsMonth == 2 && IsLeapYear() == true)
{
return ( itsDay >= 0 && itsDay <= 29);
}
else
{
if( itsMonth == 1 || itsMonth == 3 || itsMonth == 5 || itsMonth == 7 ||
itsMonth == 8 || itsMonth == 10 || itsMonth == 12)
{
return( itsDay >= 0 && itsDay <= 31);
}
else
{
return(itsDay >= 0 && itsDay <= 30);
}
}
}

inline Date& Date::AddYear(int year)
{
if( itsYear > 0)
itsYear += year;
else
itsYear = itsYear + year + 1;
return *this;
}

inline Date& Date::AddMonth(int month)
{
for ( int mon = month; mon > 0; mon--)
{
if( itsMonth == 12)
{
AddYear( 1);
itsMonth = 1;
}
else
itsMonth++;
}
return *this;
}

inline Date& Date::AddDay(int day)
{
for(int d = day; d > 0; d--)
{
if( itsMonth == 2 && IsLeapYear() == false)
{
if( itsDay == 28)
{
AddMonth( 1);
itsDay = 1;
}
else
itsDay++;
}
else if( itsMonth == 2 && IsLeapYear() == true)
{
if( itsDay == 29)
{
AddMonth( 1);
itsDay = 1;
}
else
itsDay++;
}
else
{
if( itsMonth == 1 || itsMonth == 3 || itsMonth == 5 || itsMonth == 7 ||
itsMonth == 8 || itsMonth == 10 || itsMonth == 12)
{
if( itsDay == 31)
{
AddMonth( 1);
itsDay = 1;
}
else
itsDay++;
}
else
{
if( itsDay == 30)
{
AddMonth( 1);
itsDay = 1;
}
else
itsDay++;
}
}
}
SetWeek();
return *this;
}

inline Date& Date::MinusYear(int year)
{
if( itsYear > 0)
itsYear -= year;
else
itsYear = itsYear - year - 1;
return *this;
}

inline Date& Date::MinusMonth(int month)
{
for ( int mon = month; mon > 0; mon--)
{
if( itsMonth == 1)
{
MinusYear( 1);
itsMonth = 12;
}
else
itsMonth--;
}
return *this;
}

inline Date& Date::MinusDay(int day)
{
for(int d = day; d > 0; d--)
{
if( itsMonth == 3 && IsLeapYear() == false)
{
if( itsDay == 1)
{
MinusMonth( 1);
itsDay = 28;
}
else
itsDay--;
}
else if( itsMonth == 3 && IsLeapYear() == true)
{
if( itsDay == 1)
{
MinusMonth( 1);
itsDay = 29;
}
else
itsDay--;
}
else
{
if( itsMonth == 2 || itsMonth == 4 || itsMonth == 6 ||
itsMonth == 9 || itsMonth == 11 || itsMonth == 1)
{
if( itsDay == 1)
{
MinusMonth( 1);
itsDay = 31;
}
else
itsDay--;
}
else
{
if( itsDay == 1)
{
MinusMonth( 1);
itsDay = 30;
}
else
itsDay--;
}
}
}
SetWeek();
return *this;
}

inline long Date::ToDays() const
{
long days = 0;
Date tempDate( GetMonth() - 1, GetDay(), GetYear());
for( ; tempDate.GetYear() > 1; tempDate.MinusYear( 1))
{
if( tempDate.IsLeapYear() == true)
{
days += 366;
}
else days += 265;
}
for( ; tempDate.GetMonth() >= 1 && tempDate.GetYear() >= 1; tempDate.MinusMonth( 1))
{
if( tempDate.GetMonth() == 2 && tempDate.IsLeapYear() == true)
days += 29;
else if( tempDate.GetMonth() == 2 && tempDate.IsLeapYear() == false)
days += 28;
else
{
if( tempDate.GetMonth() == 1 || tempDate.GetMonth() == 3 || tempDate.GetMonth() == 5 ||
tempDate.GetMonth() == 7 || tempDate.GetMonth() == 8 || tempDate.GetMonth() == 10 ||
tempDate.GetMonth() == 12)
{
days += 31;
}
else
{
days += 30;
}
}
}
days += tempDate.GetDay();
return days;
}

inline bool Date::operator ==( const Date& date)
{
return( itsYear == date.itsYear && itsMonth == date.itsMonth && itsDay == date.itsDay);
}

inline bool Date::operator !=( const Date& date)
{
return( !( itsYear == date.itsYear && itsMonth == date.itsMonth && itsDay == date.itsDay));
}

inline Date& Date::operator +=( const Date& date)
{
itsYear += date.itsYear;
itsMonth += date.itsMonth;
itsDay += date.itsDay;
SetWeek();
return *this;
}

inline Date& Date::operator -=( const Date& date)
{
itsYear -= date.itsYear;
itsMonth -= date.itsMonth;
itsDay -= date.itsDay;
//SetWeek();
return *this;
}

inline Date& Date::operator ++()
{
return (AddDay( 1));
}

inline Date& Date::operator --()
{
return (MinusDay( 1));
}
//以下不是成员函数
Date operator +( Date& dateOne, int day)
{
dateOne.AddDay(day);
//SetWeek();
return dateOne;
}
Date operator +( Date& dateOne, Date& dateTwo)
{
Date date;
date.SetYear( dateOne.GetYear() + dateTwo.GetYear());
date.SetMonth( dateOne.GetMonth() + dateTwo.GetMonth());
date.SetDay( dateOne.GetDay() + dateTwo.GetDay());
//date.SetWeek();
return date;
}

Date operator -( Date& dateOne, Date& dateTwo)
{
Date date;
date.SetYear( dateOne.GetYear() - dateTwo.GetYear());
date.SetMonth( dateOne.GetMonth() - dateTwo.GetMonth());
date.SetDay( dateOne.GetDay() - dateTwo.GetDay());
//date.SetWeek();
return date;
}

ostream& operator <<( ostream& ostreamOne, Date& date)
{
date.SetWeek();
string monthName;
string weekName;
switch(date.GetMonth())
{
case 1:monthName = "January";break;
case 2:monthName = "Febrary";break;
case 3:monthName = "March";break;
case 4:monthName = "April";break;
case 5:monthName = "May";break;
case 6:monthName = "June";break;
case 7:monthName = "July";break;
case 8:monthName = "August";break;
case 9:monthName = "September";break;
case 10:monthName = "October";break;
case 11:monthName = "Novenber";break;
case 12:monthName = "December";break;
default:cerr<<"Erroe month!";exit(-1);
}
switch(date.GetDayOfWeek())
{
case 1:weekName = "Monday";break;
case 2:weekName = "Tuesday";break;
case 3:weekName = "Wendesday";break;
case 4:weekName = "Thursday";break;
case 5:weekName = "Frisday";break;
case 6:weekName = "Saterday";break;
//case 7:weekName = "Sunday";break;
case 0:weekName = "Sunday";break;
//default:cerr<<"Erroe week!";exit(-1);
}
cout << weekName << " "<< monthName << " "<< int( date.GetDay()) << " " << date.GetYear();
return ostreamOne;
}
istream& operator >>( istream& ostreamOne, Date& date)
{
int month = 0;
int day = 0;
int year = 0;
cin >> month >> day >> year;
date.SetMonth( month);
date.SetDay( day);
date.SetYear( year);
date.IsRightDate();//判断是否坏值,保证有意义的值材会被计算
return ostreamOne;
}

int main()
{
try
{
Date dateOne;
dateOne.GetSystemDate( dateOne);
cout << dateOne << endl;
Date dateTwo;
cout << "请输入: (格式: \"月/日/年\" 的数字形式.)";
cin >> dateTwo;
cout << dateTwo << endl;
//cout << "Input Error!";
if( dateOne == dateTwo)
cout << "dateOne is equal to dateTwo!" << endl;
else
cout << "dateOne isn\'t equal to dateTwo!" << endl;
dateTwo++;
cout << dateTwo << endl;
cout << "你可以为这个日期加上一个天数,请输入你想加上的天数 d = ";
int d;
cin >> d;
dateTwo = dateTwo + d;
//dateTwo.AddDay(365);
cout << "After add d: " << dateTwo << endl;
dateTwo--;
dateTwo--;
cout << dateTwo << endl;
dateOne += dateTwo;
cout << dateOne << endl;
Date dateThree( 10, 1, 1);
cout << "There are " << dateThree.ToDays() << "days in dateThree." << endl;
cout << "There are " << dateTwo.ToDays() << "days in dateTwo." << endl;
}
catch(Date::BadDate)
{
cout << "Bad Date!";
}
catch(...)
{
cout << "Unknow Exceptions!";
}
return 0;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式