C++ 如何打破函数返回链
我正在编写一个具有递归函数的代码。但它执行了两次,实际上我知道为什么会发生,但我想阻止它,请帮助C++ 如何打破函数返回链,c++,C++,我正在编写一个具有递归函数的代码。但它执行了两次,实际上我知道为什么会发生,但我想阻止它,请帮助 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; co
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();
}
}
int getMatrix()
{
库特罗瓦;
coutcolumn_a;
库特罗布;
coutcolumn_b;
这->检查物理性(列a,行b);
库特
将checkPhysibility
的返回类型更改为bool
我不认为有必要在该函数中写入任何消息。由于getMatrice
拥有获取数据和写入消息的所有代码,因此在该函数中打印消息更有意义
将getMatrix
更改为返回ifcheckPhysicability
返回false
当您进行此操作时,将检查物理性
更改为检查可行性
bool检查可行性(int列a,int行b)
{
返回(列a==行b);
}
int getMatrix()
{
库特罗瓦;
coutcolumn_a;
库特罗布;
coutcolumn_b;
if(false==检查可行性(列a,行b))
{
cout简单的答案是传递一个“depth”参数。每次调用时递增一个。然后您就知道函数处于哪个级别
P> >长期的答案是,这不是一个使用递归的好方法。如果用户输入错误,请跳回循环,重新输入它。不要尝试用递归来循环,至少在C/C++中。有些LISP程序的人会告诉你不同的。< /P>这不是C。你的代码有语法错误。你能确定你正在发布真正的代码吗?哟。你还应该得到一些警告,比如checkPhysicability
从不返回整数。将它们全部修复,然后查看是否仍然存在问题。最后,向我们展示一个工作类。
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;
}