class多项式(链表实现)

直接上代码是不是不好?

如果是大佬就直接跳过这个阶段。

链表实现的多项式链表

以每一个节点代表多项式的一个项,常数项可以理解为次数为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;
	 
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是Python链表实现多项式多项式的代码: ```python class Node: def __init__(self, coefficient, exponent, next=None): self.coefficient = coefficient self.exponent = exponent self.next = next class LinkedList: def __init__(self): self.head = None def add_term(self, coefficient, exponent): if self.head is None: self.head = Node(coefficient, exponent) else: current = self.head while current.next is not None: current = current.next current.next = Node(coefficient, exponent) def print_list(self): current = self.head while current is not None: print(f"{current.coefficient}x^{current.exponent}", end="") if current.next is not None: print(" + ", end="") current = current.next print() def multiply(self, other): result = LinkedList() current = self.head while current is not None: current_other = other.head while current_other is not None: coefficient = current.coefficient * current_other.coefficient exponent = current.exponent + current_other.exponent result.add_term(coefficient, exponent) current_other = current_other.next current = current.next return result poly1 = LinkedList() poly1.add_term(2, 3) poly1.add_term(5, 2) poly1.add_term(1, 0) print("Polynomial 1: ", end="") poly1.print_list()) poly2 = LinkedList() poly2.add_term(3, 2) poly2.add_term(2, 1) poly2.add_term(1, 0) print("Polynomial 2: ", end="") poly2.print_list() result = poly1.multiply(poly2) print("Result: ", end="") result.print_list() ``` 在这个实现中,我们定义了一个`Node`类来表示多项式中的每个项。每个节点包含一个系数和指数,并且可以链接到下一个节点。我们还定义了一个`LinkedList`类来表示整个多项式。这个类包含一个头节点,并且可以添加一个新的项。我们还定义了一个`multiply`函数,用于将一个多项式乘以另一个多项式,并返回一个新的多项式作为结果。 在这个代码中,我们首先创建了两个多项式,然后将它们打印出来。接下来,我们将这两个多项式相乘,并将结果打印出来。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肥宅_Sean

公众号“肥宅Sean”欢迎关注

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值