C++中数字的N-TH根

问题描述

我正在尝试使用 pow 在 c++ 中计算数字的 n 根,所以我尝试按照以下方式进行

#include <cmath.h>
...
double n_root_of_a = pow(a,1.0/n);
...

但这不起作用,它给了我以下执行错误“进程返回 -1073741571 (0xC00000FD)”。但是如果我在在线编译器中运行相同的代码,它就可以正常工作。 我在 Windows 上使用带有 MINGW 的代码块,但不知道发生了什么。

编辑: 完整代码

#include <iostream>
#include <cstdlib>
#include <vector>
#include <stack>
#include <cmath>
#include <cctype>

const char ADD = '+';
const char SUB = '-';
const char MUL = '*';
const char DIV = '/';
const char POW = '^';
const char ROOT = '~';

using namespace std;

template<typename elem>
ostream& operator<<(ostream& os,const vector<elem>& vec)
{
    typename vector<elem>::const_iterator it = vec.begin();
    os << "{";
    while(it != vec.end())
    {
        os << *it;
        it++;
        if(it == vec.end())
            os << "}";
        else
            os << ",";
    }
    return os;
}

double add(double op1,double op2)
{
    return op1 + op2;
}

double sub(double op1,double op2)
{
    return op1 - op2;
}

double mul(double op1,double op2)
{
    return op1 * op2;
}

double div(double op1,double op2)
{
    return op1 / op2;
}

double pow(double op1,double op2)
{
    return pow(op1,op2);
}

double root(double op1,double op2)
{
    cout << op1 << endl;
    cout << op2 << endl;
    cout << 1.0/op2 << endl;
    cout << pow(4.0,0.5) << endl;
    return pow(op1,1.0/op2);
}



bool calculate(vector<char> rpn,double& res)
{
    vector<char>::iterator it = rpn.begin();
    stack<double> operands;
    bool error = false;
    while(it != rpn.end() && !error)
    {
        cout << "while with it: " << *it << endl;
        switch(*it)
        {
        case ADD:
            {
            if(operands.size() < 2)
                error = true;
            double op2 = operands.top();
            operands.pop();
            double op1 = operands.top();
            operands.pop();
            res = add(op1,op2);
            cout << "add with op1: " << op1 << ",op2: " << op2 << " y res: " << res << endl;
            operands.push(res);
            break;
            }
        case SUB:
            {
            if(operands.size() < 2)
                error = true;
            double op2 = operands.top();
            operands.pop();
            double op1 = operands.top();
            operands.pop();
            res = sub(op1,op2);
            cout << "sub with op1: " << op1 << ",op2: " << op2 << " y res: " << res << endl;
            operands.push(res);
            break;
            }
        case MUL:
            {
            if(operands.size() < 2)
                error = true;
            double op2 = operands.top();
            operands.pop();
            double op1 = operands.top();
            operands.pop();
            res = mul(op1,op2);
            cout << "mul with op1: " << op1 << ",op2: " << op2 << " y res: " << res << endl;
            operands.push(res);
            break;
            }
        case DIV:
            {
            if(operands.size() < 2)
                error = true;
            double op2 = operands.top();
            operands.pop();
            double op1 = operands.top();
            operands.pop();
            res = div(op1,op2);
            cout << "div with op1: " << op1 << ",op2: " << op2 << " y res: " << res << endl;
            operands.push(res);
            break;
            }
        case POW:
            {
            if(operands.size() < 2)
                error = true;
            double op2 = operands.top();
            operands.pop();
            double op1 = operands.top();
            operands.pop();
            res = pow(op1,op2);
            cout << "pow with op1: " << op1 << ",op2: " << op2 << " y res: " << res << endl;
            operands.push(res);
            break;
            }
        case ROOT:
            {
            if(operands.size() < 2)
                error = true;
            double op2 = operands.top();
            operands.pop();
            double op1 = operands.top();
            operands.pop();
            res = root(op1,op2);
            cout << "root with op1: " << op1 << ",op2: " << op2 << " y res: " << res << endl;
            operands.push(res);
            break;
            }
        default:
            {
            cout << "entra al default" << endl;
            int op;
            if (isdigit(*it))
                op = (int) *it - '0';
            else
                op = (int) *it - 'a' + 10;
            operands.push(op);
            break;
            }
        }
        it++;
    }
    if(operands.size() == 1){
        res = operands.top();
        return true;
    }
    return false;
}

char inttochar(int a) {
    if (a >= 0 && a <= 9)
        return '0' + a;
    else
        return 'a' + a - 10;
}


int main(int argc,char *argv[])
{
    cout << "Hello world!" << endl;
    double xd = pow(4.0,0.5); // <-------------- here it's chrashes
    cout << xd << endl;
    vector<char> operators{ADD,SUB,MUL,DIV,POW,ROOT};
    vector<int> numbers(0);
    for(int i=1; i<argc; i++)
    {
        numbers.push_back(atoi(argv[i]));
    }
    cout << operators << endl;
    cout << numbers << endl;

    vector<char> rpn{'4','2',ROOT};
    double result = -20.0;
    if(calculate(rpn,result))
        cout << result << endl;

    vector<char> aux(0);
    /*for(int i=0; i<7; i++) {
        for()
    }*/
    return 0;
}

解决方法

错误在于您在第 54 行重新定义了 pow,它调用了自身:

double pow(double op1,double op2)
{
    return pow(op1,op2);
}

这是一个无限递归函数。 您可以通过在重载函数中调用 std::pow 来修复。 或者干脆完全移除重载,因为它没有任何价值。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...