模拟自由落体下落,弹回程序.
我觉得编程并不是一件枯燥的事情,自己可以利用所学的其它科的知识编一些有趣的程序,像下面的程序就不光用到编程知识,还用到高中的自由落体知识,当我们把所学知识都学以致用时,我们才是真正的掌握了知识.方法1
#include<graphics.h>
#include<dos.h>
#define g 10
main()
{int x=320,y,t;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"");
cleardevice();
setcolor(4);
do{
for(t=0;t<10;t++)
{y=0.5*g*t*t;
circle(x,y,20);
delay(40000);
cleardevice();
}
for(t=0;t<10;t++)
{y=405-(90*t-0.5*g*t*t);
circle(x,y,20);
delay(40000);
cleardevice();
}
}while(!kbhit());
closegraph();
}
方法2
#include<graphics.h>
#include<dos.h>
#define g 10
main()
{int x=320,y,t;
int size;
int gdriver=DETECT,gmode;
void *buffer;
initgraph(&gdriver,&gmode,"");
cleardevice();
circle(320,10,10);
size=imagesize(310,0,330,20);
buffer=malloc(size);
getimage(310,0,330,20,buffer);
cleardevice();
do{
for(t=0;t<10;t++)
{y=0.5*g*t*t;
putimage(x,y,buffer,COPY_PUT);
delay(40000);
cleardevice();
}
for(t=0;t<10;t++)
{y=405-(90*t-0.5*g*t*t);
putimage(x,y,buffer,COPY_PUT);
delay(40000);
cleardevice();
}
}while(!kbhit());
closegraph();
}