时间类--规定好增加的时间

/*
*程序的版权和版本声明部分:
*Copyright(c)2013,烟台大学计算机学院学生
*All rights reserved.
*文件名称:
*作者:尚振伟
*完成日期:2014年3月15日
*版本号:v0.1
*对任务及求解方法的描述部分:
*输入描述:无
*问题描述:
*程序输入:
*程序输出:
*问题分析:
*算法设计:
*我的程序:
*/
#include <iostream>
using namespace std;
class Time
{
public:
    void set_time();
    void show_time();
    void add_a_sec()
    {
        int h,m,s;
        h=hour;
        m=minute;
        s=second;
        s+=1;
        if(s==60)
        {
            s=0;
            m+=1;
        }
        if(m==60)
        {
            m=0;
            h+=1;
        }
        if(h==24)
        {
            h=0;
        }
        cout<<h<<":"<<m<<":"<<s;
    }
    void add_a_min()
    {
        int h,m,s;
        h=hour;
        m=minute;
        s=second;
        m+=1;
        if(m==60)
        {
            m=0;
            h+=1;
        }
        if(h==24)
        {
            h=0;
        }
        cout<<h<<":"<<m<<":"<<s;
    }
    void add_a_hour()
    {
        int h,m,s;
        h=hour;
        m=minute;
        s=second;
        h+=1;
        if(h==24)
        {
            h=0;
        }
        cout<<h<<":"<<m<<":"<<s;
    }
private:
    bool is_time(int,int,int);
    int hour;
    int minute;
    int second;
};
void Time::set_time()
{
    char c1,c2;
    cout<<"请输入时间(格式为时:分:秒):"<<endl;
    while(1)
    {
        cin>>hour>>c1>>minute>>c2>>second;
        if(c1!=':'||c2!=':')
            cout<<"格式不正确,请重新输入:"<<endl;
        else if(!is_time(hour,minute,second))
            cout<<"时间错误,请重新输入:"<<endl;
        else
            break;
    }
}
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;
    else
        return true;
}
void Time::show_time()
{
    cout<<"输入的正确时间为:"<<endl;
    cout<<hour<<":"<<minute<<":"<<second;
}
int main()
{
    int n;
    Time t;
    t.set_time();
    t.show_time();
    while(1)
    {
        cout<<endl;
        cout<<"1 增加一秒 2 增加一分钟"<<endl;
        cout<<"3 增加一小时 0 退出"<<endl;
        cout<<"请输入你的选择:";
        cin>>n;
        while(n==0)
        {
            return 0;
        }
        switch(n)
        {
        case 1:
            cout<<"增加后时间为:"<<endl;
            t.add_a_sec();
            break;
        case 2:
            cout<<"增加后时间为:"<<endl;
            t.add_a_min();
            break;
        case 3:
            cout<<"增加后时间为:"<<endl;
            t.add_a_hour();
            break;
        case 0:
            cout<<"欢迎使用:"<<endl;
            break;
        default:
            cout<<"输入有误:"<<endl;
            break;
        }
    }
    return 0;
}


结果展示:

心得体会:昨天的那篇删了,今天重新发。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尚振伟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值