数据结构和算法课件_第1页
数据结构和算法课件_第2页
数据结构和算法课件_第3页
数据结构和算法课件_第4页
数据结构和算法课件_第5页
已阅读5页,还剩125页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

数据结构与算法数据结构与算法1作为抽象数据类型的数组一维数组一维数组的示例作为抽象数据类型的数组一维数组2一维数组的特点连续存储的线性聚集(别名向量)除第一个元素外,其他每一个元素有一个且仅有一个直接前驱。除最后一个元素外,其他每一个元素有一个且仅有一个直接后继。一维数组的特点连续存储的线性聚集(别名向量)3数组的定义和初始化#include<iostream.h>classszcl{inte; public:

szcl(){e=0;}

szcl(intvalue){e=value;} intget_value(){returne;} }数组的定义和初始化#include<iostream.h>4main

(){

szcla1[3]={3,5,7},*elem;

for(int

i=0,i<3,i++)

cout<<a1[i].get_value()<<“\n”; //打印静态数组

elem=&a1;

for(int

i=0,i<3,i++){

cout<<elem→get_value()<<“\n”; //打印动态数组

elem++;

}

return0;}

main(){5一维数组(Array)类的定义#include<iostream.h>#include<stdlib.h>template<classType>classArray{Type

*elements;//数组存放空间

intArraySize;//当前长度

voidgetArray();//建立数组空间

public:Array(intSize=DefaultSize);

Array(const

Array<Type>&x);一维数组(Array)类的定义#include<iost6~Array(){delete[]elements;} Array<Type>

&

operator

=

(const

Array<Type>

&A);Type&operato[](inti); operatorType*

()

const

{return

elements;}int

Length()const{returnArraySize;} voidReSize(intsz); }~Array(){delete[]e7template<classType>void

Array<Type>::getArray(){

//私有函数:创建数组存储空间

elements=newType[ArraySize];if(elements==0)

cerr<<"MemoryAllocationError"<<endl;}一维数组公共操作的实现template<classType>一维数组公共操8template<classType>void

Array<Type>::Array(intsz){

//构造函数

if(sz<=0){cerr<<"InvalidArraySize"<<

endl;return;}

ArraySize=sz;getArray();

}template<classType>9template<classType> Array<Type>::

Array(const

Array<Type>

&x){

//复制构造函数

int;

elements=newType[n]; if(elements==0)

cerr

<<"MemoryAllocationError"<<endl;

Type;

Type

*destptr=elements;

while

(n--

)*destptr++=*srcptr++;}template<classType> Array<T10template<classType>Type&Array<Type>::operator[](inti){

//按数组名及下标i,取数组元素的值

if(i<0

||i>ArraySize-1)

cerr<<"IndexoutofRange"<<

endl;

returnelement[i]; }

使用该函数于赋值语句

Position[i]=Position[i-1]+Number[i

-1]template<classType>11template<classType>void

Array<Type>::Resize(int

sz){if(sz>=0&&

sz!=ArraySize){Type*newarray=

newType[sz]; if(newarray==0)

cerr

<<"MemoryAllocationError"<<

endl;intn=(sz<=ArraySize)?sz:ArraySize; Type

*srcptr=elements; Type*destptr=newarray; while(n--)*destptr++=*srcptr++;

delete[]elements; elements=newarray; ArraySize=sz; }}

template<classType>12

二维数组三维数组行向量下标

i页向量下标i列向量下标

j行向量下标j

列向量下标k二维数组三维数组行向量下标13数组的连续存储方式一维数组LOC(i)=LOC(i-1)+l=α+i*l数组的连续存储方式一维数组LOC(i)=LOC(14

二维数组行优先LOC(j,k)=j*m+k

二维数组行优先LOC(j,k)=j*m15n维数组各维元素个数为

m1,m2,m3,…,mn下标为i1,i2,i3,…,in

的数组元素的存储地址:n维数组各维元素个数为m1,m2,m3,…,m16顺序表(SequentialList)顺序表的定义和特点定义n(0)个表项的有限序列

(a1,a2,…,an)

ai是表项,n是表长度。 特点顺序存取遍历逐项访问从前向后从后向前从两端向中间顺序表(SequentialList)顺序表的定义和特点17顺序表(SeqList)类的定义template<classType> class

SeqList{Type*data;//顺序表存储数组

intMaxSize; //最大允许长度

int

last; //当前最后元素下标public:

SeqList(intMaxSize=defaultSize);

~SeqList()

{delete[]data;}

intLength()

const

{return

last+1;

}

intFind(Type

&x)const;顺序表(SeqList)类的定义template<clas18

intIsIn(Type

&x);

int

Insert(Type&x,int

i);

int

Remove(Type

&x);

intNext(Type

&x);

intPrior(Type

&x);intIsEmpty()

{return

last==-1;

} int

IsFull()

{returnlast==MaxSize-1;

} Type

&Get(

inti){return

i<0

||i>last?NULL

:

data[i];}}

intIsIn(Type&x); 19顺序表部分公共操作的实现template<classType>

SeqList<Type>::SeqList(intsz){//构造函数

if(sz>0)

{

MaxSize=sz;last=-1;

data=newType[MaxSize];

} }顺序表部分公共操作的实现template<classT20template<classType> int

SeqList<Type>::Find(Type

&x)const{//搜索函数:在表中从前向后顺序查找x

inti=0;

while

(i<=last&&data[i]

!=x)

i++;

if(i>last)return

-1;

elsereturni; }template<classType> 21顺序搜索图示x=48x=50顺序搜索图示x=4822搜索成功

若搜索概率相等,则

搜索不成功数据比较n

次搜索成功

若搜索概率相等,则

23表项的插入表项的插入24template<classType>int

SeqList<Type>::Insert(

Type

&x,

int

i){//在表中第i

个位置插入新元素x

if(i<0

||

i>last+1

||

last==MaxSize-1)

return0;//插入不成功

else{

last++;

for(

intj=last;j>i;j--

)

data[j]=data[j-1];

data[i]=x;return1;//插入成功

}}template<classType>25表项的删除表项的删除26template<classType>int

SeqList<Type>::Remove(

Type

&x){//在表中删除已有元素x

int

i=Find(x); //在表中搜索x

if(i>=0

){

last--;

for(intj=i;j<=last;j++)

data[j]=data[j+1];

return1; //成功删除

} return0;//表中没有

x}template<classType>27顺序表的应用:集合的“并”运算

template<classType>void

Union(SeqList<Type>

&LA,SeqList<Type>

&LB){

intn=LA.Length();

intm=LB.Length();

for(inti=1;i<=m;i++){ Type(i);//在LB中取一元素

int(x);//在LA中搜索它

if(k==

-1)//若未找到插入它

{LA.Insert(n+1,x);n++;}}}顺序表的应用:集合的“并”运算template<clas28template<classType> voidIntersection(SeqList<Type>

&LA,SeqList<Type>

&LB){

intn=LA.Length();

intm=LB.Length();inti=1;

while(i<n){Type(i);//在LA中取一元素

int(x);//在LB中搜索它

if(k==

-1){LA.Remove(i);n--;} elsei++;//未找到在LA中删除它

}}顺序表的应用:集合的“交”运算template<classType> 顺序表的应用:29多项式(Polynomial)n阶多项式Pn(x)有n+1项。系数a0,a1,a2,…,an

指数0,1,2,…,n。按升幂排列多项式(Polynomial)n阶多项式Pn(x)有n+130classPolynomial

{public:

Polynomial();//构造函数

intoperator!

();//判是否零多项式

CoefficientCoef(Exponente);

Exponent

LeadExp();//返回最大指数

Polynomial

Add(Polynomial

poly);

Polynomial

Mult(Polynomialpoly);

floatEval(float

x); //求值}多项式(Polynomial)的抽象数据类型classPolynomial{多项式(Polynomi31#include<iostream.h>class

power{

double

x;

int

e;

double

mul;

public:

power(double

val,intexp);

double

get_power(){returnmul;}

};创建power类,计算x的幂#include<iostream.h>创建power类32power::power(double

val,int

exp){

//按val值计算乘幂

x=val;

e=exp;mul;

if(exp==0)return;

for(;

exp>0;

exp--)mul=mul*x;

}main(){

powerpwr(1.5,2);

cout<<pwr.get_power()<<“\n”;

}power::power(doubleval,int33

多项式的存储表示第一种:

private:(静态数

intdegree; 组表示)floatcoef[maxDegree+1];

Pn(x)可以表示为:

=n [i]=ai,0

i

n多项式的存储表示第一种:private:34第二种:private:

(动态数

int

degree;组表示)

float*coef;

Polynomial::Polynomial(int

sz){

degree=sz;

coef=newfloat[degree+1];

}以上两种存储表示适用于指数连续排列的多项式。但对于指数不全的多项式如P101(x)=3+5x50-14x101,不经济。第二种:private: 35第三种:

class

Polynomial;

classterm{

//多项式的项定义

friendPolynomial;

private:

floatcoef;//系数

intexp; //指数

};第三种:36classPolynomial{//多项式定义public:……private:

statictermtermArray[MaxTerms];//项数组

staticintfree;//当前空闲位置指针

//term

Polynomial::termArray[MaxTerms];//intPolynomial::free=0;

intstart,finish; //多项式始末位置 }classPolynomial{//37A(xx1000B(xx50x101

两个多项式存放在termArray中A(xx1000两个多项式存放在termArray中38两个多项式的相加结果多项式另存扫描两个相加多项式,若都未检测完:若当前被检测项指数相等,系数相加。若未变成0,则将结果加到结果多项式。若当前被检测项指数不等,将指数小者加到结果多项式。若有一个多项式已检测完,将另一个多项式剩余部分复制到结果多项式。两个多项式的相加结果多项式另存39PolynomialPolynomial::Add(PolynomialB){

PolynomialC;

int

a=start;

int

;

C.start=free;

floatc;

while(a<=finish&&)

Switch

(compare(termArray[a].exp,termArray[b].exp)){

//比较对应项指数

case‘=’://指数相等

c=termArray[a].coef+termArray[b].coef;if(c)NewTerm(c,termArray[a].exp);

a++;b++;

break;

PolynomialPolynomial::Add(P40case'>':

NewTerm(termArray[b].coef,

termArray[b].exp);

b++;

break;

case'<':

NewTerm(termArray[a].coef,

termArray[a].exp);

a++;

}for(;a<=finish;a++)//a未检测完时

NewTerm(termArray[a].coef,

termArray[a].exp);

case'>':41for(

;;b++)//b未检测完时

NewTerm(termArray[b].coef,

termArray[b].exp);

C.finish=free-1;

return

C;}for(;;b++)//b未检测完时42在多项式中加入新的项

void

Polynomial::NewTerm(floatc,

inte){

//把一个新的项加到多项式C(x)中

if(free>=maxTerms){cout<<"Toomanytermsinpolynomials”<<

endl;return;

}

termArray[free].coef=c;

termArray[free].exp=e;

free++;}

在多项式中加入新的项43稀疏矩阵

(SparseMatrix)行数m=6,列数n=7,非零元素个数t=6

稀疏矩阵(SparseMatrix)行数m=6,44稀疏矩阵(SparseMatrix)的抽象数据类型

template<classType>class

SparseMatrix

{

int

Rows,Cols,Terms;//行/列/非零元素数

Trituple<Type>smArray[MaxTerms];

public://三元组表

SparseMatrix(int

MaxRow,intMaxcol);

SparseMatrix<Type>Transpose();//转置

SparseMatrix<Type>//相加

Add(SparseMatrix<Type>b);SparseMatrix<Type>//相乘

Multiply(SparseMatrix<Type>b);}

稀疏矩阵(SparseMatrix)的抽象数据类型45

三元组(Trituple)类的定义

template<classType>class

SparseMatrix;template<classType>classTrituple

{ friendclass

SparseMatrixprivate:

int

row,col; //非零元素所在行号/列号

Typevalue;//非零元素的值

}

三元组(Trituple)类的定义46稀疏矩阵转置矩阵稀疏矩阵转置矩阵47用三元组表表示的稀疏矩阵及其转置用三元组表表示的稀疏矩阵及其转置48稀疏矩阵转置算法思想设矩阵列数为Cols,对矩阵三元组表扫描Cols次。第k次检测列号为k的项。第k次扫描找寻所有列号为k的项,将其行号变列号、列号变行号,顺次存于转置矩阵三元组表。设矩阵三元组表总共有Terms项,其时间代价为O(Cols*Terms)。若矩阵有200行,200列,10,000个非零元素,总共有2,000,000次处理。稀疏矩阵转置算法思想设矩阵列数为Cols,对矩阵三元组表扫描49稀疏矩阵的转置

template<classType>SparseMatrix<Type>SparseMatrix<Type>::

Transpose(){

SparseMatrix<Type>b;

b.Rows=Cols;

b.Cols=Rows;

b.Terms=Terms;//转置矩阵的列数,行数和非零元素个数

if(Terms>0){

int

CurrentB=0; //转置三元组表存放指针稀疏矩阵的转置50for(intk=0;k<Cols;k++)

for(inti=0;i<Terms;i++)

if(smArray[i].col==k){ [CurrentB].row=k;

[CurrentB].col=smArray[i].row; [CurrentB].value=smArray[i].value;

CurrentB++;

}}

returnb;}for(intk=0;k<Cols51快速转置算法建立辅助数组rowSize和rowStart,记录矩阵转置后各行非零元素个数和各行元素在转置三元组表中开始存放位置。扫描矩阵三元组表,根据某项的列号,确定它转置后的行号,查rowStart表,按查到的位置直接将该项存入转置三元组表中。转置时间代价为O(max(Terms,Cols))。若矩阵有200列,10000个非零元素,总共需10000次处理。快速转置算法建立辅助数组rowSize和rowStart,记52for(inti=0;i<Cols;i++)rowSize[i]=0; for(i=0;i<Terms;i++)rowSize[smArray[i].col]++;

rowStart[0]=0; for(i=1;i<Cols;i++) rowStart[i]=rowStart[i-1]+rowSize[i-1];for(inti=0;i<Cols;i++)53稀疏矩阵的快速转置template<classType> SparseMatrix<Type>SparseMatrix<Type>::FastTranspos(){

int

*rowSize=newint[Cols];

int*rowStart=

newint[Cols];

SparseMatrix<Type>b;

b.Rows=Cols;b.Cols=Rows;

b.Terms=Terms;if(Terms>0){for(inti=0;i<Cols;i++)rowSize[i]=0;for(i=0;i<Terms;i++)

rowSize[smArray[i].col]++;稀疏矩阵的快速转置54

rowStart[0]=0;

for(i=1;i<Cols;i++) rowStart[i]=rowStart[i-1]+rowSize[i-1]; for(i=0;i<Terms;i++){ intj=rowStart[smArray[i].col]; [j].row=smArray[i].col;[j].col=smArray[i].row;[j].value=smArray[i].value; rowStart[smArray[i].col]++;

}}

delete

[]rowSize;

delete[]rowStart;

returnb;} rowStart[0]=0; 55字符串(String)字符串是n(0)个字符的有限序列,记作S:“c1c2c3…cn”

其中,S是串名字“c1c2c3…cn”是串值

ci是串中字符

n是串的长度。字符串(String)字符串是n(0)个字符的56constintmaxLen=128;

classString{intcurLen;//串的当前长度

char

*ch;//串的存储数组

public:

String(constString&ob);

String(constchar*init);

String(

);~String(){delete[]ch;}

intLength()const{

return

curLen;}字符串抽象数据类型和类定义constintmaxLen=128; 字符串抽57

String

&operator()

(int

pos,

intlen);

intoperator

==(const

String&ob)const{returnstrcmp(ch,)==0;}

intoperator!=(const

String&ob)const{returnstrcmp(ch,)!=0;}

intoperator!()

const

{returncurLen==0;}

String

&operator=(const

String

&ob);

String&operator+=(const

String&ob);char&operator[](inti);

intFind(Stringpat)const;}String&operator()(int58

String::String(constString&ob){

//复制构造函数:从已有串ob复制

ch=newchar[maxLen+1];

if(!ch){

cout<<“AllocationError\n”;

exit(1);

}

curLen=

;

strcpy(ch,ob.ch);}

字符串部分操作的实现String::String(constStrin59String::String(const

char*init){//复制构造函数:从已有字符数组*init复制

ch=newchar[maxLen+1];

if(!ch){cout<<“AllocationError\n”;

exit(1);

}

curLen=

strlen(init);

strcpy(ch,init);

}String::String(constchar*i60String::String(

){//构造函数:创建一个空串

ch=newchar[maxLen+1];

if(!ch){

cout<<“AllocationError\n”;

exit(1);}

curLen=0;

ch[0]=‘\0’;}String::String(){61提取子串的算法示例pos+len-1 pos+len-1

curLen-1

curLen提取子串的算法示例pos+len-1 62String&String::operator()(intpos,

int

len){//从串中第pos个位置起连续提取len个字符

//形成子串返回

if(pos<0

||pos+len-1>=maxLen

||len<0){

temp.curLen=0;

//返回空串

[0]='\0';

}

else{//提取子串

String*temp=newString;//动态分配String&String::63

if(pos+len-1>=curLen)

len=curLen-pos;

temp→curLen=len;//子串长度

for(inti=0,j=pos;i<len;i++,j++)

temp→ch[i]=ch[j];//传送串数组

temp→ch[len]=‘\0’;//子串结束

}

return

temp;}

if(pos+len-1>=curLen)64String&String::operator=

(const

String&ob){//串赋值:从已有串ob复制

if(&ob!=this){

delete[]ch;

ch=new

char[maxLen+1];//重新分配

if(!ch){cerr<<“OutOfMemory!\n”;

exit(1);

}

curLen=;//串复制

strcpy(ch,);

}String&String::65数据结构与算法数据结构与算法66作为抽象数据类型的数组一维数组一维数组的示例作为抽象数据类型的数组一维数组67一维数组的特点连续存储的线性聚集(别名向量)除第一个元素外,其他每一个元素有一个且仅有一个直接前驱。除最后一个元素外,其他每一个元素有一个且仅有一个直接后继。一维数组的特点连续存储的线性聚集(别名向量)68数组的定义和初始化#include<iostream.h>classszcl{inte; public:

szcl(){e=0;}

szcl(intvalue){e=value;} intget_value(){returne;} }数组的定义和初始化#include<iostream.h>69main

(){

szcla1[3]={3,5,7},*elem;

for(int

i=0,i<3,i++)

cout<<a1[i].get_value()<<“\n”; //打印静态数组

elem=&a1;

for(int

i=0,i<3,i++){

cout<<elem→get_value()<<“\n”; //打印动态数组

elem++;

}

return0;}

main(){70一维数组(Array)类的定义#include<iostream.h>#include<stdlib.h>template<classType>classArray{Type

*elements;//数组存放空间

intArraySize;//当前长度

voidgetArray();//建立数组空间

public:Array(intSize=DefaultSize);

Array(const

Array<Type>&x);一维数组(Array)类的定义#include<iost71~Array(){delete[]elements;} Array<Type>

&

operator

=

(const

Array<Type>

&A);Type&operato[](inti); operatorType*

()

const

{return

elements;}int

Length()const{returnArraySize;} voidReSize(intsz); }~Array(){delete[]e72template<classType>void

Array<Type>::getArray(){

//私有函数:创建数组存储空间

elements=newType[ArraySize];if(elements==0)

cerr<<"MemoryAllocationError"<<endl;}一维数组公共操作的实现template<classType>一维数组公共操73template<classType>void

Array<Type>::Array(intsz){

//构造函数

if(sz<=0){cerr<<"InvalidArraySize"<<

endl;return;}

ArraySize=sz;getArray();

}template<classType>74template<classType> Array<Type>::

Array(const

Array<Type>

&x){

//复制构造函数

int;

elements=newType[n]; if(elements==0)

cerr

<<"MemoryAllocationError"<<endl;

Type;

Type

*destptr=elements;

while

(n--

)*destptr++=*srcptr++;}template<classType> Array<T75template<classType>Type&Array<Type>::operator[](inti){

//按数组名及下标i,取数组元素的值

if(i<0

||i>ArraySize-1)

cerr<<"IndexoutofRange"<<

endl;

returnelement[i]; }

使用该函数于赋值语句

Position[i]=Position[i-1]+Number[i

-1]template<classType>76template<classType>void

Array<Type>::Resize(int

sz){if(sz>=0&&

sz!=ArraySize){Type*newarray=

newType[sz]; if(newarray==0)

cerr

<<"MemoryAllocationError"<<

endl;intn=(sz<=ArraySize)?sz:ArraySize; Type

*srcptr=elements; Type*destptr=newarray; while(n--)*destptr++=*srcptr++;

delete[]elements; elements=newarray; ArraySize=sz; }}

template<classType>77

二维数组三维数组行向量下标

i页向量下标i列向量下标

j行向量下标j

列向量下标k二维数组三维数组行向量下标78数组的连续存储方式一维数组LOC(i)=LOC(i-1)+l=α+i*l数组的连续存储方式一维数组LOC(i)=LOC(79

二维数组行优先LOC(j,k)=j*m+k

二维数组行优先LOC(j,k)=j*m80n维数组各维元素个数为

m1,m2,m3,…,mn下标为i1,i2,i3,…,in

的数组元素的存储地址:n维数组各维元素个数为m1,m2,m3,…,m81顺序表(SequentialList)顺序表的定义和特点定义n(0)个表项的有限序列

(a1,a2,…,an)

ai是表项,n是表长度。 特点顺序存取遍历逐项访问从前向后从后向前从两端向中间顺序表(SequentialList)顺序表的定义和特点82顺序表(SeqList)类的定义template<classType> class

SeqList{Type*data;//顺序表存储数组

intMaxSize; //最大允许长度

int

last; //当前最后元素下标public:

SeqList(intMaxSize=defaultSize);

~SeqList()

{delete[]data;}

intLength()

const

{return

last+1;

}

intFind(Type

&x)const;顺序表(SeqList)类的定义template<clas83

intIsIn(Type

&x);

int

Insert(Type&x,int

i);

int

Remove(Type

&x);

intNext(Type

&x);

intPrior(Type

&x);intIsEmpty()

{return

last==-1;

} int

IsFull()

{returnlast==MaxSize-1;

} Type

&Get(

inti){return

i<0

||i>last?NULL

:

data[i];}}

intIsIn(Type&x); 84顺序表部分公共操作的实现template<classType>

SeqList<Type>::SeqList(intsz){//构造函数

if(sz>0)

{

MaxSize=sz;last=-1;

data=newType[MaxSize];

} }顺序表部分公共操作的实现template<classT85template<classType> int

SeqList<Type>::Find(Type

&x)const{//搜索函数:在表中从前向后顺序查找x

inti=0;

while

(i<=last&&data[i]

!=x)

i++;

if(i>last)return

-1;

elsereturni; }template<classType> 86顺序搜索图示x=48x=50顺序搜索图示x=4887搜索成功

若搜索概率相等,则

搜索不成功数据比较n

次搜索成功

若搜索概率相等,则

88表项的插入表项的插入89template<classType>int

SeqList<Type>::Insert(

Type

&x,

int

i){//在表中第i

个位置插入新元素x

if(i<0

||

i>last+1

||

last==MaxSize-1)

return0;//插入不成功

else{

last++;

for(

intj=last;j>i;j--

)

data[j]=data[j-1];

data[i]=x;return1;//插入成功

}}template<classType>90表项的删除表项的删除91template<classType>int

SeqList<Type>::Remove(

Type

&x){//在表中删除已有元素x

int

i=Find(x); //在表中搜索x

if(i>=0

){

last--;

for(intj=i;j<=last;j++)

data[j]=data[j+1];

return1; //成功删除

} return0;//表中没有

x}template<classType>92顺序表的应用:集合的“并”运算

template<classType>void

Union(SeqList<Type>

&LA,SeqList<Type>

&LB){

intn=LA.Length();

intm=LB.Length();

for(inti=1;i<=m;i++){ Type(i);//在LB中取一元素

int(x);//在LA中搜索它

if(k==

-1)//若未找到插入它

{LA.Insert(n+1,x);n++;}}}顺序表的应用:集合的“并”运算template<clas93template<classType> voidIntersection(SeqList<Type>

&LA,SeqList<Type>

&LB){

intn=LA.Length();

intm=LB.Length();inti=1;

while(i<n){Type(i);//在LA中取一元素

int(x);//在LB中搜索它

if(k==

-1){LA.Remove(i);n--;} elsei++;//未找到在LA中删除它

}}顺序表的应用:集合的“交”运算template<classType> 顺序表的应用:94多项式(Polynomial)n阶多项式Pn(x)有n+1项。系数a0,a1,a2,…,an

指数0,1,2,…,n。按升幂排列多项式(Polynomial)n阶多项式Pn(x)有n+195classPolynomial

{public:

Polynomial();//构造函数

intoperator!

();//判是否零多项式

CoefficientCoef(Exponente);

Exponent

LeadExp();//返回最大指数

Polynomial

Add(Polynomial

poly);

Polynomial

Mult(Polynomialpoly);

floatEval(float

x); //求值}多项式(Polynomial)的抽象数据类型classPolynomial{多项式(Polynomi96#include<iostream.h>class

power{

double

x;

int

e;

double

mul;

public:

power(double

val,intexp);

double

get_power(){returnmul;}

};创建power类,计算x的幂#include<iostream.h>创建power类97power::power(double

val,int

exp){

//按val值计算乘幂

x=val;

e=exp;mul;

if(exp==0)return;

for(;

exp>0;

exp--)mul=mul*x;

}

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论