一、静态成员应用
#include <iostream>
using namespace std;
class Time{
public:
Time(int=0,int=0,int=0);
void show_time( ); //根据is_24和from0,输出适合形式-20:23:5/8:23:5 pm/08:23:05 pm
void add_seconds(int); //增加n秒钟
void add_minutes(int); //增加n分钟
void add_hours(int); //增加n小时
static void change24(); //改变静态成员is_24,在12和24时制之间转换
static void changefrom0(); //改变静态成员from0,切换是否前导0
private:
static bool is_24; //为true时,24小时制,如20:23:5;为flase,12小时制,显示为8:23:5 pm
static bool from0; //为true时,前导0,8:23:5显示为08:23:05
int hour;
int minute;
int sec;
};
Time::Time(int h,int m,int s):hour(h),minute(m),sec(s){}
void Time::add_seconds(int n)
{
sec=sec+n;
if(sec>59)
{
minute=minute+sec/60;
sec=sec%60;
if(minute>59)
{
hour=hour+minute/60;
minute=minute%60;
}
}
}
void Time::add_minutes(int n)
{
minute=minute+n;
if(minute>=60)
{
hour=hour+minute/60;
minute=minute%60;
}
}
void Time::add_hours(int n)
{
hour=(hour+n)%24;
}
bool Time::is_24=true;
bool Time::from0=false;
void Time::show_time()
{
if(is_24)
{
if(from0)
{
cout<<(hour<10?"0":" ")<<hour<<":"<<(minute<10?"0":" ")<<minute<<":"<<(sec<10?"0":" ")<<sec<<endl;
}
else
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
}
else
{
if(from0)
{
cout<<(hour<10||(hour-12>=0&&hour-12<10)?"0":"")<<(hour>12?hour-12:hour)<<":"<<(minute<10?"0":"")<<minute<<":"<<(sec<10?"0":"")<<sec<<" "<<(hour>11?"pm":"am")<<endl;
}
else
{
cout<<(hour>12?hour-12:hour)<<":"<<minute<<":"<<sec<<" "<<(hour>11?"pm":"am")<<endl;
}
}
}
void Time::change24()
{
is_24=!is_24;
}
void Time::changefrom0()
{
from0=!from0;
}
int main( )
{
Time t1(23,14,25),t2(8,45,6);
cout<<"24时制,不前导:"<<endl;
cout<<"t1是:"<<endl;
t1.show_time();
cout<<"t2是:"<<endl;
t2.show_time();
t1.add_hours(10);
t2.add_hours(10);
Time::changefrom0();
cout<<"10小时后,切换是否前导:"<<endl;
cout<<"t1是:"<<endl;
t1.show_time();
cout<<"t2是:"<<endl;
t2.show_time();
Time::change24();
cout<<"换一种制式:"<<endl;
cout<<"t1是:"<<endl;
t1.show_time();
cout<<"t2是:"<<endl;
t2.show_time();
return 0;
}
二、友元类
#include <iostream>
using namespace std;
class Date; //对Date类的提前引用声明
class Time
{
public:
Time(int,int,int);
void add_a_second(Date &); //增加1秒,1秒后可能会到了下一天,乃到下一月、下一年
void display(Date &); //显示时间,格式:月/日/年 时:分:秒
private:
int hour;
int minute;
int sec;
};
class Date
{
public:
Date(int,int,int);
friend class Time; //Time为Date的友元类
private:
int month;
int day;
int year;
};
Date::Date(int m,int d,int y):year(y),day(d),month(m){}
Time::Time(int h,int m,int s):hour(h),minute(m),sec(s){}
void Time::add_a_second(Date &d)
{
sec=sec+1;
if(sec==60)
{
minute=minute+1;
sec=0;
if(minute==60)
{
hour=hour+1;
minute=0;
if(hour>=24)
{
d.day=d.day+1;
hour=0;
if(d.day==29)
{
while(d.month==2)
{
d.month=d.month+1;
}
d.day=1;
}
if(d.day==31)
{
while(d.month==4||d.month==6||d.month==9||d.month==11)
{
d.month=d.month+1;
}
d.day=1;
}
if(d.day==32)
{
while(d.month==1||d.month==3||d.month==5||d.month==7||d.month==8||d.month==10||d.month==12)
{
d.month=d.month+1;
}
d.day=1;
if(d.month==13)
{
d.year=d.year+1;
d.month=1;
}
}
}
}
}
}
void Time::display(Date &d)
{
cout<<d.year<<"/"<<d.month<<"/"<<d.day<<" "<<hour<<":"<<minute<<":"<<sec<<endl;
}
int main( )
{
Time t1(23,59,32);
Date d1(12,31,2013); //测试时,再试试Date d1(2,28,2013)会如何
for(int i=0; i<=100; i++)
{
t1.add_a_second(d1);
t1.display(d1);
}
return 0;
}
//下面定义两个类中的成员函数,要求不得再增加成员函数
//注意体会在Time的成员函数中可以调用Date类的私有数据成员
C++作业3
最新推荐文章于 2024-03-19 17:24:19 发布