Data structure C++ implements unary polynomial operations (chained storage)

1. Experimental purpose

1. Understand the characteristics of linear tables and their application in practical problems.
2. Master the implementation method of linked storage of linear tables and its basic operations, and learn to use singly linked lists to solve problems.

2. Experimental content

Question: Use a singly linked list with the head node to store each item of a one-variable polynomial, and realize the addition, subtraction and multiplication of a one-variable polynomial. The specific requirements are: use five functions to realize the creation, output, addition, subtraction and multiplication of one-variable polynomials respectively; then call these functions in the main function to realize the display of these functions and present them in the form of a menu.

3. Code content

1. Creation of polynomials

Polynomials can be entered in random order and will be automatically sorted in ascending order when created.

void creatlist(polynomial &p) {
    
    
	//输入n项的系数和指数,建立表示多项式的有序链表p
	p = new PNode;	//为链表P1申请一个空间;
	int n;
	cout << "请输入多项式项数:";
	cin >> n;
	p->next = NULL;	//建立一个带头节点的单链表
	polynomial t,q;	//用于标记位置
	for (int i = 1; i <= n; i++) {
    
    //依次输入n个非0项,并将他们排序
		polynomial s = new PNode;	//生成一个新节点,用于存储输入的系数和指数
		cout << "请分别输入系数和指数: " ;
		cin >> s->coef >> s->expn;	//输入系数和指数
		t = p;	//t用于保存q的前驱,初值为头节点,为了让p只表示头节点
		q = p->next;	//q初始化,指向首元节点
		while (q && q->expn < s->expn) {
    
    //从头到尾开始比较指数找到第一个大于输入指数的项*q
			t = q;
			q = q->next;	//q指向下一个节点
		}	//while
		s->next = q;	//将输入项s插入到q和其他前驱节点pre之间
		t->next = s;
	}	//for
}

2. Output function

The output of the last term has "+" and my use of '\b' did not eliminate the plus sign. I had to traverse the polynomial coefficients first and then output the last term separately.

void printout(polynomial p) {
    
    
	int n = 0;
	polynomial t = p;
	while (t=t->next) {
    
    
		n++;	//多项式项数
	}	//while
	p = p->next;
	for (int i = 1; i < n; i++) {
    
    
		if (p->coef != 0 && p->expn == 0) //判断是否为常数项
			cout << p->coef << "+";
		else if (p->coef < 0) {
    
    
			cout << "\b";
			cout << p->coef << "x^" << p->expn << "+";
		}
		else
			cout << p->coef << "x^" << p->expn << "+";
		p = p->next;
	}
	//单独输出最后一项
	if (p->coef < 0) {
    
    
		cout << "\b";
		cout << p->coef << "x^" << p->expn;
	}
	else
		cout << p->coef << "x^" << p->expn;
}

3. Polynomial addition and subtraction

The implementation of addition and subtraction has not changed much.
In order to save space, I did not create a storage space and saved the result in the p1 space.
This also resulted in the need to reconstruct the input polynomial for subsequent addition and subtraction operations.

