一、实验目的
1、 了解线性表的特性,以及它在实际问题中的应用。 2、掌握线性表的链式存储的实现方法以及它的基本操作,学会运用单链表来解决问题。
二、实验内容
题目:用带头节点的单链表存储一元多项式的每一项,实现求一元多项式的加法、减法和乘法。具体要求为:用五个函数分别实现一元多项式的创建、输出、加法、减法和乘法;然后在主函数中调用这些函数实现这些功能的展示,以菜单的形式呈现。
三、代码内容
1、多项式创建
可以乱序输入多项式,在创建时自动会升序排序
void creatlist(polynomial &p) {
p = new PNode;
int n;
cout << "请输入多项式项数:";
cin >> n;
p->next = NULL;
polynomial t,q;
for (int i = 1; i <= n; i++) {
polynomial s = new PNode;
cout << "请分别输入系数和指数: " ;
cin >> s->coef >> s->expn;
t = p;
q = p->next;
while (q && q->expn < s->expn) {
t = q;
q = q->next;
}
s->next = q;
t->next = s;
}
}
2、输出函数
最后一项的输出带有“+”而我用’\b’并没有消除加号,我只好先遍历出多项式系数再单独输出最后一项
void printout(polynomial p) {
int n = 0;
polynomial t = p;
while (t=t->next) {
n++;
}
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、多项式加减法
加法和减法的实现没有太大变化 这里我为了节约空间没有创建储存空间,把结果保存到的p1的空间中 这也导致了后续想要进行加减法运算需要重建输入多项式
void add_polynomial(polynomial p1,polynomial p2) {
polynomial t = p1;
polynomial flag = t;
p1 = p1->next;
p2 = p2->next;
while (p1 && p2) {
if (p1->expn == p2->expn) {
p1->coef += p2->coef;
if (p1->coef) {
t->next = p1;
t = p1;
p1 = p1->next;
polynomial r = p2;
p2 = p2->next;
delete r;
}
else {
polynomial r = p1;
p1 = p1->next;
delete r;
polynomial rr = p2;
p2 = p2->next;
delete rr;
}
}
else if (p1->expn < p2->expn) {
t->next = p1;
t = p1;
p1 = p1->next;
}
else {
t->next = p2;
t = p2;
p2 = p2->next;
}
}
t->next = p1 ? p1 : p2;
cout << "相加的结果为:" << endl;
printout(flag);
delete p2;
}
void subtruct_polynomial(polynomial p1,polynomial p2) {
polynomial t = p1;
polynomial flag = t;
p1 = p1->next;
p2 = p2->next;
while (p1 && p2) {
if (p1->expn == p2->expn) {
p1->coef -= p2->coef;
if (p1->coef) {
t->next = p1;
t = p1;
p1 = p1->next;
polynomial r = p2;
p2 = p2->next;
delete r;
}
else {
polynomial r = p1;
p1 = p1->next;
delete r;
polynomial rr = p2;
p2 = p2->next;
delete rr;
}
}
else if (p1->expn < p2->expn) {
t->next = p1;
t = p1;
p1 = p1->next;
}
else {
p2->coef *= -1;
t->next = p2;
t = p2;
p2 = p2->next;
}
}
if (p1)
t->next = p1;
if (p2)
while (p2) {
p2->coef *= -1;
t->next = p2;
t = p2;
p2 = p2->next;
}
cout << "相减的结果为:" << endl;
printout(flag);
delete p2;
}
4、冒泡排序
为了后续乘法运算做铺垫
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) {
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;
}
t1 = t1->next;
}
t3 = t1;
t1 = p->next;
}
}
5、多项式乘法
乘法运算:先相乘,然后再排好序,最后再合并同类项
void multiply_polynomial(polynomial& p1, polynomial& p2) {
polynomial head2 = p2;
polynomial head = new PNode;
polynomial flag;
flag = head;
p1 = p1->next;
p2 = p2->next;
while (p1) {
while (p2) {
polynomial t = new PNode;
t->coef = p1->coef * p2->coef;
t->expn = p1->expn + p2->expn;
flag->next = t;
t->next = NULL;
flag = t;
p2 = p2->next;
}
p1 = p1->next;
p2 = head2->next;
}
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;
}
cout << "相乘的结果为:" << endl;
printout(head);
}
6、总代码
#include<iostream>
using namespace std;
typedef struct PNode{
float coef;
int expn;
struct PNode *next;
}PNode,*polynomial;
void creatlist(polynomial &p) {
p = new PNode;
int n;
cout << "请输入多项式项数:";
cin >> n;
p->next = NULL;
polynomial t,q;
for (int i = 1; i <= n; i++) {
polynomial s = new PNode;
cout << "请分别输入系数和指数: " ;
cin >> s->coef >> s->expn;
t = p;
q = p->next;
while (q && q->expn < s->expn) {
t = q;
q = q->next;
}
s->next = q;
t->next = s;
}
}
void printout(polynomial p) {
int n = 0;
polynomial t = p;
while (t=t->next) {
n++;
}
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) {
polynomial t = p1;
polynomial flag = t;
p1 = p1->next;
p2 = p2->next;
while (p1 && p2) {
if (p1->expn == p2->expn) {
p1->coef += p2->coef;
if (p1->coef) {
t->next = p1;
t = p1;
p1 = p1->next;
polynomial r = p2;
p2 = p2->next;
delete r;
}
else {
polynomial r = p1;
p1 = p1->next;
delete r;
polynomial rr = p2;
p2 = p2->next;
delete rr;
}
}
else if (p1->expn < p2->expn) {
t->next = p1;
t = p1;
p1 = p1->next;
}
else {
t->next = p2;
t = p2;
p2 = p2->next;
}
}
t->next = p1 ? p1 : p2;
cout << "相加的结果为:" << endl;
printout(flag);
delete p2;
}
void subtruct_polynomial(polynomial p1,polynomial p2) {
polynomial t = p1;
polynomial flag = t;
p1 = p1->next;
p2 = p2->next;
while (p1 && p2) {
if (p1->expn == p2->expn) {
p1->coef -= p2->coef;
if (p1->coef) {
t->next = p1;
t = p1;
p1 = p1->next;
polynomial r = p2;
p2 = p2->next;
delete r;
}
else {
polynomial r = p1;
p1 = p1->next;
delete r;
polynomial rr = p2;
p2 = p2->next;
delete rr;
}
}
else if (p1->expn < p2->expn) {
t->next = p1;
t = p1;
p1 = p1->next;
}
else {
p2->coef *= -1;
t->next = p2;
t = p2;
p2 = p2->next;
}
}
if (p1)
t->next = p1;
if (p2)
while (p2) {
p2->coef *= -1;
t->next = p2;
t = p2;
p2 = p2->next;
}
cout << "相减的结果为:" << endl;
printout(flag);
delete 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) {
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;
}
t1 = t1->next;
}
t3 = t1;
t1 = p->next;
}
}
void multiply_polynomial(polynomial& p1, polynomial& p2) {
polynomial head2 = p2;
polynomial head = new PNode;
polynomial flag;
flag = head;
p1 = p1->next;
p2 = p2->next;
while (p1) {
while (p2) {
polynomial t = new PNode;
t->coef = p1->coef * p2->coef;
t->expn = p1->expn + p2->expn;
flag->next = t;
t->next = NULL;
flag = t;
p2 = p2->next;
}
p1 = p1->next;
p2 = head2->next;
}
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;
}
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;
polynomial p2 = new PNode;
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;
}
四、运行结果
测试内容:
五、总结
这个程序还有很多不足之处,输出多项式时最后一项的“+”为什么不能用‘\b’删除,多项式的加减乘没有使用储存空间导致每次运算都需要重新输入等等,如果有人知道为什么输出多项式时最后一项的“+”为什么不能用‘\b’删除,希望能教教我。
|