线性表的基本运算及多项式的算术计算.docx

上传人:b****5 文档编号:28838718 上传时间:2023-07-20 格式:DOCX 页数:43 大小:263.55KB
下载 相关 举报
线性表的基本运算及多项式的算术计算.docx_第1页
第1页 / 共43页
线性表的基本运算及多项式的算术计算.docx_第2页
第2页 / 共43页
线性表的基本运算及多项式的算术计算.docx_第3页
第3页 / 共43页
线性表的基本运算及多项式的算术计算.docx_第4页
第4页 / 共43页
线性表的基本运算及多项式的算术计算.docx_第5页
第5页 / 共43页
点击查看更多>>
下载资源
资源描述

线性表的基本运算及多项式的算术计算.docx

《线性表的基本运算及多项式的算术计算.docx》由会员分享,可在线阅读,更多相关《线性表的基本运算及多项式的算术计算.docx(43页珍藏版)》请在冰豆网上搜索。

线性表的基本运算及多项式的算术计算.docx

线性表的基本运算及多项式的算术计算

实验报告

( / 学年第一学期)

课程名称

数据结构A

实验名称

线性表的基本运算及多项式的算术计算

实验时间

 

 

 

指导单位

 

指导教师

 

 

 

 

 

 

 

 

学生姓名

 

班级学号

 

学院(系)

 

专业

 

 

 

 

 

 

实验报告

实验名称

线性表的基本运算及多项式的算术计算

指导教师

 

实验类型

上机

实验学时

2

实验时间

 

一、实验目的和要求

实验目的:

1、深入理解线性表数据结构,掌握线性表的顺序和链接两种存储方式。

2、熟练掌握顺序表的各种基本操作。

3、学会使用线性表解决应用问题的方法。

4、加深对抽象类模板、类的继承、代码重用等C++知识的理解和使用。

内容和要求:

1、在顺序表类中增加成员函数voidReverse(),实现顺序表逆置。

2、在顺序表类中增加成员函数boolDeleteX(constT&x),删除表中所有元素值等于x的元素。

若存在则删除之且返回true,否则返回false。

3、编写main函数,调用上述新增函数。

4、设计带表头结点的单链表表示的多项式类,实现各个运算。

5、增加成员函数voidPolyMul(Polynominal&r),并重载*运算符。

6、编写main函数,测试多项式类的各个运算。

二、实验环境(实验设备)

硬件:

PC

软件:

Code:

:

Blocks(C++)

三、实验原理及内容

1、线性表的基本运算

(1)核心算法及流程图

顺序表逆置:

思路:

将顺序表中第j个结点中的元素与第(n-1-j)个结点中的元素替换,从第一个结点开始,共进行(n/2取整)次。

代码:

template

voidSeqList:

:

Reverse()

