将变量分配给函数时出现字符串下标超出范围错误

string subscript out of range error when assigning variable to function

为了更加熟悉cpp,我开始编写一个程序,使用幂法则对简单多项式求导。到目前为止,它对 5x^2+4x 等多项式工作正常。但是,如果多项式包含常数(如 5x + 3),我会得到一个字符串下标超出范围的错误。我使用了调试器并在第 33 行 (std::string term = differentiateTerm(*iter);) 上找到了错误触发器。我不太确定我在这里做错了什么,如果有任何帮助,我将不胜感激。

完整代码:

#include <iostream>
#include <string>
#include <vector>

std::vector<std::string> terms;
std::vector<std::string>::const_iterator iter;

std::string takeDerivative(std::string expression);
void separateTerms(std::string expression);
std::string differentiateTerm(std::string inputTerm);

int main()
{
    std::string userExpression;

    std::cout << "Enter a polynomial.\n";
    std::cin >> userExpression;
    
    std::string outputExpression = takeDerivative(userExpression);

    std::cout << "The derivative of your expression is: " + outputExpression;
    return 0;
}

std::string takeDerivative(std::string expression)
{
    std::string derivative;

    separateTerms(expression);

    for (iter = terms.begin(); iter != terms.end(); iter++)
    {
        std::string term = differentiateTerm(*iter);
        if (iter - terms.begin() == 0)
        {
            derivative = term;
        }
        else
        {
            derivative += "+" + term;
        }

    }

    return derivative;
}

void separateTerms(std::string expression)
{
    int previousSign = 0;
    bool firstTerm = true;

    for (int i = 0; i < expression.size() + 1; i++)
    {
        if (expression[i] == '+' || expression[i] == '-')
        {
            if (firstTerm)
            {
                terms.push_back(expression.substr(0, i));
                firstTerm = false;
                previousSign = i;
            }
            else
            {
                terms.push_back(expression.substr(previousSign + 1, i - previousSign - 1));
                previousSign = i;
            }
        }
        else if (i == expression.size())
        {
            if (firstTerm)
            {
                terms.push_back(expression.substr(previousSign, i));
            }
            else
            {
                terms.push_back(expression.substr(previousSign + 1, i - previousSign));
            }
        }
    }
}

std::string differentiateTerm(std::string inputTerm)
{
    std::string outputTerm;
    int coefficient = 1;
    int exponent = 1;

    int varPos = inputTerm.find('x');

    if (inputTerm[varPos] == std::string::npos)
    {
        outputTerm = "0";
        return outputTerm;
    }
    else {
        if (inputTerm[varPos - 1] != std::string::npos)
        {
            coefficient = std::stoi(inputTerm.substr(0, varPos));
        }
        if (inputTerm[varPos + 1] == '^')
        {
            if (inputTerm[varPos + 2] != std::string::npos)
            {
                exponent = std::stoi(std::string(1, inputTerm[varPos + 2]));
            }
        }
    }

    coefficient = coefficient * exponent;
    exponent--;

    if (exponent <= 0)
    {
        outputTerm = std::to_string(coefficient);
    }
    else if (exponent == 1)
    {
        outputTerm = std::to_string(coefficient) + "x";
    }
    else
    {
        outputTerm = std::to_string(coefficient) + "x^" + std::to_string(exponent);
    }

    return outputTerm;
}

您没有在 differentiateTerm 中正确检查 find 中的 return 值。这会导致 inputTerm[varPos] 访问越界。

正确的检查是

if (varPos == std::string::npos)

在随后的 if 语句中,由于您在 varPos 中有一个有效的下标,您应该根据字符串的大小检查它。所以你会有 if (varPos > 0)if (varPos < inputTerm.size() - 1)