gpt4 book ai didi

c++ - 动态数组 C++ 多项式类

转载 作者:行者123 更新时间:2023-11-28 02:49:37 33 4
gpt4 key购买 nike

我正在尝试在 C++ 中构建一个动态数组,以使用项目的多项式类。我是 C++ 的新手,我很迷茫。我相信我已经正确分配了内存,但是我的析构函数遇到了问题,说“正​​在释放的内存没有分配。”如果我将其注释掉,它会起作用,但在那之后我迷路了。有什么想法吗?

#ifndef __Chapter9Program__Polynomial__
#define __Chapter9Program__Polynomial__

#include <iostream>
using namespace std;

class Polynomial
{
public:
Polynomial(int deg, int coeff[]);
Polynomial operator+(Polynomial other);
Polynomial operator-(Polynomial other);
Polynomial operator*(Polynomial other);
ostream& operator<<(ostream& os);//, const Polynomial& object);
const int getDegree();
const int getCoeff(const int index);
double evaluateAt(double x); //Finds P(x)
void show();
string print();
// destructor
~Polynomial();
private:
int degree;
int *coefficient; //Alllocate memory
};

#endif /* defined(__Chapter9Program__Polynomial__) */


#include "Polynomial.h"
using namespace std;
#include <iostream>

//Constructor
Polynomial::Polynomial(int deg, int coeff[])
{
degree = deg;
coefficient = new int [degree];
for( int i = 0; i <= degree; i++)
{
coefficient[i] = coeff[i];
}
}

//Destructor
Polynomial::~Polynomial()
{
if( coefficient )
{
delete [] coefficient;
coefficient = NULL;
}
}

//finds P(x)
double Polynomial::evaluateAt(double x)
{
double sum = 0.0;
double xPow = 1.0;
if( coefficient )
for(int i=0; i<degree; i++)
{
sum += xPow*coefficient[i];
xPow *= x;
}

return sum;
}

Polynomial Polynomial::operator+(Polynomial other)
{
int first[degree];
int second[degree];
for(int i = 0; i< degree; i++)
{
first[i] = 0;
second[i] = 0;
}
int newDeg;
int temp[degree];
int final[degree];
int final2[degree];
if(degree > other.getDegree())
{
newDeg = degree;
}
else
{
newDeg = other.getDegree();
}
if(degree > other.getDegree())
{
for(int i=0;i<=degree;i++)
{
first[i] = coefficient[degree-i];
}
for(int i = 0; i <= other.getDegree(); i++)
{
second[i] = other.getCoeff(other.getDegree()-i);
}
for(int i = 0; i<degree; i++)
{
temp[i] = first[i]+second[i];
}
for(int i=degree-1; i>=0; i--)
{
final[i] = temp[i-i];
}
for(int i=0;i<=newDeg;i++)
{
final2[i] = final[(degree-1)-(newDeg-i)];
}
}
else if(degree == other.getDegree())
{
for(int i=0;i<=degree;i++)
{
first[i] = coefficient[i];
}
for( int i = 0; i <= other.getDegree(); i++)
{
second[i] = other.getCoeff(i);
}
for(int i = 0; i<degree; i++)
{
final[i] = first[i]+second[i];
}
for(int i=0;i<=newDeg;i++)
{
final2[i] = final[(degree - 1)-(newDeg-i)];
}
}
else
{
for( int i = 0; i <= other.getDegree(); i++)
{
second[i] = other.getCoeff(other.getDegree()-i);
}
for(int i = 0; i <= degree; i++)
{
first[i] = coefficient[degree-i];
}
for(int i = 0; i<degree; i++)
{
temp[i] = first[i]+second[i];
}
for(int i=degree-1; i>=0; i--)
{
final[i] = temp[i-1];
}
for(int i=0;i<=newDeg;i++)
{
final2[i] = final[(degree - 1)-(newDeg-i)];
}
}
return Polynomial(newDeg,final2);
}

