带日期带时区时钟类:(数组对象)购买十个时钟,表现十个不同时区的时间,为了计算方便,每个时区的时差按1小时计算;第一个钟总是显示北京时间,利用系统时间获取当前北京时间;其他时区的时间根据与北京时间的时差来计算;主函数为:int main(){ Clock myClock[10];//定义对象myClock int i,z,j; //z为时区,i为10个时钟的计数器,j也是为了找是否有相同时区的计数器 for(i=0;i<10;i++) //处理十个不同时区的时钟 { if(i==0) {myClock[i].SetTime();//第一个时钟总是为北京时间的时钟,设置时间为默认值 cout<<"**********第"<
<<"个时钟***********\n"; cout<
=24) { myClock[i].SetHour( myClock[i].Get_H()-24 );//时差的小时超过24的处理myClock[i].Set_Day( myClock[i].Get_Day().Get_D()+1 );} cout<<"**********第"<
<<"个时钟**********\n"; cout<
A.
#include #include using namespace std; class Date //日期类 { int year; int month; int day; public: void SetDay(int NewY, int NewM, int NewD) //设置日期 { year= NewY; month=NewM; day=NewD; } int Get_Y() {return year;} //获得时间的年份 int Get_M() {return month;} //获得时间的月份 int Get_D() {return day;} //获得时间的日期 void Set_D(int d) { day=d; } //设置时间的日期 void ShowDay(int &NewY,int &NewM,int &NewD) ////获得时间的年月日 { NewY= year; NewM=month; NewD=day; } }; class Clock //时钟类的声明(组合类) { public: //外部接口,公有成员函数 void SetTime(int z1,int NewH, int NewM, int NewS,Date d1);//根据参数设置时间 int Get_H() {return hp;} //获得小时 int Get_M() {return mp;} //获得分钟 int Get_S() {return sp;} //获得秒 Date Get_Day() {return d;} //获得日期 void Set_Day(int d1) {d.Set_D(d1);} //设置时间的日期 void SetTime() //设置默认时间 { SYSTEMTIME sys; //系统时间 GetLocalTime( &sys ); //获得本机器的当前时间 hp=sys.wHour; //设置时间为本机器的当前时间的时数 mp=sys.wMinute; //设置时间为本机器的当前时间的分钟 sp=sys.wSecond; //设置时间为本机器的当前时间的秒数 z=8; //设置时间的时区为 d.SetDay(sys.wYear,sys.wMonth,sys.wDay); //设置时间的当期日期 } void SetHour(int h){hp=h;} //设置时间的当期时数 int GetZone(); //获得时间的当前时区 void DispTime(); //显示时间 void AddOneS() //走动的时间函数 { sp++; if(sp>=60) { mp++; sp=0; if(mp>=60) { mp=0; hp++; if(hp>=24) { hp=0; } } } } private: //私有数据成员 int hp,mp,sp,z; Date d; }; //时钟类成员函数的具体实现 void Clock::SetTime(int z1,int NewH, int NewM, int NewS,Date d1) { hp=NewH; mp=NewM; sp=NewS; z=z1; d.SetDay(d1.Get_Y(),d1.Get_M(),d1.Get_D() ); } //获取时区 int Clock::GetZone() { return z; } void Clock::DispTime() { char a[25][20]={"伦敦","巴黎","开罗","莫斯科","第比利斯","新德里", "阿拉木图","曼谷","北京","东京","悉尼","马加丹","惠灵顿","埃尼维托克岛", "中途岛","夏威夷","阿拉斯加","洛杉矶","亚利桑那","亚特兰大","纽约","加拉加斯", "里约热内卢","中大西洋","佛得角群岛"}; //全球十二个区对应的城市时间 int y1,m1,d1; if(z<0) z=12+(12+z+1); //对西区的城市对应数组的下标的处理 d.ShowDay(y1,m1,d1); cout< //输出对应时区的城市时间 cout< cout< cout< cout< }
B.
#include #include using namespace std; class Date { int year; int month; int day; public: int Get_Y() {return year;} int Get_M() {return month;} int Get_D() {return day;} void ShowDay(int &NewY,int &NewM,int &NewD) { NewY= year; NewM=month; NewD=day; } }; class Clock //时钟类的声明 { public: //外部接口,公有成员函数 void SetTime(int z1,int NewH, int NewM, int NewS,Date d1); int Get_H() {return hp;} int Get_M() {return mp;} int Get_S() {return sp;} Date Get_Day() {return d;} void SetTime() { SYSTEMTIME sys; GetLocalTime( &sys ); hp=sys.wHour; mp=sys.wMinute; sp=sys.wSecond; z=8; d(sys.wYear,sys.wMonth,sys.wDay); } void SetZone(int z); int GetZone(); void DispTime(); void AddOneS() { sp++; if(sp>=60) { mp++; sp=0; if(mp>=60) { mp=0; hp++; if(hp>=12) { hp=0; } } } } private: //私有数据成员 int hp,mp,sp,z; Date d; }; //时钟类成员函数的具体实现 void Clock::SetTime(int z1,int NewH, int NewM, int NewS,Date d1) { hp=NewH; mp=NewM; sp=NewS; z=z1; d.SetDay(d1.Get_Y(),d1.Get_M(),d1.Get_D() ); } //设置时区 void Clock::SetZone(int z) { this->z=z; } //获取时区 int Clock::GetZone() { return z; } void Clock::DispTime() { char a[25][20]={"伦敦","巴黎","开罗","莫斯科","第比利斯","新德里", "阿拉木图","曼谷","北京","东京","悉尼","马加丹","惠灵顿","埃尼维托克岛", "中途岛","夏威夷","阿拉斯加","洛杉矶","亚利桑那","亚特兰大","纽约","加拉加斯", "里约热内卢","中大西洋","佛得角群岛"}; int y1,m1,d1; if(z<0) z=12+(12+z+1); d.ShowDay(y1,m1,d1); if(hp<0) d1=d1-1; else if(hp>=24){ hp=0; d1=d1+1;} cout< cout< cout< cout< cout< }
C.
#include #include using namespace std; class Date { int year; int month; int day; public: void SetDay(int NewY, int NewM, int NewD) { year= NewY; month=NewM; day=NewD; } int Get_Y() {return year;} int Get_M() {return month;} int Get_D() {return day;} void ShowDay(int &NewY,int &NewM,int &NewD) { NewY= year; NewM=month; NewD=day; } }; class Clock //时钟类的声明 { public: //外部接口,公有成员函数 void SetTime(int z1,int NewH, int NewM, int NewS,Date d1); int Get_H() {return hp;} int Get_M() {return mp;} int Get_S() {return sp;} Date Get_Day() {return d;} Clock() { SYSTEMTIME sys; GetLocalTime( &sys ); hp=sys.wHour; mp=sys.wMinute; sp=sys.wSecond; z=8; d(sys.wYear,sys.wMonth,sys.wDay); } int GetZone(); void DispTime(); void AddOneS() { sp++; if(sp>=60) { mp++; sp=0; if(mp>=60) { mp=0; hp++; if(hp>=12) { hp=0; } } } } private: //私有数据成员 int hp,mp,sp,z; Date d; }; //时钟类成员函数的具体实现 void Clock::SetTime(int z1,int NewH, int NewM, int NewS,Date d1) { hp=NewH; mp=NewM; sp=NewS; z=z1; d.SetDay(d1.Get_Y(),d1.Get_M(),d1.Get_D() ); } //获取时区 int Clock::GetZone() { return z; } void Clock::DispTime() { char a[25][20]={"伦敦","巴黎","开罗","莫斯科","第比利斯","新德里", "阿拉木图","曼谷","北京","东京","悉尼","马加丹","惠灵顿","埃尼维托克岛", "中途岛","夏威夷","阿拉斯加","洛杉矶","亚利桑那","亚特兰大","纽约","加拉加斯", "里约热内卢","中大西洋","佛得角群岛"}; int y1,m1,d1; if(z<0) z=12+(12+z+1); d.ShowDay(y1,m1,d1); if(hp<0) d1=d1-1; else if(hp>=24){ hp=0; d1=d1+1;} cout< cout< cout< cout< cout< }
D.
#include #include using namespace std; class Date { int year; int month; int day; public: void SetDay(int NewY, int NewM, int NewD) { year= NewY; month=NewM; day=NewD; } int Get_Y() {return year;} int Get_M() {return month;} int Get_D() {return day;} void ShowDay(int &NewY,int &NewM,int &NewD) { NewY= year; NewM=month; NewD=day; } }; class Clock //时钟类的声明 { public: //外部接口,公有成员函数 void SetTime(int z1,int NewH, int NewM, int NewS,Date d1); int Get_H() {return hp;} int Get_M() {return mp;} int Get_S() {return sp;} Date Get_Day() {return d;} void SetTime() { SYSTEMTIME sys; GetLocalTime( &sys ); hp=sys.wHour; mp=sys.wMinute; sp=sys.wSecond; z=8; d.SetDay(sys.wYear,sys.wMonth,sys.wDay); } void SetZone(int z); int GetZone(); void DispTime(); void AddOneS() { sp++; if(sp>=60) { mp++; sp=0; if(mp>=60) { mp=0; hp++; if(hp>=12) { hp=0; } } } } private: //私有数据成员 int hp,mp,sp,z; Date d; }; //获取时区 int Clock::GetZone() { return z; } void Clock::DispTime() { char a[25][20]={"伦敦","巴黎","开罗","莫斯科","第比利斯","新德里", "阿拉木图","曼谷","北京","东京","悉尼","马加丹","惠灵顿","埃尼维托克岛", "中途岛","夏威夷","阿拉斯加","洛杉矶","亚利桑那","亚特兰大","纽约","加拉加斯", "里约热内卢","中大西洋","佛得角群岛"}; int y1,m1,d1; if(z<0) z=12+(12+z+1); d.ShowDay(y1,m1,d1); if(hp<0) d1=d1-1; else if(hp>=24){ hp=0; d1=d1+1;} cout< cout< cout< cout< cout< }