2017-02-10 46 views
-2

我正在編寫一個具有遞歸函數的代碼。但它執行兩個時間,其實我知道原因是什麼,但我想阻止它,請幫助...如何破解函數返回鏈

int getMatrices() 
{ 
    cout<<"Enter The Number Of Rows Of First Matrix : "; 
    cin>>row_a; 
    cout<<"Enter The Number Of Column Of First Matrix : "; 
    cin>>column_a; 
    cout<<"Enter The Number Of Rows Of Second Matrix (Can Not Be Different From Number Of Column Of First): "; 
    cin>>row_b; 
    cout<<"Enter The Number Of Column Of First Matrix : "; 
    cin>>column_b; 
    this->checkPhysibility(column_a, row_b); 

    cout<<"\nEnter the first matrix : \n"; 
    fsor(int i=0;i<row_a;i++) 
     for(int j=0;j<column_a;j++) 
     { 
      cout<<"\tA("<<i+1<<", "<<j+1<<") : "; 
      cin>>a[i][j]; 
     } 
    cout<<"\nEnter the second matrix : \n"; 
    for(int i=0;i<row_b;i++) 
     for(int j=0;j<column_b;j++) 
     { 
      cout<<"\tB("<<i+1<<", "<<j+1<<") : "; 
      cin>>b[i][j]; 
     } 
} 
int checkPhysibility(int column_a, int row_b) 
{ 
    if(column_a!=row_b) 
    { 
     cout<<"\n\nYou Entered Wrong Input, The Number Of Column Of First Matrix Can Not Be Different From Row Of Second Matrix For Making Their Multiplication Physible. Please Re-Enter The Values.\n\n"; 
     this->getMatrices(); 
    } 

} 

調用它去檢查它checkPhysibility方法之後,但如果if語句進去,然後再執行getMatrices它做了一個遞歸鏈,並且getMatrices函數中的代碼(在checkPhysibility調用之後)執行了兩次我想要中斷的代碼。 return語句不起作用....

+2

這不是** C **。 – Michi

+1

您的代碼有語法錯誤。你能確定你發佈了真實的代碼嗎?你也應該得到一些警告,比如'checkPhysibility'永遠不會返回一個整數。修復它們,然後看看你是否仍然有問題。最後,向我們展示一個工人階級。 – Schwern

回答

2
  1. 將返回類型checkPhysibility更改爲bool
  2. 我沒有看到需要在該函數中編寫任何消息。由於getMatrice具有獲取數據和寫入消息的所有代碼,因此在該函數中打印消息更有意義。
  3. 更改getMatrices返回,如果checkPhysibility返回false
  4. 在此期間,請將checkPhysibility更改爲checkFeasibility
bool checkFeasibility(int column_a, int row_b) 
{ 
    return (column_a == row_b); 
} 

int getMatrices() 
{ 
    cout<<"Enter The Number Of Rows Of First Matrix : "; 
    cin>>row_a; 
    cout<<"Enter The Number Of Column Of First Matrix : "; 
    cin>>column_a; 
    cout<<"Enter The Number Of Rows Of Second Matrix (Can Not Be Different From Number Of Column Of First): "; 
    cin>>row_b; 
    cout<<"Enter The Number Of Column Of First Matrix : "; 
    cin>>column_b; 
    if (false == checkFeasibility(column_a, row_b)) 
    { 
     cout<<"\n\nYou Entered Wrong Input, The Number Of Column Of First Matrix Can Not Be Different From Row Of Second Matrix For Making Their Multiplication Physible. Please Re-Enter The Values.\n\n"; 
     // ??? 
     return 0; 
    } 

    cout<<"\nEnter the first matrix : \n"; 
    fsor(int i=0;i<row_a;i++) 
     for(int j=0;j<column_a;j++) 
     { 
     cout<<"\tA("<<i+1<<", "<<j+1<<") : "; 
     cin>>a[i][j]; 
     } 
    cout<<"\nEnter the second matrix : \n"; 
    for(int i=0;i<row_b;i++) 
     for(int j=0;j<column_b;j++) 
     { 
     cout<<"\tB("<<i+1<<", "<<j+1<<") : "; 
     cin>>b[i][j]; 
     } 

    // ??? 
    return 1; 
} 
0

簡短的回答是由一個在每次調用傳遞一個「深度」 parameter.Increment。然後你知道這個函數在哪個層次上。

長的答案是,這不是一個很好的方式來使用遞歸。如果用戶輸入錯誤,跳回一個循環重新輸入。至少在C/C++中,不要嘗試使用遞歸進行循環。一些編程Lisp的人會以不同的方式告訴你。