问题描述
- 稀疏多项式C++链表的问题
-
#include<iostream> using namespace std; #ifndef POLYNOMIAL_H #define POLYNOMIAL_H template<class T>class Polynomial; template<class T>ostream& operator<<(ostream& out, Polynomial<T>&a ); template<class T> class Polynomial{ private: class Term{ public: T xishu; unsigned mi; }; class Node{ public: Term data; Node* next; Node(T x = 0, unsigned y = 0, Node* p = 0){ data.xishu = x; data.mi = y; Node* next = p; } }; public: Polynomial(); ~Polynomial(); Polynomial<T> operator+(const Polynomial<T>&); Polynomial<T> operator*(const Polynomial<T>&); void initial(); friend ostream& operator<< <T> (ostream& out, Polynomial<T>& a); private: int highestdigit; Node* first; int num;//用来判断多项式有几项,即链表节点数,在乘法运算里也要用到 }; template<class T> Polynomial<T>::Polynomial(){ first = new Node(); first->next = new Node(); num = 0; highestdigit = 0; } template<class T> Polynomial<T>::~Polynomial(){ Node* p = first; while (p){ Node* q = p->next; delete p; p = q; } } template<class T> Polynomial<T> Polynomial<T>::operator+(const Polynomial<T>& second){ Polynomial<T> sum; Node* pa = first->next, *pb = second.first->next, *pc = sum.first; int digit = 0, num = 0; while (pa&&pb){ if (pa->data.mi == pb->data.mi) // 指数相等时 { T x = pa->data.xishu + pb->data.xishu; if (x) // 相加完的系数不为0时 { pc->next = new Node(x, pa->data.mi); pc = pc->next; ++num; digit = pa->data.mi; } pa = pa->next; pb = pb->next; } else if (pa->data.mi < pb->data.mi){ pc->next = new Node(pa->data.xishu, pa->data.mi); pc = pc->next; pa = pa->next; ++num; digit = pa->data.mi; } else{ pc->next = new Node(pb->data.xishu, pb->data.mi); pc = pc->next; pb = pb->next; ++num; digit = pb->data.mi; } } while (pa){ pc->next = new Node(pa->data.xishu, pa->data.mi); pc = pc->next; pa = pa->next; ++num; digit = pa->data.mi; } while (pb){ pc->next = new Node(pb->data.xishu, pb->data.mi); pc = pc->next; pb = pb->next; ++num; digit = pb->data.mi; } sum.num = num; sum.highestdigit = digit; return sum; } template<class T> Polynomial<T> Polynomial<T>::operator*(const Polynomial<T>& second){ Polynomial<T> multi; Node* pa = first->next, *pb = second.first->next; if (num >=second.num){ Polynomial<T>* p1 = new Polynomial<T>[second.num]; Node **p2 = new Node*[second.num]; for (int i = 0; i < second.num; ++i){ while (pa){ if (pa->next == 0){ p1[i].highestdigit = pa->data.mi*pb->data.mi; p1[i].num = num; } p2[i] = p1[i].first; p2[i]->next = new Node(pa->data.xishu*pb->data.xishu, pa->data.mi*pb->data.mi); p2[i] = p2[i]->next; pa = pa->next; } pb = pb->next; multi = multi + p1[i]; } return multi; } else{ Polynomial<T>* p1 = new Polynomial<T>[num]; Node **p2 = new Node*[num]; for (int i = 0; i < num; ++i){ while (pb){ if(pb->next == 0){ p1[i].highestdigit = pa->data.mi*pb->data.mi; p1[i].num = num; } p2[i] = p1[i].first; p2[i]->next = new Node(pb->data.xishu*pa->data.xishu, pb->data.mi*pa->data.mi); p2[i] = p2[i]->next; pb = pb->next; } pa = pa->next; multi = multi + p1[i]; } return multi; } } template<class T> void Polynomial<T>::initial(){ T a; Node* p = first; unsigned b, high=0; while (cin >> a&&a!=0&&cin>>b){ //输入a=0时结束输入 if (b >= high)high= b; ++num; p->next = new Node(a, b); p = p->next; } highestdigit = high; } template<class T> ostream& operator<<(ostream& out, Polynomial<T>& a){ Polynomial<T>::Node *p = a.first->next; if (p->data.xishu == 0)cout << 0 << endl; else{ if (p->data.xishu != 1) out << p->data.xishu << "x^" << p->data.mi; else out << "x^" << p->data.mi; } p = p->next; while (p){ out << '+'; if (p->data.xishu != 1) out << p->data.xishu << "x^" << p->data.mi; else out << "x^" << p->data.mi; p = p->next; } return out; } template<class T> Polynomial<T> Polynomial<T>::operator*(const Polynomial<T>& second){ Polynomial<T> multi; Node* pa = first->next, *pb = second.first->next; if (num >=second.num){ Polynomial<T>* p1 = new Polynomial<T>[second.num]; Node **p2 = new Node*[second.num]; for (int i = 0; i < second.num; ++i){ while (pa){ if (pa->next == 0){ p1[i].highestdigit = pa->data.mi*pb->data.mi; p1[i].num = num; } p2[i] = p1[i].first; p2[i]->next = new Node(pa->data.xishu*pb->data.xishu, pa->data.mi*pb->data.mi); p2[i] = p2[i]->next; pa = pa->next; } pb = pb->next; multi = multi + p1[i]; } return multi; } else{ Polynomial<T>* p1 = new Polynomial<T>[num]; Node **p2 = new Node*[num]; for (int i = 0; i < num; ++i){ while (pb){ if(pb->next == 0){ p1[i].highestdigit = pa->data.mi*pb->data.mi; p1[i].num = num; } p2[i] = p1[i].first; p2[i]->next = new Node(pb->data.xishu*pa->data.xishu, pb->data.mi*pa->data.mi); p2[i] = p2[i]->next; pb = pb->next; } pa = pa->next; multi = multi + p1[i]; } return multi; } } #endif 测试代码如下: #include "Polynomial.h" #include<iostream> using namespace std; int main(){ Polynomial<int> a, b, c; a.initial(); cout << a <<endl; b.initial(); cout << b << endl; cout << a + b << endl; return 0; } 看了好久好久了啊,每次输出a+b都是0
解决方案
删除了你的类析构函数,链表节点的释放改为成员函数myfree()来实现。
这样在+重载函数返回时不会自动释放链表。
#include<iostream>
using namespace std;
//约定稀疏多项式输入降幂排列
template<class T>class Polynomial;
template<class T>ostream& operator<<(ostream& out, Polynomial<T>&a );
template<class T>
class Polynomial
{
private:
class Term
{
public:
T xishu;
unsigned mi;
};
class Node
{
public:
Term data;
Node* next;
Node(T x = 0, unsigned y = 0, Node* p = 0)
{
data.xishu = x;
data.mi = y;
next = p;//修改
}
};
public:
Polynomial();
void myfree();//释放链表各节点
Polynomial<T>& operator=(const Polynomial<T>& second);
Polynomial<T> operator+(const Polynomial<T>&);
void initial();
friend ostream& operator<< <T> (ostream& out, Polynomial<T>& a);
private:
int highestdigit;
Node* first;
int num;//用来判断多项式有几项,即链表节点数,在乘法运算里也要用到
};
template<class T>
Polynomial<T>::Polynomial()
{
first = new Node;
num = 0;
highestdigit = 0;
}
template<class T>
void Polynomial<T>::myfree() //释放链表各节点
{
Node* p = first,* q;
while (p)
{
q = p->next;
delete p;
p = q;
}
}
template<class T>
Polynomial<T>& Polynomial<T>::operator=(const Polynomial<T>& second)
{
highestdigit = second.highestdigit;
num=second.num;
Node* p1=first,*p2=second.first;
while (p2->next)
{
p1->next = new Node();
p1 = p1->next;
p2 = p2->next;
*p1=*p2;
}
return *this;
}
template<class T>
Polynomial<T> Polynomial<T>::operator+(const Polynomial<T>& second)
{
Polynomial<T> sum;
Node* pa = first->next, *pb = second.first->next, *pc = sum.first;
while (pa&&pb)
{
if (pa->data.mi == pb->data.mi) // 指数相等时
{
T x = pa->data.xishu + pb->data.xishu;
if (x) // 相加完的系数不为0时
{
pc->next = new Node(x, pa->data.mi);
pc = pc->next;
++sum.num;
sum.highestdigit = pa->data.mi;
}
pa = pa->next;
pb = pb->next;
}
else if (pa->data.mi > pb->data.mi){
pc->next = new Node(pa->data.xishu, pa->data.mi);
pc = pc->next;
pa = pa->next;
++sum.num;
sum.highestdigit = pa->data.mi;
}
else{
pc->next = new Node(pb->data.xishu, pb->data.mi);
pc = pc->next;
pb = pb->next;
++sum.num;
sum.highestdigit = pb->data.mi;
}
}
while (pa){
pc->next = new Node(pa->data.xishu, pa->data.mi);
pc = pc->next;
pa = pa->next;
++sum.num;
sum.highestdigit = pa->data.mi;
}
while (pb){
pc->next = new Node(pb->data.xishu, pb->data.mi);
pc = pc->next;
pb = pb->next;
++sum.num;
sum.highestdigit = pb->data.mi;
}
return sum;
}
template<class T>
void Polynomial<T>::initial()
{
//释放链表,保留头节点
Node* p = first->next,* q;
while (p)
{
q = p->next;
delete p;
p = q;
}
p = first;
T a;
int b;
num=0; highestdigit=0;//修改
while (cin >> a&&a!=0&&cin>>b){ //输入a=0时结束输入
if (b >= highestdigit) highestdigit= b;
++num;
p->next = new Node(a, b);
p =p->next;
}
fflush(stdin);//修改
}
template<class T>
ostream& operator<<(ostream& out, Polynomial<T>& a)
{
Polynomial<T>::Node *p = a.first->next;
if( p==NULL ) return out;//修改
if (p->data.xishu == 0)cout << endl;//修改
else{
if (p->data.xishu != 1)
out << p->data.xishu << "x^" << p->data.mi;
else out << "x^" << p->data.mi;
}
p = p->next;
while (p)
{
out << '+';
if (p->data.xishu != 1)
out << p->data.xishu << "x^" << p->data.mi;
else out << "x^" << p->data.mi;
p = p->next;
}
return out;
}
int main(){
Polynomial<int> a,b, c;
a.initial();
cout <<"a= "<< a <<endl;
b.initial();
cout <<"b="<< b << endl;
c=a+b;
cout << "c="<< c << endl;
a.myfree();
b.myfree();
c.myfree();
return 0;
}
解决方案二:
多项式相加链表
解决方案三:
是不是拷贝不全?编译就没通过。
T xishu;
T是哪来的?
解决方案四:
#include
using namespace std;
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
templateclass Polynomial;
templateostream& operator<<(ostream& out, Polynomial&a );
template
class Polynomial{
private:
class Term{
public:
T xishu;
unsigned mi;
};
class Node{
public:
Term data;
Node* next;
Node(T x = 0, unsigned y = 0, Node* p = 0){
data.xishu = x;
data.mi = y;
Node* next = p;
}
};
public:
Polynomial();
~Polynomial();
Polynomial operator+(const Polynomial&);
Polynomial operator*(const Polynomial&);
void initial();
friend ostream& operator<< (ostream& out, Polynomial& a);
private:
int highestdigit;
Node* first;
int num;//用来判断多项式有几项,即链表节点数,在乘法运算里也要用到
};
template
Polynomial::Polynomial(){
first = new Node();
first->next = new Node();
num = 0;
highestdigit = 0;
}
template
Polynomial::~Polynomial(){
Node* p = first;
while (p){
Node* q = p->next;
delete p;
p = q;
}
}
template
Polynomial Polynomial::operator+(const Polynomial& second){
Polynomial sum;
Node* pa = first->next, *pb = second.first->next, *pc = sum.first;
int digit = 0, num = 0;
while (pa&&pb){
if (pa->data.mi == pb->data.mi) // 指数相等时
{
T x = pa->data.xishu + pb->data.xishu;
if (x) // 相加完的系数不为0时
{
pc->next = new Node(x, pa->data.mi);
pc = pc->next;
++num;
digit = pa->data.mi;
}
pa = pa->next;
pb = pb->next;
}
else if (pa->data.mi < pb->data.mi){
pc->next = new Node(pa->data.xishu, pa->data.mi);
pc = pc->next;
pa = pa->next;
++num;
digit = pa->data.mi;
}
else{
pc->next = new Node(pb->data.xishu, pb->data.mi);
pc = pc->next;
pb = pb->next;
++num;
digit = pb->data.mi;
}
}
while (pa){
pc->next = new Node(pa->data.xishu, pa->data.mi);
pc = pc->next;
pa = pa->next;
++num;
digit = pa->data.mi;
}
while (pb){
pc->next = new Node(pb->data.xishu, pb->data.mi);
pc = pc->next;
pb = pb->next;
++num;
digit = pb->data.mi;
}
sum.num = num;
sum.highestdigit = digit;
return sum;
}
template
Polynomial Polynomial::operator*(const Polynomial& second){
Polynomial multi;
Node* pa = first->next, pb = second.first->next;
if (num >=second.num){
Polynomial p1 = new Polynomial[second.num];
Node **p2 = new Node*[second.num];
for (int i = 0; i < second.num; ++i){
while (pa){
if (pa->next == 0){
p1[i].highestdigit = pa->data.mi*pb->data.mi;
p1[i].num = num;
}
p2[i] = p1[i].first;
p2[i]->next = new Node(pa->data.xishu*pb->data.xishu, pa->data.mi*pb->data.mi);
p2[i] = p2[i]->next;
pa = pa->next;
}
pb = pb->next;
multi = multi + p1[i];
}
return multi;
}
else{
Polynomial* p1 = new Polynomial[num];
Node **p2 = new Node*[num];
for (int i = 0; i < num; ++i){
while (pb){
if(pb->next == 0){
p1[i].highestdigit = pa->data.mi*pb->data.mi;
p1[i].num = num;
}
p2[i] = p1[i].first;
p2[i]->next = new Node(pb->data.xishu*pa->data.xishu, pb->data.mi*pa->data.mi);
p2[i] = p2[i]->next;
pb = pb->next;
}
pa = pa->next;
multi = multi + p1[i];
}
return multi;
}
}
template
void Polynomial::initial(){
T a;
Node* p = first;
unsigned b, high=0;
while (cin >> a&&a!=0&&cin>>b){ //输入a=0时结束输入
if (b >= high)high= b;
++num;
p->next = new Node(a, b);
p = p->next;
}
highestdigit = high;
}
template
ostream& operator<<(ostream& out, Polynomial& a){
Polynomial::Node p = a.first->next;
if (p->data.xishu == 0)cout << 0 << endl;
else{
if (p->data.xishu != 1)
out << p->data.xishu << "x^" << p->data.mi;
else out << "x^" << p->data.mi;
}
p = p->next;
while (p){
out << '+';
if (p->data.xishu != 1)
out << p->data.xishu << "x^" << p->data.mi;
else out << "x^" << p->data.mi;
p = p->next;
}
return out;
}
template
Polynomial Polynomial::operator=(const Polynomial&second){
Node p = first->next;
while (p){
Node* q = p->next;
delete p;
p = q;
}
Node* p2 = second.first->next;
p = first;
while (p2){
p->next = new Node(p2->data.xishu, p2->data.mi);
p = p->next;
p2 = p2->next;
}
return *this;
}
#endif
解决方案五:
#include
using namespace std;
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
templateclass Polynomial;
templateostream& operator<<(ostream& out, Polynomial&a );
template
class Polynomial{
private:
class Term{
public:
T xishu;
unsigned mi;
};
class Node{
public:
Term data;
Node* next;
Node(T x = 0, unsigned y = 0, Node* p = 0){
data.xishu = x;
data.mi = y;
Node* next = p;
}
};
public:
Polynomial();
~Polynomial();
Polynomial operator+(const Polynomial&);
Polynomial operator*(const Polynomial&);
void initial();
friend ostream& operator<< (ostream& out, Polynomial& a);
private:
int highestdigit;
Node* first;
int num;//用来判断多项式有几项,即链表节点数,在乘法运算里也要用到
};
template
Polynomial::Polynomial(){
first = new Node();
first->next = new Node();
num = 0;
highestdigit = 0;
}
template
Polynomial::~Polynomial(){
Node* p = first;
while (p){
Node* q = p->next;
delete p;
p = q;
}
}
template
Polynomial Polynomial::operator+(const Polynomial& second){
Polynomial sum;
Node* pa = first->next, *pb = second.first->next, *pc = sum.first;
int digit = 0, num = 0;
while (pa&&pb){
if (pa->data.mi == pb->data.mi) // 指数相等时
{
T x = pa->data.xishu + pb->data.xishu;
if (x) // 相加完的系数不为0时
{
pc->next = new Node(x, pa->data.mi);
pc = pc->next;
++num;
digit = pa->data.mi;
}
pa = pa->next;
pb = pb->next;
}
else if (pa->data.mi < pb->data.mi){
pc->next = new Node(pa->data.xishu, pa->data.mi);
pc = pc->next;
pa = pa->next;
++num;
digit = pa->data.mi;
}
else{
pc->next = new Node(pb->data.xishu, pb->data.mi);
pc = pc->next;
pb = pb->next;
++num;
digit = pb->data.mi;
}
}
while (pa){
pc->next = new Node(pa->data.xishu, pa->data.mi);
pc = pc->next;
pa = pa->next;
++num;
digit = pa->data.mi;
}
while (pb){
pc->next = new Node(pb->data.xishu, pb->data.mi);
pc = pc->next;
pb = pb->next;
++num;
digit = pb->data.mi;
}
sum.num = num;
sum.highestdigit = digit;
return sum;
}
template
Polynomial Polynomial::operator*(const Polynomial& second){
Polynomial multi;
Node* pa = first->next, pb = second.first->next;
if (num >=second.num){
Polynomial p1 = new Polynomial[second.num];
Node **p2 = new Node*[second.num];
for (int i = 0; i < second.num; ++i){
while (pa){
if (pa->next == 0){
p1[i].highestdigit = pa->data.mi*pb->data.mi;
p1[i].num = num;
}
p2[i] = p1[i].first;
p2[i]->next = new Node(pa->data.xishu*pb->data.xishu, pa->data.mi*pb->data.mi);
p2[i] = p2[i]->next;
pa = pa->next;
}
pb = pb->next;
multi = multi + p1[i];
}
return multi;
}
else{
Polynomial* p1 = new Polynomial[num];
Node **p2 = new Node*[num];
for (int i = 0; i < num; ++i){
while (pb){
if(pb->next == 0){
p1[i].highestdigit = pa->data.mi*pb->data.mi;
p1[i].num = num;
}
p2[i] = p1[i].first;
p2[i]->next = new Node(pb->data.xishu*pa->data.xishu, pb->data.mi*pa->data.mi);
p2[i] = p2[i]->next;
pb = pb->next;
}
pa = pa->next;
multi = multi + p1[i];
}
return multi;
}
}
template
void Polynomial::initial(){
T a;
Node* p = first;
unsigned b, high=0;
while (cin >> a&&a!=0&&cin>>b){ //输入a=0时结束输入
if (b >= high)high= b;
++num;
p->next = new Node(a, b);
p = p->next;
}
highestdigit = high;
}
template
ostream& operator<<(ostream& out, Polynomial& a){
Polynomial::Node p = a.first->next;
if (p->data.xishu == 0)cout << 0 << endl;
else{
if (p->data.xishu != 1)
out << p->data.xishu << "x^" << p->data.mi;
else out << "x^" << p->data.mi;
}
p = p->next;
while (p){
out << '+';
if (p->data.xishu != 1)
out << p->data.xishu << "x^" << p->data.mi;
else out << "x^" << p->data.mi;
p = p->next;
}
return out;
}
template
Polynomial Polynomial::operator=(const Polynomial&second){
Node p = first->next;
while (p){
Node* q = p->next;
delete p;
p = q;
}
Node* p2 = second.first->next;
p = first;
while (p2){
p->next = new Node(p2->data.xishu, p2->data.mi);
p = p->next;
p2 = p2->next;
}
return *this;
}
#endif
解决方案六:
我这个事模板类,但是复制粘贴代码的时候template粘贴不出来
解决方案七:
把代码放到代码片里。工具栏里有插入代码片。
就像下面这样:
void Polynomial::initial(){
T a;
Node* p = first;
unsigned b, high=0;
while (cin >> a&&a!=0&&cin>>b){ //输入a=0时结束输入
if (b >= high)high= b;
++num;
p->next = new Node(a, b);
p = p->next;
}
highestdigit = high;
}
解决方案八:
#include
using namespace std;
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
templateclass Polynomial;
templateostream& operator<<(ostream& out, Polynomial&a );
template
class Polynomial{
private:
class Term{
public:
T xishu;
unsigned mi;
};
class Node{
public:
Term data;
Node* next;
Node(T x = 0, unsigned y = 0, Node* p = 0){
data.xishu = x;
data.mi = y;
Node* next = p;
}
};
public:
Polynomial();
~Polynomial();
Polynomial operator+(const Polynomial&);
Polynomial operator*(const Polynomial&);
void initial();
friend ostream& operator<< (ostream& out, Polynomial& a);
private:
int highestdigit;
Node* first;
int num;//用来判断多项式有几项,即链表节点数,在乘法运算里也要用到
};
template
Polynomial::Polynomial(){
first = new Node();
first->next = new Node();
num = 0;
highestdigit = 0;
}
template
Polynomial::~Polynomial(){
Node* p = first;
while (p){
Node* q = p->next;
delete p;
p = q;
}
}
template
Polynomial Polynomial::operator+(const Polynomial& second){
Polynomial sum;
Node* pa = first->next, *pb = second.first->next, *pc = sum.first;
int digit = 0, num = 0;
while (pa&&pb){
if (pa->data.mi == pb->data.mi) // 指数相等时
{
T x = pa->data.xishu + pb->data.xishu;
if (x) // 相加完的系数不为0时
{
pc->next = new Node(x, pa->data.mi);
pc = pc->next;
++num;
digit = pa->data.mi;
}
pa = pa->next;
pb = pb->next;
}
else if (pa->data.mi < pb->data.mi){
pc->next = new Node(pa->data.xishu, pa->data.mi);
pc = pc->next;
pa = pa->next;
++num;
digit = pa->data.mi;
}
else{
pc->next = new Node(pb->data.xishu, pb->data.mi);
pc = pc->next;
pb = pb->next;
++num;
digit = pb->data.mi;
}
}
while (pa){
pc->next = new Node(pa->data.xishu, pa->data.mi);
pc = pc->next;
pa = pa->next;
++num;
digit = pa->data.mi;
}
while (pb){
pc->next = new Node(pb->data.xishu, pb->data.mi);
pc = pc->next;
pb = pb->next;
++num;
digit = pb->data.mi;
}
sum.num = num;
sum.highestdigit = digit;
return sum;
}
template
Polynomial Polynomial::operator*(const Polynomial& second){
Polynomial multi;
Node* pa = first->next, pb = second.first->next;
if (num >=second.num){
Polynomial p1 = new Polynomial[second.num];
Node **p2 = new Node*[second.num];
for (int i = 0; i < second.num; ++i){
while (pa){
if (pa->next == 0){
p1[i].highestdigit = pa->data.mi*pb->data.mi;
p1[i].num = num;
}
p2[i] = p1[i].first;
p2[i]->next = new Node(pa->data.xishu*pb->data.xishu, pa->data.mi*pb->data.mi);
p2[i] = p2[i]->next;
pa = pa->next;
}
pb = pb->next;
multi = multi + p1[i];
}
return multi;
}
else{
Polynomial* p1 = new Polynomial[num];
Node **p2 = new Node*[num];
for (int i = 0; i < num; ++i){
while (pb){
if(pb->next == 0){
p1[i].highestdigit = pa->data.mi*pb->data.mi;
p1[i].num = num;
}
p2[i] = p1[i].first;
p2[i]->next = new Node(pb->data.xishu*pa->data.xishu, pb->data.mi*pa->data.mi);
p2[i] = p2[i]->next;
pb = pb->next;
}
pa = pa->next;
multi = multi + p1[i];
}
return multi;
}
}
template
void Polynomial::initial(){
T a;
Node* p = first;
unsigned b, high=0;
while (cin >> a&&a!=0&&cin>>b){ //输入a=0时结束输入
if (b >= high)high= b;
++num;
p->next = new Node(a, b);
p = p->next;
}
highestdigit = high;
}
template
ostream& operator<<(ostream& out, Polynomial& a){
Polynomial::Node p = a.first->next;
if (p->data.xishu == 0)cout << 0 << endl;
else{
if (p->data.xishu != 1)
out << p->data.xishu << "x^" << p->data.mi;
else out << "x^" << p->data.mi;
}
p = p->next;
while (p){
out << '+';
if (p->data.xishu != 1)
out << p->data.xishu << "x^" << p->data.mi;
else out << "x^" << p->data.mi;
p = p->next;
}
return out;
}
template
Polynomial Polynomial::operator=(const Polynomial&second){
Node p = first->next;
while (p){
Node* q = p->next;
delete p;
p = q;
}
Node* p2 = second.first->next;
p = first;
while (p2){
p->next = new Node(p2->data.xishu, p2->data.mi);
p = p->next;
p2 = p2->next;
}
return *this;
}
#endif
#include "Polynomial.h"
#include<iostream>
using namespace std;
int main(){
Polynomial<int> a, b, c;
a.initial();
cout << a <<endl;
b.initial();
cout << b << endl;
cout << a + b << endl;
return 0;
}
解决方案九:
1、实现了a.initial()、<<重载、=重载
2、由于Polynomial模板类含有链表,则对象a、b的大小不同,a+b函数重载在返回sum时,sum被释放,无法得到和的返回值。
即含链表的类无法实现加法重载。因为和无法返回。
采用成员函数方法来实现的。
#include<iostream>
using namespace std;
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
//约定多项式输入降幂排列
template<class T>class Polynomial;
template<class T>ostream& operator<<(ostream& out, Polynomial<T>&a );
template<class T>
class Polynomial{
private:
class Term{
public:
T xishu;
unsigned mi;
};
class Node{
public:
Term data;
Node* next;
Node(T x = 0, unsigned y = 0, Node* p = 0){
data.xishu = x;
data.mi = y;
next = p;//修改
}
};
public:
Polynomial();
~Polynomial();
void add(const Polynomial<T>& c1,const Polynomial<T>& c2);
Polynomial<T>& operator=(const Polynomial<T>& second);
Polynomial<T> operator*(const Polynomial<T>&);
void initial();
friend ostream& operator<< <T> (ostream& out, Polynomial<T>& a);
private:
int highestdigit;
Node* first;
int num;//用来判断多项式有几项,即链表节点数,在乘法运算里也要用到
};
template<class T>
Polynomial<T>::Polynomial(){
first = new Node();
//first->next = new Node();
num = 0;
highestdigit = 0;
}
template<class T>
Polynomial<T>::~Polynomial(){
Node* p = first,* q;
while (p){
q = p->next;
delete p;
p = q;
}
}
template<class T>
Polynomial<T>& Polynomial<T>::operator=(const Polynomial<T>& second)
{
highestdigit = second.highestdigit;
num=second.num;
Node* p1=first,*p2=second.first;
while (p2->next)
{
p1->next = new Node();
p1 = p1->next;
p2 = p2->next;
*p1=*p2;
}
return *this;
}
//sum=c1+c2
template<class T>
void Polynomial<T>::add(const Polynomial<T>& c1,const Polynomial<T>& c2)
{
Node* pa = c1.first->next, *pb = c2.first->next, *pc = first;
highestdigit = 0, num = 0;
while (pa&&pb){
if (pa->data.mi == pb->data.mi) // 指数相等时
{
T x = pa->data.xishu + pb->data.xishu;
if (x) // 相加完的系数不为0时
{
pc->next = new Node(x, pa->data.mi);
pc = pc->next;
++num;
highestdigit = pa->data.mi;
}
pa = pa->next;
pb = pb->next;
}
else if (pa->data.mi > pb->data.mi){ //
pc->next = new Node(pa->data.xishu, pa->data.mi);
pc = pc->next;
pa = pa->next;
++num;
highestdigit = pa->data.mi;
}
else{
pc->next = new Node(pb->data.xishu, pb->data.mi);
pc = pc->next;
pb = pb->next;
++num;
highestdigit = pb->data.mi;
}
}
while (pa){
pc->next = new Node(pa->data.xishu, pa->data.mi);
pc = pc->next;
pa = pa->next;
++num;
highestdigit = pa->data.mi;
}
while (pb){
pc->next = new Node(pb->data.xishu, pb->data.mi);
pc = pc->next;
pb = pb->next;
++num;
highestdigit = pb->data.mi;
}
}
template<class T>
Polynomial<T> Polynomial<T>::operator*(const Polynomial<T>& second){
Polynomial<T> multi;
Node* pa = first->next, *pb = second.first->next;
if (num >=second.num){
Polynomial<T>* p1 = new Polynomial<T>[second.num];
Node **p2 = new Node*[second.num];
for (int i = 0; i < second.num; ++i){
while (pa){
if (pa->next == 0){
p1[i].highestdigit = pa->data.mi*pb->data.mi;
p1[i].num = num;
}
p2[i] = p1[i].first;
p2[i]->next = new Node(pa->data.xishu*pb->data.xishu, pa->data.mi*pb->data.mi);
p2[i] = p2[i]->next;
pa = pa->next;
}
pb = pb->next;
multi = multi + p1[i];
}
return multi;
}
else{
Polynomial<T>* p1 = new Polynomial<T>[num];
Node **p2 = new Node*[num];
for (int i = 0; i < num; ++i){
while (pb){
if(pb->next == 0){
p1[i].highestdigit = pa->data.mi*pb->data.mi;
p1[i].num = num;
}
p2[i] = p1[i].first;
p2[i]->next = new Node(pb->data.xishu*pa->data.xishu, pb->data.mi*pa->data.mi);
p2[i] = p2[i]->next;
pb = pb->next;
}
pa = pa->next;
multi = multi + p1[i];
}
return multi;
}
}
template<class T>
void Polynomial<T>::initial()
{
T a;
Node* p = first;
int b;
num=0; highestdigit=0;//修改
while (cin >> a&&a!=0&&cin>>b){ //输入a=0时结束输入
if (b >= highestdigit) highestdigit= b;
++num;
p->next = new Node(a, b);
p =p->next;
}
fflush(stdin);//修改
}
template<class T>
ostream& operator<<(ostream& out, Polynomial<T>& a)
{
Polynomial<T>::Node *p = a.first->next;
if( p==NULL ) return out;//修改
if (p->data.xishu == 0)cout << endl;//修改
else{
if (p->data.xishu != 1)
out << p->data.xishu << "x^" << p->data.mi;
else out << "x^" << p->data.mi;
}
p = p->next;
while (p){
out << '+';
if (p->data.xishu != 1)
out << p->data.xishu << "x^" << p->data.mi;
else out << "x^" << p->data.mi;
p = p->next;
}
return out;
}
#endif
int main(){
Polynomial<int> a,b, c;
a.initial();
cout <<"a= "<< a <<endl;
b.initial();
cout <<"b="<< b << endl;
c=a;
cout << "c="<< c << endl;
c.add(a,b);
cout << "c="<< c << endl;
return 0;
}