1. 程式人生 > >多項式的加減乘

多項式的加減乘

這是我學資料結構寫的第一個比較"大型"的程式,唉,還是有不足之處啊。。。

# include <stdio.h>
# include <iostream.h>
# include <stdlib.h>
# include <malloc.h>

# define OK               1
# define ERROR            0
# define OVERFLOW         -1


typedef struct Polynomial
{
	float coef;
	int expn;
	struct Polynomial *next;
}Polynomial,*LinkPolyn;

void Create_Polyn(LinkPolyn &L)
{
	int i,n;
	LinkPolyn p,s;
	L = (LinkPolyn) malloc (sizeof (Polynomial));
	if (!L)
		exit(OVERFLOW);
	L->next = NULL;
	p = L;
	cout<<"      請輸入您要建立多項式的項數:";
	cin>>n;
	for (i=0; i<n; i++)
	{
		
		s = (LinkPolyn) malloc (sizeof (Polynomial));
		p->next = s;
		cout<<"      請輸入第"<<i+1<<"項的係數:";
		cin>>s->coef;
		cout<<"      請輸入第"<<i+1<<"項的指數:";
		cin>>s->expn;

		p = s;
	}

	p->next = NULL;
}

void Display(LinkPolyn L)
{
	LinkPolyn p;
	if (!L->next )
		cout<<"      多項式的項數為0!無法顯示"<<endl;
	p = L->next ;
	while (p)
	{
	
		
		if (p->coef == 0.0)
			cout<<"";
		else if (p->expn == 0 )
			cout<<p->coef ;
		else if (p->coef == 1 && p->expn == 1)
			cout<<"X";
		else if (p->coef == 1 && p->expn != 1)
			cout<<"X^"<<p->expn;
		else if (p->coef == -1 && p->expn == 1)
			cout<<"-X";
		else if (p->coef != 1 && p->expn == 1)
			cout<<p->coef<<"X";

		else if (p->coef == -1 && p->expn != 1)
			cout<<"-X^"<<p->expn ;
		else
			cout<<p->coef <<"X^"<<p->expn ;

		

	
		if (p->next!=NULL && p->coef !=0 && p->next->coef >=0)
			cout<<"+";
		p = p->next ;
	}
	cout<<endl;
}

void SortPolyn(LinkPolyn &L)
{
	LinkPolyn p,q;
	int temp;
	float tmp;
	if (!L->next )
		cout<<"      多項式的項數為0,無法排序!"<<endl;
	
	for(p=L->next;p; p=p->next )
		for(q=L->next ;q; q=q->next)
		{
			if(p->expn < q->expn )
			{
				temp = p->expn ;
				p->expn = q->expn ;
				q->expn = temp;

				tmp = p->coef ;
				p->coef = q->coef ;
				q->coef = tmp;
			}
			else if (p != q && p->expn == q->expn)
			{
				p->coef +=q->coef ;
				q->coef = 0;
				q->expn = 0;
				
			
			}
		
		}

		
}

void Diff_Polyn(LinkPolyn &L)
{
	LinkPolyn p;
	if (!L->next )
		cout<<"      多項式的項數為0,無法求導!"<<endl;
	p = L->next ;
	while (p)
	{
		
		p->coef *= p->expn ;
		p->expn -=1;
		p = p->next ;
	}
}

void Int_Polyn(LinkPolyn &L)
{
	LinkPolyn p;
	if (!L->next )
		cout<<"      多項式的項數為0,無法求積分!"<<endl;
	p = L->next ;
	while (p)
	{
		
		p->coef = p->coef/(p->expn+1);
		p->expn +=1;
		p = p->next ;
	}
}

int cmp (int a, int b)
{
	if (a>b)
		return 1;
	else if (a == b)
		return 0;
	return -1;
}

int Add_Polyn(LinkPolyn &PA, LinkPolyn &PB)
{
	LinkPolyn qc,qa,qb,temp;
    float sum;
	qc = PA;
	qa = PA->next ;
	qb = PB->next ;

	while (qa && qb)
	{
		switch(cmp(qa->expn ,qb->expn ))
		{
		case -1:
			qc->next = qa;
			qc = qa;
			qa = qa->next;
			break;
			
		case 0:
			sum = qa->coef + qb->coef ;
			if(sum !=0.0)
			{
				qa->coef = sum;
				qc->next = qa;
				qc = qa;
				qa = qa->next;
			}

			else
			{
				temp = qa;
				qa = qa->next ;
				free(temp);
			}
			
			temp = qb;
			qb = qb->next ;
			free(temp);
			break;

		case 1:
			qc->next = qb;
			qc = qb;
			qb = qb->next;
			break;
		}
	}

	while(qa)
	{
			qc->next = qa;
			qc = qa;
			qa = qa->next;
	}
	while(qb)
	{
			qc->next = qb;
			qc = qb;
			qb = qb->next;
	}

	free(qb);
	return OK;
}


int Sub_Polyn(LinkPolyn &PA, LinkPolyn &PB)
{
	LinkPolyn qc,qa,qb,temp;
    float sum;
	qc = PA;
	qa = PA->next ;
	qb = PB->next ;

	while (qa && qb)
	{
		
		switch(cmp(qa->expn ,qb->expn ))
		{
			
		case -1:
			qc->next = qa;
			qc = qa;
			qa = qa->next;
			break;
			
		case 0:
			sum = qa->coef - qb->coef ;
			if(sum !=0.0)
			{
				qa->coef = sum;
				qc->next = qa;
				qc = qa;
				qa = qa->next;
			}

			else
			{
				temp = qa;
				qa = qa->next ;
				free(temp);
			}
			
			temp = qb;
			qb = qb->next ;
			free(temp);
			break;

		case 1:
			qb->coef =-qb->coef ;
			qc->next = qb;
			qc = qb;
			qb = qb->next;
			break;
		}
	}

	while(qa)
	{
		qc->next = qa;
		qc = qa;
		qa = qa->next;
	}
	while(qb)
	{
		qb->coef =-qb->coef ;
		qc->next = qb;
		qc = qb;
		qb = qb->next;
	}

	free(qb);
	return OK;
}

