C++ 电子时钟代码示例:简单实现
以下是一个简单的 C++ 程序,用于创建一个基本的电子时钟:
#include <iostream>
#include <ctime>
int main() {
while (true) {
// 获取当前时间
std::time_t currentTime = std::time(0);
std::tm* now = std::localtime(¤tTime);
// 输出当前时间
std::cout << (now->tm_hour < 10 ? '0' : '') << now->tm_hour << ':'
<< (now->tm_min < 10 ? '0' : '') << now->tm_min << ':'
<< (now->tm_sec < 10 ? '0' : '') << now->tm_sec << std::endl;
// 延迟一秒
std::this_thread::sleep_for(std::chrono::seconds(1));
}
return 0;
}
这个程序使用了 C++ 的 <ctime> 头文件中的 time 函数来获取当前时间,并使用 <iostream> 头文件中的 cout 来输出时间。程序会一直循环输出当前时间,每秒更新一次。
注意,这只是一个简单的示例程序,没有考虑特定的用户界面或其他功能。实际的电子时钟可能需要更复杂的实现。
AI 优化版
原文地址:https://www.cveoy.top/t/topic/mWZF 著作权归作者所有。请勿转载和采集!