
C++做的倒计时器, 如果用C语言做的话,下面的代码要如何改
#include<stdio.h>#include<time.h>voiddisplay_menu(){printf("欢迎使用!\n");printf("*******...
#include <stdio.h>
#include <time.h>
void display_menu()
{
printf( " 欢迎使用!\n");
printf( "**************************\n" );
printf( "-*1. 倒计时 *-\n" );
printf( "**************************\n" );
}
struct Clock
{
int sec;
int min;
int hour;
void tick();
void show();
void run();
void set( int h, int m, int s );
};
void Clock::tick()
{
long t = time( NULL );
while ( t == time( NULL ) )
;
if ( --sec < 0 )
{
sec = 59;
if ( --min < 0 )
{
min = 59;
--hour;
}
}
};
void Clock::show()
{
printf( "\r" );
if ( hour < 10 )
printf( "0" );
printf( "%d:", hour );
if ( min < 10 )
printf( "0" );
printf( "%d:", min );
if ( sec < 10 )
printf( "0" );
printf( "%d", sec );
};
void Clock::run()
{
while ( sec != 0 || min != 0 || hour != 0 )
{
tick();
show();
}
};
void Clock::set( int h, int m, int s )
{
hour = h;
min = m;
sec = s;
};
int count_down()
{
int s;
int m;
int h;
printf( "请输入时间(hh mm ss):" );
scanf( "%d%d%d", &h, &m, &s );
Clock c;
c.set( h, m, s );
c.run();
printf( "\n" );
printf( "time is over!!!\n" );
return(0);
}
void main()
{
int num;
display_menu();
scanf( "%d", &num );
switch ( num )
{
case 1: count_down(); break;
}
}
谢谢! 展开
#include <time.h>
void display_menu()
{
printf( " 欢迎使用!\n");
printf( "**************************\n" );
printf( "-*1. 倒计时 *-\n" );
printf( "**************************\n" );
}
struct Clock
{
int sec;
int min;
int hour;
void tick();
void show();
void run();
void set( int h, int m, int s );
};
void Clock::tick()
{
long t = time( NULL );
while ( t == time( NULL ) )
;
if ( --sec < 0 )
{
sec = 59;
if ( --min < 0 )
{
min = 59;
--hour;
}
}
};
void Clock::show()
{
printf( "\r" );
if ( hour < 10 )
printf( "0" );
printf( "%d:", hour );
if ( min < 10 )
printf( "0" );
printf( "%d:", min );
if ( sec < 10 )
printf( "0" );
printf( "%d", sec );
};
void Clock::run()
{
while ( sec != 0 || min != 0 || hour != 0 )
{
tick();
show();
}
};
void Clock::set( int h, int m, int s )
{
hour = h;
min = m;
sec = s;
};
int count_down()
{
int s;
int m;
int h;
printf( "请输入时间(hh mm ss):" );
scanf( "%d%d%d", &h, &m, &s );
Clock c;
c.set( h, m, s );
c.run();
printf( "\n" );
printf( "time is over!!!\n" );
return(0);
}
void main()
{
int num;
display_menu();
scanf( "%d", &num );
switch ( num )
{
case 1: count_down(); break;
}
}
谢谢! 展开
展开全部
修改后代码如下
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void display_menu()
{
printf( " 欢迎使用!\n");
printf( "**************************\n" );
printf( "-*1. 倒计时 *-\n" );
printf( "**************************\n" );
}
struct Clock
{
int sec;
int min;
int hour;
//void tick();
//void show();
//void run();
//void set( int h, int m, int s );
};
void tick(struct Clock *a)
{
long t = time( NULL );
while ( t == time( NULL ) )
;
if ( --(a->sec) < 0 )
{
a->sec = 59;
if ( --(a->min) < 0 )
{
a->min = 59;
--(a->hour);
}
}
};
void show(struct Clock*a)
{
printf( "\r" );
if ( a->hour < 10 )
printf( "0" );
printf( "%d:", a->hour );
if ( a->min < 10 )
printf( "0" );
printf( "%d:", a->min );
if ( a->sec < 10 )
printf( "0" );
printf( "%d", a->sec );
};
void run(struct Clock*a)
{
while ( a->sec != 0 || a->min != 0 || a->hour != 0 )
{
tick(a);
show(a);
}
};
void set(struct Clock*a, int h, int m, int s )
{
a->hour = h;
a->min = m;
a->sec = s;
};
int count_down()
{
int s;
int m;
int h;
printf( "请输入时间(hh mm ss):" );
scanf( "%d%d%d", &h, &m, &s );
Clock *c=(struct Clock*)malloc(sizeof(struct Clock));
set(c, h, m, s );
run(c);
printf( "\n" );
printf( "time is over!!!\n" );
return(0);
}
int main()
{
int num;
display_menu();
scanf( "%d", &num );
switch ( num )
{
case 1: count_down(); break;
}
return 0;
}

2021-01-14 广告
在学习嵌入式之前,肯定要有C语言基础; 推荐创客学院的嵌入式课,有同学完感觉还不错; 我之前也是差点在实体学了,考虑到价格问题,只选择在网络上学,自我感觉还是不错的,每天学一个小时即可,强度也不需要太大! 更专业的解答可联系意法半导体,产品...
点击进入详情页
本回答由意法半导体(中国)投资有限公司提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询