1 Star 0 Fork 1

sdbzmyh/learnC2

forked from guohuacai/learnC2 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
c语言利用结构体实现时钟功能 1.28 KB
一键复制 编辑 原始数据 按行查看 历史
#include <stdio.h>
#include <time.h>
#include <unistd.h>
typedef struct clock
{
int hour;
int minute;
int second;
} Clock;
int getHour()
{
struct tm *ptm;
time_t lt;
time(&lt);
ptm = localtime(&lt);
int hour = ptm->tm_hour;
return hour;
}
int getMinutes()
{
struct tm *ptm;
time_t lt;
time(&lt);
ptm = localtime(&lt);
return ptm->tm_min;
}
int getSeconds()
{
struct tm *ptm;
time_t lt;
time(&lt);
ptm = localtime(&lt);
return ptm->tm_sec;
}
int main(int argc, char const *argv[])
{
Clock clock;
clock.hour = getHour();
clock.minute = getMinutes();
clock.second = getSeconds();
printf("当前时间为:\n");
while (1)
{
sleep(1);
clock.second++;
if (clock.second >= 60)
{
clock.second = 0;
clock.minute++;
if (clock.minute >= 60)
{
clock.minute = 0;
clock.hour++;
if (clock.hour >= 23)
{
clock.hour = 0;
}
}
}
printf("\r%02d:%02d:%02d \n", clock.hour, clock.minute, clock.second);
fflush(stdout);
}
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sdbzmyh/learn-c2.git
git@gitee.com:sdbzmyh/learn-c2.git
sdbzmyh
learn-c2
learnC2
master

搜索帮助