#include <iostream>
using namespace std;
class Time
{public:
void set_time( );
void show_time( );
void add_n_sec(int );
void add_n_minute(int );
void add_n_hour(int );
void add_a_sec( );
void add_a_minute( );
void add_a_hour( );
private:
bool is_time(int, int, int);
int hour;
int minute;
int sec;
};
int main( )
{
int s,m,h;
Time t1;
Time &t2=t1;
t1.set_time( );
t2.show_time( );
cout<<"输入加的秒:";
cin>>s;
t1.add_n_sec(s);
t1.show_time( );
cout<<"输入加的分钟:";
cin>>m;
t1.add_n_minute (m);
t1.show_time( );
cout<<"输入加的小时:";
cin>>h;
t1.add_n_hour( h);
t1.show_time( );
t1.add_a_sec();
cout<<"加入一秒的结果:";
t1.show_time( );
t1.add_a_minute ();
cout<<"加入一分的结果:";
t1.show_time( );
t1.add_a_hour( );
cout<<"加入一小时的结果:";
t1.show_time( );
return 0;
}
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;
}
void Time::add_n_sec(int n)
{
int h,m,s;
s =(sec +n)%60;
m=(sec +n)/60+minute ;
minute =m%60;
h=m/60+ hour ;
hour =h%24;
sec=s;
}
void Time::add_n_minute(int n)
{
int h;
minute=(minute+n)%60;
h=hour+(minute+n)/60;
hour=h%24;
}
void Time::add_n_hour(int n)
{
hour=(hour+n)%24;
}
void Time::add_a_sec()
{
int h,m,s;
s =(sec +1)%60;
m=(sec +1)/60+minute ;
minute =m%60;
h=m/60+ hour ;
hour =h%24;
sec=s;
}
void Time::add_a_minute()
{
int h;
minute=(minute+1)%60;
h=hour+(minute+1)/60;
hour=h%24;
}
void Time::add_a_hour()
{
hour=(hour+1)%24;
}
第二周实验报告2
最新推荐文章于 2022-03-20 22:25:15 发布