南邮数据结构实验一.docx

上传人:b****4 文档编号:11563637 上传时间:2023-03-19 格式:DOCX 页数:17 大小:69.47KB
下载 相关 举报
南邮数据结构实验一.docx_第1页
第1页 / 共17页
南邮数据结构实验一.docx_第2页
第2页 / 共17页
南邮数据结构实验一.docx_第3页
第3页 / 共17页
南邮数据结构实验一.docx_第4页
第4页 / 共17页
南邮数据结构实验一.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

南邮数据结构实验一.docx

《南邮数据结构实验一.docx》由会员分享,可在线阅读,更多相关《南邮数据结构实验一.docx(17页珍藏版)》请在冰豆网上搜索。

南邮数据结构实验一.docx

南邮数据结构实验一

 

实验报告

(2014/2015学年第二学期)

 

课程名称

数据结构

实验名称

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

实验时间

2015

9

28

指导单位

计算机科学与技术系

指导教师

黄海平

 

学生姓名

陈明阳

班级学号

Q14010119

学院(系)

贝尔英才

专业

信息科技强化班

实验报告

实验名称

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

指导教师

黄海平

实验类型

验证

实验学时

4

实验时间

9.28

一、实验目的和要求

内容:

实现顺序表和单链表的基本运算,多项式的加法和乘法算术运算。

要求:

能够正确演示线性表的查找、插入、删除运算。

实现多项式的加法和乘法运算操作。

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

VSUALSTUDIO2015

三、实验原理及内容

LinearlistseqlistLA,LB

函数调用数据类型如下图

源码:

Linearlist.h:

#include

usingnamespacestd;

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;

protected:

intn;

};

 

Seqlist.h:

#include"linearlist.h"

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;

};

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

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(i<0||i>n-1)

{

cout<<"outofbounds"<

returnfalse;

}

if(!

n)

{

cout<<"overflow"<

returnfalse;

}

for(intj=i+1;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<

}

源.cpp:

#include"seqlist.h"

constintSIZE=20;

voidmain()

{

SeqListLA(SIZE);

inti=0;

for(i=0;i<5;i++)LA.Insert(i-1,i);

LA.Insert(-1,10);

LA.Output(cout);

}

实现在线性表LA中插入0-4然后在一开始插入10

运行截图如下:

多项式实验:

定义类如下

重构函数如下:

源码:

#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(0==val.coef)

returnout;

if(1!

=val.coef)

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

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

};

Polynominal:

:

Polynominal()

{

theList=newTerm(0,-1);//头结点

theList->link=NULL;//单链表尾结点指针域为空

}

Polynominal:

:

~Polynominal()

{

Term*p=theList->link;

while(p!

=NULL)

{

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;

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

if(0>=e)break;

}

}

voidPolynominal:

:

Output(ostream&out)const

{

intfirst=1;

Term*p=theList->link;

for(;p!

=NULL&&p->exp>=0;p=p->link)

{

if(!

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

first=0;

out<<*p;

}

cout<

}

voidPolynominal:

:

PolyAdd(Polynominal&r)

{

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

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

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

while(p!

=NULL&&p->exp>=0)//对r的单循环链表遍历,知道全部结点都处理完

{

while(p->expexp)//跳过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;//若相加后系数不为0,则移动q1和q

q=q->link;

}

}

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

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

p=p->link;

}

}

voidPolynominal:

:

PolyMul(Polynominal&r)

{

Polynominalresult;//定义相乘后的数据

Term*n=result.theList;//n指向result的头结点

n=n->InsertAfter(0,0);//在result的头结点后插入新结点,系数指数均为0

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

while(p->exp>=0)//对r的单循环链表遍历

{

Polynominaltmp;//存储某段相乘后的数据

Term*m=tmp.theList;//m指向tmp的头结点

Term*q=theList->link;//q指向表头结点的后继结点

while(q->exp>=0)//对当前对象的单循环环链表遍历

{

m=m->InsertAfter((p->coef)*(q->coef),(p->exp)+(q->exp));//生成新结点插入n后

q=q->link;

}

result.PolyAdd(tmp);//将temp加到result上

p=p->link;

}

Term*q=theList->link;//q指向表头结点的后继结点

while(q!

=NULL)//删除原对象的所有数据

{

theList->link=q->link;

deleteq;

q=theList->link;

}

q=theList;

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

PolyAdd(result);//将result加到当前对象上

}

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

{

intchoose;

cout<<"choosecalculatetype1.add2.mul"<

cin>>choose;

Polynominalp,q;

switch(choose)

{

case1:

cin>>p;cout<

cin>>q;cout<

q=q+p;

break;

case2:

cin>>p;cout<

cin>>q;cout<

q=q*p;

break;

}

cout<

return0;

}

实现多项式相加或相乘

运行截图:

实验报告

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

这两个实验的重难点都在于正确灵活使用,大部分代码在书上都有提供,真正的操作重点在于理解这些代码的意义和使用方法。

第二个实验用到了运算符的重载,对于编程能力有不少挑战。

目前第二个代码仍然有几个问题,比如不能加法乘法混用,计算后第一个多项式的内容会被覆盖等,下一步改进方向是实现加法乘法混用,并且将结果传递给第三变量,增加代码实用性。

五、指导教师评语

成绩

批阅人

日期

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

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

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

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