【数据结构】 实现两个多项式的相加、减.要求:输出两个多项式原式,以及计算结果.
来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/26 06:58:54
【数据结构】 实现两个多项式的相加、减.要求:输出两个多项式原式,以及计算结果.
用C语言……
用C语言……
#include "stdio.h"
/* 链表结点结构 */
typedef struct LNode{
double coef; /* 系数 */
int exp; /* 指数 */
struct LNode *next;
}LNode;
/* 初始化链表 */
LNode *Init()
{
LNode *head = (LNode*)malloc(sizeof(LNode));
head->next = NULL;
return head;
}
/* 将值为data的结点插入到head链表的最后 */
void AddNode(LNode *head,double coef,int exp)
{
LNode *pre = head->next;
LNode *temp;
temp = (LNode*)malloc(sizeof(LNode));
temp->coef = coef;
temp->exp = exp;
temp->next = NULL;
if(pre == NULL)
{
head->next = temp;
return;
}
for(; pre->next!=NULL; pre=pre->next);
pre->next = temp;
}
/* 输出head链表的所有结点的值 */
void List(LNode *head)
{
LNode *curr;
printf("All nodes :");
for(curr=head->next; curr!=NULL; curr=curr->next)
{
printf("(%lf,%d)\t",curr->coef,curr->exp);
}
printf("\n");
}
/* 返回head链表的所有结点的数量 */
int Size(LNode *head)
{
int len = 0;
LNode *curr;
for(curr=head->next; curr!=NULL; curr=curr->next,len++);
return len;
}
LNode *Add(LNode *headA,LNode *headB)
{
LNode *currA,*currB,*headC;
double sum;
currA = headA->next;
currB = headB->next;
headC = Init();
while(currA!=NULL && currB!=NULL)
{
if(currA->exp > currB->exp)
{
AddNode(headC,currA->coef,currA->exp);
currA = currA->next;
}
else if(currA->exp < currB->exp)
{
AddNode(headC,currB->coef,currB->exp);
currB = currB->next;
}
else
{
sum = currA->coef + currB->coef;
if(sum != 0)
{
AddNode(headC,sum,currA->exp);
}
currA = currA->next;
currB = currB->next;
}
}
while(currA != NULL)
{
AddNode(headC,currA->coef,currA->exp);
currA = currA->next;
}
while(currB != NULL)
{
AddNode(headC,currB->coef,currB->exp);
currB = currB->next;
}
return headC;
}
void main()
{
LNode *headA,*headB,*headC;
headA = Init();
headB = Init();
AddNode(headA,1.0,5);
AddNode(headA,-1.0,3);
AddNode(headA,1,0);
AddNode(headB,0.5,5);
AddNode(headB,1.0,4);
AddNode(headB,1.0,3);
List(headA);
List(headB);
headC = Add(headA,headB);
List(headC);
}\x1a
/* 链表结点结构 */
typedef struct LNode{
double coef; /* 系数 */
int exp; /* 指数 */
struct LNode *next;
}LNode;
/* 初始化链表 */
LNode *Init()
{
LNode *head = (LNode*)malloc(sizeof(LNode));
head->next = NULL;
return head;
}
/* 将值为data的结点插入到head链表的最后 */
void AddNode(LNode *head,double coef,int exp)
{
LNode *pre = head->next;
LNode *temp;
temp = (LNode*)malloc(sizeof(LNode));
temp->coef = coef;
temp->exp = exp;
temp->next = NULL;
if(pre == NULL)
{
head->next = temp;
return;
}
for(; pre->next!=NULL; pre=pre->next);
pre->next = temp;
}
/* 输出head链表的所有结点的值 */
void List(LNode *head)
{
LNode *curr;
printf("All nodes :");
for(curr=head->next; curr!=NULL; curr=curr->next)
{
printf("(%lf,%d)\t",curr->coef,curr->exp);
}
printf("\n");
}
/* 返回head链表的所有结点的数量 */
int Size(LNode *head)
{
int len = 0;
LNode *curr;
for(curr=head->next; curr!=NULL; curr=curr->next,len++);
return len;
}
LNode *Add(LNode *headA,LNode *headB)
{
LNode *currA,*currB,*headC;
double sum;
currA = headA->next;
currB = headB->next;
headC = Init();
while(currA!=NULL && currB!=NULL)
{
if(currA->exp > currB->exp)
{
AddNode(headC,currA->coef,currA->exp);
currA = currA->next;
}
else if(currA->exp < currB->exp)
{
AddNode(headC,currB->coef,currB->exp);
currB = currB->next;
}
else
{
sum = currA->coef + currB->coef;
if(sum != 0)
{
AddNode(headC,sum,currA->exp);
}
currA = currA->next;
currB = currB->next;
}
}
while(currA != NULL)
{
AddNode(headC,currA->coef,currA->exp);
currA = currA->next;
}
while(currB != NULL)
{
AddNode(headC,currB->coef,currB->exp);
currB = currB->next;
}
return headC;
}
void main()
{
LNode *headA,*headB,*headC;
headA = Init();
headB = Init();
AddNode(headA,1.0,5);
AddNode(headA,-1.0,3);
AddNode(headA,1,0);
AddNode(headB,0.5,5);
AddNode(headB,1.0,4);
AddNode(headB,1.0,3);
List(headA);
List(headB);
headC = Add(headA,headB);
List(headC);
}\x1a
关于数据结构单链表的题,给定两个多项式,实现多项式的相加算法,
数据结构(C语言)用单链表存储一元多项式,并实现两个多项式的相加运算
.一元多项式计算.能够按照指数降序排列建立并输出多项式;能够完成两个多项式的相加、相减,并将结果输入
数据结构:用链表实现两个多项式相加,用C++或者C语言实现多项式相加,求完整代码
C程序设计 急啊多项式计算输入并建立多项式;输出多项式;两个多项式相加,建立并输出和多项式两个多项式
一元多项式的计算 能够按照指数降序排列建立并输出多项式 能够完成两个多项式的相加及相减
两个以单链表作存储结构的一元多项式A和B,编写算法将多项式A和B相加,要求利用原表的结点空间和多项式.
编写一个程序用单链表存储多项式,并实现两个多项式相加的函数?
两个一元多项式相加
两个三次多项式相加 结果一定是 不超过三次的多项式 为什么
数据结构一元多项式相加
数据结构的二元多项式计算