我想作一个24小时循环定时器
发布网友
发布时间:2022-04-24 19:54
我来回答
共2个回答
热心网友
时间:2023-10-09 04:41
请试一下这个模拟时钟,看看能不能用:
#include <iostream>
#include <iomanip>
#include <ctime>
#include <windows.h>
using namespace std;
void Update();
void setTime( int h, int m, int s,int d,int mo,int y);
void Display();
void Delay(int seconds);
struct clock
{
int hour,minute,second;
int day,month,year;
}clock1;
int main()
{
clock1.hour = clock1.minute = clock1.second = 0;
// 设置初始时间//
setTime(10,30,50,2,7,2010);
while(1)
{
Update(); // 时钟更新//
Display(); // 时间显示//
Delay(1); // 模拟延时1秒//
}
return 0;
}
// 设置初始时间 //
void setTime( int h, int m, int s,int d,int mo,int y)
{
clock1.year = ( y >= 2010 && y < 3010 ) ? y : 2010;
clock1.month = ( mo >= 1 && mo < 13 ) ? mo : 1;
clock1.day = ( d >= 0 && d < 31 ) ? d : 1;
clock1.hour = ( h >= 0 && h < 24 ) ? h : 0;
clock1.minute = ( m >= 0 && m < 60 ) ? m : 0;
clock1.second = ( s >= 0 && s < 60 ) ? s : 0;
}
//时间更新//
void Update()
{
clock1.second++;
if (clock1.second == 60)
{
clock1.second = 0;
clock1.minute++;
}
if (clock1.minute == 60)
{
clock1.minute = 0;
clock1.hour++;
}
if (clock1.hour == 24)
{
clock1.hour = 0;
clock1.day++;
}
if (clock1.day == 31)
{
clock1.day = 1;
clock1.month++;
}
if (clock1.month == 13)
{
clock1.month = 1;
clock1.year++;
}
}
//时间显示//
void Display()
{
cout<<" " <<clock1.year<<"."<<( clock1.month < 10 ? "0" : "" ) << clock1.month << "."
<<( clock1.day < 10 ? "0" : "" ) << clock1.day<<" "
<< ( clock1.hour < 10 ? "0" : "" ) << clock1.hour << ":"
<< ( clock1.minute < 10 ? "0" : "" ) << clock1.minute << ":"
<< ( clock1.second < 10 ? "0" : "") << clock1.second << "\r";
}
//模拟延迟1秒//
void Delay(int seconds)
{
Sleep(1000);
}
热心网友
时间:2023-10-09 04:42
用什么语言做啊