前几天买了一本《编程之美》,今天上英语课偷偷的看一几眼,感觉真的不错,但是第一章就把我难住了,估计是自己windows编程方面的只是不足,实现了一下:
#include <Windows.h>
#include <stdlib.h>
#include <math.h>
const int SAMPLING_COUNT = 200;//设置采样数
const double PI = 3.1415926535;
const int TOTAL_AMPLITUDE = 300;//最大的振幅值,也就是过多长时间间隔采样一次,因为就是运行时间的分配,所以可以把它看做振幅
int main()
{
DWORD busy[SAMPLING_COUNT];
int amplitude = TOTAL_AMPLITUDE / 2;//振幅基量,因为不为负,相当于将图像向上平移。
double step = 0.07;//步长
double radian = 0.0;//幅度
for (int i = 0; i < SAMPLING_COUNT; i++)
{
busy[i] = amplitude + (sin(radian * PI) * amplitude);
radian += step;
}
DWORD startTime = 0;
for (int j = 0;;j = (j + 1) % SAMPLING_COUNT)
{
startTime = GetTickCount();
while((GetTickCount() - startTime) <= busy[j])
;
Sleep(TOTAL_AMPLITUDE - busy[j]);
}
return 0;
}
运行结果:
坚持看完这本书!