//多项式加法
void add_polynomial(polynomial p1,polynomial p2) {
    
    
	//多项式加法:p1=p1+p2,利用两个多项式的节点构成“和多项式”
	polynomial t = p1;	//t指向p1的头节点
	polynomial flag = t;	//用来标记p1的头节点
	p1 = p1->next;
	p2 = p2->next;	//p1,p2分别指向首元节点
	while (p1 && p2) {
    
    	//p1,p2均非空时运行
		if (p1->expn == p2->expn) {
    
    	//指数相等时
			p1->coef += p2->coef;	//系数相加,存的p1里
			if (p1->coef) {
    
    	//coef不等于0时
				t->next = p1;	//将修改后的p1当前节点链在t之后
				t = p1;	//t指向p1
				p1 = p1->next;	//p1指向下一项
				polynomial r = p2;	//临时存储p2节点
				p2 = p2->next;	//p2后移
				delete r;	//删除原p2节点
			}
			else {
    
    	//系数和为0
				polynomial r = p1;	//临时存储p1节点
				p1 = p1->next;	//p1后移
				delete r;	//删除原p1节点
				polynomial rr = p2;	//临时存储p2节点
				p2 = p2->next;	//p2后移
				delete rr;	//删除原p2节点
			}
		}
		else if (p1->expn < p2->expn) {
    
    	//p1指数小于p2指数
			t->next = p1;	//将修改后的p1当前节点链在t之后,
			t = p1;	//t指向p1
			p1 = p1->next;	//指向下一项
		}
		else {
    
    	//p1的指数大于p2的指数
			t->next = p2;	//将修改后的p2当前节点链在t之后,
			t = p2;	//t指向p2
			p2 = p2->next;	//指向下一项
		}
	}	//while
	t->next = p1 ? p1 : p2;	//插入非空多项式的剩余片段
	cout << "相加的结果为:" << endl;
	printout(flag);	//输出结果
	delete p2;	//释放p2空间
}

//多项式减法
void subtruct_polynomial(polynomial p1,polynomial p2) {
    
    
	//多项式减法:p1=p1-p2,利用两个多项式的节点构成“和多项式”
	polynomial t = p1;	//t指向p1的头节点
	polynomial flag = t;	//用来标记p1的头节点
	p1 = p1->next;
	p2 = p2->next;	//p1,p2分别指向首元节点
	while (p1 && p2) {
    
    	//p1,p2均非空时运行
		if (p1->expn == p2->expn) {
    
    	//指数相等时
			p1->coef -= p2->coef;	//系数相减,存的p1里
			if (p1->coef) {
    
    	//coef不等于0时
				t->next = p1;	//将修改后的p1当前节点链在t之后,
				t = p1;	//t指向p1
				p1 = p1->next;	//p1指向下一项
				polynomial r = p2;	//临时存储p2节点
				p2 = p2->next;	//p2后移
				delete r;	//删除原p2节点
			}
			else {
    
    	//系数差为0
				polynomial r = p1;	//临时存储p1节点
				p1 = p1->next;	//p1后移
				delete r;	//删除原p1节点
				polynomial rr = p2;	//临时存储p2节点
				p2 = p2->next;	//p2后移
				delete rr;	//删除原p2节点
			}
		}
		else if (p1->expn < p2->expn) {
    
    	//p1指数小于p2指数
			t->next = p1;	//将修改后的p1当前节点链在t之后,
			t = p1;	//t指向p1
			p1 = p1->next;	//指向下一项
		}
		else {
    
    	//p1的指数大于p2的指数
			p2->coef *= -1;	//系数要乘-1变成相反数
			t->next = p2;	//将修改后的p2当前节点链在t之后,
			t = p2;	//t指向p2
			p2 = p2->next;	//指向下一项
		}
	}	//while
	if (p1)
		t->next = p1;	//插入剩余片段
	if (p2) 
		while (p2) {
    
    
			p2->coef *= -1;	//系数要乘-1变成相反数
			t->next = p2;	//将修改后的p2当前节点链在t之后,
			t = p2;	//t指向p2
			p2 = p2->next;	//指向下一项
		}
	cout << "相减的结果为:" << endl;
	printout(flag);	//输出结果
	delete p2;	//释放p2空间
}

4. Bubble sorting

To pave the way for subsequent multiplication operations

//排序函数
void sort(polynomial p,polynomial head) {
    
    
	p = head;
	polynomial t1 = p->next;
	polynomial t2 = p->next;
	polynomial t3 = NULL;
	float ct;
	int et;
	//冒泡排序
	while (t2!=t3) {
    
    	//t1非空
		while (t1->next!=t3) {
    
    
			if ((t1->expn) > (t1->next->expn)) {
    
    
				ct = t1->coef;
				t1->coef = t1->next->coef;
				t1->next->coef = ct;
				et = t1->expn;
				t1->expn=t1->next->expn;
				t1->next->expn = et;
			}//if
			t1 = t1->next;
		}	//while
		t3 = t1;
		t1 = p->next;	//t1重置
	}//while
}	

