/*
02.* 程序的版权和版本声明部分
03.* Copyright (c)2012, 烟台大学计算机学院学生
04.* All rightsreserved.
05.* 文件名称: object.cpp
06.* 作者:刘明亮
07.* 完成日期: 2013年 4 月 14 日
08.* 版本号: v1.0
09.* 输入描述:
10.* 问题描述:
11.* 程序输出:
*/
#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::show_time()
{
int h;
if(is_24)
h=hour;
else
h=hour%12;
if(h<10 && from0) cout<<"0";
cout<<h<<":";
if(minute<10 && from0) cout<<"0";
cout<<minute<<":";
if(sec<10 && from0) cout<<"0";
cout<<sec;
if(!is_24){
if(hour<12)
cout<<" am\n";
if(hour>12)
cout<<" pm\n";
}
}
void Time::add_seconds(int s)
{
sec+=s;
int m;
s=s%60;
m=s/60;
add_minutes(m);
}
void Time::add_minutes(int m)
{
minute+=m;
int h;
m=m%60;
h=m/60;
add_hours(h);
}
void Time::add_hours(int h)
{
hour+=h;
h=h%24;
}
void Time::change24()
{
is_24=!is_24;
}
void Time::changefrom0()
{
from0=!from0;
}
bool Time::is_24=true;
bool Time::from0=true;
int main( )
{
int h,m,s;
Time t1(22,12,4),t2(11,5,33);
cout<<"24时制,前导0:\n";
cout<<"t1是:";
t1.show_time();
cout<<endl;
cout<<"t2是:";
t2.show_time();
cout<<endl;
cout<<"请输入想要增加多少小时、分钟、秒:\n";
cin>>h>>m>>s;
t1.add_hours(h);
t1.add_minutes(m);
t1.add_seconds(s);
t2.add_hours(h);
t2.add_minutes(m);
t2.add_seconds(s);
cout<<"t1是:";
t1.show_time();
cout<<endl;
cout<<"t2是:";
t2.show_time();
cout<<endl;
cout<<"切换不前导0后:\n";
Time::changefrom0();
cout<<"t1是:";
t1.show_time();
cout<<endl;
cout<<"t2是:";
t2.show_time();
cout<<endl;
cout<<"换一种制式后:\n";
t1.change24();
cout<<"t1是:";
t1.show_time();
cout<<"t2是:";
t2.show_time();
cout<<endl;
return 0;
}