线性表的应用——一元多项式的代数运算

/*
*	程序功能:一元多项式的代数运算
*	程序作者:Yannis Zhao
*
*/

#include
#include

//数据元素数据结构
typedef struct{		
	float coef;		//系数
	int expn;		//指数
}Term;

//多项式数据结构
typedef struct PolyList{
	Term term;
	struct PolyList *next;
}Polynomial;

void ShowMenu();//显示菜单
//Term结构的基本操作
float GetCoef(const Term t);	//返回数据项系数
int GetExpn(const Term t);		//返回数据项指数
int CompExpn(const Term t1,const Term t2); //比较数据项指数大小,按第一个系数小于、等于、大于第二个系数分别返回-1、0、1
float AddItem(Term *t1,const Term *t2);//两项相加,t1保存结果,返回系数
float SubtractItem(Term *t1,const Term *t2);//两项相减,t1保存结果,返回系数
Term MultiplyItem(Term t1,Term t2);//两项相乘,t1保存结果

//PolyList结构的基本操作
Polynomial* CreatePolyList();	//建立多项式
void DestroyList(Polynomial *p);//销毁多项式
void BubbleSort(Polynomial *p);//旗袍排序
void PrintPolynomial(Polynomial *p);//打印多项式
int length(Polynomial *p);//计算项数
int ChangePolynomial(Polynomial *p); //修改多项式 0--succeed 1--failure
int InsertItem(Polynomial *p,Term t);//插入项,会与已有项叠加 0--succeed 1--failure
void DeleteItem(Polynomial *p,Term t);//删除项
void AddPolyList(Polynomial *p1,const Polynomial *p2);//多项式相加,p1保存结果
void SubtractPolyList(Polynomial *p1,Polynomial *p2);//多项式相减,p1保存结果
void MultiplyPolyList(Polynomial *p1,const Polynomial *p2,Polynomial *pResult);//多项式相乘,另存结果

int main()
{
	Polynomial *p1=NULL,*p2=NULL;
	Term t;
	int choice=0;

	ShowMenu();
	while(scanf("%d",&choice))
	{
		switch(choice)
		{
		case 1:
			p1=CreatePolyList();
//			BubbleSort(p1);
			PrintPolynomial(p1);
			ShowMenu();
			break;
		case 2:
			p2=CreatePolyList();		
			PrintPolynomial(p2);
			ShowMenu();
			break;
		case 3:
			if(ChangePolynomial(p1)!=0)
			{
				printf("Polynomial changed!\n");
				BubbleSort(p1);
				PrintPolynomial(p1);
			}
			else
			{
				printf("Uncaught error!\n");
			}
			ShowMenu();
			break;
		case 4:
			if(ChangePolynomial(p2)!=0)
			{
				printf("Polynomial changed!\n");
				PrintPolynomial(p2);
			}
			else
			{
				printf("Uncaught error!\n");
			}
			ShowMenu();
			break;
		case 5:
			printf("enter item want to insert,format like: 2 2 ,means 2x(2).\n>");
			scanf("%f%d",&t.coef,&t.expn);
			if(InsertItem(p1,t)!=0)
			{
				printf("Inserted successful!\n");
				PrintPolynomial(p1);
			}
			else
			{
				printf("Uncaught error!\n");
			}
			ShowMenu();
			break;
		case 6:
			printf("enter item want to insert,format like: 2 2 ,means 2x(2).\n>");
			scanf("%f%d",&t.coef,&t.expn);
			if(InsertItem(p1,t)!=0)
			{
				printf("Inserted successful!\n");
				PrintPolynomial(p1);
			}
			else
			{
				printf("Uncaught error!\n");
			}
			ShowMenu();
			break;
		case 7:
			printf("enter item want to delete,format like: 2 2 ,means 2x(2).\n>");
			scanf("%f%d",&t.coef,&t.expn);
			DeleteItem(p1,t);
			PrintPolynomial(p1);
			ShowMenu();
			break;
		case 8:
			printf("enter item want to delete,format like: 2 2 ,means 2x(2).\n>");
			scanf("%f%d",&t.coef,&t.expn);
			DeleteItem(p2,t);
			PrintPolynomial(p2);
			ShowMenu();
			break;
		case 9:
			printf("Polynomial 1:");
			PrintPolynomial(p1);
			printf("Polynomial 2:");
			PrintPolynomial(p2);
		
			AddPolyList(p1,p2);
			ShowMenu();
			break;
		case 10:
			printf("Polynomial 1:");
			PrintPolynomial(p1);
			printf("Polynomial 2:");
			PrintPolynomial(p2);
		
			SubtractPolyList(p1,p2);
			ShowMenu();
			break;
		case 11:
			{
				Polynomial *pResult=(Polynomial*)malloc(sizeof(Polynomial));
				pResult->term.coef=-1;
				pResult->term.expn=-1;
				pResult->next=NULL;

				printf("Polynomial 1:");
				PrintPolynomial(p1);
				printf("Polynomial 2:");
				PrintPolynomial(p2);
			
				MultiplyPolyList(p1,p2,pResult);
				printf("result:");
				PrintPolynomial(pResult->next);
				ShowMenu();
			}
			break;
		default:
			printf("Please input correct order.\n");
			ShowMenu();
			break;
		}
	}
	return 0;
}

