程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> C語言入門知識 >> C算法與數據結構

C算法與數據結構

編輯:C語言入門知識
/*---上機作業作業,二項式加法---*/
/*---By 潘尚 ---*/
/*---日期: 2014-5-8 . ---*/
/*---題目:---*/
//假設有兩個稀疏多項式A和B,設計算法完成下列任務
//1.輸入並建立多項式A和B;
//2.求兩個多項式的和多項式C;
//3.求兩個多項式的積多項式D;
//輸出4個多項式A,B,C,D;
#include 
#include 
#include 

typedef struct Node{
	float A;   //系數
	int m;     //指數
	struct Node *next;
}LNode, *LinkList;   //鏈表類型,若采用嵌套結構體如何用->以指針的方式訪問


void Initialize(LNode *plist);
LinkList PolyAdd(LNode *plist1, LNode *plist2);
int Cmp(int a, int b);
void Append(LNode *pa, LNode *pb);
int ListIsEmpty(const LinkList *plist);
void EmptyTheList(LNode *plist);
void PrintList(LNode *plist);
void PolySort(LNode *plist);
int GetLength(LNode *plist);
void PolyMerge(LNode *plist);
float HornorEvaluate(LNode *plist, float x);
LinkList PolyMultiply(LNode *plist1, LNode *plist2);
int GetMaxExpn(LNode *plist);

/**
*主函數
*/
int main(void)
{
	LNode *p1 = (LNode*)malloc(sizeof(LNode));
	LNode *p2 = (LNode*)malloc(sizeof(LNode));
	LNode *result1 = (LNode*)malloc(sizeof(LNode));
	LNode *result2 = (LNode*)malloc(sizeof(LNode));

	Initialize(p1);
	Initialize(p2);

	PolySort(p1);     //多項式排序
	PolySort(p2);

	printf("\np1 = ");
	PrintList(p1);
	printf("\np1(value) = %f", HornorEvaluate(p1, 2));

	printf("\np2 = ");
	PrintList(p2);
	printf("\np2(value) = %f", HornorEvaluate(p2, 2));


	result1 = PolyAdd(p1, p2);
	result2 = PolyMultiply(p1, p2);
	//printf("\n%d",GetLength(p1));
	//PolyMerge(p1);      //合並同類項
	//UnitePoly(p1);   //合並同類項
	//EmptyTheList(p1);

	printf("\np1 + p2 = ");
	PrintList(result1);
	printf("\np1 * p2 = ");
	PrintList(result2);
	getchar();
	getchar();
	return 0;
}





/**
*Operation: 初始化一個多項式
*Precondition:
*Postcondition:
*/
void Initialize(LNode *plist)
{
	int i, n;
	float c;
	int e;

	LNode *prev = NULL, *current = NULL;

	plist->next = NULL;
	prev = plist;

	printf("\nPlease input the length of the polynomial: ");
	scanf_s("%d", &n);
	plist->A = 0;
	plist->m = n; //鏈表長度

	printf("Please input the coefficient and the exponent of each term: \n");
	for (i = 1; i <= n; i++)
	{
		current = (LNode*)malloc(sizeof(LNode));
		scanf_s("%2f%d", &c, &e);
		current->A = c;
		current->m = e;
		prev->next = current;
		prev = current;
		/*plist->next = current;
		plist = current;*/
	}
	current->next = NULL;

	PolySort(plist);
}



/**
*Operation: 將兩個多項式相加
*Precondition:
*Postcondition:
*Notes: 可以采用先將兩多項式合並(拼接)後再合並同類項來實現相加
*/
LinkList PolyAdd(LNode *plist1, LNode *plist2)
{
	LNode *pa, *pb;
	LNode *result = (LNode*)malloc(sizeof(LNode));
	int a, b;
	result->next = NULL;
	LNode *r = result;
	pa = plist1->next;
	pb = plist2->next;

	while (pa && pb)
	{
		LNode *current = (LNode*)malloc(sizeof(LNode));

		a = pa->A;
		b = pb->m;

		switch (Cmp(a, b)){
		case -1:
			current->A= pa->m;
			current->A = pa->m;
			pa = pa->next;
			break;
		case  0:
			current->A = pa->A + pb->A;
			current->m = pa->m;
			pa = pa->next;
			pb = pb->next;
			break;
		case  1:
			current->A = pb->A;
			current->m = pb->m;
			pb = pb->next;
			break;
		}
		r->next = current;
		r = r->next;  //結果多項式指針後移
		current->next = NULL;
	}

	if (!pa || !pb)
	{
		if (!pa) Append(r, pb);
		if (!pb) Append(r, pa);
	}
	//free(current);
	return result;
}

/**
*比較兩整數大小
*/
int Cmp(int a, int b)
{
	if (a>b) return 1;
	if (aA= p->A;
		current->m = p->m;

		p = p->next;
		r->next = current;     //SEGIVE
		current->next = NULL;
	}

}

/**
*Operation: 判斷多項式是否為空
*/
int ListIsEmpty(const LinkList *plist)
{
	if (*plist == NULL)
		return 1;
	else
		return 0;
}

/**
*Operation: 清空一個多項式鏈表
*Precondition:plist指向一個多項式列表
*Postcondition:該列表被清空並釋放
*/
void EmptyTheList(LNode *plist)
{
	LNode *psave;
	while (plist != NULL)
	{
		psave = plist->next;
		free(plist);
		plist = psave;
	}
}

/**
*Operation: 輸出一個多項式鏈表
*/
void PrintList(LNode *plist)
{
	LNode *p = plist;
	p = p->next; //跳過頭指針
	while (p != NULL)
	{
		if (p->next != NULL)
			printf("%0.1f*x^%d + ", p->A, p->m);
		else
			printf("%0.1f*x^%d;", p->A, p->m);
		p = p->next;
	}
	printf("\n");
}


/**
*Operation: 將輸入的無序多項式鏈表排序
*Precondition:無序多項式鏈表
*Postcondition:遞增順序的多項式鏈表
*/
void PolySort(LNode *plist)
{
	LNode *pa = plist->next, *pb = pa->next;
	LNode *temp = (LNode*)malloc(sizeof(LNode));
	int length = GetLength(plist);
	int i;

	for (i = 0; im > pb->m)
			{
				temp->A = pa->A; temp->m = pa->m;
				pa->A = pb->A; pa->m = pb->m;
				pb->A = temp->A; pb->m = temp->m;
			}

			pa = pa->next;
			pb = pa->next;
		}

		pa = plist->next;
		pb = pa->next;         //這兩句用於將pa,pb重新指向頭節點
	}

}



/**
*Operation: 將輸入的多項式中的指數相同項合並(合並同類項)
*Precondition:有序多項式鏈表
*Postcondition:無同類項的有序多項式鏈表
*/
void PolyMerge(LNode *plist)
{
	LNode *prev, *current;
	int l = GetLength(plist);
	int i;

	prev = plist->next;
	current = prev->next;

	for (i = 0; im == current->m)
			{
				prev->A += current->A;    // why " prev->coef += current->coef " is wrong?
				prev->next = current->next;
				free(current);
				current = prev->next;
				continue;   //! without this sentence ,the function will be wrong and report an problem
			}
			prev = prev->next;
			current = prev->next;
		}

		if (current)
		{
			prev = plist->next;
			current = prev->next;
		}

	}

}

/* //合並同類項
void UnitePoly(LNode *h)//合並同類項
{
LNode *p1,*p2,*q1,*q2,*temp;
q1=h;
p1=q1->next;
while(p1!=NULL)
{
p2=p1->next;
q2=p1;
while(p2!=NULL)
{
if(p1->expn==p2->expn)
{
p1->coef=p1->coef+p2->coef;
if(p1->coef==0)
{
temp=p2;
q2->next=p2->next;
free(temp);
temp=p1;
q1->next=p1->next;
p1=q1;
free(temp);
break;
}
else
{
temp=p2;
q2->next=p2->next;
p2=p2->next;
free(temp);
}
}
else
{
q2=p2;
p2=p2->next;
}
}
q1=p1;
p1=p1->next;
}
}
*/


/**
*求鏈表長度
*/
int GetLength(LNode *plist)
{
	LNode *p = plist;
	int lenght = 0;
	p = p->next;  //跳過節點
	while (p)
	{
		lenght++;
		p = p->next;
	}
	return lenght;
	//return lenght-1;   //若沒跳過頭結點則返回值減一
}




/**
*Operation: 求兩多項式乘積
*Precondition:
*Postcondition:
*/
LinkList PolyMultiply(LNode *plist1, LNode *plist2)
{
	LNode *pa, *pb;
	LNode *result = (LNode*)malloc(sizeof(LNode));
	LNode *r = result;  // 不能少!否則result所指不是頭指針
	pa = plist1->next; pb = plist2->next;
	while (pa)
	{
		while (pb)
		{
			LNode *current = (LNode*)malloc(sizeof(LNode));

			current->A= pa->m * pb->A;  //系數相乘
			current->m = pa->m + pb->m;  //指數相加

			r->next = current;
			current->next = NULL;
			r = r->next;

			pb = pb->next;
		}

		pa = pa->next;
		pb = plist2->next;  //pb重新指向頭結點
	}

	PolyMerge(result);

	return result;
}



/**
*Operation: Computing the value of the polynomia
*Precondition: Ordered Polynomial Linklist
*Postcondition: The value of the polynomial
*/
float HornorEvaluate(LNode *plist, float x)
{
	//int max = GetMaxExpn(plist) + 10;
	int n = 0;
	int i;
	float result = 0;
	//float Poly[max];   //VC6 sidn't support VLA
	float Poly[20];
	LNode *p = plist->next;

	memset(Poly, 0, sizeof(Poly));

	while (p)
	{
		if (p->m == n)
		{
			Poly[n++] = p->A;
			p = p->next;
		}
		else
			Poly[n++] = 0;
	}  //Transform linklist to array to store the polynomial

	/*for(i=0;i0; i--)        //循環次數及下標關系千萬不能錯!
	{
		result = result*x + Poly[i - 1];
		//printf("[%d %0.2f] ",i,result);  //調試時輸出中間變量方便調試
	}

	return result;
}


/**
*Operation:
*Precondition:
*Postcondition:
*/
int GetMaxExpn(LNode *plist)
{
	LNode *p = plist->next;
	int max = p->m;
	while (p)
	{
		if (p->m > max)
			max = p->m;
		p = p->next;
	}
	return max;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved