该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include
#include
#include
#include
int main()
{
double bodyA[5], bodyB[5], G, Am, Bm, q, pi, Aax, Aay, Bax, Bay, Sx, Sy;
int t, T;
G=0.5; t=1; q=1; pi=3.14;
srand(time(NULL));
printf("请输入模拟次数\n");
scanf("%d\n",T);
bodyA[0]=rand()%101;//半径
bodyA[1]=rand()%1001;;//坐标x
bodyA[2]=rand()%1001;//坐标y
bodyA[3]=(rand()%51)*pow(-1,rand());//速度x
bodyA[4]=(rand()%51)*pow(-1,rand());//速度y
bodyB[0]=rand()%101;//同上
bodyB[1]=rand()%1001;
bodyB[2]=rand()%1001;
bodyB[3]=(rand()%51)*pow(-1,rand());
bodyB[4]=(rand()%51)*pow(-1,rand());
printf("(%lf %lf)\n",bodyA[1],bodyA[2]);
printf("(%lf %lf)\n",bodyB[1],bodyB[2]);
for(t;t <= T;t=t++)
{
//质量
Am = 4*q*pi*bodyA[0]/3;
Bm = 4*q*pi*bodyB[0]/3;
//距离
Sx = bodyA[1]-bodyB[1];
Sy = bodyA[2]-bodyB[2];
//加速度
Aax = Bm*G/pow(Sx,2);
Aay = Bm*G/pow(Sy,2);
Bax = Am*G/pow(Sx,2);
Bay = Am*G/pow(Sy,2);
//加速度的方向判定
if(Sx > 0) Aax = -Aax;
else if(Sx < 0) Bax = -Bax;
else {Aax = 0; Bax = 0;}
if(Sy > 0) Aay = -Aay;
else if(Sy < 0) Bay = -Bay;
else {Aax = 0; Bay = 0;}
//新速度
bodyA[3] = bodyA[3]+Aax*t;
bodyA[4] = bodyA[4]+Aay*t;
bodyB[3] = bodyB[3]+Bax*t;
bodyB[4] = bodyB[4]+Bay*t;
//新坐标
bodyA[1] = bodyA[1]+bodyA[3]*t;
bodyA[2] = bodyA[2]+bodyA[4]*t;
bodyB[1] = bodyB[1]+bodyB[3]*t;
bodyB[2] = bodyB[2]+bodyB[4]*t;
printf("第%d次模拟\n",t);
printf("(%lf %lf)\n",bodyA[1],bodyA[2]);
printf("(%lf %lf)\n",bodyB[1],bodyB[2]);
}
return 0;
}