两个多项式相加运算(用c语言)多项式如下例所示:7.2X500+9X100+2X3+2X2+100 与 3x3000+35X2000+18X100-2x3+1000相加后的结果为:3x3000+35X2000 +7.2X500 +27X100+2X2+1100提示:a.多项式采用链表结构存储.1.\x05采

来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/26 00:57:58
两个多项式相加运算(用c语言)多项式如下例所示:7.2X500+9X100+2X3+2X2+100与3x3000+35X2000+18X100-2x3+1000相加后的结果为:3x3000+35X20

两个多项式相加运算(用c语言)多项式如下例所示:7.2X500+9X100+2X3+2X2+100 与 3x3000+35X2000+18X100-2x3+1000相加后的结果为:3x3000+35X2000 +7.2X500 +27X100+2X2+1100提示:a.多项式采用链表结构存储.1.\x05采
两个多项式相加运算(用c语言)
多项式如下例所示:
7.2X500+9X100+2X3+2X2+100 与 3x3000+35X2000+18X100-2x3+1000
相加后的结果为:3x3000+35X2000 +7.2X500 +27X100+2X2+1100
提示:a.多项式采用链表结构存储.
1.\x05采用尾插法建立多项式的存储结构即单链表
2.\x05将exp域值按递减排序构成有序单链表
3.\x05求两个有序单链表的合并:对于exp域值相同的两个结点进行合并,合并时判定两个结点的coef域值之和,不为0时创建新结点,插入到新链表中;对于exp域值不相同的结点直接按序插入到新链表中.
对于链表结构创建以下函数:
1.\x05尾插法创建链表
2.\x05对链表进行递减排序
3.\x05求两个有序链表的合并
4.\x05遍历链表,输出多项式

