数据结构(C语言)用单链表存储一元多项式,并实现两个多项式的相加运算

合集下载

C语言单链表实现多项式相加

C语言单链表实现多项式相加

C语⾔单链表实现多项式相加本⽂实例为⼤家分享了C语⾔单链表实现多项式相加的具体代码,供⼤家参考,具体内容如下//多项式的相加和相乘#include<stdio.h>#include<stdlib.h>#pragma warning(disable:4996)//兼容scanftypedef struct node {int coef;int expon;struct node* link;}Polynode,*Polynomial;Polynomial InsertPolyLinklist(Polynomial in,Polynomial Pread) {Pread->link = in;Pread = in;in->link = NULL;return Pread;}Polynomial ReadPoly(void) {Polynomial Pread = (Polynomial)malloc(sizeof(Polynode));Pread->link = NULL;Polynomial H = Pread;int N;scanf("%d ", &N);while (N--) {Polynomial p = (Polynomial)malloc(sizeof(Polynode));scanf("%d %d", &p->coef, &p->expon);Pread= InsertPolyLinklist(p,Pread);}Polynomial F;F = H->link;free(H);return F;}void PrintPoly(Polynomial F) {while(F != NULL) {printf("%d %d ", F->coef, F->expon);F = F->link;}printf("\n");}Polynomial Add(Polynomial p1, Polynomial p2) {Polynomial t1=p1,t2=p2;Polynomial p=(Polynomial)malloc(sizeof(Polynode));p->link = NULL;Polynomial q = p;Polynomial read;while (t1&&t2) {if (t1->expon == t2->expon) {if (t1->coef + t2->coef) {t1->coef = t1->coef + t2->coef;t1->expon = t1->expon;read = t1;q->link = read;q = read;t1 = t1->link;t2 = t2->link;}}else {if (t1->expon > t2->expon){read = t1;q->link = read;q = read;t1 = t1->link;}else {if (t1->expon < t2->expon) {read = t2;q->link = read;q = read;t2 = t2->link;}}}}if (t1) {q->link = t1;}if (t2) {q->link = t2;}Polynomial F = p->link;free(p);return F;}int main(void) {Polynomial p1, p2, pp, ps;p1 = ReadPoly();PrintPoly(p1);p2 = ReadPoly();PrintPoly(p2);pp = Add(p1, p2);PrintPoly(pp);// ps = Mult(p1, p2);// PrintPoly(ps);return 0;}参考MOOC 浙⼤数据结构以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

数据结构C语言实现之一元多项式的表示及相加(2)

数据结构C语言实现之一元多项式的表示及相加(2)
以单链表作为存储结构并且和多项式中的结点无需另生成则可看成是将多项式b加到多项式a中由此得到下列运算规则设pq分别指向多项式ab的一项比较结点的指数项若pexpqexp则结点p所指的结点应是和多项式中的一项令指针p后移
数据结构 C 语言实现之一元多项式的表示及相加(2)
一元多项式的表示及相加 对于符号多项式的各种操作,实际上都可以利用线性表来处理。比较典型的是关于一元多项式的处理。在
} } e>next; /*将 q 结点加入到和多项式中*/ q =q->next; } }
} if(p!=NULL)/*多项式 A 中还有剩余,则将剩余的结点加入到和多项式中*/
pre->next=p;
else /*否则,将 B 中的结点加入到和多项式中*/ pre->next=q; }
算法 2.24 多项式相加 假设 A 多项式有 M 项,B 多项式有 N 项,则上述算法的时间复杂度为 O(M+N) 图 2.20 所示为图 2.19 中两个多项式的和,其中孤立的结点代表被释放的结点。
通过对多项式加法的介绍,我们可以将其推广到实现两个多项式的相乘,因为乘法可以分解为一系列的加 法运算。
“中的结点无需另生成,则可看成是将多项式 B 加到多项式 A 中,由此得到下列运算规则(设 p、q 分别 指向多项式 A,B 的一项,比较结点的指数项)
若 p->exp< q->exp,则结点 p 所指的结点应 是“和多项式”中的一项,令指针 p 后移;若 p>exp>q->exp,则结点 q 所指的结点应是“和多项式”中的一项,将结点 q 插入在结点 p 之前, 且令指针 q 在原来的链表上后移;
(1)用单链表存储多项式的结点结构如下: struct Polynode { int coef; int exp; Polynode *next; } Polynode , * Polylist;

一元多项式(用其中的一个链表存储)

