直接上代码是不是不好?
如果是大佬就直接跳过这个阶段。
链表实现的多项式链表
以每一个节点代表多项式的一个项,常数项可以理解为次数为0的项
再用指针将项与项之间联系起来。可以说是数据结构联系必做的东西!
代码390行,慢慢啃吧,有什么不理解,就在评论区问,我还是蛮活跃的。
#include <iostream>
#include <cmath>
#include <sstream>
#include <stack>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
#define EPS 1e-6
struct Node{
int exponent;
double ratio;
Node *next;
Node (double r = 0,int e = 0,Node*n = NULL):exponent(e),ratio(r),next(n){};
bool operator > (const Node& n) {
return exponent > n.exponent;
}
bool operator < (const Node& n) {
return exponent < n.exponent;
}
bool operator == (const Node& n) {
return exponent == n.exponent;
}
bool operator >= (const Node& n) {
return exponent >= n.exponent;
}
bool operator <= (const Node& n) {
return exponent <= n.exponent;
}
};
class polynome{
Node *first;//判断终止的条件就是在这个链表走到了终点,NULL
int size;//记录节点数,用于判断定位删除和添加是否合理
public:
polynome();
polynome(const polynome & pl);
~polynome();
polynome& operator =(const polynome & pl);
void insert(double r,int e,int pos);
void remove(int pos);
void clear();
int length()const;
void sort();//将原数据排序
friend istream& operator >> (istream & cin, polynome & p);
friend ostream& operator << (ostream & cout, const polynome & p);
//输入输出
//calculate
polynome operator + ( polynome & p );
polynome operator - ( polynome & p );
polynome operator * ( polynome & p );
//辅助*
polynome help_for_multiply (double r,int e);
double getNum(double x);
polynome dericative();
};
polynome::polynome():first(NULL),size(0){};
polynome::~polynome() {
for(Node *p; p = first; delete p)//思路,如果头指针不是空,就把头指针清掉
first = first -> next;
}
void polynome::insert(double r,int e,int pos) {
if(pos > size|| pos < 0)return;
if(pos == 0){
Node* n = new Node(r,e,first);
first = n;
++size;
return ;
}
Node *current = first;
Node *prev = NULL;
int p = 0;
for (int i = 0; i < pos;++i){
prev = current;
current = current->next;
}//经过移动,使得将新节点放到prev和current之间
Node *n = new Node(r,e,current);
prev->next = n;
++size;
}
void polynome::remove(int pos) {
if (pos < 0|| pos >= size)return;
if (pos == 0){
Node *p = first;
first = first->next;
--size;
delete p;
return;
}
Node* current = first;
Node* prev = NULL;
for (int i=0;i < pos;++i){
prev = current;
current = current->next;
}//经过移动,使得要删除的点就是current
prev -> next = current ->next;
delete current;
--size;
}
int polynome::length()const {
return size;
}
void polynome::clear() {
for(Node *p; p = first; delete p)//思路,如果头指针不是空,就把头指针清掉
first = first -> next;
size = 0;
}
polynome::polynome(const polynome & pl) {
//copy constructor
if(pl.length() <= 0){
first = 0;
size = 0;
return;
}
*this = pl;
}
polynome& polynome::operator = (const polynome & pl) {
if (this == &pl)//如果两者在地址上等价
return *this;
if(pl.length() <= 0){
clear();
return *this;
}
first = new Node(pl.first -> ratio, pl.first -> exponent);
Node *current = first;
Node *current_pl = pl.first -> next;
while (current_pl != NULL){
current -> next = new Node(current_pl -> ratio,current_pl -> exponent);
current = current -> next;
current_pl = current_pl -> next;
}
size = pl.size;
return *this;
}
istream& operator >> (istream & cin, polynome & p) {
int n,e;
double r;
p.clear();
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> r>>e;
p.insert(r,e,0);
}
p.sort();
return cin;
}
ostream& operator << (ostream & cout, const polynome & p) {
if (p.length() <= 0)return cout;
Node *current = p.first;
bool first_time = true;
for (int i = 0; i < p.length() ; ++i) {
if (current -> ratio == 0) {
if (current -> exponent == 0 && p.length() == 1) {
cout << current -> ratio;
first_time = false;
} else {
cout << "the exponent = "<<current->exponent<<endl;
cout << "the ratio = "<<current->ratio<<endl;
}
current = current -> next;
continue;
}
if (current -> exponent == 0) {
if (first_time || current -> ratio < 0) {
cout <<current -> ratio;
first_time = false;
} else{
cout <<"+"<< current -> ratio;
}
} else if (current -> exponent == 1) {
if (first_time || current -> ratio < 0) {
if (current -> ratio != 1)
cout << current -> ratio << "x";
else
cout << "x";
first_time = false;
} else {
cout << "+";
if (current -> ratio != 1)
cout << current -> ratio << "x";
else
cout << "x";
}
} else {
if (first_time || current -> ratio < 0){
if (current -> ratio != 1)
cout << current -> ratio << "x^"<<current->exponent;
else
cout << "x^"<<current->exponent;
first_time = false;
}else{
cout << "+";
if (current -> ratio != 1)
cout << current -> ratio << "x^"<<current->exponent;
else
cout << "x^"<<current->exponent;
}
}
current = current -> next;
}
return cout;
}
void polynome::sort() {
for (Node *p = first; p ; p = p -> next){
for (Node* q = p -> next; q; q = q->next) {
if (*p > *q) {
int temp_exponent = p->exponent;
p->exponent = q->exponent;
q->exponent = temp_exponent;
double temp_ratio = p->ratio;
p->ratio = q->ratio;
q->ratio = temp_ratio;
}
}
}//完成排序
stack<int>stack_exponent;
Node *s,*prev;//用于删除
for (Node *p = first; p ; ) {
if (stack_exponent.empty() || stack_exponent.top() != p->exponent){
stack_exponent.push(p->exponent);
prev = p;
p = p -> next;
}else {
prev->ratio += p->ratio;
s = p;
prev->next = p->next;
p = p->next;
delete s;
size --;
}
}
//完成去重
//完成去不合理的0
prev = first;
if (first != NULL){
for (Node *p = first -> next; p ; ) {
if ( abs(p -> ratio) < EPS ) {
prev -> next = p -> next;
s = p;
p = p -> next;
delete s;
--size;
}else {
prev = p;
p = p -> next;
}
}
if (size > 1 && abs(first -> ratio) < EPS ) {
s = first;
first = first -> next;
delete s;
--size;
}
}
//去除将多项式0转换成数值0
if(size == 1 && first -> ratio == 0) {
first->exponent = 0;
} else if (size == 0) {
first = new Node(0,0,0);
size ++;
}
}
polynome polynome::operator + ( polynome & p ) {
polynome new_polynome;
sort();
p.sort();
//整理p和this,就是排序,合并重复,还有就是 去掉可以去掉的0项
Node *it_p = first, *it_q = p.first;
while (it_p != NULL && it_q != NULL) {
if ( it_p -> exponent == it_q -> exponent ) {
new_polynome.insert(it_p ->ratio + it_q->ratio, it_p->exponent,0);
it_p = it_p -> next;
it_q = it_q -> next;
} else if (it_p ->exponent < it_q -> exponent ){
new_polynome.insert(it_p -> ratio, it_p -> exponent,0);
it_p = it_p -> next;
} else if (it_p -> exponent > it_q -> exponent) {
new_polynome.insert(it_q -> ratio, it_q -> exponent,0);
it_q = it_q -> next;
}
}
while (it_p != NULL) {
new_polynome.insert(it_p -> ratio, it_p -> exponent,0);
it_p = it_p -> next;
}
while (it_q != NULL) {
new_polynome.insert(it_q -> ratio, it_q -> exponent,0);
it_q = it_q -> next;
}
new_polynome.sort();
return new_polynome;
}
polynome polynome::operator - ( polynome & p ) {
polynome new_polynome;
sort();
p.sort();
//整理p和this,就是排序,合并重复,还有就是 去掉可以去掉的0项
Node *it_p = first, *it_q = p.first;
while (it_p != NULL && it_q != NULL) {
if ( it_p -> exponent == it_q -> exponent ) {
new_polynome.insert(it_p ->ratio - it_q->ratio, it_p->exponent,0);
it_p = it_p -> next;
it_q = it_q -> next;
} else if (it_p ->exponent < it_q -> exponent ){
new_polynome.insert(it_p -> ratio, it_p -> exponent,0);
it_p = it_p -> next;
} else if (it_p -> exponent > it_q -> exponent) {
new_polynome.insert(-it_q -> ratio, it_q -> exponent,0);
it_q = it_q -> next;
}
}
while (it_p != NULL) {
new_polynome.insert(it_p -> ratio, it_p -> exponent,0);
it_p = it_p -> next;
}
while (it_q != NULL) {
new_polynome.insert(-it_q -> ratio, it_q -> exponent,0);
it_q = it_q -> next;
}
new_polynome.sort();
return new_polynome;
}
polynome polynome::help_for_multiply (double r,int e) {
polynome new_polynome;
for (Node *p = first; p ; p = p -> next) {
new_polynome.insert( p -> ratio * r, p -> exponent + e, 0);
}
new_polynome.sort();
return new_polynome;
}
polynome polynome::operator * ( polynome & p ) {
sort();
Node *it_p = first;
polynome new_polynome,temp;
for ( ; it_p ; it_p = it_p -> next) {
temp = p.help_for_multiply(it_p -> ratio,it_p -> exponent);
new_polynome = new_polynome + temp;
}
return new_polynome;
}
polynome polynome::dericative() {
polynome new_polynome;
for (Node *p = first; p ; p = p -> next ) {
if (p->exponent == 0){
continue;
}else {
new_polynome.insert(p -> exponent * p -> ratio, p -> exponent - 1, 0);
}
}
new_polynome.sort();
return new_polynome;
}
double polynome::getNum(double x) {
Node *p = first;
double sum = 0;
while ( p ) {
sum +=( pow(x,p->exponent) * p->ratio );
p = p -> next;
}
return sum;
}
int main(){
polynome p;
}