1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int main()
{
FILE *input;
int n, *num, i, total, j;
if((input=fopen("data.txt", "r"))!=NULL)
{
fscanf(input, "%d", &n);
total=n*7;
num=(int *) malloc(total*sizeof(int));
if(num!=NULL)
{
for(i=0; i<total; i++)
{
fscanf(input, "%d", &num[i]);
}
fclose(input);
clock_t start,finish, previous;
double step;
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
system ("pause");
exit(1);
}
start=clock();
previous=start;
j=0;
do
{
for(i=0; i<=n; i++)
{
finish = clock();
step = (finish-previous)*1.0/CLOCKS_PER_SEC;
if (step >= 0.03)
{
previous = finish;
setfillstyle(SOLID_FILL,BLACK);
setcolor(BLACK);
fillellipse(num[j],num[j+1],num[j+4],num[j+4]);
num[j]+= num[j+5]*step;
num[j+1]+= num[j+6]*step;
if (num[j]+num[j+4]>=getmaxx() || num[j]-num[j+4]<=0)
num[j+5] *= -1;
if (num[j+1]+num[j+4]>=getmaxy() || num[j+1]-num[j+4]<=0)
num[j+6] *= -1;
setfillstyle(SOLID_FILL,RED);
setcolor(RED);
fillellipse(num[j],num[j+1],num[j+4],num[j+4]);
}
}
}
while (!kbhit());
closegraph();
}
}
}
else
{
printf("Could not allocate memory\n\n");
}
}
else
{
printf("Could not open file\n\n");
}
system("pause");
return 0;
}
|