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

看了一个老师布置的C++作业,也顺便练习一下,提高一下自己

2014-03-11 12:38 369 查看
       自己也是在学习C++,经常逛CSDN,在CSDN博客推荐榜上,经常看到一个老师写的C++博客,把课堂搬到CSDN上,作业就在其博客上,学生交作业直接写博客就行,不但鼓励自己的学生写博客,而且还方便其他学生学习参考,还节能环保,非常感谢这位老师。

> http://blog.csdn.net/sxhelijian/article/details/20841847[/code] 
下面是参考老师的博客写的时间类:

/*************************************************************************
> File Name: time.cpp
> Author: kanty
> Mail: jiakang906@126.com
> Created Time: 2014年03月11日 星期二 09时57分36秒
************************************************************************/

#include<iostream>
using namespace std;
class Time
{
public:
void set_time();
void show_time();
void add_a_sec()
{
ss=ss+1;
}
void add_a_minute()
{
mm=mm+1;
}
void add_an_hour()
{
hh=hh+1;
}

void add_seconds(int);
void add_minutes(int);
void add_hours(int);

private:
void update();
bool istime(int ,int ,int );
int hh;
int mm;
int ss;
};

void Time::set_time()
{
cout<<"please input the time (hh:mm:ss): ";
char c1,c2;
while(1)
{
cin>>hh>>c1>>mm>>c2>>ss;
if(c1!=':'||c2!=':')
cout<<"input error,please input again!"<<endl;
else if(!istime(hh,mm,ss))
cout<<"the time is wrong!! input again"<<endl;
else
break;
}
}

bool Time::istime(int h ,int m,int s)
{
if(h<0||h>24||m<0||m>60||s<0||s>60)
return false;
else
return true;
}
void Time::update()
{
int s1,m1;
if(ss>=60)
{
s1=ss/60;
ss=ss%60;
mm=mm+s1;
}
else if(mm>=60)
{
m1=mm/60;
mm=mm%60;
hh=hh+m1;
}
else if(hh>=24)
{
hh=hh%24;
}
}

void Time::show_time()
{
update();
cout<<"the Time: "<<hh<<":"<<mm<<":"<<ss<<endl;
}

void Time::add_seconds(int s)
{

ss=ss+s;
}
void Time::add_minutes(int m)
{
mm=mm+m;
}
void Time::add_hours(int h)
{
hh=hh+h;
}

int main()
{
Time t1;
t1.set_time();
t1.add_seconds(50);
t1.add_a_minute();
t1.add_an_hour();
t1.add_hours(2);
t1.show_time();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: