1. 程式人生 > >稀疏多項式的順序儲存結構實現

稀疏多項式的順序儲存結構實現

題目摘自資料結構題集(C語言版) p20 Algo2.39-2.40

順序儲存結構

稀疏多項式的順序儲存結構SqPoly定義如下:

typedef struct {
	int coef;
	int exp;
}PolyTerm;
typedef struct
{
	PolyTerm * e;
	int length;
}SqPoly;

2.39已知稀疏多項式,其中n=em>em-1>...>e1>=0,ci!=0,m>=1。試採用儲存量同多項式項數m成正比的順序儲存結構,編寫求Pn(x0)的演算法,x0為給定值,並分析演算法的時間複雜度。

2.40 條件同2.39,編寫求P(x)=Pn1(x)-Pn2(x)的演算法,將結果多項式存放在新闢的空間中,並分析你的演算法的時間複雜度。

思路分析:2.39-從頭遍歷到表尾,x的指數次就是一個階乘,不用另外的函式,直接寫在演算法中會更加節省時間。

2.40-類似於表的merge演算法,這裡指數是從大到小排列的(我是從小到大輸入的,遞增遞減不影響結果),不需要考慮排序,設定兩個指標,兩個計數器i,j,遍歷a,b表,迴圈條件i,j均小於各自表長。

按照exp大小分成兩種情況,若不等,將指數更小的結點加入c中,該指標移位,若相等,計算新的coef,同時移位兩個指標,並將新的coef,寫入c中。(注意一下,a先遍歷完的情況,因為是a-b,負號)

標頭檔案

//Status.h
#pragma once
#define TRUE 1
#define OK 1
#define FALSE 0
#define ERROR 0
#define OVERFLOW -1
typedef int Status;
//SqPoly.h
#pragma once
#include "Status.h"
#define INITSIZE 100
typedef struct {
	int coef;
	int exp;
}PolyTerm;
typedef struct
{
	PolyTerm * e;
	int length;
}SqPoly;
Status Init(SqPoly &L);
Status MakePoly(SqPoly &L);
double Substitute(SqPoly L, int x);
Status Print_Poly(SqPoly L);
Status Subtract(SqPoly La, SqPoly Lb, SqPoly &Lc);

函式檔案

//fun.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include "SqPoly.h"
#include "Status.h"
Status Init(SqPoly &L)
{
	L.e = (PolyTerm *)malloc(INITSIZE*sizeof(PolyTerm));
	if (!L.e) exit(OVERFLOW);
	L.length = 0;
	return OK;
}//Init
Status MakePoly(SqPoly &L)
{
	PolyTerm *p = L.e;
	printf("plz input a pair of number,coefficient and exponent.(not a number to quit)\n");
	while (scanf("%d%d", &(p->coef), &(p->exp)))
	{
		p++;
		L.length++;
		if (L.length == INITSIZE)
		{
			printf("the Poly is full.\n");
			return OVERFLOW;
		}
	}
	printf("Poly init succeed!\n");
	return OK;
}//MakePoly
Status Print_Poly(SqPoly L)
{
	if (L.length == 0)
	{
		printf("empty!\n");
		return ERROR;
	}
	PolyTerm *p = L.e;
	int count = 0;
	while (count<L.length)
	{
		printf("%d(%d) ", p->coef, p->exp);
		p++;
		count++;
	}
	while (getchar() != '\n');
        putchar('\n');
	return OK;
}
double Substitute(SqPoly L,int x)
{
	if (L.length == 0)
	{
		printf("Poly is empty,plz initialize it.\n");
		return ERROR;
	}
	PolyTerm *p = L.e;
	int count = 0;
	double sum = 0;
	while (count<L.length)
	{
		int coef = p->coef;
		int exp = p->exp;
		long int a = 1;
		int i = 0;
		if (exp == 0)
			a = 1;
		else
			while (i<exp)
			{
				a = a*x;
				++i;
			}
		sum += coef*a;
		count++;
		p++;
	}
	return sum;
}
Status Subtract(SqPoly La, SqPoly Lb, SqPoly &Lc)
{
	PolyTerm *pa, *pb, *pc;
	int i, j, k;
	pa = La.e;
	pb = Lb.e;
	pc = Lc.e;
	i = j = 0;
	while (i<La.length&&j<Lb.length)
	{
		if (pa->exp < pb->exp)
		{
			pc->exp = pa->exp;
			pc->coef = pa->coef;
			pc++; pa++;
			i++; Lc.length++;
		}
		else
			if (pa->exp > pb->exp)
			{
				pc->exp = pb->exp;
				pc->coef = pb->coef;
				pb++; pc++;
				j++; Lc.length++;
			}
			else
			{
				pc->coef = pa->coef - pb->coef;
				pc->exp = pa->exp;
				pc++; pa++; pb++;
				i++; j++; Lc.length++;
			}
	}
		while (i<La.length)
		{
			pc->coef = pa->coef;
			pc->exp = pa->exp;
			i++; Lc.length++;
		}
		while (j<La.length)
		{
			pc->coef = -pb->coef;
			pc->exp = pb->exp;
			j++; Lc.length++;
		}
	return OK;
}

主函式

2.39

//main.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include"SqPoly.h"
#include "Status.h"
int main(void)
{
	SqPoly La;
	Init(La);
	MakePoly(La);
	Print_Poly(La);
	int x;
	printf("put the x you want to substitute into the poly.(not a number to quit)\n");
	while (scanf("%d",&x))
	{
		printf("the result is %f\n", Substitute(La, x));
		printf("put the x you want to substitute into the poly.(not a number to quit)\n");
	}
	system("pause");
	return 0;
}//main

執行結果


2.40

//main.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include"SqPoly.h"
#include "Status.h"
int main(void)
{
	SqPoly La, Lb, Lc;
	Init(La);
	Init(Lb);
	Init(Lc);
	MakePoly(La);
	MakePoly(Lb);
	Print_Poly(La);
	Print_Poly(Lb);
	Subtract(La, Lb, Lc);
	Print_Poly(Lc);
	system("pause");
	return 0;
}//main

執行結果

如有錯誤,歡迎留言指出 :)