C++ 显示分段问题的递归函数C++;
这段代码是用于递归函数实践的。当我运行代码时,它在“POWER”cout行停止,然后我的编译器显示一个分段错误。沿着电源线的函数应该递归地将数字“a”提高到数字“b”的幂。我不知道怎么解决这个问题,有人能帮忙吗C++ 显示分段问题的递归函数C++;,c++,recursion,C++,Recursion,这段代码是用于递归函数实践的。当我运行代码时,它在“POWER”cout行停止,然后我的编译器显示一个分段错误。沿着电源线的函数应该递归地将数字“a”提高到数字“b”的幂。我不知道怎么解决这个问题,有人能帮忙吗 #include <iostream> #include <string> #include <vector> using namespace std; /**** Recursive backwards print, prints a string
#include <iostream>
#include <string>
#include <vector>
using namespace std;
/**** Recursive backwards print, prints a string starting from last index to first*****/
void printReverse(string s, int i)
{
if(i < s.size())
{
printReverse(s.substr(1), i);
cout<<s[i];
}
else
{
return;
}
}
/**** Recursive power function, computes a^b, where b can be positive or negative*****/
int recPower(double a, int b)
{
int i = b; //i = b, so int a can be multiplied int b times
if (i == 0) //base
return 1;
else //multiply A by B, B times
{
a *= b;
return recPower(a, b); //recursive
i--; //decrement i until it equals 0
}
}
/**** Recursive string replace, replaces all instances of a character in a string with another character*****/
string recReplace(string s2, int i, char old, char neW)
{
if(s2[i] == old) //search for old char
{
i = neW; //replace it
i++; //iterate i
}
recReplace(s2, i, old, neW); //call function
return s2;
}
/**** Recursive list find > Searches if x exists in list, returns true if found, false otherwise*****/
int recListFind(vector<int> v, int i, int x)
{
if(v[i] == x)
{
cout << x << " exists in the vector."<<endl;
i++;
recListFind(v, i, x);
}
return true;
}
int main()
{
cout << "PRINT REVERSE" << endl;
cout << "----------" << endl;
string s1 = "hello world";
cout << "String: " << s1 << endl;
cout << "Reversed: ";
printReverse(s1, 0);
cout << endl;
/* Computes a^b (power function) */
cout << "POWER" << endl;
cout << "----------" << endl;
int a = 2, b = -3;
cout << a << "^" << b << " = ";
cout << recPower(a, b) << endl;
cout << endl;
/* Replaces a character in a string with a new one */
cout << "REPLACE" << endl;
cout << "----------" << endl;
string s2 = "-h-e-l-l-o-";
char oldChar = '-';
char newChar = ' ';
cout << "String: " << s2 << endl;
cout << "> Replace '" << oldChar << "' with '" << newChar << endl;
recReplace(s2, 0, oldChar, newChar);
cout << "String: " << s2 << endl;
cout << endl;
/* Searches for value in vector */
cout << "FIND" << endl;
cout << "----------" << endl;
int x = 7;
cout << "Does " << x << " exist in the vector? "; vector<int> v = {5, 1, 6, 7, 9};
cout << recListFind(v, 0, 7) << endl;
cout << endl;
return 0;
}
#包括
#包括
#包括
使用名称空间std;
/****递归向后打印,打印从最后一个索引到第一个索引的字符串*****/
void printReverse(字符串s,int i)
{
如果(i cout问题很简单,您正在使用b
执行recPower
函数。在该函数中,如果b
不是0,则使用未修改的b
值调用recPower
(同时修改a
)。这将始终以无限递归结束,这将使堆栈溢出
解决办法可以是:
int recPower(double a, int b, int times) {
if (times == 0)
return a;
else
return b * recPower(a, b, --times);
}
int recPower(double a, int b) {
return recPower(a, b, b);
}
<> P>即使你解决了这个问题,你也有另一个问题。<代码> B<代码>可以是否定的,基于你的逻辑将在递减时继续递归,直到它溢出并返回到0。你将用第一个测试用例导致这个情况。你应该考虑这个函数中允许的类型,考虑使它们未签名,或者DEA。用否定的b
案例显式使用ling。同样适用于recReplace
函数。From clang cl:warning:通过此函数的所有路径都将称自己为[-Winfinite recursion]@AdrianMole是的,但问题只是(或至少应该是)关于第一个。我认为这些问题应该去掉其余的。