int Mul_Polyn(LinkPolyn pa,LinkPolyn pb,LinkPolyn &pc)
{
	LinkPolyn qa,qb,qc,s;
	pc = (LinkPolyn) malloc (sizeof (Polynomial));
	pc->next = NULL;
	qc = pc;
	for(qa=pa->next;qa;qa=qa->next)
	{
		for(qb=pb->next;qb;qb=qb->next)
		{
			s = (LinkPolyn) malloc (sizeof (Polynomial));
			qc->next = s;
			s->coef=qa->coef*qb->coef;
			s->expn=qa->expn+qb->expn;
			qc = s;
			
		}
	}
	qc->next = NULL;
	
	SortPolyn(pc);
	return OK;
}


int main (void)
{
	char i;
	LinkPolyn L;
	LinkPolyn PA,PB,PC;
    
	cout<<endl;
	cout<<"      ********************************************************"<<endl;
	cout<<"      |                  歡迎使用本系統!                     |"<<endl;
	cout<<"      |                  製作人:風塵三俠                     |"<<endl;
	cout<<"      |  1:建立一個多項式               2:對一個多項式求導   |"<<endl;
	cout<<"      |  3:對一個多項式求積分           4:兩個多項式相加     |"<<endl;
	cout<<"      |  5:兩個多項式相減               6:兩個多項式相乘     |"<<endl;
	cout<<"      |  7:幫助                         8:退出               |"<<endl;
	cout<<"      ********************************************************"<<endl;
	cout<<endl;
	do
	{
		cout<<"      請選擇您要執行的操作:";
		cin>>i;
		switch (i)
		{
		case '1':
			Create_Polyn(L);
			SortPolyn(L);
			cout<<"      您建立的多項式是: F(X) = ";
			Display(L);
			break;
		case '2':
			cout<<"      對一個多項式求導前請先建立一個多項式!"<<endl;
			Create_Polyn(L);
			SortPolyn(L);
			cout<<"      您建立的多項式是: F(X) = ";
			Display(L);
			Diff_Polyn(L);
			cout<<"      求導後的多項式是: F'(X) = ";
			Display(L);
			break;
		case '3':
			cout<<"      對一個多項式求積分前請先建立一個多項式!"<<endl;
			Create_Polyn(L);
			SortPolyn(L);
			cout<<"      您建立的多項式是: F(X) = ";
			Display(L);
			Int_Polyn(L);
			cout<<"      積分後的多項式是: F'(X) = ";
			Display(L);
			break;
		case '4':
			cout<<"      請先建立兩個多項式!"<<endl;
			cout<<"      建立第一個多項式!"<<endl;
			Create_Polyn(PA);
			SortPolyn(PA);
			cout<<"      您建立的第一個多項式是: F(X) = ";
			Display(PA);

			cout<<"      建立第二個多項式!"<<endl;
			Create_Polyn(PB);
			SortPolyn(PB);
			cout<<"      您建立的第二個多項式是: G(X) = ";
			Display(PB);

			cout<<"      兩個多項式相加後:F(X) + G(X) = ";
			Add_Polyn(PA, PB);
			Display(PA);
			break;
		case '5':
			cout<<"      請先建立兩個多項式!"<<endl;
			cout<<"      建立第一個多項式!"<<endl;
			Create_Polyn(PA);
			SortPolyn(PA);
			cout<<"      您建立的第一個多項式是: F(X) = ";
			Display(PA);

			cout<<"      建立第二個多項式!"<<endl;
			Create_Polyn(PB);
			SortPolyn(PB);
			cout<<"      您建立的第二個多項式是: G(X) = ";
			Display(PB);

			cout<<"      兩個多項式相減後:F(X) - G(X) = ";
			Sub_Polyn(PA, PB);
			Display(PA);
			break;
			
		case '6':
			cout<<"      請先建立兩個多項式!"<<endl;
			cout<<"      建立第一個多項式!"<<endl;
			Create_Polyn(PA);
			SortPolyn(PA);
			cout<<"      您建立的第一個多項式是: F(X) = ";
			Display(PA);

			cout<<"      建立第二個多項式!"<<endl;
			Create_Polyn(PB);
			SortPolyn(PB);
			cout<<"      您建立的第二個多項式是: G(X) = ";
			Display(PB);

			cout<<"      兩個多項式相乘後:F(X) * G(X) = ";
			Mul_Polyn(PA, PB,PC);
			Display(PC);
			break;
		case '7':
			cout<<"            建立多項式!          "<<endl;
			cout<<"      先輸入多項式的項數,要輸入自然數!"<<endl;
			cout<<"      可以輸入重複的項,先輸入係數,再輸入指數!"<<endl;
			cout<<"      係數可以是任意的數字,指數要輸入自然數!"<<endl;
			break;
		case '8':
			cout<<"      謝謝使用,再見!"<<endl;
			exit(OVERFLOW);
			break;
		default:
			{
			cout<<"      您的輸入有誤,請重新輸入!"<<endl;
			break;
			}
		}
	}while (i != 8);

	return 0;
}