稀疏多项式的顺序储存结构实现

题目摘自数据结构题集(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 
#include 
#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 (countcoef, 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 (countcoef;
		int exp = p->exp;
		long int a = 1;
		int i = 0;
		if (exp == 0)
			a = 1;
		else
			while (iexp < 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 (icoef = pa->coef;
			pc->exp = pa->exp;
			i++; Lc.length++;
		}
		while (jcoef = -pb->coef;
			pc->exp = pb->exp;
			j++; Lc.length++;
		}
	return OK;
}

主函数

2.39

//main.cpp
#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#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

运行结果

稀疏多项式的顺序储存结构实现_第1张图片

2.40

//main.cpp
#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#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

运行结果

稀疏多项式的顺序储存结构实现_第2张图片

如有错误,欢迎留言指出 :)

你可能感兴趣的:(数据结构,严蔚敏,数据结构题集,顺序表,稀疏多项式,链表)