【发布时间】:2021-04-14 12:33:08
【问题描述】:
希望你们一切都好 我正在做一项任务,我已经被困在这里很久了,我的代码中没有错误或警告我不知道有什么问题。我会很感激帮助 我检查了所有指针和 null 相关的东西,但我仍然无法找出问题所在
#pragma once
#include<iostream>
using namespace std;
struct Node {
public:
int coefficient;
int power;
Node* Next;
int getcoefficient()
{
return this->coefficient;
}
int getpower()
{
return this->power;
}
};
class Polynomial {
private:
Node* Head;
Node* Tail;
public:
Polynomial();
int getco();
int getpow();
Polynomial(int arraycoefficients[], int arraypower[], int n);
~Polynomial();
void insert(int coefficient1, int power1);
void remove(int power1);
double Evaluate(double value);
Polynomial operator+(Polynomial O2);
/*Polynomial& operator=(Polynomial& p);*/
int degree()const;
void merge(Polynomial p);
void removeAll();
friend istream& operator>>(istream& in, Polynomial& P);
friend ostream& operator<<(ostream& out, const Polynomial& P);
};
这是头文件的cpp
#include "Polynomial.h"
#include<math.h>
#include <iostream>
using namespace std;
Polynomial::Polynomial()
{
Head = Tail = NULL;
}
int Polynomial::getco()
{
return Head->coefficient;
}
int Polynomial::getpow()
{
return Head->power;
}
Polynomial::Polynomial(int arraycoefficients[], int arraypower[], int n)
{
Head = NULL;
Tail = NULL;
for (int i = 0; i < n; i++) {
insert(arraycoefficients[i], arraypower[i]);
}
}
Polynomial::~Polynomial()
{
removeAll();
}
void Polynomial::insert(int coefficient1, int power1)
{
Node* node = new Node;
node->coefficient = coefficient1;
node->power = power1;
node->Next = NULL;
if (Head == NULL) {
Head = Tail = node;
return;
}
//addition at head
if (power1 == Head->power) {
Head->coefficient = Head->coefficient + coefficient1;
return;
}
//insert at head
if (power1 < Head->power) {
node->Next = Head;
Head = node;
return;
}
Node* temporary = Head;
while (temporary->Next != NULL) {
if (temporary->power == power1) {
temporary->coefficient = temporary->coefficient + coefficient1;
return;
}
if (power1 > temporary->power && power1 < temporary->Next->power) {
node->Next = temporary->Next;
temporary->Next = node;
return;
}
temporary = temporary->Next;
}
//insert at last
if (temporary->power == power1) {
temporary->coefficient = temporary->coefficient + coefficient1;
return;
}
temporary->Next = node;
Tail = node;
}
void Polynomial::remove(int pw)
{
if (Head == NULL)
return;
//only one node
if (Head->power == pw && Head == Tail) {
delete Head;
Head = Tail = NULL;
return;
}
Node* temporary = Head;
while (temporary->Next != NULL) {
if (pw == temporary->Next->power)
{
Node* node = temporary->Next;
temporary->Next = temporary->Next->Next;
if (temporary->Next == NULL)
Tail = temporary;
delete node;
return;
}
temporary = temporary->Next;
}
}
double Polynomial::Evaluate(double value)
{
Node* temporary = Head;
double result = 0.0;
while (temporary != NULL) {
result =result+ (temporary->coefficient * pow(value, temporary->power));
temporary = temporary->Next;
}
return result;
}
/*Polynomial& Polynomial::operator=(Polynomial& p)
{
Node* temporary = p.Head;
int pp;
int cc;
removeAll();
while (temporary != nullptr) {
pp = temporary->getpower();
cc = temporary->getcoefficient();
insert(cc,pp);
temporary = temporary->Next;
}
return *this;
}*/
int Polynomial::degree() const
{
if (Head == NULL)
{
cout << "Empty polynomial\n";
return -1;
}
return Tail->power;
}
void Polynomial::merge(Polynomial p)
{
int count = 0;
Node* temporary = p.Head;
while (temporary != NULL) {
insert(temporary->coefficient, temporary->power);
temporary = temporary->Next;
}
p.removeAll();
return;
}
void Polynomial::removeAll()
{
if (Head == NULL)
return;
Node* CurrentNode = Head;
Node* NextNode = NULL;
while (CurrentNode != NULL)
{
NextNode = CurrentNode->Next;
delete CurrentNode;
CurrentNode = NextNode;
}
Head = Tail = NULL;
}
istream& operator>>(istream& in, Polynomial& P)
{
int num;
cout << "Please enter number of nodes\n";
cin >> num;
int c, p;
for (int i = 0; i < num; i++)
{
cout << "Please enter the pair of (coefficient,power)\n";
in >> c >> p;
P.insert(c, p);
}
return in;
}
ostream& operator<<(ostream& out, const Polynomial& P)
{
Node* temporary = P.Head;
if (temporary == NULL)
{
out << "Empty polynomial\n";
return out;
}
else {
out << "Polynomial is:\n";
while (temporary != NULL) {
if (temporary->power == 0)
out << temporary->coefficient << " + ";
else if (temporary->Next == NULL)
out << temporary->coefficient << "x^" << temporary->power;
else
out << temporary->coefficient << "x^" << temporary->power << " + ";
temporary = temporary->Next;
}
}
out << endl;
return out;
}
//Polynomial operator+(Polynomial& p1, Polynomial& p2)
Polynomial Polynomial::operator+(Polynomial O2)
{
Polynomial SumOfPolies;
Node* temporary = Head;
while (temporary != NULL) {
SumOfPolies.insert(temporary->coefficient, temporary->power);
temporary = temporary->Next;
}
temporary = O2.Head;
while (temporary != NULL) {
SumOfPolies.insert(temporary->coefficient, temporary->power);
temporary = temporary->Next;
}
return SumOfPolies;
}
这是主要的
#include <iostream>
#include "Polynomial.h"
using namespace std;
int main()
{
int X[] = { 2,4,1,3,5 };
int Z[] = { 1,2,0,3,4 };
Polynomial F1(X, Z, 5);
cout << F1 << endl;
cout << F1.degree() << endl;
Polynomial F2;
F2.insert(3, 2);
F2.insert(1, 6);
F2.insert(2, 7);
cout << F2 << endl;
Polynomial F3;
cout << "Here\n";
F3 = F1 + F2;
cout << "Here\n";
cout << F3;
return 0;
}
【问题讨论】:
-
流提取 (
>>) 不应该与用户交互。考虑一下如果你从一个文件中读取 1000 个多项式会发生什么——你真的想先输入节点的数量,然后看着一堆提示滚动显示吗? -
你需要阅读“三法则”和深拷贝的概念。
标签: c++ exception linked-list