多项式的加减原理就是链表的合并 ,注意二级指针的使用
也需要注意一些简化代码的技巧的使用
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define OK 1
#define ERROR 0
typedef int Status;
typedef struct polynode{
float coef;
int expn;
polynode *next;
}polynode;
typedef polynode* polylist;
Status creat(polylist* L){//多项式头结点
*L=(polynode*)malloc(sizeof(polynode));
if(*L==NULL){
printf("空间分配失败!\n");
return ERROR;
}
(*L)->next=NULL;
return OK;
}
Status init(polylist* L,int n){//插入多项式
polynode* tail=*L;
for(int i=1;i<=n;i++){
polynode *newx;
newx=(polynode*)malloc(sizeof(polynode));
if(!newx){
printf("空间分配失败!\n");
return ERROR;
}
newx->next=NULL;
printf("\t\t输入第%d项的系数和指数: ",i);
scanf("%f%d",&newx->coef,&newx->expn);
tail->next=newx;
tail=tail->next;
}
return OK;
}
void print(polynode* p){//输出函数
if(p->expn==0)
printf("%f",p->coef);
else if(p->coef==1)
printf("X^%d",p->expn);
else if(p->expn==1)
printf("%fX",p->coef);
else
printf("%fX^%d",p->coef,p->expn);
}
Status show(polylist L){// 注意输出的格式
polynode* p=L->next;
if(!p){//链表为空==多项式为0
printf("\t\t\t0\n");
return OK;
}
int flag=0;
while(p){
if(fabs(p->coef)<=1e-6){
p=p->next;
continue;
}
else if(!flag){
print(p);
flag=1;
}
else{
if(p->coef>0)
printf(" + ");
print(p);
}
p=p->next;
}
printf("\n");
return OK;
}
//copy到新链,二级指针
Status link(polynode **pc,float coef,int expn){
polynode* newx;
newx=(polynode*)malloc(sizeof(polynode));
if(!newx){
printf("空间分配失败!\n");
return ERROR;
}
newx->next=NULL;
newx->coef=coef;
newx->expn=expn;
(*pc)->next=newx;
(*pc)=(*pc)->next;
return OK;
}
//多项式相加 异地合并
Status add_show_poly(polylist L1,polylist L2){
polylist L3;
creat(&L3);
polynode *pa,*pb,*pc,*del;
pa=L1->next;
pb=L2->next;
pc=L3;//不能next
while(pa&&pb){
if(pa->expn<pb->expn){
link(&pc,pa->coef,pa->expn);
del=pa;
pa=pa->next;
free(del);
}
else if(pa->expn>pb->expn){
link(&pc,pb->coef,pb->expn);
del=pb;
pb=pb->next;
free(del);
}
else {
float e=pa->coef+pb->coef;
if(e)
link(&pc,e,pa->expn);
del=pa;
pa=pa->next;
free(del);
del=pb;
pb=pb->next;
free(del);
}
}
if(!pa) pc->next=pb;
else pc->next=pa;
show(L3);
return OK;
}
Status sub_show_poly(polylist L1,polylist L2){
polynode* p=L2->next;
while(p){
p->coef*=-1;//加相反数
p=p->next;
}
add_show_poly(L1,L2);
return OK;
}
void db(polylist *l1,polylist *l2){//多项式的创建
int len1,len2;
creat(l1);
creat(l2);
printf("\t\t\t输入第一个多项式的长度: ");
scanf("%d",&len1);
init(l1,len1);
printf("\n");
printf("\t\t\t第一个多项式为: \n\t\t\t");
show(*l1);
printf("\n");
printf("\t\t\t输入第二个多项式的长度: ");
scanf("%d",&len2);
init(l2,len2);
printf("\n");
printf("\t\t\t第二个多项式为: \n\t\t\t");
show(*l2);
printf("\n");
}
void menu(){
printf("\t\t--------------一元多项式的简单操作--------------\n");
printf("\t\toptions:\n");
printf("\t\t\t1、实现两个多项式相加\n");
printf("\t\t\t2、实现两个多项式相减\n");
printf("\t\t\t0、退出程序\n");
printf("\t\t请输入你的选项:\n\t\t");
}
void add(){
polylist l1,l2;
db(&l1,&l2);
printf("\t\t第一个多项式与第二个多项式相加后的结果为: \n\t\t");
add_show_poly(l1,l2);
}
void sub(){
polylist l1,l2;
db(&l1,&l2);
printf("\t\t\t第一个多项式与第二个多项式相减后的结果为: \n\t\t\t");
sub_show_poly(l1,l2);
}
int main()
{
while(1){
int n;
menu();
scanf("%d",&n);
switch(n){
case 0:{
system("cls");
printf("\n\t\t\t谢谢您的使用,再见!\n");
exit(0);
break;
}
case 1:{
system("cls");
add();
printf("\n");
break;
}
case 2:{
system("cls");
sub();
printf("\n");
break;
}
default:
system("cls");
printf("\t\t你的输入有误,请重新输入!\n");
break;
}
}
return 0;
}