在这里向大家介绍在C++中延时函数的编写及调用,并以逐行展开点阵显示的“武汉加油”字样作为演示。
程序运行效果
首先将实现效果展示如下:
延时函数介绍
简要介绍一下利用C++中<time.h>库完成函数延时功能,语句较为简单在此不做特殊说明。
#include <time.h>
void delay(int seconds)
{
clock_t start = clock();
clock_t lay =(clock_t)seconds *CLOCKS_PER_SEC;
while((clock()-start) < lay);
}
完整代码
完整代码如下:
#include <iostream>
#include <time.h>
void delay(int seconds);
int main()
{
using namespace std;
cout << "*" << endl;
delay(1);
cout << "*" << endl;
cout << endl;
cin.get();
cout << " *** ** *** \n"; delay(1);
cout << " *** * ** ** *** *** \n"; delay(1);
cout << " *** **** *** ************ *** ** ** \n"; delay(1);
cout << " ******** *** *** *** *************** *** **** ** \n"; delay(1);
cout << " ******* ** * ** *** *** * ** ** \n"; delay(1);
cout << " ** ****** **** *** *** *** ** ** ** ** \n"; delay(1);
cout << " ************************** * ** *** *** ****************** ************ * *** ** ******\n"; delay(1);
cout << "************ *** **** ** *** ** ******* ** *** ** *** ********************** \n"; delay(1);
cout << " *** *** *** ** *** *** *** ** ** ** **** ** ** ** ** \n"; delay(1);
cout << " *** ** * ** ****** *** ** ** ** * ** ** ** ** \n"; delay(1);
cout << " *** ******* ** *** **** ** ** ** ** ** ** ** ** ** \n"; delay(1);
cout << " *** ******* ** *** **** ** ** ** ** ** ******************* \n"; delay(1);
cout << " *** *** ** ** *** *** ** ** ** ** ** ******************* \n"; delay(1);
cout << " *** *** *** ** ** ** *** *** ** ** *********** ** ** ** *** \n"; delay(1);
cout << " *** ******* ** ** *** ** *** *** ** * ** *** ** *** ** ** *** \n"; delay(1);
cout << " ********* ** ** ***** *** *** ** *** *** ** ****** ** ** **** *** \n"; delay(1);
cout << " ***** **** ** ** ** *** ** ***** **** ******************* \n"; delay(1);
cout << " ***** * ** ********* ** ** * ** ** \n"; delay(1);
cout << " ** ** * * * * \n"; delay(1);
cin.get();
cin.get();
return 0;
}
void delay(int seconds)
{
clock_t start = clock();
clock_t lay =(clock_t)seconds *CLOCKS_PER_SEC;
while((clock()-start) < lay);
}
程序运行时最好全屏播放,以免因宽度问题出现乱码。
抗疫关键期,感谢医护人员付出,祝各位博友身体健康,愿我们早日战胜疫情。