5. Polynomial multiplication

Multiplication operation: first multiply, then sort, and finally combine similar items

void multiply_polynomial(polynomial& p1, polynomial& p2) {
    
    
	//先相乘,再排序,后合并
	polynomial head2 = p2;	//标记p2头结点
	polynomial head = new PNode;
	polynomial flag;
	flag = head;
	//先相乘
	p1 = p1->next;
	p2 = p2->next;
	while (p1) {
    
    //为NULL时退出循环
		while (p2) {
    
    //为NULL时退出循环
			polynomial t = new PNode;
			t->coef = p1->coef * p2->coef;	//系数相加,存到t中
			t->expn = p1->expn + p2->expn;	//指数相加
			flag->next = t;
			t->next = NULL;
			flag = t;
			p2 = p2->next;
		}
		p1 = p1->next;
		p2 = head2->next;	//重置p2
	} 	//while
	//再排序
	sort(flag, head);
	//合并
	for (polynomial q = head->next; q && q->next;) {
    
    
		if (q->expn == q->next->expn) {
    
    
			q->coef += q->next->coef;	//系数相加
			q->next = q->next->next;	//后一位插入
		}
		else q = q->next;
	}	//for
	cout << "相乘的结果为:" << endl;
	printout(head);	//输出结果
}

6. Total code

#include<iostream>
using namespace std;

//定义多项式结构体
typedef struct PNode{
    
    
	float coef;	//系数
	int expn;	//指数
	struct PNode *next;	//指针域
}PNode,*polynomial;

//创建多项式
void creatlist(polynomial &p) {
    
    
	//输入n项的系数和指数,建立表示多项式的有序链表p
	p = new PNode;	//为链表P1申请一个空间;
	int n;
	cout << "请输入多项式项数:";
	cin >> n;
	p->next = NULL;	//建立一个带头节点的单链表
	polynomial t,q;	//用于标记位置
	for (int i = 1; i <= n; i++) {
    
    //依次输入n个非0项,并将他们排序
		polynomial s = new PNode;	//生成一个新节点,用于存储输入的系数和指数
		cout << "请分别输入系数和指数: " ;
		cin >> s->coef >> s->expn;	//输入系数和指数
		t = p;	//t用于保存q的前驱,初值为头节点,为了让p只表示头节点
		q = p->next;	//q初始化,指向首元节点
		while (q && q->expn < s->expn) {
    
    //从头到尾开始比较指数找到第一个大于输入指数的项*q
			t = q;
			q = q->next;	//q指向下一个节点
		}	//while
		s->next = q;	//将输入项s插入到q和其他前驱节点pre之间
		t->next = s;
	}	//for
}

//输出多项式
void printout(polynomial p) {
    
    
	int n = 0;
	polynomial t = p;
	while (t=t->next) {
    
    
		n++;	//多项式项数
	}	//while
	p = p->next;
	for (int i = 1; i < n; i++) {
    
    
		if (p->coef != 0 && p->expn == 0) //判断是否为常数项
			cout << p->coef << "+";
		else if (p->coef < 0) {
    
    
			cout << "\b";
			cout << p->coef << "x^" << p->expn << "+";
		}
		else
			cout << p->coef << "x^" << p->expn << "+";
		p = p->next;
	}
	//单独输出最后一项
	if (p->coef < 0) {
    
    
		cout << "\b";
		cout << p->coef << "x^" << p->expn;
	}
	else
		cout << p->coef << "x^" << p->expn;
}

