*/
* 程序的版权和版本声明部分
*Copyright (c)2013, 烟台大学计算机学院学生
* All rightsreserved.
*文件名称: array.cpp
* 作 者:杨绍宁
* 完成日期: 2012 年3 月17日
* 版本号: v1.0
*
* 输入描述: 略
*问题描述: 增加1,n秒分小时
*输出: 增加后的时间
*/
#include <iostream>
using namespace std;
class Time
{
public:
void set_time( );
void show_time( );
inline void add_a_sec(); //增加1秒
inline void add_a_minute(); //增加1分
inline void add_a_hour(); //增加1小时
void add_seconds(int); //增加n秒
void add_minutes(int); //增加n分
void add_hour(int); //增加n小时
private:
bool is_time(int, int, int);
int hour;
int minute;
int sec;
};
void Time::set_time( )
{ char c1,c2;
cout<<"请输入时间(格式hh:mm:ss)";
while(1)
{ cin>>hour>>c1>>minute>>c2>>sec;
if(c1!=':'||c2!=':')
cout<<"格式不正确,请重新输入"<<endl;
else if (!is_time(hour,minute,sec))
cout<<"时间非法,请重新输入"<<endl;
else
break;
}
}
void Time::show_time( )
{ cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
bool Time::is_time(int h,int m, int s)
{ if (h<0 ||h>24 || m<0 ||m>60 || s<0 ||s>60)
return false;
return true;
}
inline void Time::add_a_sec()
{
sec+=1;
if(sec>59){
++minute;
sec=0;
}
}
inline void Time::add_a_minute()
{
minute+=1;
if(minute>59){
++hour;
minute=0;
}
}
inline void Time::add_a_hour()
{
hour+=1;
if(hour>23){
hour=0;
}
}
void Time::add_seconds(int n)
{
cin>>n;
sec+=n;
if(sec>59){
sec=sec%60;
minute=sec/60;
}
}
void Time::add_minutes(int n)
{
cin>>n;
minute+=n;
if(minute>59){
minute=minute%60;
hour=minute/60;
}
}
void Time::add_hour(int n)
{
cin>>n;
hour+=n;
if(hour>23){
hour=hour/24;
}
}
int main( )
{
int n;
Time t1;
Time &t2=t1;
t1.set_time( );
t2.show_time( );
t1.add_a_sec();
cout<<"增加1秒"<<endl;
t1.show_time();
t1.add_a_minute();
cout<<"增加1分"<<endl;
t1.show_time();
t1.add_a_hour();
cout<<"增加1小时"<<endl;
t1.show_time();
cout<<"增加n秒"<<endl;
t1.add_seconds(n);
t1.show_time();
cout<<"增加n分"<<endl;
t1.add_minutes(n);
t1.show_time();
cout<<"增加n小时"<<endl;
t1.add_hour(n);
t1.show_time();
return 0;
}
结果:
感受:有一致性