实验报告1线性表及表达式.docx

上传人:b****7 文档编号:24009489 上传时间:2023-05-23 格式:DOCX 页数:23 大小:57.61KB
下载 相关 举报
实验报告1线性表及表达式.docx_第1页
第1页 / 共23页
实验报告1线性表及表达式.docx_第2页
第2页 / 共23页
实验报告1线性表及表达式.docx_第3页
第3页 / 共23页
实验报告1线性表及表达式.docx_第4页
第4页 / 共23页
实验报告1线性表及表达式.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

实验报告1线性表及表达式.docx

《实验报告1线性表及表达式.docx》由会员分享,可在线阅读,更多相关《实验报告1线性表及表达式.docx(23页珍藏版)》请在冰豆网上搜索。

实验报告1线性表及表达式.docx

实验报告1线性表及表达式

 

实验报告

(学年第学期)

 

课程名称

数据结构A

实验名称

实验一线性表的基本运算及多项式的算术运算

实验时间

指导单位

指导教师

 

学生姓名

班级学号

学院(系)

专业

 

实验报告

实验名称

实验一线性表的基本运算及多项式的算术运算

指导教师

实验类型

验证

实验学时

实验时间

一、实验目的和要求

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

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

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

(4)加深对抽象模板类。

类的继承、代码重用、重载等C++语言机制的理解和使用。

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

硬件:

微型计算机

软件:

MicrosoftVisualC++6.0

三、实验原理及内容

实验题一:

线性表操作

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

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

若表中存在这样的元素则删除之,且函数返回true;否则函数返回false。

(3)编写main函数,调用上述函数测试其功能。

源代码:

 

实验报告

#include

template

classLinearList

{

public:

virtualboolIsEmpty()const=0;

virtualintLength()const=0;

virtualboolFind(inti,T&X)const=0;

virtualintSearch(Tx)const=0;

virtualboolInsert(inti,Tx)=0;

virtualboolDelete(inti)=0;

virtualboolUpdate(inti,Tx)=0;

virtualvoidOutput(ostream&out)const=0;

virtualvoidReverse()=0;

virtualboolDeleteX(constT&x)=0;

protected:

intn;

};

template

classSeqlist:

publicLinearList

{

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;

};

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"<

}

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"<

}

if(n==maxLength)

{

cout<<"OverFlow"<

}

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"<

}

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

{

cout<<"OutofBounds"<

}

实验报告

for(intj=i+1;j

n--;returntrue;

}

template

boolSeqlist:

:

Update(inti,Tx)

{

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

{

cout<<"OutofBounds"<

}

elements[i]=x;

returntrue;

}

template

voidSeqlist:

:

Output(ostream&out)const

{

for(inti=0;i

out<

}

template//对线性表进行逆置

voidSeqlist:

:

Reverse()

{

Tt;

for(inti=0;i

{

t=elements[i];

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

elements[n-i-1]==t;

}

}

template

boolSeqlist:

:

DeleteX(constT&x)

实验报告

{

if(Search(x)==-1)

returnfalse;

for(inti=0;i

{

if(elements[i]==x)

{

Delete(i);

i--;(这样才能保证删除所有相同的元素)

}

}

returntrue;

}

template

voidUnion(Seqlist&LA,SeqlistLB)

{

Tx;

for(inti=0;i

{

LB.Find(i,x);

if(LA.Search(x)==-1)

LA.Insert(LA.Length()-1,x);

}

}

constintSIZE=20;

voidmain()//主函数用于测试对线性表的各种操作

{

SeqlistLA(SIZE);

//SeqlistLB(SIZE);

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

LA.Insert(i-1,i);

LA.Output(cout);

实验报告

LA.Insert(2,2);

LA.Insert(2,2);

LA.Insert(2,2);

LA.Output(cout);

LA.DeleteX

(2);

LA.Output(cout);

LA.Reverse();

//for(i=3;i<8;i++)LB.Insert(i-4,i);

//LB.Insert(-1,0);

//LB.Insert(3,2);

//LB.Insert(LB.Length()-1,4);

//Union(LA,LB);

LA.Output(cout);

}

测试结果

实验报告

实验题二:

一元多项式的相加和相乘

(1)设计带表头结点的单链表表示的多项式类,结点类单独定义;

(2)在多项式类中定义和实现:

1多项式的逐项输入,再重载输入流cin;

2多项式的相加运算,再重载加法运算符:

operator+;

3多项式的相乘运算,增加成员函数voidPolyMul(Polynominal&r),再重载乘法运算符:

operator*;

4多项式的输出,再重载输出流cout。

(3)实现菜单驱动的main函数,测试多项式加法和乘法运算,要求能:

①定义多项式对象,并调用cin建立一个多项式;

②调用cout打印(显示)一个多项式;

③实现两个多项式相加,将相加后的结果输出;

④实现两个多项式相乘,将相乘后的结果输出

1.类的层次结构:

线性表的基本运算程序中包括三个文件,分别为:

linearlist.h,seqlist.h,LinearListMain.cpp。

其中顺序表类seqlist.h中,私有段封装了两个私有数据成员maxLenght和elements,同时继承了LinearList类中的n,分别存储表中元素最多个数,元素和元素的实际个数。

多项式的加法和乘法算术运算程序中包括了三个文件,分别为Polynominal.h,Term.h,main.cpp。

其中项结点类Team中定义了三个私有数据域,即系数coef、指数exp和指向下一个项结点的指针域link并且polynominal被声明成了项结点类Team的友元。

 

实验报告

多项式加法

 

多项式乘法

实验报告

2.实验原理:

以线性表来描述一元多项式,存储结构采用单链表,每个结点存储的多项式中某一项的系数和指数,建立单链表时指数高的结点列于指数低的结点之后,即线性表的元素按指数递增有序排列。

3.思想算法:

以线性表来描述一元多项式,存储结构采用单链表,每个结点存储的多项式中某一项的系数和指数,建立单链表时指数高的结点列于指数低的结点之后,即线性表的元素按指数递增有序排列。

4.算法分析:

线性表的基本运算程序的主要算法的算法时间复杂度和空间复杂度为O(n),多项式的加法和乘法算术运算程序的主要算法的时间复杂程度和和空间复杂程度为O(n)。

 

实验报告

5.源代码

a.多项式基本运算

template

classSeqList:

publicLinearList

{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;

private:

intmaxLength;

T*elements;

};

SeqListLObj(SIZE);

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<<"OutOfBounts"<

}

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"<

}

if(n==maxLength){

cout<<"OverFlow"<

}

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"<

}

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

cout<<"OutOfBounds"<

}