//多项式加法
void add_polynomial(polynomial p1,polynomial p2) {
    
    
	//多项式加法:p1=p1+p2,利用两个多项式的节点构成“和多项式”
	polynomial t = p1;	//t指向p1的头节点
	polynomial flag = t;	//用来标记p1的头节点
	p1 = p1->next;
	p2 = p2->next;	//p1,p2分别指向首元节点
	while (p1 && p2) {
    
    	//p1,p2均非空时运行
		if (p1->expn == p2->expn) {
    
    	//指数相等时
			p1->coef += p2->coef;	//系数相加,存的p1里
			if (p1->coef) {
    
    	//coef不等于0时
				t->next = p1;	//将修改后的p1当前节点链在t之后
				t = p1;	//t指向p1
				p1 = p1->next;	//p1指向下一项
				polynomial r = p2;	//临时存储p2节点
				p2 = p2->next;	//p2后移
				delete r;	//删除原p2节点
			}
			else {
    
    	//系数和为0
				polynomial r = p1;	//临时存储p1节点
				p1 = p1->next;	//p1后移
				delete r;	//删除原p1节点
				polynomial rr = p2;	//临时存储p2节点
				p2 = p2->next;	//p2后移
				delete rr;	//删除原p2节点
			}
		}
		else if (p1->expn < p2->expn) {
    
    	//p1指数小于p2指数
			t->next = p1;	//将修改后的p1当前节点链在t之后,
			t = p1;	//t指向p1
			p1 = p1->next;	//指向下一项
		}
		else {
    
    	//p1的指数大于p2的指数
			t->next = p2;	//将修改后的p2当前节点链在t之后,
			t = p2;	//t指向p2
			p2 = p2->next;	//指向下一项
		}
	}	//while
	t->next = p1 ? p1 : p2;	//插入非空多项式的剩余片段
	cout << "相加的结果为:" << endl;
	printout(flag);	//输出结果
	delete p2;	//释放p2空间
}

//多项式减法
void subtruct_polynomial(polynomial p1,polynomial p2) {
    
    
	//多项式减法:p1=p1-p2,利用两个多项式的节点构成“和多项式”
	polynomial t = p1;	//t指向p1的头节点
	polynomial flag = t;	//用来标记p1的头节点
	p1 = p1->next;
	p2 = p2->next;	//p1,p2分别指向首元节点
	while (p1 && p2) {
    
    	//p1,p2均非空时运行
		if (p1->expn == p2->expn) {
    
    	//指数相等时
			p1->coef -= p2->coef;	//系数相减,存的p1里
			if (p1->coef) {
    
    	//coef不等于0时
				t->next = p1;	//将修改后的p1当前节点链在t之后,
				t = p1;	//t指向p1
				p1 = p1->next;	//p1指向下一项
				polynomial r = p2;	//临时存储p2节点
				p2 = p2->next;	//p2后移
				delete r;	//删除原p2节点
			}
			else {
    
    	//系数差为0
				polynomial r = p1;	//临时存储p1节点
				p1 = p1->next;	//p1后移
				delete r;	//删除原p1节点
				polynomial rr = p2;	//临时存储p2节点
				p2 = p2->next;	//p2后移
				delete rr;	//删除原p2节点
			}
		}
		else if (p1->expn < p2->expn) {
    
    	//p1指数小于p2指数
			t->next = p1;	//将修改后的p1当前节点链在t之后,
			t = p1;	//t指向p1
			p1 = p1->next;	//指向下一项
		}
		else {
    
    	//p1的指数大于p2的指数
			p2->coef *= -1;	//系数要乘-1变成相反数
			t->next = p2;	//将修改后的p2当前节点链在t之后,
			t = p2;	//t指向p2
			p2 = p2->next;	//指向下一项
		}
	}	//while
	if (p1)
		t->next = p1;	//插入剩余片段
	if (p2) 
		while (p2) {
    
    
			p2->coef *= -1;	//系数要乘-1变成相反数
			t->next = p2;	//将修改后的p2当前节点链在t之后,
			t = p2;	//t指向p2
			p2 = p2->next;	//指向下一项
		}
	cout << "相减的结果为:" << endl;
	printout(flag);	//输出结果
	delete p2;	//释放p2空间
}
//排序函数
void sort(polynomial p,polynomial head) {
    
    
	p = head;
	polynomial t1 = p->next;
	polynomial t2 = p->next;
	polynomial t3 = NULL;
	float ct;
	int et;
	//冒泡排序
	while (t2!=t3) {
    
    	//t1非空
		while (t1->next!=t3) {
    
    
			if ((t1->expn) > (t1->next->expn)) {
    
    
				ct = t1->coef;
				t1->coef = t1->next->coef;
				t1->next->coef = ct;
				et = t1->expn;
				t1->expn=t1->next->expn;
				t1->next->expn = et;
			}//if
			t1 = t1->next;
		}	//while
		t3 = t1;
		t1 = p->next;	//t1重置
	}//while
}	