float GetCoef(const Term t)
{
	return t.coef;
}

int GetExpn(const Term t)
{
	return t.expn;
}

int CompExpn(const Term t1,const Term t2)
{
	if(t1.expn==t2.expn)
		return 0;
	else
		return t1.expncoef+=t2->coef;
}
float SubtractItem(Term *t1,const Term *t2)
{
	return t1->coef-=t2->coef;
}

Term MultiplyItem(Term t1,Term t2)
{
	Term temp;
	temp.coef=t1.coef*t2.coef;
	temp.expn=t1.expn+t2.expn;
	return temp;
}

Polynomial* CreatePolyList()
{
	int i;
	int n;//项数
	Polynomial *head=NULL;
	Polynomial *p1,*p2;
	p2=(Polynomial*)malloc(sizeof(Polynomial));

	printf("请输入项数:");
		scanf("%d",&n);
		
	printf("Please enter each item like: 3 2 ,means 3x(2). must enter data order by expn asc.\n");
	for(i=1;i<=n;i++)
	{
		p1=(Polynomial*)malloc(sizeof(Polynomial));
		printf("%dth item:",i);
		scanf("%f%d",&p1->term.coef,&p1->term.expn);
		
		if(i==1)
			head=p1;
		else
			p2->next=p1;
		p2=p1;
	}
	p2->next=NULL;
	BubbleSort(head);
	return head;
}

void DestroyList(Polynomial *p)
{
	free(p);
}

void BubbleSort(Polynomial *p)
{
	int i,j;
	int len=length(p);
	Term temp;
	Polynomial *pi=NULL;

	for(i=0;i<=len-1;i++)
	{
		pi=p;
		for(j=0;jterm.expn > pi->next->term.expn)
			{
				temp=pi->term;
				pi->term=pi->next->term;
				pi->next->term=temp;
			}
			pi=pi->next;
		}
	}
}

void PrintPolynomial(Polynomial *p)
{
	if(p==NULL)
	{
		printf("Polynomial is NULL.\n");
		return;
	}
	while(p->next!=NULL)
	{
		if(p->term.expn==0)
			printf("%.2f + ",p->term.coef);
		else
			printf("%.2fx(%d) + ",p->term.coef,p->term.expn);
		p=p->next;
	}
	if(p->term.expn==0)
		printf("%.2f",p->term.coef);
	else
		printf("%.2fx(%d).",p->term.coef,p->term.expn);
	printf("\n");
}

int length(Polynomial *p)
{
	int n=1;//项数计数
	
	while(p->next!=NULL)
	{
		n++;
		p=p->next;
	}

	return n;
}

int ChangePolynomial(Polynomial *p)
{
	int n=1;//计数器

	if(p==NULL)		//指针为空
	{
		printf("Polynomial is null,please make sure you have enter a polymial before change!\n");
		return 0;
	}
	//输入新的多项式
	printf("please input new polynomial:\n");
	while(p!=NULL)
	{
		printf("%dth item:",n);
		scanf("%f%d",&p->term.coef,&p->term.expn);
		p=p->next;
		n++;
	}
	
	return 1;
}

