新手每日一练:初见结构体
/*****************************************************************************************编程写出一个系统的时钟显示程序,形式为hh:mm:ss
******************************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
struct Clock{
int hour;
int minute;
int second;
};
typedef struct Clock CLOCK;
void init(CLOCK *myclock);
void Update(CLOCK *myclock);
void Display(CLOCK *myclock);
int main(void)
{
CLOCK myclock;
int i = 1000000,time = 1000;
init(&myclock);
printf("\n\t ********this is my clock********");
printf("\n\t-------------------------------------\n");
while(i > 0)
{
i--;
Update(&myclock);
Display(&myclock);
Sleep(time);
}
return 0;
}
void init(CLOCK *myclock)
{
myclock->hour = 0;
myclock->minute = 0;
myclock->second = 0;
}
void Update(CLOCK *myclock)
{
myclock->second++;
if(60 == myclock->second)
{
myclock->second = 0;
myclock->minute++;
}
if(60 == myclock->minute)
{
myclock->minute = 0;
myclock->hour++;
}
if(24 == myclock->second)
{
myclock->hour = 0;
}
}
void Display(CLOCK *myclock)
{
printf("\t\t ==|%2d:%2d:%2d |==\r",myclock->hour,myclock->minute,myclock->second);
}