You need to enable JavaScript to run this app. C++递归练习代码运行至POWER输出行触发段错误求修复
优惠活动
大模型
产品
解决方案
定价
更多

C++递归练习代码运行至POWER输出行触发段错误求修复

核心段错误根因
  • 触发崩溃的直接原因是recPower函数无限递归耗尽栈空间:递归调用时始终传入未修改的b值,用来计数的i--写在return语句之后,永远无法执行。当传入的b不为0时(如示例中的b=-3),永远无法命中i==0的递归终止基准条件,栈空间被持续消耗直到触发段错误。
  • 其余三个递归函数存在同类逻辑缺陷,后续运行也会触发越界、栈溢出类崩溃:
    • recReplace:将字符串索引i错误赋值为替换字符neW,直接导致索引跳转到内存越界位置;缺少统一的索引自增逻辑,非目标字符分支会陷入无限递归。
    • recListFind:缺少递归终止条件,索引遍历到vector边界后仍会继续访问,触发越界;无论是否找到目标值都直接返回true,完全不符合查找逻辑。
各函数修复方案

递归幂函数(recPower)

  • 修正递归参数传递逻辑:每次递归将指数值做减1操作传入,保证能命中终止条件
  • 补充负指数计算规则:负指数等价于1/(a^|b|),递归时先处理指数符号
  • 调整返回值类型为double,匹配负指数的小数计算结果,避免精度丢失
    修复后的函数实现:
double recPower(double a, int b)
{
    // 基准条件:任意数的0次幂为1
    if (b == 0)
        return 1.0;
    // 负指数转换:取正指数结果的倒数
    if (b < 0)
        return 1.0 / recPower(a, -b);
    // 正指数递归:a * a^(b-1)
    return a * recPower(a, b - 1);
}

递归字符串替换(recReplace)

  • 补充递归终止条件:当索引i超出字符串长度时,返回处理后的最终字符串
  • 修正字符替换逻辑:匹配到目标字符时直接修改字符串对应位置的字符,不再错误修改索引值i
  • 统一索引自增逻辑:不管当前位置是否匹配旧字符,索引都逐步向后移动,避免无限递归
  • 接住递归返回的修改后字符串,保证替换结果逐层传递
    修复后的函数实现:
string recReplace(string s2, int i, char old, char neW)
{
    // 递归终止:索引超出字符串长度
    if (i >= s2.size())
        return s2;
    // 匹配到旧字符直接替换
    if (s2[i] == old)
        s2[i] = neW;
    // 索引后移,递归处理下一个字符,接住返回的结果
    return recReplace(s2, i + 1, old, neW);
}

递归向量查找(recListFind)

  • 补充递归终止条件:当索引i超出vector长度时,返回false表示未找到目标
  • 匹配到目标值时立即返回true,不再继续递归遍历
  • 未匹配到目标值时,索引自增递归查找下一个元素
    修复后的函数实现:
bool recListFind(vector<int> v, int i, int x)
{
    // 递归终止:索引超出vector范围,未找到目标
    if (i >= v.size())
        return false;
    // 匹配到目标直接返回true
    if (v[i] == x)
        return true;
    // 未匹配,递归查找下一个元素
    return recListFind(v, i + 1, x);
}

递归倒序打印字符串(printReverse)

原函数虽能输出倒序结果,但每次递归都拷贝完整字符串、固定传入索引0,空间效率极低。优化为直接按索引从尾向头递归:
优化后的函数实现:

void printReverse(string s, int i)
{
    // 递归终止:索引小于0时停止
    if (i < 0)
        return;
    cout << s[i];
    printReverse(s, i - 1);
}
// 调用时传入起始索引为s.size()-1即可
完整可运行修复代码
#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 < 0)
    return;
  cout << s[i];
  printReverse(s, i - 1);
}


/****   Recursive power function, computes a^b, where b can be positive or negative*****/
double recPower(double a, int b)
{
    if (b == 0)
        return 1.0;
    if (b < 0)
        return 1.0 / recPower(a, -b);
    return a * recPower(a, b - 1);
}


/****   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 (i >= s2.size())
        return s2;
    if (s2[i] == old)
        s2[i] = neW;
    return recReplace(s2, i + 1, old, neW);
}


/****   Recursive list find   > Searches if x exists in list, returns true if found, false otherwise*****/
bool recListFind(vector<int> v, int i, int x)
{
    if (i >= v.size())
        return false;
    if (v[i] == x)
        return true;
    return recListFind(v, i + 1, x);
}


int main()
  {     
    cout << "PRINT REVERSE" << endl;
    cout << "----------" << endl;    
    string s1 = "hello world";    
    cout << "String: " << s1 << endl;    
    cout << "Reversed: ";    
    printReverse(s1, s1.size()-1);       
    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;    
    s2 = 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) ? "Yes" : "No") << endl;    
    cout << endl;    
  return 0;
}

内容的提问来源于stack exchange,提问作者leslie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.17 03:06:04