2012-08-02 48 views
0

我在C++中創建了一個無效的環境函數中的向量調用核和單元。然後我調用創建病毒點向量的病毒函數。然後我想訪問病毒調用的另一個功能中的載體細胞核和細胞。是否有可能在不通過病毒功能的情況下調用病毒?如果不是什麼是最好的辦法做到這一點?附件是我的代碼。先謝謝了。我也沒有添加的所有代碼的函數內...只通過代碼中的某些功能傳遞向量

struct point { 
     float x; 
     float y; 
    }; 
    struct space{ 
     point point1; 
     point point2; 
     point point3; 
     point point4; 
    }; 

     int gelato(vector<point> & virus,int& times,int& iv, ofstream& data_file) 
    { 
     int a; 
     int b; 

     bool nc,cc,cmc; 
     for(int i =0; i<virus.size(); i++) 
     { 
      a = virus[i].x; 
      b = virus[i].y; 

      nc = nucleus_check(nucleus,a,b); // **Need to call vector cell and nucleus** 
      cc = cytoplasm_check(cell,a,b); 
      cmc = cell_membrane_check(cell,a,b); 
     } 
    } 

int moves(char n) 
{ 
    int moved; 
    int trial; 

    if(n =='A') 
    { 
     moved = rand()%4; 
    } 
    else if(n=='B') 
    { 
     moved= rand()%3+1; 
    } 
    else if(n=='C') 
    { 
     trial= rand()%4; 
     if(trial ==1) 
     { 
      moves('C'); 
     } 
     else 
     { 
      moved = trial; 
     } 
    } 
    else if(n =='D') 
    { 
     trial = rand()%4; 
     if(trial == 2) 
     { 
      moves('D'); 
     } 
     else 
     { 
      moved = trial; 
     } 
    } 
    else if(n=='E') 
    { 
     moved = rand()%3; 
    } 

    return moved; 
} 
int v_move(vector<point>& virus, int& iv, ofstream& data_file) 
{ 

     gelato(virus,times,iv,data_file); 
} 
int rand_in(char a) 
{} 
void virus(ofstream& data_file) 
{ 

    v_move(virus, iv, data_file); 
} 
void cell_space(int r) 
{ 

    vector<point>cell; 
} 
void nucleus_space(int r) 
{ 

    vector<point>nucleus; 
} 

void environment() //**Where the vector nucleus and cell are created** 
{ 
    //cell 
    cell_space(16) 

    //nucleus 
    nucleus_space(4); 
    //cout<<"Environment"<<endl; 
} 
int main() 
{ 
    srand(time(NULL)); 
    data_file.open("data.csv"); 
    environment(); 
    virus(data_file); 

    return 0; 
} 

回答

0

我假設你還沒有學會/掌握面向對象編程(OOP),以及您想使用來實現你的程序procedural programming paradigm。這很好,因爲C++支持多種範例,並且不會強制您使用OOP。

由於「細胞空間」和「核空間」是指將您撥打的「環境」的實體一起使用時,你應該定義一個environment結構,結合了兩個:

struct environment 
{ 
    vector<point> cells; 
    vector<point> nuclei; 
}; 

功能,準備環境看起來是這樣的:

void prepare_environment(environment& env, // environment passed by reference 
         int cellCount, int nucleusCount) 
{ 
    prepare_cells(env.cells, cellCount); 
    prepare_nuclei(env.nuclei, nucleusCount); 
}; 

void prepare_cells(std::vector<point>& cells, // cell vector passed by reference 
        int cellCount) 
{ 
    cells.resize(cellCount); 
    // Do other stuff to initialize cells 
} 

void prepare_nuclei(std::vector<point>& nuclei, int cellCount) {...} 

在你的主,你通過環境結構需要操作它的功能:

int main() 
{ 
    Environment env; 

    prepare_environment(env, 16, 4); 
    move_virusus(env); 
} 

在程序編程範例中,數據與對該數據進行操作的程序保持分離。數據不斷作爲幫助函數鏈中的參數傳遞是正常的。抵制將數據存儲在全局變量中的誘惑,並讓您的函數直接訪問這些全局變量。全局變量導致代碼難以測試,難以閱讀並且難以重複使用。

您應該在執行動作後指定您的程序。名稱中應該有某種動作動詞,例如prepare_environment。如果該過程對某種數據進行操作,則該數據的名稱成爲該動詞的對象,例如, prepare_environment

相反,你應該在你的結構在他們建模的實體之後命名。名稱中應該有一個名詞,例如environment

一旦你瞭解了過程編程的原理,我想你會發現它更容易理解面向對象編程。