#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(PNode)
typedef struct Node{
float ratio;//系数
int index;//指数
struct Node *next;//下一个结点的指针域
}*PNode , *LinkList;
//多项式链表建立---返回所建立的链表的头结点;
LinkList Create_Link(int num){
LinkList head;
PNode p , q;
float ratio;//系数
int index;//指数
//创建空链表
head = (LinkList)malloc(LEN);
head->next = NULL;
q = head;
for(int i = 1 ; i <= num ; i++){
printf("请输入第%d个项的系数和指数:" , i);
scanf("%f%d" , &ratio , &index);
p = (PNode)malloc(LEN);
p->next = NULL;
p->ratio = ratio;
p->index = index;
//采用尾插法的方式插入新结点
q->next = p;
q = p;
}
printf("\n===========================\n\n");
return head;
}
//多项式排序:将所建立的多项式按指数非递减(从小到大)进行排序;
void Sort_Link(LinkList head){
PNode p , q;
//定义中间变量进行数据交换
float temp1;
int temp2;
//进行冒泡排序
for(p = head->next ; p != NULL ; p = p->next){
for(q = p->next ; q != NULL ; q = q->next){
if(p->index > q->index){
temp1 = q->ratio;
q->ratio = p->ratio;
p->ratio = temp1;
temp2 = q->index;
q->index = p->index;
p->index = temp2;
}
}
}
}
//多项式相加:实现两个多项式相加操作。操作生成一个新的多项式,原有的两个多项式不变,返回生成的多项式的头指针;
LinkList Add_List(LinkList heada , LinkList headb){
PNode ha , hb , hc , p , temp;
LinkList headc;
//hc存放新结点
//创建一个存放headc的空链表
headc = (LinkList)malloc(LEN);
headc->next = NULL;
p = headc;
ha = heada->next;
hb = headb->next;
hc = headc->next;
while(ha != NULL || hb != NULL){
temp = (PNode)malloc(LEN);
temp->next = NULL;
//如果都不为空,就可以找到合适的项加加减减
if(ha != NULL && hb != NULL){
//a的指数小
if(ha->index < hb->index){
temp->index = ha->index;
temp->ratio = ha->ratio;
p->next = temp;
p = temp;
ha = ha->next;
}
//a b 的指数一样大
else if(ha->index == hb->index){
if(ha->ratio + hb->ratio != NULL){
temp->index = ha->index;
temp->ratio = ha->ratio + hb->ratio;
p->next = temp;
p = temp;
}
ha = ha->next;
hb = hb->next;
}
//b的指数小
else if(ha->index > hb->index){
temp->index = hb->index;
temp->ratio = hb->ratio;
p->next = temp;
p = temp;
hb = hb->next;
}
}
//b的所有项处理完了a还没有处理完
else if(ha != NULL && hb == NULL){
temp->index = ha->index;
temp->ratio = ha->ratio;
p->next = temp;
p = temp;
ha = ha->next;
}
//a的所有项处理完了b还没有处理完
else if(ha == NULL && hb != NULL){
temp->index = hb->index;
temp->ratio = hb->ratio;
p->next = temp;
p = temp;
hb = hb->next;
}
}
return headc;
}
//多项式相减:实现两个多项式相减操作。操作生成一个新的多项式,原有的两个多项式不变,返回生成的多项式的头指针;
LinkList Sub_List(LinkList heada , LinkList headb){
PNode ha , hb , hc , p , temp;
LinkList headc;
//hc存放新结点
//创建一个存放headc的空链表
headc = (LinkList)malloc(LEN);
headc->next = NULL;
p = headc;
ha = heada->next;
hb = headb->next;
hc = headc->next;
while(ha != NULL || hb != NULL){
temp = (PNode)malloc(LEN);
temp->next = NULL;
//如果都不为空,就可以找到合适的项加加减减
if(ha != NULL && hb != NULL){
//a的指数小
if(ha->index < hb->index){
temp->index = ha->index;
temp->ratio = ha->ratio;
p->next = temp;
p = temp;
ha = ha->next;
}
//a b 的指数一样大
else if(ha->index == hb->index){
if(ha->ratio - hb->ratio != NULL){
temp->index = ha->index;
temp->ratio = ha->ratio - hb->ratio;
p->next = temp;
p = temp;
}
ha = ha->next;
hb = hb->next;
}
//b的指数小
else if(ha->index > hb->index){
temp->index = hb->index;
temp->ratio = -hb->ratio;
p->next = temp;
p = temp;
hb = hb->next;
}
}
//b的所有项处理完了a还没有处理完
else if(ha != NULL && hb == NULL){
temp->index = ha->index;
temp->ratio = ha->ratio;
p->next = temp;
p = temp;
ha = ha->next;
}
//a的所有项处理完了b还没有处理完
else if(ha == NULL && hb != NULL){
temp->index = hb->index;
temp->ratio = -hb->ratio;
p->next = temp;
p = temp;
hb = hb->next;
}
}
return headc;
}
//多项式的输出:按照p0+p1x+p2x2+ooo+pnxn格式输出多项式;
void Print_Link(LinkList head){
LinkList p = head->next;
while(p != NULL){
if(p->ratio > 0)
printf("%.1fX^%d" , p->ratio , p->index);
else if(p->ratio < 0)
printf("(%.1fX^%d)" , p->ratio , p->index);
if(p->next != NULL) printf(" + ");
p = p->next;
}
}
int main(){
int n1 , n2;
printf("请输入多项式A的项数:");
scanf("%d" , &n1);
LinkList heada = Create_Link(n1);
printf("请输入多项式B的项数:");
scanf("%d" , &n2);
LinkList headb = Create_Link(n2);
//输出多项式A和B
printf("\n多项式A: ");
Print_Link(heada);
printf("\n多项式B: ");
Print_Link(headb);
printf("\n\n===========================\n");
//输出排序后的多项式AB
printf("\n排序后的多项式A: ");
Sort_Link(heada);
Print_Link(heada);
printf("\n排序后的多项式B: ");
Sort_Link(headb);
Print_Link(headb);
//多项式A与多项式B相加
printf("\n\n===========================\n");
printf("多项式A加B: ");
LinkList headc = Add_List(heada , headb);
Print_Link(headc);
//多项式A与多项式B相减
LinkList headd = Sub_List(heada , headb);
printf("\n===========================\n");
printf("多项式A减B: ");
Print_Link(headd);
return 0;
}