Polynomial::Polynomial(const Polynomial& a) { this->mNmb = a.mNmb; mCount++; delete[] this->mXs; delete[] this->mJi; this->mXs = a.mXs; this->mJi = a.mJi; this->mSize = a.mSize; } Polynomial::~Polynomial() //析构函数 { mCount--; //多项式个数-1 //这里不释放内存,否则在运算符重载部分会出错,内存的释放需要手动释放 } void Polynomial::release() { if (mXs) { delete[] mXs; mXs = 0; } if (mJi) { delete[] mJi; mJi = 0; } } void Polynomial::add(int xs, int mj) //增加多项式 { if (mNmb >= mSize) { mSize += 10; //扩容 mXs = (int*)realloc(mXs, sizeof(int) * mSize); mJi = (int*)realloc(mJi, sizeof(int) * mSize); } mXs[mNmb] = xs; mJi[mNmb] = mj; mNmb++; Sort(); }
时间: 2023-06-19 10:06:12 浏览: 148
这段代码实现了多项式类的拷贝构造函数和析构函数,以及增加多项式的函数。其中,拷贝构造函数用于创建一个新的多项式对象并将其初始化为另一个多项式对象的副本,析构函数用于释放多项式对象所占用的内存,而增加多项式的函数则用于在多项式对象中添加新的多项式项。需要注意的是,增加多项式项时会自动进行排序。另外,这里使用了realloc函数来动态扩展数组大小,所以在释放内存时需要使用delete[]而不是free函数。
相关问题
class Polynomial { public: Polynomial() : head(nullptr) {} ~Polynomial() { Node* curr = head; while (curr) { Node* temp = curr->next; delete curr; curr = temp; } } void insert(int coeff, int exp) { Node* curr = head; Node* prev = nullptr; while (curr && curr->exp > exp) { prev = curr; curr = curr->next; } if (curr && curr->exp == exp) { curr->coeff += coeff; if (curr->coeff == 0) { if (prev) { prev->next = curr->next; } else { head = curr->next; } delete curr; } } else { Node* newNode = new Node(coeff, exp); if (prev) { prev->next = newNode; } else { head = newNode; } newNode->next = curr; } } void print() const { Node* curr = head; while (curr) { std::cout << curr->coeff << "x^" << curr->exp << " "; curr = curr->next; } std::cout << std::endl; } Node* head; // 多项式头结点 }; Polynomial add(const Polynomial& p1, const Polynomial& p2) { Node* curr1 = p1.head; Node* curr2 = p2.head; Polynomial result; while (curr1 && curr2) { if (curr1->exp > curr2->exp) { result.insert(curr1->coeff, curr1->exp); curr1 = curr1->next; } else if (curr1->exp < curr2->exp) { result.insert(curr2->coeff, curr2->exp); curr2 = curr2->next; } else { int sum = curr1->coeff + curr2->coeff; result.insert(sum, curr1->exp); curr1 = curr1->next; curr2 = curr2->next; } } while (curr1) { result.insert(curr1->coeff, curr1->exp); curr1 = curr1->next; } while (curr2) { result.
在这段代码中,定义了一个多项式类 Polynomial,其中包含一个链表结构,每个节点表示多项式的一个单项式,具有系数和指数两个成员变量。类中包含了一个插入单项式的方法 insert,一个打印多项式的方法 print,还有一个多项式相加的全局函数 add。在 add 函数中,使用了两个指针 curr1 和 curr2 分别指向两个多项式的头结点,通过比较指数的大小,向结果多项式中依次插入单项式,如果指数相同则将系数相加。最后将指针剩下的单项式插入到结果多项式中。
class Polynomial { public: Polynomial() : head(nullptr) {} ~Polynomial() { Node* curr = head; while (curr) { Node* temp = curr->next; delete curr; curr = temp; } } void insert(int coeff, int exp) { Node* curr = head; Node* prev = nullptr; while (curr && curr->exp > exp) { prev = curr; curr = curr->next; } if (curr && curr->exp == exp) { curr->coeff += coeff; if (curr->coeff == 0) { if (prev) { prev->next = curr->next; } else { head = curr->next; } delete curr; } } else { Node* newNode = new Node(coeff, exp); if (prev) { prev->next = newNode; } else { head = newNode; } newNode->next = curr; } } void print() const { Node* curr = head; while (curr) { std::cout << curr->coeff << "x^" << curr->exp << " "; curr = curr->next; } std::cout << std::endl; } Node* head; // 多项式头结点 };
这段代码实现了多项式类,其中包含插入方法 insert 和打印方法 print。与之前的代码不同的是,这里的 insert 方法实现了系数为 0 时删除节点的功能,而且使用了更加简洁的写法。具体实现是通过遍历链表,找到合适的位置插入新节点,如果指数已存在则将系数相加。如果相加后系数为 0,则删除该节点。多项式的数据结构是一个链表,每个节点包含系数和指数。头结点 head 是公有成员,可以在类外部访问。打印方法 print 用于打印多项式的内容。
阅读全文