for(intj=i+1;j

n--;returntrue;

}

template

boolSeqList:

:

Update(inti,Tx)

{

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

cout<<"OutOfBounds"<

}

elements[i]=x;

returntrue;

}

template

voidSeqList:

:

Output(ostream&out)const

{

for(inti=0;i

out<

}

B.多项式的加法和乘法

#include

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);

voidPolyMulti(Polynominal&r);

private:

Term*theList;

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

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

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

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

};

Polynominal:

:

Polynominal()

{

theList=newTerm(0,-1);

theList->link=theList;

}

Polynominal:

:

~Polynominal()

{Term*p=theList->link;

while(p!

=theList){

theList->link=p->link;

deletep;

p=theList->link;

}

deletetheList;

}

实验报告

voidPolynominal:

:

AddTerms(istream&in)

{Term*q=theList;

intc,e;

for(;;)

{cout<<"Inputaterm(coef,exp):

\n"<

in>>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!

=theList;p=p->link)

{if(!

first&&(p->coef>0))out<<"+";

first=0;

out<<*p;

}

cout<<"\n"<

}

voidPolynominal:

:

PolyAdd(Polynominal&r)

{Term*q,*q1=theList,*p;//q1指向表头结点

p=r.theList->link;//p指向第一个要处理的结点

q=q1->link;//q1是q的前驱,p和q就指向两个当前进行比较的项

while(p->exp>=0)//对r的单循环链表遍历,直到全部结点处理完

{while(p->expexp)//t跳过q->exp大的项

{q1=q;q=q->link;

}

if(p->exp==q->exp)//当指数相等时,系数相加

实验报告

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

if(q->coef==0)//若相加后系数为0,则删除q

{q1->link=q->link;delete(q);

q=q1->link;//重置q指针

}

else

{q1=q;q=q->link;}//若相加后系数不为0,则移动q1和q

}

Else//p->exp>q->exp的情况

q1=q1->InsertAfter(p->coef,p->exp);//以p的系数和指数生产新结点,插入q1后

p=p->link;

}

}

voidPolynominal:

:

PolyMulti(Polynominal&r)

{

Term*q,*q1=theList,*p;//q1指向表头结点

q=q1->link;//q1是q的前驱,p和q就指向两个当前进行比较的项

while(q->exp>=0)//对r的单循环链表遍历,直到全部结点处理完

{

p=r.theList->link;//p指向第一个要处理的结点

while(p->exp>=0)

{

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

p=p->link;

}

q1->link=q->link;

delete(q);

q=q1->link;

}

}

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

实验报告

{x.Output(out);

returnout;

}

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

{x.AddTerms(in);

returnin;

}

Polynominal&operator+(Polynomin

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

当前位置:首页 > 经管营销 > 经济市场

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

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