我正在編寫一個具有遞歸函數的代碼。但它執行兩個時間,其實我知道原因是什麼,但我想阻止它,請幫助...如何破解函數返回鏈
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語句不起作用....
這不是** C **。 – Michi
您的代碼有語法錯誤。你能確定你發佈了真實的代碼嗎?你也應該得到一些警告,比如'checkPhysibility'永遠不會返回一個整數。修復它們,然後看看你是否仍然有問題。最後,向我們展示一個工人階級。 – Schwern