Polynomial Polynomial::operator-(Polynomial other)
{
int first[degree];
int second[degree];
for(int i = 0; i< degree; i++)
{
first[i] = 0;
second[i] = 0;
}
int newDeg;
int temp[degree];
int final[degree];
int final2[degree];
if(degree > other.getDegree())
{
newDeg = degree;
}
else
{
newDeg = other.getDegree();
}
if(degree > other.getDegree())
{
for(int i=0;i<=degree;i++)
{
first[i] = coefficient[degree-i];
}
for(int i = 0; i <= other.getDegree(); i++)
{
second[i] = other.getCoeff(other.getDegree()-i);
}
for(int i = 0; i<degree; i++)
{
temp[i] = first[i]-second[i];
}
for(int i=0; i<degree; i++)
{
final[i] = temp[(degree-1)-i];
}
for(int i=0;i<=newDeg;i++)
{
final2[i] = final[(degree - 1)-(newDeg-i)];
}
}
else if(degree == other.getDegree())
{
for(int i=0;i<=degree;i++)
{
first[i] = coefficient[i];
}
for( int i = 0; i <= other.getDegree(); i++)
{
second[i] = other.getCoeff(i);
}
for(int i = 0; i<degree; i++)
{
final[i] = first[i]-second[i];
}
for(int i=0;i<=newDeg;i++)
{
final2[i] = final[(degree - 1)-(newDeg-i)];
}
}
else
{
for( int i = 0; i <= other.getDegree(); i++)
{
second[i] = other.getCoeff(other.getDegree()-i);
}
for(int i = 0; i <= degree; i++)
{
first[i] = coefficient[degree-i];
}
for(int i = 0; i<degree; i++)
{
temp[i] = first[i]-second[i];
}
for(int i=0; i<degree; i++)
{
final[i] = temp[(degree - 1)-i];
}
for(int i=0;i<=newDeg;i++)
{
final2[i] = final[(degree - 1)-(newDeg-i)];
//cout<<final2[i]<<endl;
}
}
return Polynomial(newDeg,final2);
}

Polynomial Polynomial::operator*(Polynomial other)
{
int newDeg = degree + other.getDegree();
int resultCoeff[degree];
for(int i = 0; i<degree; i++)
{
resultCoeff[i] = 0;
}
//cout<<resultCoeff[5]<<endl;
for(int i = 0; i<=degree; i++)
{
for(int j = 0; j<=other.getDegree(); j++)
{
resultCoeff[i+j] += (other.getCoeff(j)*coefficient[i]);
//cout<<i+j<<endl;
//cout<<other.getCoeff(j)<<endl;
//cout<<coefficient[i]<<endl;
//cout<<resultCoeff[i+j]<<endl;
}
}
return Polynomial(newDeg,resultCoeff);
}


string Polynomial::print()
{
string result;
int deg = degree;
for(int i = 0; i<=degree; i++)
{
if(result == "")
{
//cout<<coefficient[i]<<endl;
result = to_string(coefficient[i]);
result += "x^";
result += to_string(deg);
deg -= 1;
}
else //if(coefficient[i] >= 0)
{
//cout<<coefficient[i]<<endl;
result += "+";
result += to_string(coefficient[i]);
result += "x^";
result += to_string(deg);
deg -= 1;
}
//else if(coefficient[i] < 0)
//{
//cout<<coefficient[i]<<endl;
// result += "-";
// result += to_string(coefficient[i]);
// result += "x^";
// result += to_string(deg);
// deg -= 1;
//}
}
return result;
}

const int Polynomial::getDegree()
{
return degree;
}

const int Polynomial::getCoeff(int index)
{
return coefficient[index];
}

主要

#include <iostream>
#include "Polynomial.h"

int main()
{
using namespace std;
int degree = 2;
int coefficients[10] = {2,3,5};
int degree2 = 3;
int coefficients2[10] = {1,5,2,3};

Polynomial test = Polynomial(degree, coefficients);
cout << &test<<endl;//.print()<<endl;
Polynomial test2 = Polynomial(degree2, coefficients2);
cout << &test2<<endl;//.print()<<endl;
Polynomial test3 = test + test2;
cout << &test3<<endl;//.print()<<endl;
Polynomial test4 = test - test2;
cout << &test4<<endl;//.print()<<endl;
Polynomial test5 = test * test2;
cout << &test5<<endl;//.print();
return 0;
}

最佳答案

您的类没有定义复制构造函数。您将需要其中之一,因为默认复制构造函数执行浅复制,它只复制指针值而不复制系数数组的内容。

当您复制您的对象时(您到处都在做),问题就出现了,因为这两个拷贝稍后会在它们被破坏时尝试删除同一个数组。

您还需要定义一个赋值运算符。查看Rule of Three .

关于c++ - 动态数组 C++ 多项式类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23353881/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com