您的位置:首页 > 编程语言 > C语言/C++

一个关于时间的静态变量的应用

2016-04-07 19:58 204 查看
/*  Copyright  (c)  2016

*    All rights reserved

*    文件名称:3.cpp

*   作者:刘丽

*   完成日期:2016年 4  月 7日

*   版本号: v1.0

*

*   问题描述:一个关于时间类的静态成员应用

*/

#include <iostream>
#include<string>
using namespace std;
class Time
{
public:
Time(int=0,int =0,int =0);
void show_time ();
void add_seconds(int );
void add_minutes(int);
void add_hours(int n);
static void change24();
static void changefrom();
private:
static bool is_24;
static bool from0;
int hour;
int minute;
int sec;

};
bool Time::is_24=true;
bool Time::from0=false;
Time::Time(int hour,int minute,int sec):hour(hour),minute(minute),sec(sec)
{
}
void Time::show_time ()
{
int t;
if(from0==true)
{
if(is_24==true)
{
if(hour<10)
cout<<"0";    cout<<hour<<":";
if(minute<10)
cout<<"0";    cout<<minute<<":";
if(sec<10)
cout<<"0";    cout<<sec<<endl;
}
else
{

if(hour>12)
{
t=hour%12;
if(t<10)
cout<<"0";    cout<<t<<":";
if(minute<10)
cout<<"0";    cout<<minute<<":";
if(sec<10)
cout<<"0";    cout<<sec<<endl;
}
else
{
if(hour<10)
cout<<"0";    cout<<hour<<":";
if(minute<10)
cout<<"0";    cout<<minute<<":";
if(sec<10)
cout<<"0";    cout<<sec<<endl;

}

}

}
else //from0==false
{
if(is_24==true)
cout<<hour<<":"<<minute<<":"<<sec<<endl;
else if(hour>12)
cout<<hour%12<<":"<<minute<<":"<<sec<<" pm"<<endl;
else
cout<<hour<<":"<<minute<<":"<<sec<<" am"<<endl;

}

}
void Time::add_seconds(int n)
{
if(sec+n>60)
{

sec=(sec+n)%60;
add_minutes((sec+n)/60);
}
else sec+=n;

}
void Time::add_minutes(int n)
{
if(minute+n>60)
{

minute=(minute+n)%60;
add_hours((minute+n)/60);
}
else minute+=n;
}
void Time::add_hours(int n)
{
hour+=n;
if(hour>=24)
hour=hour%24;
}
void Time::change24()
{
if(is_24==true)
is_24=false;
else
is_24=true;

}
void Time::changefrom()
{
if(from0==true)
from0=false;
else
from0=true;

}
int main()
{
Time t1(23,14,25),t2(8,45,6);
cout<<"24时制,不前导0:"<<endl;
cout<<"t1是:";
t1.show_time();
cout<<"t2是:";
t2.show_time();

cout<<"10小时后,切换是否前导0:"<<endl;
t1.add_hours(10);
t1.changefrom();
t1.show_time();
t2.add_hours(10);
t2.show_time();

cout<<"换一种形式"<<endl;
t1.change24();
t1.show_time();
t2.show_time();

return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ class 初学