C语言作业:结构体编程练习 在屏幕上模拟显示一个数字式时钟 源代码能给我的话+50,按如下方法定义一个时钟结构体类型:struct clock{\x05int hour;\x05int minute;\x05int second;};typedef struct clock CLOCK;
来源:学生作业帮助网 编辑:作业帮 时间:2024/07/13 08:23:40
![C语言作业:结构体编程练习 在屏幕上模拟显示一个数字式时钟 源代码能给我的话+50,按如下方法定义一个时钟结构体类型:struct clock{\x05int hour;\x05int minute;\x05int second;};typedef struct clock CLOCK;](/uploads/image/z/14393095-7-5.jpg?t=C%E8%AF%AD%E8%A8%80%E4%BD%9C%E4%B8%9A%EF%BC%9A%E7%BB%93%E6%9E%84%E4%BD%93%E7%BC%96%E7%A8%8B%E7%BB%83%E4%B9%A0+%E5%9C%A8%E5%B1%8F%E5%B9%95%E4%B8%8A%E6%A8%A1%E6%8B%9F%E6%98%BE%E7%A4%BA%E4%B8%80%E4%B8%AA%E6%95%B0%E5%AD%97%E5%BC%8F%E6%97%B6%E9%92%9F+%E6%BA%90%E4%BB%A3%E7%A0%81%E8%83%BD%E7%BB%99%E6%88%91%E7%9A%84%E8%AF%9D%2B50%2C%E6%8C%89%E5%A6%82%E4%B8%8B%E6%96%B9%E6%B3%95%E5%AE%9A%E4%B9%89%E4%B8%80%E4%B8%AA%E6%97%B6%E9%92%9F%E7%BB%93%E6%9E%84%E4%BD%93%E7%B1%BB%E5%9E%8B%EF%BC%9Astruct+clock%7B%5Cx05int+hour%3B%5Cx05int+minute%3B%5Cx05int+second%3B%7D%3Btypedef+struct+clock+CLOCK%3B)
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;
}