您的位置:首页 > 其它

结构体实现模拟时钟

2015-11-15 17:16 288 查看
#include <stdio.h>
typedef struct clock
{
int hour,minute,second;
}CLOCK;
//函数功能:时分秒时间的更新
void Update(CLOCK *t)
{
t->second++;
if (t->second == 60)
{
t->second=0;
t->minute++;
/* code */
}
if (t->minute ==60)
{
t->minute = 0;
t->hour++;
/* code */
}
if (t->hour == 24)
{
t->hour=0;
/* code */
}
}
//函数功能:时、分、秒时间的显示;
void Display(CLOCK *t)
{
printf("%2d:%2d:%2d\n",t->hour,t->minute,t->second);
}
//函数功能:模拟延迟1s的时间
void Delay(void)
{
long t;
for (t = 0; t<50000000 ; ++t)
{
/* code */
}
}
int main()
{

CLOCK myclock;
myclock.hour=myclock.minute=myclock.second=0;
for (long i = 0; i < 100000; ++i)
{
Update(&myclock);
Display(&myclock);
Delay();
/* code */
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: