*/
* Copyright (c) 2016,烟台大学计算机与控制工程学院
* All rights reserved.
* 文件名:time.cpp
* 作者:常轩
* 微信公众号:Worldhello
* 完成日期:2016年3月30日
* 版本号:V1.1
* 问题描述:时间类
* 程序输入:12:12:12
* 程序输出:见运行结果
*/
#include<iostream>
using namespace std;
class Time
{
public:
void set_time();
void show_time();
void add_a_sec(int n1);
void add_a_minute(int n2);
void add_a_hour(int n3);
private:
int sec;
int minute;
int hour;
bool is_time(int,int,int);
};
void Time::set_time()
{
char c1,c2;
cout<<"请输入时间(格式hh:mm:ss)"<<endl;
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(int n1)
{
if(n1+sec>59)
{
sec=(n1+sec)%60;
minute=(n1+sec)/60+minute;
if(minute>59)
{
hour++;
minute=0;
}
}
else
{
sec=sec+n1;
}
}
inline void Time::add_a_minute(int n2)
{
if(n2+minute>59)
{
minute=(n2+minute)%60;
hour=(n2+minute)/60+hour;
if(hour>23)
{
hour=hour-24;
}
} else
{
minute=minute+n2;
} }
inline void Time::add_a_hour(int n3)
{
if(n3+hour>23)
{
hour=(n3+hour)%24; }
else
{
hour=hour+n3;
}
}
int main()
{
Time t1;
t1.set_time();
t1.add_a_sec(1);
t1.add_a_minute(11);
t1.add_a_hour(33);
t1.show_time();
return 0;
}

运行结果:

心得:

应注意超过六十秒,六十分钟,二十四时的情况

C++走向远洋——28(项目三,时间类,2)的更多相关文章

  1. Java实验项目三——职工类对象数组按照职工生日排序

    Program: 修改项目三(1)中的第3题,实现比较方法,将对象数组的数据按照生日的大小给职工排序. Description:令日期类MyDate和员工类Worker类实现接口Comparable, ...

  2. C++走向远洋——(项目二、存储班长信息的学生类、派生)

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  3. C++走向远洋——39(指向学生类的指针)

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:zhizhen.cpp * 作者:常轩 * 微信公众号:Worl ...

  4. C++走向远洋——29(长方柱类)

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:长方柱类.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  5. C++走向远洋——27(项目三,时间类)

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:time.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  6. java三个时间类常用法

    1.System.currentTimeMillis();    获取当前时间戳    System的获取时间戳的方法,只能获取不能进行其他的操作,简单的毫秒计算可以使用 2.Date(),Date( ...

  7. Java实验项目三——面向对象定义职工类和日期类

    Program:按照如下要求设计类: (1)设计一个日期类,用于记录年.月.日,并提供对日期处理的常用方法. (2)设计一个职工类,该职工类至少具有下面的属性:职工号,姓名,性别,生日,工作部门,参加 ...

  8. 日期类时间类,日期时间类,单例模式,装箱与拆箱,数字类随机数,BigDecimal总结

    1.日期类,时间类,日期时间类 初步日期使用方法及格式转换方法(旧方法): 格式://Mon Jul 30 11:26:05 CST 2018             年月日时分秒    CST代表北 ...

  9. JAVA核心技术I---JAVA基础知识(时间类)

    一:时间类库了解 java.util.Date(基本废弃,Deprecated) –getTime(),返回自1970..1以来的毫秒数 java.sql.Date(和数据库对应的时间类) //与数据 ...

随机推荐

  1. tomcat高并发配置

    最近在项目中负责Tomcat高并发优化方案写一写新得. 优化1)tomcat默认的并发是75,可以启用线程池根据生产环境硬件设定线程池大小. <Executor name="tomca ...

  2. 怎么设置tomcat在get请求的中文也不乱码?两种情况下配置

    我们都知道,get请求和post请求的一个比较显著区别就是,在使用post请求的时候,中文不会乱码,但是在使用get请求的时候,如果url后面带有中文就会乱码了.那么这个怎么解决呢? 前提:配置项目的 ...

  3. unity学习 5.x打包

    using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEditor; publi ...

  4. 使用node 做静态文件服务器

    # 1. 使用server-static 包 使用node可以非常快速的方法把指定目录共享出去 前提条件:安装了node,附带有npm 要托管的文件目录为 /root/www # 先创建一个目录用来存 ...

  5. 如何将本地未提交的更改合并到另一个Git分支?

    如何在Git中执行以下操作? 我当前的分支是branch1,我做了一些本地更改. 但是,我现在意识到我实际上是要将这些更改应用于branch2. 有没有办法应用/合并这些更改,以便它们成为branch ...

  6. 2018 ACM-ICPC 宁夏 H.Fight Against Monsters(贪心)

    It is my great honour to introduce myself to you here. My name is Aloysius Benjy Cobweb Dartagnan Eg ...

  7. 吴裕雄--天生自然C语言开发:函数指针

    #include <stdio.h> int max(int x, int y) { return x > y ? x : y; } int main(void) { /* p 是函 ...

  8. CSS样式表---------第三章:样式属性

    三.样式属性 1.背景与前景 background-color:#90; ------------背景色,样式表优先级高. background-image:url(路径)-------------- ...

  9. [LC] 222. Count Complete Tree Nodes

    Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...

  10. [LC] 256. Paint House

    There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...