倒计时器

大家是否现在还为走神太久,而导致写程序写太久呢?今天,我们我们用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();    

}

猜你喜欢

转载自www.cnblogs.com/yangzhicheng-blog/p/10349261.html