我试图用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
进行修复。或者简单地完全消除过载,因为它没有提供任何价值。