單鏈表實現一元多項式的的加減操作

多項式的加減原理就是鏈表的合併 ,注意二級指針的使用

也需要注意一些簡化代碼的技巧的使用

#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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章