两个多项式相加运算(用c语言)多项式如下例所示:7.2X500+9X100+2X3+2X2+100 与 3x3000+35X2000+18X100-2x3+1000相加后的结果为:3x3000+35X2000 +7.2X500 +27X100+2X2+1100提示:a.多项式采用链表结构存储.1.\x05采
#include
#include
#define Null 0
typedef struct Node
{
\x09int coeff;
\x09int expo;
\x09Node *next;
}listNode,*list;
list createList()
{
\x09list head;
\x09head = (list)malloc(sizeof(listNode));
\x09head = NULL;
\x09printf("want to create a new node?y/n\n");
\x09char ch;
\x09fflush(stdin);
\x09scanf("%c",&ch);
\x09while(ch=='Y' || ch== 'y')
\x09{
\x09\x09list p;
\x09\x09p = (list)malloc(sizeof(listNode));
\x09\x09printf("input coeff\n");
\x09\x09int coeff;
\x09\x09scanf("%d",&coeff);
\x09\x09p->coeff = coeff;
\x09\x09printf("input expo\n");
\x09\x09int expo;
\x09\x09scanf("%d",&expo);
\x09\x09p->expo = expo;
\x09\x09p->next = NULL;
\x09\x09//链表为空的时候,即添加首个元素
\x09\x09if(head == NULL)
\x09\x09{
\x09\x09\x09head=p;//添加代码
\x09\x09}
\x09\x09else
\x09\x09{
\x09\x09\x09list prev,curr;
\x09\x09\x09curr = head;
\x09\x09\x09prev = NULL;
\x09\x09\x09//找到添加位置
\x09\x09\x09while(curr!=NULL && curr->expo>p->expo)
\x09\x09\x09{
\x09\x09\x09\x09prev=curr;
\x09\x09\x09\x09curr=curr->next;//添加代码
\x09\x09\x09}
\x09\x09\x09if(curr!=NULL && curr->expo == p->expo)
\x09\x09\x09{
\x09\x09\x09\x09curr->coeff = curr->coeff + p->coeff;
\x09\x09\x09\x09printf("want to create a new node?y/n\n");
\x09\x09\x09\x09fflush(stdin);
\x09\x09\x09\x09scanf("%c",&ch);
\x09\x09\x09\x09if(ch=='Y' || ch== 'y')
\x09\x09\x09\x09\x09continue;
\x09\x09\x09\x09else
\x09\x09\x09\x09\x09return head;
\x09\x09\x09}
\x09\x09\x09//插入结点,结点非首
\x09\x09\x09if(prev != NULL)
\x09\x09\x09{
\x09\x09\x09\x09p->next=curr;
\x09\x09\x09\x09prev->next=p;
//添加代码
\x09\x09\x09}
\x09\x09\x09//插入结点,结点为首
\x09\x09\x09else
\x09\x09\x09{
\x09\x09\x09\x09p->next=curr;
\x09\x09\x09\x09head=p;
//添加代码
\x09\x09\x09}
\x09\x09}
\x09\x09printf("want to create a new node?y/n\n");
\x09\x09fflush(stdin);
\x09\x09scanf("%c",&ch);
\x09}
\x09return head;
}
list add(list head1,list head2)
{
\x09list head,newNode,ptr1,ptr2,ptr3;
\x09ptr1 = head1;
\x09ptr2 = head2;
\x09head = NULL;\x09
\x09while(ptr1 != NULL && ptr2 != NULL)
\x09{
\x09\x09newNode = (list)malloc(sizeof(listNode));
\x09\x09if(ptr1->expo > ptr2->expo)
\x09\x09{
\x09\x09\x09newNode->coeff = ptr1->coeff;
\x09\x09\x09newNode->expo = ptr1->expo;
\x09\x09\x09newNode->next = NULL;
\x09\x09\x09ptr1 = ptr1->next;
\x09\x09}
\x09\x09else if(ptr1->expo < ptr2->expo)
\x09\x09{
\x09\x09\x09\x09newNode->coeff = ptr2->coeff;
\x09\x09\x09newNode->expo = ptr2->expo;
\x09\x09\x09newNode->next = NULL;
\x09\x09\x09ptr2 = ptr2->next;//添加代码
\x09\x09}
\x09\x09else
\x09\x09{
\x09\x09\x09newNode->coeff = ptr1->coeff + ptr2->coeff;
\x09\x09\x09newNode->expo = ptr1->expo;
\x09\x09\x09newNode->next = NULL;
\x09\x09\x09ptr1 = ptr1->next;
\x09\x09\x09ptr2 = ptr2->next;
\x09\x09}
\x09\x09if(head==NULL)
\x09\x09{
\x09\x09\x09head = newNode;
\x09\x09}
\x09\x09else
\x09\x09{
\x09\x09\x09ptr3 = head;
\x09\x09\x09//添加代码
\x09\x09\x09while(ptr3->next != NULL)
\x09\x09\x09\x09ptr3 = ptr3->next;
\x09\x09\x09ptr3->next = newNode;
\x09\x09}
\x09}
\x09while(ptr1 != NULL)
\x09{
\x09\x09newNode = (list)malloc(sizeof(listNode));
\x09\x09newNode->coeff = ptr1->coeff;
\x09\x09newNode->expo = ptr1->expo;
\x09\x09newNode->next = NULL;
\x09\x09ptr3 = head;
\x09\x09if(ptr3 == NULL)
\x09\x09\x09head = ptr3 = newNode;
\x09\x09else
\x09\x09{
\x09\x09\x09while(ptr3->next != NULL)
\x09\x09\x09\x09ptr3 = ptr3->next;
\x09\x09\x09ptr3->next = newNode;//添加代码
\x09\x09}
\x09\x09ptr1 = ptr1->next;
\x09}
\x09while(ptr2 != NULL)
\x09{
\x09\x09newNode = (list)malloc(sizeof(listNode));
\x09\x09newNode->coeff = ptr2->coeff;
\x09\x09newNode->expo = ptr2->expo;
\x09\x09ptr3 = head;
\x09\x09if(ptr3 == NULL)
\x09\x09\x09head = ptr3 = newNode;
\x09\x09else
\x09\x09{
\x09\x09\x09while(ptr3->next != NULL)
\x09\x09\x09\x09ptr3 = ptr3->next;
\x09\x09\x09ptr3->next = newNode;
\x09\x09}
\x09\x09ptr2 = ptr2->next;
\x09}
\x09return head;
}
void display(list head)
{
\x09list ptr = head;
\x09while(ptr != NULL)
\x09{
\x09\x09if(ptr != head )
\x09\x09\x09printf("+");
\x09\x09printf("%d",ptr->coeff);
\x09\x09printf("x^");
\x09\x09printf("%d",ptr->expo);
\x09\x09ptr = ptr->next;
\x09}
\x09printf("\n");
}
int main(int argc, char* argv[])
{
\x09list head,head1,head2;
\x09printf("input the first list\n");
\x09head1 = createList();
\x09display(head1);
\x09printf("input the second list\n");
\x09head2 = createList();
\x09display(head2);
\x09head = add(head1,head2);
\x09display(head);
\x09return 0;
}