我目前正在尝试在C++中建模弹性碰撞。我有一个叫做粒子的结构,我用它来描述对象。修改一个结构数组的值
//Defining the particle structure
struct particle {
double x; // Position
double p; // Momentum
double im; // Inverse Mass
double v; // Velocity
double T; // Kinetic Energy
double a; // Radius of a Particle
};
我使用这些结构创建了一个数组。
particle AP[n+2];
我的程序的一部分需要使用时间变量和它们的速度增加每个结构的位置。
我使用这个代码
void LeapForward(struct particle AP[],double tfc,int n)
{
for(int cycle=0; cycle<n+2; cycle++)
{
double second;
second=AP[cycle].x+tfc*AP[cycle].v;
AP[cycle].x=second;
}
}
然而,看来正在修改的阵列心不是而是重新创建。我想知道是否有人知道这是为什么,如果是的话如何解决这个问题。
我的完整代码可以在下面找到。任何帮助将不胜感激。
//This program will be about particle collsions
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
//Function for collision of particles
void Collision(struct particle AP[],int chosen);
void Assignment(struct particle AP[],int n);
void LeapForward(struct particle AP[], double tfc, int n);
void TimeForCollision(struct particle AP[],double tfc, int chosen, int n);
//Defining the particle structure
struct particle {
double x; // Position
double p; // Momentum
double im; // Inverse Mass
double v; // Velocity
double T; // Kinetic Energy
double a; // Radius of a Particle
};
//Variable Declaration
int n; //Number of particles
double tfc; //This is going to be the time to leap forward
int chosen; //Which particles are to collide
//Main program
int main()
{
cout<<"How many particle do you want to model: ";
cin>>n;
particle AP[n+2];
//Assigning value to Particles
Assignment(AP,n);
cout<<endl<<"Two walls have been placed at x=0 and 20"<<endl;
//Assignment of Wall Data.
AP[0].im=0.000001,AP[n+1].im=0.000001;
AP[0].x=0,AP[n+1].v=0;
AP[0].x=0,AP[n+1].x=20;
AP[0].a=0,AP[n+1].a=0;
ofstream Port;
Port.open ("Positions.txt");
for(int model=0; model<2;model++)
{
//Time for next collision
TimeForCollision(AP,tfc,chosen,n);
LeapForward(AP,tfc,n);
Collision(AP,chosen);
for(int plot=0; plot<n+2;plot++)
{
Port<<AP[plot].x<<"\t";
}
Port<<endl;
}
}
void Collision(struct particle AP[],int chosen)
{
double combinemass=1/AP[chosen].im+1/AP[chosen+1].im;
double result1=(AP[chosen].v*(1/(AP[chosen].im)-1/(AP[chosen+1].im))+(2*AP[chosen+1].v*1/(AP[chosen+1].im)))/combinemass;
double result2=(AP[chosen+1].v*(1/(AP[chosen+1].im)-1/(AP[chosen].im))+(2*AP[chosen].v*1/(AP[chosen].im)))/combinemass;
AP[chosen].v=result1;
AP[chosen+1].v=result2;
}
void Assignment(struct particle AP[],int n)
{
for(int cycle=1;cycle<=n;cycle++)
{
cout<<"Data Input for particle number "<<cycle<<endl;
double mass;
cout<<"What is the position of the particle : ";
cin>>AP[cycle].x;
cout<<"What is the mass of the particle : ";
cin>>mass;
AP[cycle].im=1/mass;
cout<<"What is the velocity of the particle : ";
cin>>AP[cycle].v;
cout<<"What is the radius of the particle : ";
cin>>AP[cycle].a;
//Data calculation
AP[cycle].p=AP[cycle].v*mass;
AP[cycle].T=0.5*AP[cycle].v*mass*mass;
}
}
void LeapForward(struct particle AP[],double tfc,int n)
{
for(int cycle=0; cycle<n+2; cycle++)
{
double second;
second=AP[cycle].x+tfc*AP[cycle].v;
AP[cycle].x=second;
}
}
void TimeForCollision(struct particle AP[],double tfc, int chosen, int n)
{
double balance=0;
tfc=0;
for(int cycle=0;cycle<n+1;cycle++)
{
double placeh=abs(((AP[cycle+1].x-AP[cycle+1].a)-(AP[cycle].x+AP[cycle].a)))/(AP[cycle].v-AP[cycle+1].v);
if(placeh>tfc && placeh>0)
{
tfc=placeh;
chosen=cycle;
}
}
cout<<tfc<<endl;
}
请不要将您的问题中的代码更改为基于您收到的答案而工作的版本。它使您的整个问题无效。 – 2014-11-22 18:35:59
@CaptainObvlious你能证实我的回答是正确的吗?我认为这就是为什么他的功能没有修改这些值! – Quest 2014-11-22 18:43:42
对不起,我是新来的网站,并没有意识到。我很抱歉。 – SiberianSloth 2014-11-22 18:55:36