时间类

/*  

* Copyright (c) 2011, 烟台大学计算机学院  

* All rights reserved.  

* 作    者:赵兰

* 完成日期:2013  年 3  月  19日  

* 版 本 号:v1.0  

* 输入描述: 略

 * 问题描述:实现时间累加。 

* 问题分析: 在类的基础上运用内置函数

* 算法设计:略  

* 程序输出:如下 

*/    

#include <iostream>
using namespace std;
class Time
{
public:                   //公用成员函数
 void set_time( );     
 void show_time( );
    inline void add_a_sec();  //声明内置函数  
    inline void add_a_minute();  
    inline void add_an_hour();  
    void add_seconds(int n);  
    void add_minutes(int n );  
    void add_hours(int n);  
  private: 
 bool is_time(int, int, int);  //数据成员为私有的
 int hour;
 int minute;
 int sec;
};
void Time::set_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++;  
    if(sec>60)  
        add_a_minute();  
}  
inline void Time::add_a_minute()  
{  
    minute++;  
    if(minute>60)  
        add_an_hour();  
}  
inline void Time::add_an_hour()  
{  
    hour++;  
    if(hour>23)  
        hour=0;  
}  
void Time::add_seconds(int n)  
{  
    sec+=n;  
    if(sec>60)  
    {  
        add_minutes(sec/60);  
    sec=sec%60;  
    }  
}  
void Time::add_minutes(int n)  
{  
    minute+=n;  
    if(minute>60)  
    {  
        add_hours(minute/60);  
        minute%=60;  
    }  
}  
void Time::add_hours(int n)  
{  
    hour+=n;  
    if(hour>23)  
    {  
         hour=hour%24;  
    }  
}
int main()
{
 Time t1;      
    Time &t2=t1;    
    t1.set_time( );  
        cout<<"输出时间:"<<endl;  
    t2.show_time( );  
    t1.add_a_sec();  
        cout<<"增加1秒后为:"<<endl;  
    t1.show_time( );  
    t1.add_a_minute();  
        cout<<"增加一分钟后为:"<<endl;  
    t2.show_time( );  
    t1.add_an_hour();  
        cout<<"增加一小时后为:"<<endl;  
    t1.show_time( );  
    t1.add_seconds(70);  
        cout<<"增加70秒后为:"<<endl;  
    t1.show_time( );  
    t1.add_minutes(75);  
        cout<<"增加75分钟后为:"<<endl;  
    t1.show_time( );  
    t1.add_hours(5);  
        cout<<"增加5小时后为:"<<endl;  
    t1.show_time( );  
    return 0;    

}



 

少了一个括号找了大半天以后写程序括号成对成对的写

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值