//多项式乘法
void multiply_polynomial(polynomial& p1, polynomial& p2) {
    
    
	//先相乘,再排序,后合并
	polynomial head2 = p2;	//标记p2头节点
	polynomial head = new PNode;
	polynomial flag;
	flag = head;
	//先相乘
	p1 = p1->next;
	p2 = p2->next;
	while (p1) {
    
    //为NULL时退出循环
		while (p2) {
    
    //为NULL时退出循环
			polynomial t = new PNode;
			t->coef = p1->coef * p2->coef;	//系数相加,存到t中
			t->expn = p1->expn + p2->expn;	//指数相加
			flag->next = t;
			t->next = NULL;
			flag = t;
			p2 = p2->next;
		}
		p1 = p1->next;
		p2 = head2->next;	//重置p2
	} 	//while
	//再排序
	sort(flag, head);
	//合并
	for (polynomial q = head->next; q && q->next;) {
    
    
		if (q->expn == q->next->expn) {
    
    
			q->coef += q->next->coef;	//系数相加
			q->next = q->next->next;	//后一位插入
		}
		else q = q->next;
	}	//for
	cout << "相乘的结果为:" << endl;
	printout(head);	//输出结果
}

//菜单函数
void display() {
    
    
	cout << "========================" << endl;
	cout << "1.创建多项式" << endl;
	cout << "2.多项式加法" << endl;
	cout << "3.多项式减法" << endl;
	cout << "4.多项式乘法" << endl;
	cout << "0.退出" << endl;
	cout << "========================" << endl;
}

int main() {
    
    
	int n;
	polynomial p1 = new PNode;	//为链表P1申请一个空间;
	polynomial p2 = new PNode;	//为链表P2申请一个空间;
	display();
	while (true) {
    
    
		cout << "请输入要执行的操作:";
		cin >> n;
		switch (n) {
    
    
		case 1:
			creatlist(p1);
			cout << "第一个多项式为:";
			printout(p1);
			cout << endl;
			creatlist(p2);
			cout << "第二个多项式为:";
			printout(p2);
			cout << endl;
			break;
		case 2:
			//多项式相加
			add_polynomial(p1,p2);
			cout << endl;
			break;
		case 3:
			//多项式减法
			subtruct_polynomial(p1,p2);
			cout << endl;
			break;
		case 4:
			//多项式相乘
			multiply_polynomial(p1,p2);
			cout << endl;
			break;
		case 0:
			exit(1);
		default:
			cout << "输入不合法,请重新输入!" << endl;
			cout << endl;
			break;
		}
	}
	return 0;
}

4. Operation results

Test content:
Insert image description here
Insert image description here

5. Summary

There are still many shortcomings in this program. Why can't the "+" in the last term of the polynomial be deleted using '\b'? The addition, subtraction and multiplication of polynomials do not use storage space, so each operation needs to be re-entered, etc. If anyone I know why the "+" in the last term when outputting a polynomial cannot be deleted using '\b'. I hope you can teach me.
Insert image description here

Guess you like

Origin blog.csdn.net/qq_54388490/article/details/123612031