c++设计时钟类,数据成员有时,分,秒,函数成员可实现修改时钟时间,显示时钟时间,

其他要求:1,构造函数重载;2,有形参缺省值;3,有析构函数;4有内联函数;5有主函数... 其他要求:
1,构造函数重载;
2,有形参缺省值;
3,有析构函数;
4 有内联函数;
5 有主函数
展开
 我来答
92061212
2011-12-26
知道答主
回答量:6
采纳率:0%
帮助的人:6.8万
展开全部
#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;
}
小只木a
2011-12-23 · TA获得超过190个赞
知道小有建树答主
回答量:199
采纳率:0%
帮助的人:80万
展开全部
#include<iostream>
#include<iomanip>
#include<ctime>
#include <windows.h>
//#include<cunistd>
using namespace std;
class myclock
{
int s;
int f;
int m;
public:
myclock();
//myclock(int hh,int mm=9,int ss=9);
// myclock(int hh,int mm=0,int ss=9):s(hh),f(mm),m(ss)
// {}
void show_dtime(void);
void show_time(void);
void set_time(int a,int b,int c);
void show_ntime(void);
void show_utime(void);
void set_mtime(int m);
void miaobiao(int n);
};
int main(void)
{
// int q,w,e;
// cin >>q;
// cin >>w;
// cin >>e;
myclock tt;
// tt.set_time(q,w,e);
// tt.show_time();
// tt.show_dtime();
// tt.show_utime();
tt.miaobiao(4);
}
void myclock::miaobiao(int n)
{ while(1){
set_mtime(n--);
cout<<flush<<"\r";
sleep(1);
}

}

void myclock::set_mtime(int m)
{
int ss=0,ff=0,mm=0;
mm=m%60;
m=m/60;
if(m>59)
{ss=m/60;
ff=m%60;}
else
ff=m;
mm--;
if(mm<0)
{ff--;
mm=59;}
if(ff<0)
{
ss--;
ff=59;
mm=59;
}
cout.fill('0');
cout<<setw(2)<<ss<<":"<<setw(2)<<ff<<":"<<setw(2)<<mm;

if(ss==0&&ff==0&&mm==0)
{
system("ls");
return; }
}
void myclock::show_utime(void)
{ int i=0;
set_time(0,0,0);

do{

if(i>999)
{
i=0;
m++;
}
show_time();
cout<<'.'<<i;
cout<<flush<<"\r";
usleep(1);
i++;
}while(1);
}
void myclock::show_dtime(void)
{ while(1){
show_ntime();
cout<<flush<<"\r";
sleep(1);
}

}

void myclock::set_time(int a,int b,int c)
{
s=a;
f=b;
m=c;
}
void myclock::show_time(void)
{
if(m==60)
{
f++;
m=0;
if(f==60)
{
s++;
f=0;
if(s==24)
{
s=0;

}
}
}

cout.fill('0');
cout<<setw(2)<<s<<":"<<setw(2)<<f<<":"<<setw(2)<<m;
}
void myclock::show_ntime(void)
{
m++;
if(m==60)
{
f++;
m=0;
if(f==60)
{
s++;
f=0;
if(s==24)
{
s=0;

}
}
}
cout.fill('0');
// cout<<flush<<"\r"<<setw(2)<<s<<":"<<setw(2)<<f<<":"<<setw(2)<<m<<endl;
cout<<setw(2)<<s<<":"<<setw(2)<<f<<":"<<setw(2)<<m;
}
myclock::myclock(){
char *p,*a;
time_t *t;
t=new time_t(100);
time(t);
p=ctime(t);
a=strchr(p,':');
*a='\0';
*(a+3)='\0';
s=atoi(a-2);
f=atoi(a+1);
m=atoi(a+4);

}

以前linux系统下写的...windows下好像c++没有sleep函数,要引用下windows.h吧!
追问
程序有错误
追答
是的  把windows.h头文件去掉就可以在linux编译通过的,但windows下好像只有加windows.h
才能用sleep,然后我就加了试试,发的时候忘删除了
如果不是这个问题...那就算了,你觉得有用就看看,别用就当我没回答过,无视我就行...刚学c++的时候写的 ,不想重看了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2011-12-23
展开全部
#include<iostream>
using namespace std;
class CLOCK{
private:
int chour;int cminute;int csecond;int hour;int minute;int second;
public:
CLOCK(int = 0,int = 0,int = 0);
void SETALARM(int ,int ,int );
void RUN();
void SHOWTIME();
};
CLOCK::CLOCK(int chour,int cminute,int csecond)
{
this->chour=chour;
this->cminute=cminute;
this->csecond=csecond;
}
void CLOCK::SETALARM(const int hour,const int minute,const int second)
{
this->hour=hour;
this->minute=minute;
this->second=second;
}
void CLOCK::RUN()
{
for(;chour<hour;chour++)
{
for(;cminute<=60;cminute++)
{
for(;csecond<=60;csecond++);
}
}
for(cminute=0;cminute<minute;cminute++)
{
for(csecond=0;csecond<=60;csecond++);
}
for(csecond=0;csecond<=second;csecond++)
if(chour==hour&&cminute==minute&&csecond==second)
cout<<"the time to!!!get up!!the time is:"<<hour<<":"<<minute<<":"<<second<<endl;
}
void CLOCK::SHOWTIME()
{
cout<<chour<<":"<<cminute<<":"<<csecond<<endl;
}
void main()
{
CLOCK time(8,21,14);
time.SHOWTIME();
time.SETALARM(10,35,12);
time.RUN();
time.SHOWTIME();
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友dd16e7f218
2011-12-23 · 超过50用户采纳过TA的回答
知道小有建树答主
回答量:145
采纳率:0%
帮助的人:101万
展开全部
#include<iostream>
using namespace std;
class clock
{
private:
int Hour;
int Minute;
int Second;
public:
void SetTime(int NewH=0, int NewM=0, int NewS=0);
void ShowTime();
clock(int SetH, int SetM, int SetS);
~clock(){};
};

void clock::SetTime(int NewH, int NewM, int NewS)
{
Hour=NewH;
Minute=NewM;
Second=NewS;
}
clock::clock(int NewH, int NewM, int NewS)
{
Hour=NewH;
Minute=NewM;
Second=NewS;
}
void clock::ShowTime()
{
cout<<Hour<<":"<<Minute<<":"<<Second<<endl;
}
int main()
{
clock myclock(2,6,9);
myclock.ShowTime();
myclock.SetTime(3,6,0);
myclock.ShowTime();
return 0;

}
追问
代码基本符合要求 但是还是不行 运行结果显示为静态 要能要像电子表一样显示 请改进一下
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名_热心网友
2012-01-08 · TA获得超过719个赞
知道大有可为答主
回答量:3827
采纳率:0%
帮助的人:8399万
展开全部
int y,m,d;
cout<<"请输入姓名:";
cin>>name;
cout<<"请输入性别<F(f) or M(m)>:";
cin>>sex;
cout<<"请输入出生日期(用空格分隔):";
cin>>age;
this->name=name;
this->sex=sex;

}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式