2014-11-22 2 views
-2

현재 C++에서 탄성 충돌을 모델링하려고합니다. 객체를 설명 할 때 사용하는 Particle이라는 구조가 있습니다.strucutres의 배열 값을 수정하십시오.

//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; 
} 
+2

질문에있는 코드를받은 답변을 기반으로 작동하는 버전으로 변경하지 마십시오. 전체 질문을 무효화합니다. –

+0

@Captain 궁금한 점이 있다면 내 대답이 맞는지 확인할 수 있습니까? 이것이 그의 기능이 가치를 수정하지 않는 이유라고 생각합니다! – Quest

+0

죄송합니다. 사이트를 처음 접해 보았지만 깨닫지 못했습니다. 내 사과. – SiberianSloth

답변

0

.

이 같은 기준으로 값을 전달 :

void LeapForward(struct particle *AP, double tfc, int n); 

또한, 귀하의 LeapForward 기능에 임시 더블 변수를 만들 필요가 없습니다. 당신은 같은 것을 사용할 수 있습니다

AP[cycle].x += tfc * AP[cycle].v; 

Live demo.

+0

@ SiberianSloth Ups 미안 아래에 썼습니다. 내 코드를 업데이트했습니다. 이제 작동합니다. – Quest

관련 문제