C语言作业:结构体编程练习 在屏幕上模拟显示一个数字式时钟 源代码能给我的话+50,按如下方法定义一个时钟结构体类型:struct clock{\x05int hour;\x05int minute;\x05int second;};typedef struct clock CLOCK;

来源:学生作业帮助网 编辑:六六作业网 时间:2024/05/02 18:52:06
C语言作业:结构体编程练习在屏幕上模拟显示一个数字式时钟源代码能给我的话+50,按如下方法定义一个时钟结构体类型:structclock{\x05inthour;\x05intminute;\x05i

C语言作业:结构体编程练习 在屏幕上模拟显示一个数字式时钟 源代码能给我的话+50,按如下方法定义一个时钟结构体类型:struct clock{\x05int hour;\x05int minute;\x05int second;};typedef struct clock CLOCK;
C语言作业:结构体编程练习 在屏幕上模拟显示一个数字式时钟 源代码能给我的话+50,
按如下方法定义一个时钟结构体类型:
struct clock
{
\x05int hour;
\x05int minute;
\x05int second;
};
typedef struct clock CLOCK;
然后,将下列用全局变量编写的时钟模拟显示程序改成用CLOCK结构体变量类型重新编写.已知用全局变量编写的时钟模拟显示程序如下:
#include
int hour,minute,second; /*全局变量定义*/
/*
函数功能:时、分、秒时间的更新
函数参数:无
函数返回值:无
*/
void Update(void)
{
\x05second++;
\x05if (second == 60) \x05 /*若second值为60,表示已过1分钟,则 minute值加1*/
\x05{
\x05\x05second = 0;
\x05\x05minute++;
\x05}
\x05if (minute == 60)\x05\x05/*若minute值为60,表示已过1小时,则 hour值加1*/
\x05{
\x05\x05minute = 0;
\x05\x05hour++;
\x05}
\x05if (hour == 24)\x05\x05\x05/*若hour值为24,则hour的值从0开始计时*/
\x05{
\x05\x05hour = 0;
\x05}
}
/*函数功能:时、分、秒时间的显示
函数参数:无
函数返回值:无
*/
void Display(void)\x05\x05\x05/*用回车符'\r'控制时、分、秒显示的位置*/
{
\x05printf("%2d:%2d:%2d\r",hour,minute,second);
}
/*函数功能:模拟延迟1秒的时间
函数参数:无
函数返回值:无
*/
void Delay(void)
{
\x05long\x05t;
\x05for (t=0; t

C语言作业:结构体编程练习 在屏幕上模拟显示一个数字式时钟 源代码能给我的话+50,按如下方法定义一个时钟结构体类型:struct clock{\x05int hour;\x05int minute;\x05int second;};typedef struct clock CLOCK;
#include
struct clock {
\x09int hour;
\x09int minute;
\x09int second;
};
typedef struct clock CLOCK;
/*
函数功能:时、分、秒时间的更新
函数参数:无
函数返回值:无
*/
void Update(CLOCK *myclock) {
\x09myclock->second++;
\x09if (myclock->second == 60) {\x09 /*若second值为60,表示已过1分钟,则 minute值加1*/
\x09\x09myclock->second = 0;
\x09\x09myclock->minute++;
\x09}
\x09if (myclock->minute == 60){\x09\x09/*若minute值为60,表示已过1小时,则 hour值加1*/
\x09\x09myclock->minute = 0;
\x09\x09myclock->hour++;
\x09}
\x09if (myclock->hour == 24)\x09{\x09\x09/*若hour值为24,则hour的值从0开始计时*/
\x09\x09myclock->hour = 0;
\x09}
}
/*
函数功能:时、分、秒时间的显示
函数参数:无
函数返回值:无
*/
void Display(CLOCK *myclock)\x09{\x09\x09/*用回车符'\r'控制时、分、秒显示的位置*/
\x09printf("%2d:%2d:%2d\r", myclock->hour, myclock->minute, myclock->second);
}
/*
函数功能:模拟延迟1秒的时间
函数参数:无
函数返回值:无
*/
void Delay(void) {
\x09long t;
\x09for (t = 0; t < 290000000; t++) {
\x09\x09/*循环体为空语句的循环,起延时作用*/
}
}
int main(){
\x09CLOCK myclock;
\x09long i;
\x09myclock.hour = myclock.minute = myclock.second = 0;\x09\x09/*hour,minute,second赋初值0*/
\x09for (i = 0; i < 100000; i++) {\x09/*利用循环结构,控制时钟运行的时间*/
\x09\x09Update(&myclock); \x09\x09/*时钟更新*/
\x09\x09Display(&myclock); \x09\x09/*时间显示*/
\x09\x09Delay(); \x09\x09\x09\x09/*模拟延时1秒*/
\x09}
\x09return 0;
}