简体   繁体   English

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

[英]string subscript out of range error when assigning variable to function

In order to become more familiar with cpp I began making a program that takes the derivative of simple polynomials using the power rule.为了更加熟悉 cpp,我开始编写一个程序,该程序使用幂规则对简单多项式进行导数。 So far, it is working fine for polynomials such as 5x^2+4x .到目前为止,它适用于多项式,例如5x^2+4x However, if the polynomial contains a constant (like 5x + 3 ) I get a string subscript out of range error.但是,如果多项式包含一个常数(如5x + 3 ),我会得到一个字符串下标超出范围错误。 I used the debugger and found the error triggers on line 33 ( std::string term = differentiateTerm(*iter); ).我使用调试器并在第 33 行( std::string term = differentiateTerm(*iter); )上找到了错误触发器。 I'm not exactly sure what I'm doing wrong here and I would appreciate any help.我不确定我在这里做错了什么,我将不胜感激。

Full code:完整代码:

#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;
}

You're not checking the return value from find correctly in differentiateTerm .您没有在 distinctTerm 中正确检查find的返回differentiateTerm This causes inputTerm[varPos] to access out of bounds.这会导致inputTerm[varPos]访问越界。

The correct check is正确的检查是

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

In the subsequent if statements, since you have a valid subscript in varPos , you should check that against the size of the string.在随后的if语句中,由于您在varPos中有一个有效的下标,您应该根据字符串的大小检查它。 So you'd have if (varPos > 0) , if (varPos < inputTerm.size() - 1) , etc.所以你会有if (varPos > 0)if (varPos < inputTerm.size() - 1)等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM