2022/11/2
1、按照课件要求完成稀疏多项式Polynomial类模板的建立、求值等过程。要求使用模板(template ),具体任务如下:
(1)具备稀疏多项式类的基本功能(构造、析构、赋值、复制构造函数以及加法操作);(5分)
(2)为类模板添加一个输入操作,例如: void read(istream & in);(5分)
(3)为类模板添加一个输出操作,例如:void display (ostream & out);(5分)
(4)为类模板添加一个减法操作,例如:Polynomial operator-(const Polynomial & secondPoly);(5分)
(5)为类模板添加一个乘法操作,例如:Polynomial operator*(const Polynomial & secondPoly);(10分)
(6)为类模板添加一个求值操作,例如: CoefType value(CoefType xValue);//计算并返回多项式在xValue上的值。(10分)
Polynomial.h
#pragma once
#include<iostream>
#include<algorithm>
using namespace std;
template <class CoefType>
class Polynomial {
public:
Polynomial(int d = 0);
~Polynomial();
Polynomial(const Polynomial& po);
void read(istream& in);
void display(ostream& out)const;
Polynomial operator+(const Polynomial& secondPoly);
Polynomial operator-(const Polynomial& secondPoly);
Polynomial operator*(const Polynomial& secondPoly);
template <class CoefTypee>
friend ostream& operator<< (ostream& out, const Polynomial<CoefTypee>& p);
CoefType value(CoefType x);
private:
class Term {
public:
CoefType coef;
int expo;
};
class Node {
public:
Term data;
Node* next;
Node(CoefType co = 0, int ex = 0, Node* ptr = 0) {
data.coef = co;
data.expo = ex;
next = ptr;
}
};
typedef Node* nodepointer;
int degree;
nodepointer head;
};
Polynomial.cpp
#include<iostream>
#include<algorithm>
#include"Polynomial.h"
using namespace std;
template <class CoefType>
void f(CoefType x) {
if (x > 0) {
cout << "+" << x;
}
}
template <class CoefType>
Polynomial<CoefType>::Polynomial(int d) {
degree = d;
}
template <class CoefType>
Polynomial<CoefType>::~Polynomial() {
nodepointer temp = head->next;
while (temp != nullptr) {
head->next = temp->next;
delete temp;
temp = head->next;
}
}
template <class CoefType>
Polynomial<CoefType>::Polynomial(const Polynomial& po) {
degree = po.degree;
head = po.head;
nodepointer temp = head;
nodepointer tempp = po.head;
while (tempp->next->next != nullptr) {
temp.data.coef = tempp.data.coef;
temp.data.expo = tempp.data.expo;
temp->next = new nodepointer;
temp = temp->next;
tempp = tempp->next;
}
temp.data.coef = tempp.data.coef;
temp.data.expo = tempp.data.expo;
}
template <class CoefType>
void Polynomial<CoefType>::read(istream& in) {
nodepointer temp = head;
cout << "请输入最高次" << endl;
in >> degree;
cout << "依次输入次数和系数" << endl;
in >> temp.data.coef >> temp.data.expo;
while (degree > temp.data.expo) {
temp->next = new nodepointer;
temp = temp->next;
in >> temp.data.coef >> temp.data.expo;
}
temp = temp->next;
}
template <class CoefType>
void Polynomial<CoefType>::display(ostream& out)const {
nodepointer temp = head;
if (temp.data.expo == 0) {
out << temp.data.coef;
}
else {
out << temp.data.coef << "x^" << temp.data.expo;
}
while (degree > temp.data.expo) {
temp = temp->next;
f(temp.data.coef);
out << "x^" << temp.data.expo;
}
out << endl;
}
template <class CoefType>
Polynomial<CoefType> Polynomial<CoefType>::operator+(const Polynomial<CoefType>& secondPoly){
Polynomial thirdPoly(max(degree, secondPoly.degree));
nodepointer temp = thirdPoly.head;
nodepointer temp1 = head;
nodepointer temp2 = secondPoly.head;
for (int i = 0; i <= min(degree, secondPoly.degree); i++) {
int flag = 1;
if (temp1.data.expo == i) {
if (temp2.data.expo == i) {
temp.data.coef=temp1.data.coef+temp2.data.coef;
temp2=temp2->next;
if (temp.data.coef == 0) {
flag = 0;
}
}
else {
temp.data.coef = temp1.data.coef;
}
temp1=temp1->next;
}
else {
if (temp2.data.expo == i) {
temp.data.coef = temp2.data.coef;
temp2 = temp2->next;
}
else {
flag = 0;
}
}
if (flag) {
temp.data.expo = i;
temp->next = new nodepointer;
temp = temp->next;
}
}
nodepointer tempp = temp1;
if (degree <= secondPoly.degree) {
tempp = temp2;
}
while (tempp->next->next != nullptr) {
temp.data.coef = tempp.data.coef;
temp.data.expo = tempp.data.expo;
temp->next = new nodepointer;
temp = temp->next;
tempp = tempp->next;
}
temp.data.coef = tempp.data.coef;
temp.data.expo = tempp.data.expo;
thirdPoly.degree = temp.data.expo;
return thirdPoly;
}
template <class CoefType>
Polynomial<CoefType> Polynomial<CoefType>::operator-(const Polynomial<CoefType>& secondPoly) {
Polynomial thirdPoly(max(degree, secondPoly.degree));
nodepointer temp = thirdPoly.head;
nodepointer temp1 = head;
nodepointer temp2 = secondPoly.head;
for (int i = 0; i <= min(degree, secondPoly.degree); i++) {
int flag = 1;
if (temp1.data.expo == i) {
if (temp2.data.expo == i) {
temp.data.coef = temp1.data.coef - temp2.data.coef;
temp2 = temp2->next;
if (temp.data.coef == 0) {
flag = 0;
}
}
else {
temp.data.coef = temp1.data.coef;
}
temp1 = temp1->next;
}
else {
if (temp2.data.expo == i) {
temp.data.coef = -temp2.data.coef;
temp2 = temp2->next;
}
else {
flag = 0;
}
}
if (flag) {
temp.data.expo = i;
temp->next = new nodepointer;
temp = temp->next;
}
}
int k = 1;
nodepointer tempp = temp1;
if (degree <= secondPoly.degree) {
tempp = temp2;
}
while (tempp->next->next != nullptr) {
temp.data.coef = k * tempp.data.coef;
temp.data.expo = tempp.data.expo;
temp->next = new nodepointer;
temp = temp->next;
tempp = tempp->next;
}
temp.data.coef = k * tempp.data.coef;
temp.data.expo = tempp.data.expo;
thirdPoly.degree = temp.data.expo;
return thirdPoly;
}
template <class CoefType>
Polynomial<CoefType> Polynomial<CoefType>::operator*(const Polynomial<CoefType>& secondPoly) {
int i = 0;
nodepointer temp1 = head;
Polynomial thirdPoly(degree + secondPoly.degree);
while (i <= degree) {
int j = 0;
Polynomial poly;
nodepointer temp = poly.head;
nodepointer temp2 = secondPoly.head;
while (j <= secondPoly.degree) {
temp.data.coef = temp1.data.coef * temp2.data.coef;
temp.data.expo = temp1.data.expo * temp2.data.expo;
temp->next = new nodepointer;
temp = temp->next;
temp2 = temp2->next;
}
temp1 = temp1->next;
thirdPoly = thirdPoly + poly;
~poly();
}
return thirdPoly;
}
template <class CoefType>
CoefType Polynomial<CoefType>::value(CoefType x) {
CoefType ans;
nodepointer temp = head;
while (temp->next->next != nullptr) {
ans = ans + temp.data.coef * pow(x, temp.data.expo);
temp = temp->next;
}
ans = ans + temp.data.coef * pow(x, temp.data.expo);
return ans;
}
template <class CoefType>
ostream& operator<<(ostream& out, const Polynomial<CoefType>& p) {
p.display(out);
return out;
}
main.cpp
#include<iostream>
#include<algorithm>
#include"Polynomial.h"
using namespace std;
int main() {
Polynomial<int> po1;
Polynomial<int> po2;
po1.read(cin);
po2.read(cin);
cout << po1;
cout << po2;
po1 = po1 + po2;
cout << po1;
po1 = po1 * po2;
cout << po1;
return 0;
}