一元多项式(用其中的一个链表存储)
{
int ceof; //系数
int expn; //指数
struct ploynode *next;
}node,*Linklist;
/*创建链表*/
Linklist creat(int n)
{
Linklist p,newnode,head;
int i;
p->next=newnode;
p=newnode;
}
p->next=NULL;
return head;
}
/*输出链表*/
void print(Linklist h)
{
Linklist p,q;
q=h;
p=q->next;
if(p)
while(p!=NULL)
{
printf("%dX^%d",p->ceof,p->expn);
p=p->next;
if(p!=NULL)
printf("+");
}
}
/*相加函数*/
void ploy_add(Linklist La,Linklist Lb) //将所加的函数保存到La中
Linklist list2=creat(4);
printf("第一个一元多项式为:");
print(list1);
printf("\n第二个一元多项式为:");
print(list2);
printf("\n相加为");
ploy_add(list1,list2);
{

多项式相加c语言

多项式相加c语言
Print(&sum);
reset(&p);
reset(&q);
reset(&sum);
return 0;
}
```
在上述代码中,首先定义了一个`Node`结构体,包含多项式的系数和指数,并通过链表的方式存储多项式的每一项。然后,实现了一系列函数,包括多项式的增加、输出、相加和消除。在`main`函数中,调用这些函数来完成多项式的输入、相加和输出。
请注意,这只是一个简单的示例,实际应用中可能需要考虑更多的边界情况和错误处理。
以下是一个简单的 C 语言示例,展示了如何实现多项式相加:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义结构的数据部分
struct poly {it zhishu;float xishu;
};
// 定义一个多项式结构
typedef struct node {
s->date.xishu = s->date.xishu + s->next->date.xishu;
s->next = s->next->next;
} else {
s = s->next;
}
}
d = d->next;
}
}
// 链表的消除函数
void reset(Node *pList) {
Node *p = pList;
while (p->next) {
if (flog == 1)
printf(" + ");
else {

数据结构C语言描述——用单链表实现多项式的相加

数据结构C语言描述——用单链表实现多项式的相加

数据结构C语⾔描述——⽤单链表实现多项式的相加#include <stdio.h>#include <stdlib.h>typedef DataType;typedef struct Node2{DataType xishu;DataType zhisu;struct Node2 *Next;}Node2;typedef struct Node2* PNode2;//多项式按照指数⼤⼩排序void insertNewPoint_link(PNode2 head,PNode2 qNode){PNode2 p=head;while (p->Next!=NULL){if (p->Next->zhisu>qNode->zhisu){qNode->Next=p->Next;p->Next=qNode;break;}p=p->Next;}if (p->Next==NULL){p->Next=qNode;}}//打印多项式void printLinkeLink(PNode2 head){PNode2 temp=head->Next;while (temp!=NULL){printf("%d %d",temp->xishu,temp->zhisu);printf("\n");temp=temp->Next;}}//多项式的加法计算void add_poly(Node2 *pa,Node2 *pb){Node2 *p=pa->Next;Node2 *q=pb->Next;Node2 *pre=pa;Node2 *u;while (p!=NULL&&q!=NULL){if (p->zhisu<q->zhisu){pre=p;p=p->Next;}else if(p->zhisu==q->zhisu){float x=p->xishu+q->xishu;if (x!=0){p->xishu=x;pre=p;}else{pre->Next=p->Next;//指向下⼀个结点free(p);}p=pre->Next;u=q;q=q->Next;free(u);}else{u=q->Next;q->Next=p;pre->Next=q;pre=q;q=u;}}if (q){pre->Next=q;}free(pb);}void main( ){int zhishu;float xishu;PNode2 head1=(PNode2)malloc(sizeof(struct Node2));PNode2 head2=(PNode2)malloc(sizeof(struct Node2));PNode2 tem=NULL;head1->Next=NULL;head2->Next=NULL;printf("输⼊链表⼀的系数和指数,如:3,2 以0,0结束输⼊:\n"); scanf("%f,%d",&xishu,&zhishu);while (xishu!=0||zhishu!=0){tem=(PNode2)malloc(sizeof(struct Node2));tem->xishu=xishu;tem->zhisu=zhishu;tem->Next=NULL;insertNewPoint_link(head1,tem);scanf("%f,%d",&xishu,&zhishu);}printf("链表⼀按指数升序排序后的多项式为:\n");printLinkeLink(head1);printf("\n");printf("输⼊链表⼀的系数和指数,如:3,2 以0,0结束输⼊:\n"); scanf("%f,%d",&xishu,&zhishu);while (xishu!=0||zhishu!=0){tem=(PNode2)malloc(sizeof(struct Node2));tem->xishu=xishu;tem->zhisu=zhishu;tem->Next=NULL;insertNewPoint_link(head2,tem);scanf("%f,%d",&xishu,&zhishu);}printf("链表⼆按指数升序排序后的多项式为:\n");printLinkeLink(head2);printf("\n");add_poly(head1,head2);printf("多项式相加后的结果为:\n");printLinkeLink(head1);}。

数据结构-单链表-多项式相加

数据结构-单链表-多项式相加

数据结构-单链表-多项式相加【问题描述】编写⼀个程序⽤单链表存储多项式,并实现两个⼀元多项式A与B相加的函数。

A,B刚开始是⽆序的,A与B之和按降序排列。

例如:多项式A: 1.2X^0 2.5X^1 3.2X^3 -2.5X^5多项式B: -1.2X^0 2.5X^1 3.2X^3 2.5X^5 5.4X^10多项式A与B之和:5.4X^10 6.4X^3 5X^1【输⼊形式】任意两个多项式A和B的项数及对应的系数和指数,要查询的第⼏项【输出形式】多项式中某⼀项的系数与指数,系数保留⼀位⼩数【输⼊样例】4 1.2 0 2.5 1 3.2 3 -2.5 55 -1.2 0 2.5 1 3.2 3 2.5 5 5.4 102【输出样例】6.4 31 #include<bits/stdc++.h>2 #include<stdlib.h>3using namespace std;4 typedef long long ll;56struct node{7double data;8int index;9 node* next;10 node():index(0){}11 node* operator [] (int n){12 node* end=next;13while(end&&n--)end=end->next;14return end;15 }16bool operator < (const node &t) const {17return index>t.index;18 }19 node operator * (node& t);20 };2122void newList(node & head,int length){23 node *a=new node[length];//这是这个函数的解释node* operator [] (int n)P11⾏申请空间的⽅式 new int[10]申请10个int类型的空间再返回node指针类型24for(int i=0;i<length;++i)cin>>a[i].data>>a[i].index;25//a是node类型数组啊26 sort(a,a+length);//p16⾏的函数解释27 node* end=&head;28for(int i=0;i<length;++i){29 node* t=new node;30 t->data=a[i].data;31 t->index=a[i].index;32 end->next=t;33 end=t;34 }//他这好像就特别简单 end=xx 之后就申请了新节点赋值然后挂上去35delete[] a;36 }37void show(node& head){//传递的这个是引⽤38 node* end=head.next;//就还是这个类型所以⽤的是.39while(end){40if(end->index==1) cout<<end->data<<"X^"<<(end->next?" + ":"\n");41else cout<<end->data<<"X^"<<end->index<<(end->next?" + ":"\n");//末尾加的这个的意思是如果下⼀个节点还有就+上如果没有就换⾏42 end=end->next;43 }44 }4546///多项式相加:47void combine(node& a, node& b){//传递的是引⽤48 node* p,*q,*tail,*temp;49double s;50 p=a.next;51 q=b.next;52 tail=&a;//就直接修改a链表了53while(p&&q){54if(p->index>q->index){55 tail->next=p;tail=p;p=p->next;56 }else if(p->index==q->index){57 s=p->data+q->data;58if(s){59 p->data=s;60 tail->next=p; tail=p;p=p->next;61 temp=q;q=q->next;delete temp;62 }else{63 temp=p;p=p->next;delete temp;64 temp=q;q=q->next;delete temp;65 }//删除没有⽤的节点这点甚是符合朕⼼厉害66 }else{67 tail->next=q; tail=q; q=q->next;68 }69 }70if(p)tail->next=p;71else tail->next=q;72 }7374int main(){75 node a,b;76int n1,n2;cin>>n1;77 newList(a,n1);78 cin>>n2;79 newList(b,n2);80 combine(a,b);8182 cin>>n1;83 cout<<fixed<<setprecision(1)<<a[n1-1]->data<<""<<a[n1-1]->index<<endl;//这⾥可以这么骚也是因为p11⾏⽜逼84 show(a);//给引⽤传参数就是传本⾝不是传指针85 }。

单链表实现两个多项式的求和

单链表实现两个多项式的求和

北京理工大学珠海学院《数据结构》课程设计报告题目:用重概念的单链表存储一元多项式,并实现两个多项式的相加运算所在学院:运算机科学技术学院专业班级:学生姓名:指导教师:2010年月日目录1.前言 (5)2.概要设计 (5)数据结构设计 (5)3.详细设计 (6)3.1 算法设计 (6)3.1.1 成立链表的算法 (6)3.1.2 链表插入一个元素的算法 (6)3.1.3 一元多项式的相加 (6)ADT描述 (7)功能模块分析 (7)数据存储结构设计: (8)主要算法流程图(流程图在下一页) (8)3.软件测试 (10)5.设计体会 (15)致谢: (16)参考文献: (16)附录: (16)1.前言数据结构是运算机程序设计的重要理论技术基础,它不仅是运算机学科的核心课程,学会分析研究运算机加工的数据结构的特性,以便为应用涉及的数据选择适当的逻辑结构及其相应的算法并初步掌握算法的时刻分析和空间分析的技术。

在许多类型的程序的设计中,数据结构的选择是一个大体的设计考虑因素。

许多大型系统的构造经验表明,系统实现的困难程度和系统构造的质量都严峻的依赖于是不是选择了最优的数据结构。

许多时候,肯定了数据结构后,算法就容易患到了。

有些时候情形也会反过来,咱们按照特定算法来选择数据结构与之适应。

不论哪一种情形,选择适合的数据结构都是超级重要的。

2.概要设计数据结构设计数据的链接存储表示又被称为链接表。

当链接表中的每一个结点只含有一个指针称为单链表。

在数据的顺序存储中,由于每一个元素的存储位置都能够通过简单计算取得,所以元素的时刻都相同;而在数据的链接存储中,由于每一个元素的存储位置是保留在它的{或后继结点中的,所以只有当访问到其前驱结点或后继结点后才能够按指针访问到自访问任一元素的时刻与该元素结点在链接存储中的位置有关。

线性表是最大体、最简单、也是最常常利用的一种数据结构。

线性表中数据元素之间的关系是一对一的关系,即除第一个和最后一个数据元素之外,其它数据元素都是首尾相接的。

(链表实现)写出两个一元多项式相加的算法

(链表实现)写出两个一元多项式相加的算法

(链表实现)写出两个⼀元多项式相加的算法int c, e;head=new node; //⽣成头结点head->next=NULL;cout<<"请分别输⼊新的⼀项的系数、指数(以输⼊9999作为结束):"<<endl;cin>>c>>e;while(c!=9999 && e!=9999) //输⼊并检测结束{s=new node; //⽣成新结点s->co = c; //装⼊数据s->exp = e;p=head;while(p->next!=NULL){p=p->next;}p->next = s; //插到最后⼀个结点后s->next = NULL;cout<<"请分别输⼊新的⼀项的系数、指数(以输⼊9999作为结束):"<<endl;cin>>c>>e;}return head;}void Display(node * head){if(head==NULL){cout<<"多项式不存在!"<<endl;return ;}node* p;p=head->next;while(p!=NULL){if(p->co>0)cout<<p->co<<"x^"<<p->exp;elsecout<<"("<<p->co<<")"<<"x^"<<p->exp;if(p->next!=NULL)cout<<"+";p=p->next;}cout<<endl<<endl;}node* Add(node * A, node * B){node * C=new node;C->next=NULL;node * p1=A->next;node * p2=B->next;node * p3;node * rearC=C;while(p1!=NULL && p2!=NULL){。

一元多项式相加 数据结构实验报告

一元多项式相加 数据结构实验报告

南昌航空大学实验报告课程名称:数据结构实验名称:实验二线性表的链式存储结构班级:080611 学生姓名:学号:08指导教师评定:签名:题目:设计并实现以下算法:给出用单链表存储多项式的结构,利用后接法生成多项式的单链表结构,实现两个多项式相加的运算,并就地逆置相加后的多项式链式。

一、需求分析1.用户可以根据自己的需求分别输入两个一元多项式,并且能够实现输入的一元多项式的显示。

2.能够完成两个一元多项式的相加功能,而且还能显示相加后的逆置的一元多项式。

3.程序执行的命令包括:(1)构造链表A (2)构造链表B (3)两个链表的相加(4)求链表的长度(5)打印(显示)已有的链表(6)将已相加的链表进行逆序排列二、概要设计⒈为实现上述算法,需要线性表的抽象数据类型:ADT Polynomial {数据对象:D={a i:|a i∈TermSet,i=1…n,n≥0TermSet 中的每个元素包含一个表示系数的实数和表示指数的整数} 数据关系:R1={<a i-1,a i>|a i-1,a i∈D,且a i-1中的指数值< a i中的指数值i=2,…n≥0}基本操作:initlink(& L)操作结果:构造一个空的链表L。

Creatlink(&L,n)操作结果:输入n项的系数和指数,建立一个非空的一元多项式L。

LinkLength(L)初始条件:链表L已经存在。

操作结果:返回一元多项式L中的项数。

Displaylink(L)初始条件:链表L已经存在。

操作结果:打印输出一元多项式L。

Addpolyn(A,B,&C)初始条件:一元多项式A和B已存在。

操作结果:完成多项式相加运算,即:C=A+B,并且带回C。

subtracypolyn(&La,&Lb,)初始条件:一元多项式La和Lb已存在。

操作结果:完成多项式的相减运算La=La+Lb,并且销毁多项式Lb。

数据结构单链表实现多项式加法的算法实现

数据结构单链表实现多项式加法的算法实现

上机实习三一,实验题目:单链表多项式加法算法实现二,实验目的:结合实际问题掌握线性表链式存储结构的C语言描述及运算算法的实现。

设计一个一元多项式加法器,界面为菜单形式。

程序功能:1、(菜单)主程序2、输入并建立多项式3、多项式a和b相加,建立多项式a+b。

4、输出多项式,输出形式为整数序列n,c1,e1,c2,e2,…,cn,en。

n是多项式的项数,ci、ei分别是第i项的系数和指数。

三,功能层次图主函数调用创建函数创建A和B多项式调用加多项式函数加A和B调用输出函数程序结束四,运行结果创建A表创建B表加两个A,B的多项赋给C式A,B,C的结果五,小结在做这个程序的时候,虽然遇到一些问题,但最后都被我解决,自信心上得到比较大的提升,这也是这次实践最大的收获。

同时,知识上的收获也是不可忽视的,亲手解决问题的过程也是很好的学习过程,并且积累了一些经验,相信会为以后的学习发展带来非常积极的帮助。

源代码:#include<stdio.h>#include<conio.h>#include<alloc.h>#include<stdlib.h>typedef struct pnode{float coef; /* xishu */int exp; /* zhishu */struct pnode *next;}polynode;polynode *createList(){float xishu;int zhishu;polynode *head,*r;r=malloc(sizeof(polynode));r->coef=0;r->exp=-1;r->next=r;head=r;printf("<<Input XiShu 111 to stop>>\n");printf("Input XiShu:");scanf("%f",&xishu);printf("Input ZhiShu:");scanf("%d",&zhishu);while(1){r->next=malloc(sizeof(polynode));r=r->next;r->coef=xishu;r->exp=zhishu;printf("Input XiShu:");scanf("%f",&xishu);if(xishu==111) break;printf("Input ZhiShu:");scanf("%d",&zhishu);}r->next=head;return head;}void display(polynode *head){int i=1;polynode *t;if(head==NULL){printf("List is empty\n");return;}t=head->next;printf("[%0.0f][%d]\t",head->coef,head->exp);while(1){printf("[%0.0f][%d]\t",t->coef,t->exp);t=t->next;i++;if(t==head) break;}}polynode *POLYADD(polynode *A,polynode *B) {int i,j,k;polynode *ptr,*q,*q1,*q2;float x;q1=A;q2=B;q=malloc(sizeof(polynode));q->coef=0;q->exp=-1;q->next=q;ptr=q;q1=q1->next;q2=q2->next;while((q1!=A)&&(q2!=B)){if(q1->exp==q2->exp){x=q1->coef+q2->coef;if(x!=0){q->next=malloc(sizeof(polynode));q=q->next;q->coef=x;q->exp=q1->exp;}q1=q1->next;q2=q2->next;}else{q->next=malloc(sizeof(polynode));q=q->next;if(q1->exp>q2->exp){q->coef=q2->coef;q->exp=q2->exp;q2=q2->next;}else{q->coef=q1->coef;q->exp=q1->exp;q1=q1->next;}}}while(q1!=A){q->next=malloc(sizeof(polynode));q=q->next;q->coef=q1->coef;q->exp=q1->exp;q1=q1->next;}while(q2!=B){q->next=malloc(sizeof(polynode));q=q->next;q->coef=q2->coef;q->exp=q2->exp;q2=q2->next;}q->next=ptr;return ptr;}char caiDan(){char ch;do{printf("1:Create A list\n");printf("2:Create B list\n");printf("3:Add Two Polylist\n");printf("4:Display Polylist\n");printf("5:Exit\n");printf("Please Choose:");}while(ch=getch(),ch!='1'&&ch!='2'&&ch!='3'&&ch!='4'&&ch!='5');return ch;}void main(){polynode *a,*b,*c;char ch;do{clrscr();ch=caiDan();printf("%c",ch);getch();printf("\n");switch(ch){case '1': printf("Create A list\n");a=createList();printf("A list was created successfully");getch();break;case '2': printf("Create B list\n");b=createList();printf("B list was created successfully");getch();break;case '3': c=POLYADD(a,b);printf("C(C=A+B) list was created successfully\n");getch();break;case '4': printf("A List:\t");display(a);printf("\nB List:\t");display(b);printf("\nC List:\t");display(c);getch();break;case '5': exit(0);}}while(ch!='5');}。

实验报告编写一个程序用单链表存储多项式,并实现两个多项式相加的函数

实验报告编写一个程序用单链表存储多项式,并实现两个多项式相加的函数

实验报告编写一个程序用单链表存储多项式,并实现两个多项式相加的函数/*多项式加法和乘法示例*/#include<list>#include<iostream>#include<cassert>usingnamespacestd;//定义多项式的项类classterm{public:intcoef;//多项式系数intexp;//多项式指数//初始化项的系数和指数term(intc=0,inte=0):coef(c),exp(e){}};//定义多项式类classPolyArith{private:list<term>m_poly_list_first;//存储第一个多项式list<term>m_poly_list_second;//存储第二个多项式list<term>m_poly_list_result;//用以存储运算结果//多项式私有成员函数,用以乘法时的调用list<term>Poly_add(list<term>&poly_list_first,\list<term>&poly_list_second){list<term>poly_list_result;//用以存储运算结果list<term>::iteratoriter_first=poly_list_first.begin();list<term>::iteratoriter_second=poly_list_second.begin();//该while循环针对两个链表迭代器都没有指到结尾的情形while(iter_first!=poly_list_first.end()&&\iter_second!=poly_list_second.end()){termt_temp;termt_first=(term)*iter_first;termt_second=(term)*iter_second;if(t_first.exp>t_second.exp){poly_list_result.push_back(t_first);iter_first++;}elseif(t_second.exp>t_first.exp){poly_list_result.push_back(t_second);iter_second++;}else{t_temp.coef=t_first.coef+t_second.coef;t_temp.exp=t_first.coef;poly_list_result.push_back(t_temp);iter_first++;iter_second++;}}//该for循环针对第一个多项式的迭代器没有指到结尾//第二个指到结尾的情形for(;iter_first!=poly_list_first.end();iter_first++){poly_list_result.push_back(*iter_first);}//该for循环针对第二个多项式的迭代器没有指到结尾//第一个指到结尾的情形for(;iter_second!=poly_list_second.end();iter_second++){poly_list_result.push_back(*iter_second);}returnpoly_list_result;}public://输入函数,用以输入多项式voidPoly_input(){intn;cout<<"请输入第一个多项式的项数:"<<endl;cin>>n;cout<<"按降幂输入第一个多项式的每一项的系数和指数:"; cout<<endl;for(inti=1;i<=n;i++){termt_temp;cout<<"请输入第"<<i<<"项系数和指数,以'enter'为界:"; cout<<endl;cin>>t_temp.coef;cin>>t_temp.exp;m_poly_list_first.push_back(t_temp);}n=0;cout<<"请输入第二个多项式的项数:"<<endl;cin>>n;cout<<"按降幂输入第二个多项式的每一项的系数和指数:"; cout<<endl;for(intj=1;j<=n;j++){termt_temp;cout<<"请输入第"<<j<<"项系数和指数,以'enter'为界:"; cout<<endl;cin>>t_temp.coef;cin>>t_temp.exp;m_poly_list_second.push_back(t_temp);}}//输出函数,用以输出多项式voidPoly_output(){//用以指向输出多项式的第一个元素list<term>::iteratoriter=m_poly_list_result.begin();//输出多项式的每一项for(;iter!=m_poly_list_result.end();){termt_temp=*iter;cout<<t_temp.coef<<"x^"<<t_temp.exp;if(++iter!=m_poly_list_result.end())cout<<"+";}cout<<endl;}//加法函数,其基本思想同上边的私有成员函数Poly_add() //此处不带参数,多项式运算对象为私有数据成员voidPoly_add(){list<term>::iteratoriter_first=m_poly_list_first.begin();list<term>::iteratoriter_second=\m_poly_list_second.begin();while(iter_first!=m_poly_list_first.end()&&\iter_second!=m_poly_list_second.end()){termt_temp;termt_first=(term)*iter_first;termt_second=(term)*iter_second;if(t_first.exp>t_second.exp){m_poly_list_result.push_back(t_first);iter_first++;elseif(t_second.exp>t_first.exp){m_poly_list_result.push_back(t_second);iter_second++;}else{t_temp.coef=t_first.coef+t_second.coef;t_temp.exp=t_first.exp;m_poly_list_result.push_back(t_temp);iter_first++;iter_second++;}}for(;iter_first!=m_poly_list_first.end();iter_first++){m_poly_list_result.push_back(*iter_first);}for(;iter_second!=m_poly_list_second.end();iter_second++) {m_poly_list_result.push_back(*iter_second);}}//乘法函数,用以作多项式乘法voidPoly_multi(){list<term>poly_list_result;list<term>::iteratoriter_first=m_poly_list_first.begin();for(;iter_first!=m_poly_list_first.end();iter_first++){list<term>poly_list_temp;//用以存储多项式的中间运算结果list<term>::iteratoriter_second=\m_poly_list_second.begin();for(;iter_second!=m_poly_list_second.end();\iter_second++){termt_temp;//用以存储项的中间运算结果termt_first=(term)*iter_first;termt_second=(term)*iter_second;//此处实现多项式项的相乘t_temp.coef=t_first.coef*t_second.coef;//系数相乘t_temp.exp=t_first.exp+t_second.exp;//指数相加poly_list_temp.push_back(t_temp);//此处调用私有成员函数Poly_add()poly_list_result=\Poly_add(poly_list_temp,poly_list_result);}//将运算结果赋值给私有数据成员,用以输出m_poly_list_result=poly_list_result;}};//测试函数intmain(){cout<<"************本程序实现多项式的加法与乘法************"; cout<<endl;PolyArithpoly_a;poly_a.Poly_input();//输入多项式poly_a.Poly_add();//多项式加法cout<<"多项式加法的运算结果:"<<endl;poly_a.Poly_output();//输出多项式cout<<endl;poly_a.Poly_multi();//多项式乘法cout<<"多项式乘法的运算结果:"<<endl;poly_a.Poly_output();system("pause");return0;}。

一元多项式的相加(链表)

一元多项式的相加(链表)

#include<iostream>using namespace std;typedef struct PNode{float coef;int expn;struct PNode *next;}PNode,*Polynomial;void Addpolyn(Polynomial &pa,Polynomial &pb) //多项式加法:pa=pa+pb, 利用两个多项式的结点构成"和多项式" {float sum;Polynomial p1,p2,p3,r;p1=pa->next;p2=pb->next; //p1和p2初值分别指向pa和pb的第一个结点p3=pa;while(p1&&p2){if(p1->expn==p2->expn){sum=p1->coef+p2->coef;if(sum!=0){p1->coef=sum;p3->next=p1;p3=p1;p1=p1->next;r=p2;p2=p2->next;delete r;}else{r=p1;p1=p1->next;delete r;r=p2;p2=p2->next;delete r;}}else if(p1->expn<p2->expn){p3->next=p1;p3=p1;p1=p1->next;}else{p3->next=p2;p3=p2;p2=p2->next;}}p3->next=p1?p1:p2;delete pb;p3=pa->next;cout<<"合并后的系数为:"<<endl;while(p3){cout<<p3->coef<<endl;p3=p3->next;}p3=pa->next;cout<<"合并后的指数为:"<<endl;while(p3){cout<<p3->expn<<endl;p3=p3->next;}}void creatpolyn(Polynomial &p,int m){int i;Polynomial pre,q;p=new PNode;p->next=NULL;cout<<"请输入各项的系数和指数"<<endl;for(i=1;i<=m;++i){PNode *s;s=new PNode;cin>>s->coef>>s->expn;pre=p;q=p->next;while(q&&q->expn<s->expn){pre=q;q=q->next;}s->next=q;pre->next=s;}}int main(int argc, char* argv[]){int m,n;void creatpolyn(Polynomial &p,int m);void Addpolyn(Polynomial &pa,Polynomial &pb);PNode *a,*b;cout<<"请输入第一个多项式项数"<<endl;cin>>m;creatpolyn(a,m);cout<<"请输入第二个多项式项数"<<endl;cin>>n;creatpolyn(b,n);Addpolyn(a,b);cout<<"相加后的结果为:"<<endl;return 0;}。

C语言实现一元多项式相加(源代码)

C语言实现一元多项式相加(源代码)

C语言实现一元多项式相加(源代码)#include<stdio.h>#include<malloc.h>//动态申请空间的函数的头文件typedef struct node //定义节点类型{float coef; //多项式的系数int expn; //多项式的指数struct node * next; //结点指针域}PLOYList;void insert(PLOYList *head,PLOYList *input) //查找位置插入新链节的函数,且让输入的多项式呈降序排列{PLOYList *pre,*now;int signal=0;pre=head;if(pre->next==NULL) {pre->next=input;} //如果只有一个头结点,则把新结点直接连在后面else{now=pre->next;//如果不是只有一个头结点,则设置now指针while(signal==0){if(input->expn < now->expn){if(now->next==NULL){now->next=input;signal=1;}else{pre=now;now=pre->next;//始终让新输入的数的指数与最后一个结点中的数的指数比较,小于则插在其后面}}else if( input->expn > now->expn ){input->next=now;pre->next=input;signal=1;}//若新结点中指数比最后一个结点即now中的指数大,则插入now之前else//若指数相等则需合并为一个结点,若相加后指数为0则释放该结点{now->coef=now->coef+input->coef;signal=1;free(input);if(now->coef==0){pre->next=now->next;free(now);}}//else} //while}//else}//voidPLOYList *creat(char ch) //输入多项式{PLOYList *head,*input;float x;int y;head=(PLOYList *)malloc(sizeof(PLOYList)); //创建链表头head->next=NULL;scanf("%f %d",&x,&y);//实现用户输入的第一个项,包括其指数和系数while(x!=0)//当用户没有输入结束标志0时可一直输入多项式的项,且输入一个创建一个结点{input=(PLOYList *)malloc(sizeof(PLOYList)); //创建新链节input->coef=x;input->expn=y;input->next=NULL;insert(head,input); //每输入一项就将其排序,是的链表中多项式呈降序排列scanf("%f %d",&x,&y);}return head;}PLOYList *add(PLOYList *head,PLOYList *pre) //多项式相加,head为第一个多项式建立的链表表头,pre 为第二个多项式建立的链表表头{PLOYList *input;int flag=0;while(flag==0){if(pre->next==NULL)flag=1; //若该链表为空,则无需进行加法运算,跳出循环else{pre=pre->next;input=(PLOYList *)malloc(sizeof(PLOYList));//申请空间input->coef=pre->coef;input->expn=pre->expn;input->next=NULL;insert(head,input); // 把g(x)插入到f(x)中,相当于两者相加,结果保存于f(x) }}return head;}void print(PLOYList *fun) //输出多项式,fun指要输出的多项式链表的表头{PLOYList *printing;int flag=0;printing=fun->next;if(fun->next==NULL)//若为空表,则无需输出{printf("0\n");return;}while(flag==0){if(printing->coef>0&&fun->next!=printing)printf("+");if(printing->coef==1);else if(printing->coef==-1)printf("-");elseprintf("%f",printing->coef);if(printing->expn!=0) printf("x^%d",printing->expn);else if((printing->coef==1)||(printing->coef==-1))printf("1");if(printing->next==NULL)flag=1;elseprinting=printing->next;}printf("\n");}void start() //用户选择界面{printf(" * 1.两个一元多项式相加*\n");printf(" * 0.退出系统*\n");printf(" \n");printf(" 注:输入多项式格式为:系数1 指数1 系数2 指数2 ……,并以0 0 结束:\n");printf(" \n");printf(" 请选择操作: ");}void main(){PLOYList *f,*g,*pf,*hf,*p;int sign=-1;start();while(sign!=0){scanf("%d",&sign);switch(sign){case 0:break;case 1://多项式相加{printf(" 你选择的操作是多项式相加:\n");printf(" 请输入第一个多项式f(x):");f=creat('f');printf(" 第一个多项式为:f(x)=");print(f);printf(" 请输入第二个多项式g(x):");g=creat('g');printf(" 第二个多项式为:g(x)=");print(g);printf(" 结果为:F(x)=f(x)+g(x)=");f=add(f,g);print(f);printf("\n\n");printf(" 继续请选择相应操作,退出请按0. ");break;}}}}。

数据结构 :链表的应用-求两个一元多项式之和

数据结构  :链表的应用-求两个一元多项式之和

实验:链表的应用-求两个一元多项式之和班级:计科114学号:20111515415姓名:顾晴晴一·实验目的:熟悉链表的基本操作,掌握用线性链表表示多项式加法运算的方法。

二.实验内容:1.实验要求用单链表存储一元多项式,将两个存储一元多项式的单链表相加产生结果单链表。

2.源程序如下#include<stdio.h>#include<stdlib.h>typedef struct Listnode{int xishu;struct Listnode *next;int mi;}* Pnode;Pnode init(int t){Pnode phead = (Pnode)malloc(sizeof(Pnode));if(NULL == phead){exit(-1);}Pnode ptail = phead;ptail->next = NULL;int a,b;for(int i = 0; i < t; i++){Pnode pnew = (Pnode)malloc(sizeof(Pnode));if(NULL == pnew){exit(-1);}printf("输入该项的系数:");scanf("%d",&a);pnew->xishu = a;printf("输入幂数:");scanf("%d",&b);pnew->mi = b;ptail->next = pnew;pnew->next = NULL;ptail = pnew;}return phead;}Pnode jia(Pnode p1,Pnode p2){Pnode phead = (Pnode)malloc(sizeof(Pnode));if(NULL == phead){exit(-1);}Pnode ptail = phead;ptail->next = NULL;p1 = p1->next;p2 = p2->next;while(((p1 != NULL && p2 == NULL) || (p1 == NULL && p2 != NULL)) || p1 != NULL || p2 != NULL){if(p1 == NULL && p2 != NULL){Pnode pnew = (Pnode)malloc(sizeof(Pnode));if(NULL == pnew){exit(-1);}pnew->mi = p2->mi;pnew->xishu = p2->xishu;ptail->next = pnew;pnew->next = NULL;ptail = pnew;p2 = p2->next;}else if(p1 != NULL && p2 == NULL){Pnode pnew = (Pnode)malloc(sizeof(Pnode));if(NULL == pnew){exit(-1);}pnew->mi = p1->mi;pnew->xishu = p1->xishu;ptail->next = pnew;pnew->next = NULL;ptail = pnew;p1 = p1->next;}else if(p1->mi == p2->mi ){Pnode pnew = (Pnode)malloc(sizeof(Pnode));if(NULL == pnew){exit(-1);}pnew->xishu = p1->xishu + p2->xishu;pnew->mi = p1->mi;ptail->next = pnew;pnew->next = NULL;ptail = pnew;p1 = p1->next;p2 = p2->next;}else if(p1->mi > p2->mi ){Pnode pnew = (Pnode)malloc(sizeof(Pnode));if(NULL == pnew){exit(-1);}pnew->mi = p1->mi;pnew->xishu = p1->xishu;ptail->next = pnew;pnew->next = NULL;ptail = pnew;p1 = p1->next;}else if(p1->mi < p2->mi ){Pnode pnew = (Pnode)malloc(sizeof(Pnode));if(NULL == pnew){exit(-1);}pnew->mi = p2->mi;pnew->xishu = p2->xishu;ptail->next = pnew;pnew->next = NULL;ptail = pnew;p2 = p2->next;}}return phead;}void print(Pnode phead , char x ){char y = '+';Pnode p = phead->next;printf("两个一元多项式的和 = ");while(NULL != p){if(p->mi == 0){printf("%d ",p->xishu);}else{printf("%d",p->xishu);printf("%c",x);printf("%d ",p->mi);printf("%c ",y);}p = p->next;}printf("\n");}int main(void){Pnode p1,p2;int a,b;char x;printf("输入未知字母(如x,y,z等):");scanf("%c",&x);printf("输入第一个多项式的项数:");scanf("%d",&a);p1 = init(a);printf("输入第二个多项式的项数:");scanf("%d",&b);p2 = init(b);print(jia(p1,p2),x);return 0;}3.程序调试过程记录:1.第一次调试:--------------------配置: mingw5 - CUI Debug, 编译器类型: MinGW--------------------检查文件依赖性...正在编译 C:\Users\Administrator\Documents\C-Free\Projects\未命名 2.cpp... [Error] C:\Users\Administrator\Documents\C-Free\Projects\未命名 2.cpp:53: error: `P2' was not declared in this scope[Error] C:\Users\Administrator\Documents\C-Free\Projects\未命名 2.cpp:53: error: `P1' was not declared in this scope[Error] C:\Users\Administrator\Documents\C-Free\Projects\未命名 2.cpp:53: error: expected `)' before '!' token[Error] C:\Users\Administrator\Documents\C-Free\Projects\未命名 2.cpp:53: error: expected primary-expression before '=' token[Error] C:\Users\Administrator\Documents\C-Free\Projects\未命名 2.cpp:53: error: expected `;' before ')' token[Error] C:\Users\Administrator\Documents\C-Free\Projects\未命名 2.cpp:55: error: stray '\163' in program[Error] C:\Users\Administrator\Documents\C-Free\Projects\未命名 2.cpp:55: error: stray '\161' in program[Error] C:\Users\Administrator\Documents\C-Free\Projects\未命名2.cpp:118: error: a function-definition is not allowed here before '{' token[Error] C:\Users\Administrator\Documents\C-Free\Projects\未命名2.cpp:121: error: stray '\163' in program[Error] C:\Users\Administrator\Documents\C-Free\Projects\未命名2.cpp:121: error: stray '\187' in program[Error] C:\Users\Administrator\Documents\C-Free\Projects\未命名2.cpp:140: error: a function-definition is not allowed here before '{' token[Error] C:\Users\Administrator\Documents\C-Free\Projects\未命名2.cpp:148: error: `a' was not declared in this scope[Error] C:\Users\Administrator\Documents\C-Free\Projects\未命名2.cpp:151: error: `b' was not declared in this scope[Error] C:\Users\Administrator\Documents\C-Free\Projects\未命名2.cpp:154: error: `x' was not declared in this scope[Error] C:\Users\Administrator\Documents\C-Free\Projects\未命名2.cpp:154: error: `print' was not declared in this scope[Warning] C:\Users\Administrator\Documents\C-Free\Projects\未命名2.cpp:156:2: warning: no newline at end of file构建中止未命名2: 15 个错误, 1 个警告2.最后一次调试--------------------配置: mingw5 - CUI Debug, 编译器类型: MinGW--------------------检查文件依赖性...完成构建未命名2: 0 个错误, 0 个警告生成 C:\Users\Administrator\Documents\C-Free\Projects\未命名2.exe 4.实验结果1.程序运行结果 5x^3+10测试数据:第一个一元多项式为:3x^3+2x^2-5x+6第二个一元多项式为:2x^3-2x^2+5x+42.实验总结通过该实验总结了第二章所学内容,掌握了如何用线性链表表示多项式的加法运算。

用单链表实现任意两个一元多项式的加减运算

用单链表实现任意两个一元多项式的加减运算

软件综合设计报告书二○一六年六月一.需求分析:1.设计题目:用单链表实现任意两个一元多项式的加减运算2.设计要求:编程实现以下功能:①分别输入一元多项式p n (x)和Q n (x)。

从键盘输入一元对项式中各项的系数和指数,并用单链表加以表示。

②分别对一元多项式p n (x)和Q n (x)进行升幂排序。

将一元多项式中各子项按照指数从小到大的顺序排序。

③分别输出一元多项式p n (x)和Q n (x)。

将用单链表表示的一元多项式输出,即打印多项式的系数和指数。

④任意输入一个实数x0,分别求出一元多项式p n (x0)和Q n (x0)的值。

⑤已知有两个一元多项式分别为P n (x)和Q n (x),求出两个多项式的和R n (x)和差T n (x),分别用单链表表示R n (x)和T n (x),并将二者输出,(R n (x)=P n (x)+Q n (x),T n (x)=P n (x)-Q n (x))⑥保存多项式,即分别将一元多项式p n (x)和Q n (x)各项的系数和指数保存到外部磁盘文件。

⑦由程序从所存文件中读出多项式的系数和指数,重新构建一元多项式P n (x) 和Q n (x),并可对其再次进行运算操作。

3.系统功能需求分析:①用单链表表示出一元多项式②将多项式进行升幂排序③输出多项式④计算多项式在x0的值⑤建立并输出多项式的和与差⑥将多项式以及他的系数指数进行保存⑦读取多项式二.概要设计:包括系统总体设计框架和系统功能模块;系统功能模块(1)功能选择函数:通过输入对应功能的数字,进行多项式的运算。

该函数在主函数中调用。

(2)输入数据函数:通过建立单链表,输入两个多项式的各项指数和系数。

(3)升幂函数:通过冒泡排序法对两个多项式进行升幂排序。

(4)求和、求差函数:定义空链用来存储结果,将两个多项式相加减。

(5)输出函数:输出上一步的运行结果。

三.详细设计:包括主要功能模块的算法设计思路以及对应的工作流程图;设计思路:(1)定义结构体struct(2)建立单链表(3)建立一元多项式(4)显示一元多项式(5)一元多项式的加法计算(6)一元多项式的减法计算功能选择函数:数据输入函数:求和函数:求差函数:四.主要源程序代码:包括存储结构设计说明,以及完整源程序;存储结构设计说明:一元多项式的表示在计算机内可以用链表来表示,为了节省存储空间,只存储多项式中系数非零的项。

一元多项式相加C语言代码

一元多项式相加C语言代码

数据结构作业一元多项式相加C语言代码#include<stdio.h>#include<malloc.h>typedef struct node{int exp,coef;struct node *link;}PolyNode,*Polylinklist;Polylinklist Creat(int n){Polylinklist p,r=NULL,list=NULL;int coef,exp,i;for(i=1;i<=n;i++){scanf("%d %d",&coef,&exp);p=(Polylinklist)malloc(sizeof(PolyNode));p->coef=coef;p->exp=exp;p->link=NULL;if(list==NULL)list=p;elser->link=p;r=p;}return(list);}Polylinklist ATTACH(int coef,int exp,Polylinklist r){Polylinklist w;w=(Polylinklist)malloc(sizeof(PolyNode));w->exp=exp;w->coef=coef;r->link=w;return w;}Polylinklist PADD(Polylinklist a,Polylinklist b){Polylinklist c;Polylinklist r,p=a,q=b;int x;c=(Polylinklist)malloc(sizeof(PolyNode));r=c;while(p!=NULL&&q!=NULL)if(p->exp==q->exp){x=p->coef+q->coef;if(x!=0)r=ATTACH(x,q->exp,r);p=p->link;q=q->link;}else if(p->exp<q->exp){r=ATTACH(q->coef,q->exp,r);q=q->link;}else {r=ATTACH(p->coef,p->exp,r);p=p->link;}while(p!=NULL){r=ATTACH(p->coef,p->exp,r);p=p->link;}while(q!=NULL){r=ATTACH(q->coef,q->exp,r);q=q->link;}r->link=NULL;p=c;c=c->link;free(p);return c;}void Result(Polylinklist w){Polylinklist m;m=w;while(w==NULL){printf("0");break;}while(w!=NULL){if(w->exp==0)printf("%d",w->coef);elseprintf("%d*x^%d",w->coef,w->exp);w=w->link;while(w!=NULL){if(w->coef>0){if(w->exp==0)printf("+%d",w->coef);elseprintf("+%d*x^%d",w->coef,w->exp);}else{if(w->exp!=0)printf("%d*x^%d",w->coef,w->exp);elseprintf("%d",w->coef);}w=w->link;}}}void main(){Polylinklist c=NULL;PolyNode *Lengtha;PolyNode *Lengthb;int a1,b1;printf("Please input a's Length:");scanf("%d",&a1);Lengtha=Creat(a1);printf(" a=");Result(Lengtha);printf("\n");printf("Please input b's Length:");scanf("%d",&b1);Lengthb=Creat(b1);printf(" b=");Result(Lengthb);printf("\n");c=PADD(Lengtha,Lengthb);printf("\n");printf(" c=");Result(c);printf("\n");}。

数据结构(C语言)用单链表存储一元多项式,并实现两个多项式的相加运算

数据结构(C语言)用单链表存储一元多项式,并实现两个多项式的相加运算

#include<stdio.h>#include<stdlib.h>#include<malloc.h>typedef int ElemType;/*单项链表的声明*/typedef struct PolynNode{int coef; // 系数int expn; // 指数struct PolynNode *next;}PolynNode,*PolynList;/*正位序(插在表尾)输入n个元素的值,建立带表头结构的单链线性表*/ /*指数系数一对一对输入*/void CreatePolyn(PolynList &L,int n){int i;PolynList p,q;L=(PolynList)malloc(sizeof(PolynNode)); // 生成头结点L->next=NULL;q=L;printf("成对输入%d个数据\n",n);for(i=1;i<=n;i++){p=(PolynList)malloc(sizeof(PolynNode));scanf("%d%d",&p->coef,&p->expn); //指数和系数成对输入q->next=p;q=q->next;}p->next=NULL;}// 初始条件:单链表L已存在// 操作结果: 依次对L的每个数据元素调用函数vi()。

一旦vi()失败,则操作失败void PolynTraverse(PolynList L,void(*vi)(ElemType, ElemType)){PolynList p=L->next;while(p){vi(p->coef, p->expn);if(p->next){printf(" + "); //“+”号的输出,最后一项后面没有“+”}p=p->next;}printf("\n");}/*ListTraverse()调用的函数(类型要一致)*/void visit(ElemType c, ElemType e){if(c != 0){printf("%dX^%d",c,e); //格式化输出多项式每一项 }}/* 多项式相加,原理:归并 *//* 参数:两个已经存在的多项式 *//* 返回值:归并后新的多项式的头结点 */PolynList MergeList(PolynList La, PolynList Lb){PolynList pa, pb, pc, Lc;pa = La->next;pb = Lb->next;Lc = pc = La; // 用La的头结点作为Lc的头结点while(pa&&pb){if(pa->expn < pb->expn){pc->next = pa; //如果指数不相等,pc指针连上指数小的结点,pc = pa;pa = pa->next; //指向该结点的指针后移}else if(pa ->expn > pb->expn ){pc->next = pb; //pc指针连上指数小的结点,pc = pb;pb = pb->next; //指向该结点的指针后移}else//(pa ->expn = pb->expn ){pa->coef = pa->coef + pb->coef; //指数相等时,系数相加pc->next = pa;pc = pa;pa = pa->next; //两指针都往后移pb = pb->next;}}pc->next = pa ? pa:pb; // 插入剩余段return Lc;}void main(){PolynList ha,hb,hc;printf("非递减输入多项式ha, ");CreatePolyn(ha,5); // 正位序输入n个元素的值 printf("非递减输入多项式hb, ");CreatePolyn(hb,5); // 正位序输入n个元素的值 printf("多项式ha :");PolynTraverse(ha, visit);printf("\n");printf("多项式hb :");PolynTraverse(hb, visit);printf("\n");hc = MergeList(ha,hb);PolynTraverse(hc, visit);}。

算法与数据结构实验一元多项式求和

算法与数据结构实验一元多项式求和

《算法与数据结构》实验报告姓名:***班级:计科01学号:**********实验题目:链表的应用实验内容:一元多项式求和把任意给定的两个一元多项式P(x),Q(x)输入计算机,计算它们的和并输出计算结果。

设计分析:一元多项式可以用单链表表示,结点结构图示如下:Array一元多项式链表的结点结构一元多项式算法伪代码如下:源程序代码:#include"StdAfx.h"#include<stdlib.h>typedef struct LNode{int x,z;struct LNode *next;}LinkList;void OutLinkList(LinkList *L);void PutLinkList(LinkList *&L,int n);LinkList *AddLinkList(LinkList *a,LinkList *b);void OutXLinkList(LinkList *L);void OutZLinkList(LinkList *L);void main(){int n,m;LinkList *a,*b,*c;printf("\t\t\t本À?程¨¬序¨°可¨¦以°?完ª¨º成¨¦两¢?个?一°?元a多¨¤项?式º?的Ì?加¨®法¤¡§运?算?。

¡ê\n");printf("请?输º?入¨?一°?元a多¨¤项?式º?a的Ì?项?数ºym:êo");scanf("%d",&m);printf("请?按ã¡ä照?从䨮低̨ª次ä?到Ì?高?次ä?的Ì?顺3序¨°依°¨¤此ä?输º?入¨?一°?元a多¨¤项?式º?a的Ì?系¦Ì数ºy和¨ª指?数ºy:êo\n");PutLinkList(a,m);printf("a=");OutLinkList(a);printf("请?输º?入¨?一°?元a多¨¤项?式º?b的Ì?项?数ºyn:êo");scanf("%d",&n);printf("请?按ã¡ä照?从䨮低̨ª次ä?到Ì?高?次ä?的Ì?顺3序¨°依°¨¤此ä?输º?入¨?一°?元a多¨¤项?式º?b的Ì?系¦Ì数ºy和¨ª指?数ºy:êo\n");PutLinkList(b,n);printf("b=");OutLinkList(b);c=AddLinkList(a,b);printf("两¢?个?多¨¤项?式º?的Ì?和¨ª为a:êo\na+b=");OutLinkList(c);}void PutLinkList(LinkList *&L,int n){LinkList *s,*r;L=(LinkList *)malloc(sizeof(LinkList));r=L;for(int i=0;i<n;i++){s=(LinkList *)malloc(sizeof(LinkList));printf("请?输º?入¨?第̨²%d项?的Ì?系¦Ì数ºy:êo",i+1);scanf("%d",&s->x);printf("请?输º?入¨?第̨²%d项?的Ì?指?数ºy:êo",i+1);scanf("%d",&s->z);r->next=s;r=s;}r->next=NULL;}void OutLinkList(LinkList *L) {char FuHao;LinkList *p=L->next;FuHao=p->x>0? '+':'-';if(FuHao=='-'){printf("%c",FuHao);if(p->x==-1)printf("1");}OutXLinkList(p);OutZLinkList(p);p=p->next;while(p!=NULL){FuHao=p->x>0? '+':'-';printf("%c",FuHao);OutXLinkList(p);OutZLinkList(p);p=p->next;}printf("\n");}void OutXLinkList(LinkList *L) {int xi=L->x>0? L->x:-L->x;if(L->x==1||L->x==-1);elseprintf("%d",xi);}void OutZLinkList(LinkList *L) {if(L->z==0);else if(L->z==1||L->z==-1){if(L->z<0){if(L->x==1||L->x==-1)printf("1");printf("/");}printf("X");}else{if(L->z<0)printf("/");int zhi=L->z>0? L->z:-L->z;printf("X^%d",zhi);}}LinkList *AddLinkList(LinkList *a,LinkList *b){a=a->next;b=b->next;LinkList *c,*d,*s;c=(LinkList *)malloc(sizeof(LinkList));d=c;while(a!=NULL&&b!=NULL){if(a->z<b->z){s=(LinkList *)malloc(sizeof(LinkList));s->x=b->x;s->z=b->z;d->next=s;d=s;b=b->next;}else if(a->z>b->z){s=(LinkList *)malloc(sizeof(LinkList));s->x=a->x;s->z=a->z;d->next=s;d=s;a=a->next;}else{s=(LinkList *)malloc(sizeof(LinkList));s->x=a->x+b->x;s->z=a->z;if(s->x==0);else{d->next=s;d=s;}a=a->next;b=b->next;}}if(a!=NULL)d->next=a;else if(b!=NULL)d->next=b;elsed->next=NULL;return c;}测试用例:当a=3x^8-x^5+2x^3+7x^2+5x,b=5x^5+3x^4-7x^2-3x^(-3)时,运行结果如下:试验总结:通过本次试验,学会了链表的应用,加深了对链表的理解,知道了链表是把线性表中的元素按照链式储存方式到计算机中的一片连续的储存空间中。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef int ElemType;
/*单项链表的声明*/
typedef struct PolynNode{
int coef; // 系数
int expn; // 指数
struct PolynNode *next;
}PolynNode,*PolynList;
/*正位序(插在表尾)输入n个元素的值,建立带表头结构的单链线性表*/ /*指数系数一对一对输入*/
void CreatePolyn(PolynList &L,int n)
{
int i;
PolynList p,q;
L=(PolynList)malloc(sizeof(PolynNode)); // 生成头结点
L->next=NULL;
q=L;
printf("成对输入%d个数据\n",n);
for(i=1;i<=n;i++)
{
p=(PolynList)malloc(sizeof(PolynNode));
scanf("%d%d",&p->coef,&p->expn); //指数和系数成对输入
q->next=p;
q=q->next;
}
p->next=NULL;
}
// 初始条件:单链表L已存在
// 操作结果: 依次对L的每个数据元素调用函数vi()。

一旦vi()失败,则操作失败void PolynTraverse(PolynList L,void(*vi)(ElemType, ElemType))
{
PolynList p=L->next;
while(p)
{
vi(p->coef, p->expn);
if(p->next)
{
printf(" + "); //“+”号的输出,最后一项后面没有“+”
}
p=p->next;
}
printf("\n");
}
/*ListTraverse()调用的函数(类型要一致)*/
void visit(ElemType c, ElemType e)
{
if(c != 0)
{
printf("%dX^%d",c,e); //格式化输出多项式每一项 }
}
/* 多项式相加,原理:归并 */
/* 参数:两个已经存在的多项式 */
/* 返回值:归并后新的多项式的头结点 */
PolynList MergeList(PolynList La, PolynList Lb)
{
PolynList pa, pb, pc, Lc;
pa = La->next;
pb = Lb->next;
Lc = pc = La; // 用La的头结点作为Lc的头结点
while(pa&&pb)
{
if(pa->expn < pb->expn)
{
pc->next = pa; //如果指数不相等,pc指针连上指数小的结点,
pc = pa;
pa = pa->next; //指向该结点的指针后移
}
else if(pa ->expn > pb->expn )
{
pc->next = pb; //pc指针连上指数小的结点,
pc = pb;
pb = pb->next; //指向该结点的指针后移
}
else//(pa ->expn = pb->expn )
{
pa->coef = pa->coef + pb->coef; //指数相等时,系数相加
pc->next = pa;
pc = pa;
pa = pa->next; //两指针都往后移
pb = pb->next;
}
}
pc->next = pa ? pa:pb; // 插入剩余段
return Lc;
}
void main()
{
PolynList ha,hb,hc;
printf("非递减输入多项式ha, ");
CreatePolyn(ha,5); // 正位序输入n个元素的值 printf("非递减输入多项式hb, ");
CreatePolyn(hb,5); // 正位序输入n个元素的值 printf("多项式ha :");
PolynTraverse(ha, visit);
printf("\n");
printf("多项式hb :");
PolynTraverse(hb, visit);
printf("\n");
hc = MergeList(ha,hb);
PolynTraverse(hc, visit);
}。

相关文档
最新文档