1.首先,在构造函数内进行初始化,并规定小于零则重置为零。
- Counter(int h,int m,int s)
- {
- if(h<0)
- {
- hour=0;
- }
- else if(m<0)
- {
- minute=0;
- }
- else if(s<0)
- {
- second=0;
- }
- else
- {
- hour=h;
- minute=m;
- second=s;
- }
- }
2.在函数countDown()内实现倒计时的功能。
- void countDown()
- {
- for( i=hour;i>=0;i--)
- {
- for( j=minute;j>=0;j--)
- {
- for( k=second;k>=0;k--)
- {
- long temp=time(NULL);
- while(time(NULL)==temp);
- if(--second<=0)
- {
- second=59;
- if(--minute<=0)
- {
- minute=59;
- if(--hour<=0)
- {
- hour=23;
- }
- }
- }
- Sleep(1000);
- system("cls");
- cout << "The remaining time is ";
- if(i<10)
- cout << "0";
- cout << i << " : ";
- if(j<10)
- cout << "0";
- cout << j << " : ";
- if(k<10)
- cout << "0";
- cout << k << endl;
- }
- }
- }
- system("color f4");
- cout<<"Time is up !!!" <
- system("pause");
- }
3.测试程序功能。
- int main()
- {
- //测试计数器类
- system("color f1");
- int hour_;
- int minute_;
- int second_;
- cout << "Please enter the time ! " << endl;
- cout << "hour : " <
- cin >> hour_;
- cout << "minute : "<
- cin >> minute_;
- cout << "second : "<
- cin >> second_;
- cout << endl;
- Counter text1(hour_,minute_,second_);
- text1.countDown();
- return 0;
- }
完整代码如下:
- #include
- #include
- #include
- #include
- #include
- #include
- using namespace std;
- //计数器类
- class Counter
- {
- public:
- Counter(int h,int m,int s)
- {
- if(h<0)
- {
- hour=0;
- }
- else if(m<0)
- {
- minute=0;
- }
- else if(s<0)
- {
- second=0;
- }
- else
- {
- hour=h;
- minute=m;
- second=s;
- }
- }
- void countDown()
- {
- for( i=hour;i>=0;i--)
- {
- for( j=minute;j>=0;j--)
- {
- for( k=second;k>=0;k--)
- {
- long temp=time(NULL);
- while(time(NULL)==temp);
- if(--second<=0)
- {
- second=59;
- if(--minute<=0)
- {
- minute=59;
- if(--hour<=0)
- {
- hour=23;
- }
- }
- }
- Sleep(1000);
- system("cls");
- cout << "The remaining time is ";
- if(i<10)
- cout << "0";
- cout << i << " : ";
- if(j<10)
- cout << "0";
- cout << j << " : ";
- if(k<10)
- cout << "0";
- cout << k << endl;
- }
- }
- }
- system("color f4");
- cout<<"Time is up !!!" <
- system("pause");
- }
-
- private:
- int hour;
- int minute;
- int second;
- int i,j,k;
-
- };
- int main()
- {
- //测试计数器类
- system("color f1");
- int hour_;
- int minute_;
- int second_;
- cout << "Please enter the time ! " << endl;
- cout << "hour : " <
- cin >> hour_;
- cout << "minute : "<
- cin >> minute_;
- cout << "second : "<
- cin >> second_;
- cout << endl;
- Counter text1(hour_,minute_,second_);
- text1.countDown();
- return 0;
- }
-
相关阅读:
Android Jetpack 全家桶系列(二): Lifecycle(使用篇)
gitlab之cicd的gitlab-runner集成-dockerfile构建环境
springboot物流配送推荐系统毕业设计源码072257
【21天python打卡】第14天 网络爬虫(5)
Python找不到其他文件夹下的py文件并提示ModuleNotFoundError: No module named ‘xxx‘
2022牛客多校九 B-Two Frogs(概率DP+前缀和优化)
MySQL数据库期末考试试题及参考答案(08)
nest.js创建以及error相关问题
LOSER的预习笔记——————————清除setlinterval定时器
Beam failure Recovery
-
原文地址:https://blog.csdn.net/weixin_74287172/article/details/134020250
-
最新文章
-
.NET 11 Preview 4 正式发布:Runtime-Async 全面启用、Process API 大幅扩展
大数据没那么远:把散乱数据理顺,让业务敢用
高性能百度OCR ONNX Runtime C#实现
C#/.NET/.NET Core技术前沿周刊 | 第 70 期(2026年5.01-5.10)
Triton学习 · Part 1 · Hello, world!
manyspeech-cli 本地命令行语音识别工具
PortSwigger SQL注入LAB5 & LAB6
Go-Spring 项目概览
"HyFormer: Revisiting the Roles of Sequence Modeling and Feature Interaction in CTR Prediction" 论文笔记
WIZnet-EVB-Pico2开始,用MicroPython玩转以太网开发