两个多项式相加运算(用c语言)
来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/11/09 10:42:42
两个多项式相加运算(用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遍历链表,输出多项式
多项式如下例所示:
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遍历链表,输出多项式
#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;
}
#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;
}
数据结构(C语言)用单链表存储一元多项式,并实现两个多项式的相加运算
数据结构:用链表实现两个多项式相加,用C++或者C语言实现多项式相加,求完整代码
数据结构(C语言) 如何分别用顺序和链式 实现一元多项式的相加?
C语言写多项式相加怎么写?
如何用C语言实现两个一元多项式的相加和相乘?
C语言中安位或运算如果遇到两个1相加呢?
两个一元多项式相加
用C语言编写:两个整数相加和两个小数相加后相乘,并显示两数相乘的结果
编写程序,实现两个集合的交运算(用C语言)
C程序设计 急啊多项式计算输入并建立多项式;输出多项式;两个多项式相加,建立并输出和多项式两个多项式
两个4次多项式相加
用c语言证明大于8的偶数由两个素数相加