int InsertItem(Polynomial *p,Term t)
{
	Polynomial *pCur=p,*pPre=p;

	if(p==NULL)	//多项式为空
	{
		printf("Target polynomial does not exist!\n");
		return 0;
	}
	//current term's expn less than t's
	while(CompExpn(pCur->term,t)==-1 && pCur->next!=NULL)
	{
		pPre=pCur;
		pCur=pCur->next;
	}
	//equal
	if(CompExpn(pCur->term,t)==0)
	{
		//相加后系数和为0
		if(AddItem(&pCur->term,&t)==0.0)
		{
			pPre->next=pCur->next;//删除当前项
		}
		return 1;
	}
	//more than
	if(CompExpn(pCur->term,t)==1)
	{
		Polynomial *temp=(Polynomial*)malloc(sizeof(Polynomial));//为该项分配内存
		temp->term=t;
		temp->next=pCur;
		pPre->next=temp;
		return 1;
	}
	//arrive the end
	if(pCur->next==NULL)
	{
		Polynomial *temp=(Polynomial*)malloc(sizeof(Polynomial));
		temp->term=t;
		temp->next=NULL;
		pCur->next=temp;
	}
	return 1;
}

void DeleteItem(Polynomial *p,Term t)
{
	Polynomial *pCur=p,*pPre=p;
	
	if(p==NULL)	//多项式为空
	{
		printf("Target polynomial does not exist!\n");
		return;
	}

	while(pCur!=NULL)
	{
		//当前项为指定删除项
		if(pCur->term.coef==t.coef && pCur->term.expn==t.expn)
		{
			pPre->next=pCur->next;
			printf("Item delete successful!\n");
			return;
		}
		else//保存当前项为前一项,当前项指向下一项
		{
			pPre=pCur;
			pCur=pCur->next;
		}
	}

	printf("The item pointed not found!");
}

void AddPolyList(Polynomial *p1,const Polynomial *p2)
{
	if(p1==NULL || p2==NULL)
	{
		printf("Polynomial must not null!\n");
		return;
	}
	while(p2!=NULL)
	{
		InsertItem(p1,p2->term);
		p2=p2->next;
	}
	printf("sum:");
	PrintPolynomial(p1);
}

void SubtractPolyList(Polynomial *p1,Polynomial *p2)
{
	if(p1==NULL || p2==NULL)
	{
		printf("Polynomial must not null!\n");
		return;
	}
	while(p2!=NULL)
	{
		p2->term.coef*=-1;		//change nagetive
		InsertItem(p1,p2->term);
		p2=p2->next;
	}
	printf("Sub:");
	PrintPolynomial(p1);
}

void MultiplyPolyList(Polynomial *p1,const Polynomial *p2,Polynomial* pResult)
{
	Polynomial *p1_i=p1;
	Term pTempTerm;

	while(p2!=NULL)	//遍历p2
	{
		p1_i=p1;
		while(p1_i!=NULL)	//遍历p1
		{
			pTempTerm=MultiplyItem(p1_i->term,p2->term);
			if(pTempTerm.coef!=0)
			{
				InsertItem(pResult,pTempTerm);
			}
			p1_i=p1_i->next;
		}
		p2=p2->next;
	}
}

void ShowMenu()
{
	printf("1.	输入多项式1.\t\t\t");
	printf("2.	输入多项式2.\n");
	printf("3.	修改多项式1.\t\t\t");
	printf("4.	修改多项式2.\n");
	printf("5.	插入项到多项式1.\t\t");
	printf("6.	插入项到多项式2.\n");
	printf("7.	删除项从多项式1.\t\t");
	printf("8.	删除项从多项式2.\n");
	printf("9.	多项式相加运算.\t\t\t");
	printf("10.	多项式相减运算.\n");
	printf("11.	多项式相乘运算.\n");
	printf("----------------------------------------\n");
	printf("Please enter num of each order:	");
}

你可能感兴趣的:(数据结构与算法)