{

intm=n/2;

for(intj=0;j

{

Ttemp=elements[j];

elements[j]=elements[n-1-j];

elements[n-1-j]=temp;

}

}

流程图:

删除表中所有元素值等于x的元素:

思路:

遍历顺序表,没搜索到一次x,就将其后所有结点前移,考虑到x连续存在的情况,将所有结点前移之后,i自减,再循环进行。

代码:

template

boolSeqList:

:

DeleteX(constT&x)

{

intflag=n;

for(inti=0;i

{

if(elements[i]==x)

{

for(intj=i+1;j

{

elements[j-1]=elements[j];

}

i--;

n--;

}

}

if(flag

{

returntrue;

}

else

{

returnfalse;

}

}

流程图:

(2)完整代码:

#include

usingnamespacestd;

template

classSeqList

{

public:

SeqList(intmSize);

~SeqList(){delete[]elements;}

boolIsEmpty()const;

intLength()const;

boolFind(inti,T&x)const;

intSearch(Tx)const;

boolInsert(inti,Tx);

boolDelete(inti);

boolUpdate(inti,Tx);

voidOutput(ostream&out)const;

voidReverse();

boolDeleteX(constT&x);

private:

intmaxLength;

T*elements;

intn;

};

template

SeqList:

:

SeqList(intmSize)

{

maxLength=mSize;

elements=newT[maxLength];

n=0;

}

template

boolSeqList:

:

IsEmpty()const

{

returnn==0;

}

template

intSeqList:

:

Length()const

{

returnn;

}

template

boolSeqList:

:

Find(inti,T&x)const

{

if(i<0||i>n-1)

{

cout<<"OutofBounds"<

returnfalse;

}

x=elements[i];

returntrue;

}

template

intSeqList:

:

Search(Tx)const

{

for(intj=0;j

{

if(elements[j]==x)

{

returnj;

}

}

return-1;

}

template

boolSeqList:

:

Insert(inti,Tx)

{

if(i<-1||i>n-1)

{

cout<<"OutofBounds"<

returnfalse;

}

if(n==maxLength)

{

cout<<"OverFlow"<

returnfalse;

}

for(intj=n-1;j>i;j--)

{

elements[j+1]=elements[j];

}

elements[i+1]=x;

n++;

returntrue;

}

template

boolSeqList:

:

Delete(inti)

{

if(!

n)

{

cout<<"UnderFlow"<

returnfalse;

}

if(i<0||i>n-1)

{

cout<<"OutofBounds"<

returnfalse;

}

for(intj=i+1;j

{

elements[j-1]=elements[j];

}

n--;

returntrue;

}

template

boolSeqList:

:

Update(inti,Tx)

{

if(i<0||i>n-1)

{

cout<<"OutofBounds"<

returnfalse;

}

elements[i]=x;

returntrue;

}

template

voidSeqList:

:

Output(ostream&out)const

{

for(inti=0;i

{

out<

}

out<

}

template

voidSeqList:

:

Reverse()

{

intm=n/2;

for(intj=0;j

{

Ttemp=elements[j];

elements[j]=elements[n-1-j];

elements[n-1-j]=temp;

}

}

template

boolSeqList:

:

DeleteX(constT&x)

{

intflag=n;

for(inti=0;i

{

if(elements[i]==x)

{

for(intj=i+1;j

{

elements[j-1]=elements[j];

}

i--;

n--;

}

}

if(flag

{

returntrue;

}

else

{

returnfalse;

}

}

intmain()

{

intn,m,k;

cout<<"请输入元素个数:

";

cin>>n;

SeqLista(n);

cout<<"请输入各个元素:

";

for(inti=0;i

{

cin>>m;

a.Insert(i-1,m);

}

cout<<"请输入您想删除的元素:

";

cin>>k;

cout<

";

a.Output(cout);

a.Reverse();

cout<<"逆置后:

";

a.Output(cout);

a.DeleteX(k);

cout<<"删除后:

";

a.Output(cout);

return0;

}

(3)测试用例和结果:

输入顺序表长度为10:

输入十个数分别为1355684259:

输入要删除的元素5:

 

 

 

 

 

 

2、多项式的算术运算

(1)核心算法及流程图

设计带表头的单链表表示的多项式类,实现书本上的各个运算:

思路:

在书本上的带表头的循环单链表的代码基础上进行一些修改。

代码的修改部分已在“完整代码”中注释了出来

增加成员函数voidPolyMul(Polynominal&r),并重载*运算符

思路:

将乘数多项式的每一项分别与被乘数多项式的所有项分别相乘(系数相乘,指数相加),得到中间多项式,再调用多项式相加的函数将这些中间多项式全部加起来形成结果多项式。

代码:

voidPolynominal:

:

PolyMul(Polynominal&r)

{

Polynominalresult;

Term*res=result.theList;

res=res->InsertAfter(0,0);

Term*q,*p;

for(q=theList->link;q;q=q->link)

{

Polynominaltemp;

Term*s=temp.theList;

for(p=r.theList->link;p;p=p->link)

{

s=s->InsertAfter(q->coef*p->coef,q->exp+p->exp);

}

result.PolyAdd(temp);

}

q=theList->link;

while(q)

{

theList->link=q->link;

deleteq;

q=theList->link;

}

q=theList;

for(res=result.theList->link;res;res=res->link)

{

q=q->InsertAfter(res->coef,res->exp);

}

}

Polynominal&operator*(Polynominal&a,Polynominal&b)

{

a.PolyMul(b);returna;

}

 

实验报告

流程图:

(2)完整代码:

#include

usingnamespacestd;

classTerm

{

public:

Term(intc,inte);

Term(intc,inte,Term*nxt);

Term*InsertAfter(intc,inte);

private:

intcoef;

intexp;

Term*link;

friendostream&operator<<(ostream&,constTerm&);

friendclassPolynominal;

};

Term:

:

Term(intc,inte)

{

coef=c;

exp=e;

link=0;

}

Term:

:

Term(intc,inte,Term*nxt)

{

coef=c;

exp=e;

link=nxt;

}

Term*Term:

:

InsertAfter(intc,inte)

{

link=newTerm(c,e,link);

returnlink;

}

ostream&operator<<(ostream&out,constTerm&val)

{

if(val.coef==0)

{

returnout;

}

out<

switch(val.exp)

{

case0:

break;

case1:

out<<"X";break;

default:

out<<"X"<

}

returnout;

}

classPolynominal

{

public:

Polynominal();

~Polynominal();

voidAddTerms(istream&in);

voidOutput(ostream&out)const;

voidPolyAdd(Polynominal&r);

voidPolyMul(Polynominal&r);

private:

Term*theList;

friendostream&operator<<(ostream&,constPolynominal&);

friendistream&operator>>(istream&,Polynominal&);

friendPolynominal&operator+(Polynominal&,Polynominal&);

};

Polynominal:

:

Polynominal()

{

theList=newTerm(0,-1);

theList->link=NULL;//修改部分

}

Polynominal:

:

~Polynominal()

{

Term*p=theList->link;

while(p)//修改部分

{

theList->link=p->link;

deletep;

p=theList->link;

}

deletetheList;

}

voidPolynominal:

:

AddTerms(istream&in)

{

Term*q=theList;

intc,e;

for(;;)

{

cout<<"Inputaterm(coef,exp):

\n"<

cin>>c>>e;

if(e<0)break;

q=q->InsertAfter(c,e);

}

}

voidPolynominal:

:

Output(ostream&out)const

{

intfirst=1;

Term*p=theList->link;

cout<<"Thepolynominalis:

\n"<

for(;p;p=p->link)//修改部分

{

if(!

first&&(p->coef>0))

{

out<<"+";

}

first=0;

out<<*p;

}

cout<<"\n"<

}

voidPolynominal:

:

PolyAdd(Polynominal&r)

{

Term*q,*q1=theList,*p;

p=r.theList->link;

q=q1->link;

while(p&&p->exp>=0)//修改部分

{

while(p->expexp)

{

q1=q;

q=q->link;

}

if(p->exp==q->exp)

{

q->coef=q->coef+p->coef;

if(q->coef==0)

{

q1->link=q->link;

delete(q);

q=q1->link;

}

else

{

q1=q;

q=q->link;

}

}

else

{

q1=q1->InsertAfter(p->coef,p->exp);

}

p=p->link;

}

}

voidPolynominal:

:

PolyMul(Polynominal&r)

{

Polynominalresult;

Term*res=result.theList;

res=res->InsertAfter(0,0);

Term*q,*p;

for(q=theList->link;q;q=q->link)

{

Polynominaltemp;

Term*s=temp.theList;

for(p=r.theList->link;p;p=p->link)

{

s=s->InsertAfter(q->coef*p->coef,q->exp+p->exp);

}

result.PolyAdd(temp);

}

q=theList->link;

while(q)

{

theList->link=q->link;

deleteq;

q=theList->link;

}

q=theList;

for(res=result.theList->link;res;res=res->link)

{

q=q->InsertAfter(res->coef,res->exp);

}

}

ostream&operator<<(ostream&out,constPolynominal&x)

{

x.Output(out);returnout;

}

istream&operator>>(istream&in,Polynominal&x)

{

x.AddTerms(in);returnin;

}

Polynominal&operator+(Polynominal&a,Polynominal&b)

{

a.PolyAdd(b);returna;

}

Polynominal&operator*(Polynominal&a,Polynominal&b)

{

a.PolyMul(b);returna;

}

intmain()

{

Polynominalp,q;

inta;

cout<<"1、多项式相加2、多项式相乘请选择功能:

";

cin>>a;

switch(a)

{

case1:

{

cin>>p;

cout<

cin>>q;

cout<

q=q+p;

cout<

break;

}

case2:

{

cin>>p;

cout<

cin>>q;

cout<

q=q*p;

cout<

break;

}

}

return0;

}

(3)测试用例及结果:

选择多项式相加功能:

输入5x^5+4x^3:

输入4x^5+2x^3,并求出多项式的和:

选择多项式相乘的功能:

输入4x^3+3x^2:

输入5x^5+6x^3+2x^2,输出多项式的乘积:

 

四、实验小结(包括问题和解决方法、心得体会、意见与建议等)

这次实验可以被分为两个问题,线性表的基本运算和多项式的算术运算。

主要是要求编写出三个函数,其中,顺序表的逆置非常简单,删除顺序表中所有的x这一函数只要注意到x可能连续存在的情况也就没什么问题了。

在编写多项式乘法的函数时,由于最初听课时没有将多项式运算那一部分作为重点去学习,在对带表头的非循环链表的知识使用时总是出错,但是我在静下心来仔细阅读理解从头到尾的完整代码之后,终于理清了这些知识的关系,并成功地编写出了多项式相乘的代码。

这次实验让我明白了学过的知识要经常复习,遇到问题不能急躁,要静下心来解决,也让我更加深入地理解了线性表数据结构,更加清楚了线性表的顺序和链接两种存储的方式,体会到了线性表功能的强大,加深了对重载等C++语言的理解和使用。

以后我将更加认真学习数据结构,并且更多地将数据结构的知识运用起来。

五、指导教师评语

成绩

 

批阅人

 

日期

 

 

 

 

 

 

 

 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 中国风

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1