当前位置: 移动技术网 > IT编程>开发语言>C/C++ > 倒计时器

倒计时器

2019年02月03日  | 移动技术网IT编程  | 我要评论

笔记本无线网卡,夫妻生辰八字配对,木制百叶窗

大家是否现在还为走神太久,而导致写程序写太久呢?今天,我们我们用c++做一个倒计时器来控制时间吧!

代码如下:

#include<iostream>
#include<ctime>
#include<stdlib.h>  
using namespace std;

//本程序由杨至诚制作,请勿复制或修改!
class clock{
    int h;
    int m;
    int s;
public:
    void set(int hour,int min,int sec);
    void tick();
    void show();
    void run();
};
void clock::set(int hour,int min,int sec)
{
    h=hour;
    m=min;
    s=sec;
}
void clock::tick()
{
    time_t t=time(null);
    while(time(null)==t);
    if(--s<0){
        s=59;
        if(--m<0){
            m=59;
            if(h>0)
            {
                h--;
            }
        }
    }
}
void clock::show()
{
    cout<<'\r';
    cout<<"        ";
    if(h<10)cout<<0;
    cout<<h<<':';
    if(m<10)cout<<0;
    cout<<m<<':';
    if(s<10)cout<<0;
    cout<<s<<flush;
}
void clock::run()
{
    while(h!=0||m!=0||s!=0){
        tick();
        show();
    }
    system("cls");
    cout<<endl<<"        时间到!"<<endl;
    cout<<'\a';
    system("pause");
    system("cls");
}
int main()
{
    cout<<"  本程序由杨至诚制作,请勿复制或修改!"<<endl<<endl<<endl;
    system("pause");
    system("cls");
    clock c;
    cout<<"  请输入倒计时(小时):";
    int h1,m1,s1;
    cin>>h1;
    cout<<"  请输入倒计时(分钟):";
    cin>>m1;
    if(m1<60&&m1>=0)
    {
    }
    else
    {
        system("cls");
        cout<<"        error"<<endl<<endl;
        cout<<"  本程序由杨至诚制作,请勿复制或修改!"<<endl<<endl<<endl;
        return 0;
    }
    cout<<"  请输入倒计时(秒):";
    cin>>s1;
    if(s1<60&&s1>=0)
    {
    }
    else
    {
        system("cls");
        cout<<"        error"<<endl<<endl; 
        cout<<"  本程序由杨至诚制作,请勿复制或修改!"<<endl<<endl<<endl;
        return 0;
    }
    system("cls");
    c.set(h1,m1,s1);
    c.run();    

}

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网