C++ 计时器软件编写指南:简单易懂的代码示例
以下是一个简单的计时器软件的 C++ 代码示例:
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
int main() {
int seconds = 0;
cout << 'Enter the number of seconds: ';
cin >> seconds;
for (int i = seconds; i >= 0; i--) {
cout << i << endl;
this_thread::sleep_for(chrono::seconds(1));
}
cout << 'Time's up!' << endl;
return 0;
}
该程序使用了 C++11 中的 chrono 和 thread 库来实现计时器功能。用户输入了需要计时的秒数,程序会从输入的秒数开始倒计时,每隔一秒打印出当前剩余秒数。当倒计时结束后,程序会输出'Time's up!'。
原文地址: https://www.cveoy.top/t/topic/lNuY 著作权归作者所有。请勿转载和采集!