第6周项目4 静态成员函数


/*
02. *Copyright (c) 2016,烟台大学计算机学院
03. *All rights reserved.
 04. *文件名称:cpp
 05. *作    者:孙亚茹
06. *完成日期:2016年4月10日
07. *版 本 号:v1.0
 09. *问题描述:输出分数的各种形式。
*/
#include <iostream>
using namespace std;
class time
{
private:
    static bool is_24;
    static bool from0;
    int hour;
    int minute;
    int sec;

public:
    time(int=0,int=0,int=0);
    void show_time();
    void add_seconds(int);
    void add_minutes(int);
    void add_hours(int);
    static void change24();
    static void changefrom0();
};
bool time::is_24=true;
bool time::from0=false;
time::time(int h,int m,int s)
{
    hour=h;
    minute=m;
   sec=s;
}
void time::show_time()
{
  if(from0)
      {
            if(hour<10)
                cout<<"0"<<hour<<":";
           else
                cout<<hour<<":";
           if(minute<10)
                cout<<"0"<<minute<<":";
            else
                cout<<minute<<":";
            if(sec<10)
                cout<<"0"<<sec<<endl;
            else
               cout<<sec<<endl;
      }



}
void time::add_hours(int z)
{
    hour+=z;
    if(hour>=24)
   {
        hour=hour%24;
    }

}
void time::add_minutes(int y)
{
  minute+=y;
   if(minute>=60)
  {
       hour+=minute/60;
       minute=minute%60;
   }

}


void time::add_seconds(int x)
{
  sec+=x;
    if(sec>=60)
    {
        minute+=sec/60;
       sec=sec%60;
   }

}
void time::changefrom0()
{
    is_24=!is_24;
}
void time::change24()
{
    from0=!from0;
}

int main()
{
    time t1(23,14,25);
    time t2(8,45,6);
    cout<<"24小时制,不前导0:"<<endl;
    t1.show_time();
    t2.show_time();
    cout<<"10小时后,切换是否前导0:"<<endl;
    t1.add_hours(10);
    t2.add_hours(10);
    time::change24();
    cout<<"t1是:";
    t1.show_time();
    cout<<"t2是:";
    t2.show_time();
    cout<<"换一种时制:"<<endl;
    t1.changefrom0();
    cout<<"t1是:";
    t1.show_time();
    t2.changefrom0();
    cout<<"t2是:";
    t2.show_time();

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值