数据结构课程设计报告Word格式文档下载.docx
《数据结构课程设计报告Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《数据结构课程设计报告Word格式文档下载.docx(21页珍藏版)》请在冰豆网上搜索。
Selemtype*top;
//栈顶指针
intstacksize;
//当前分配的存储空间,以元素为单位
}SqStack;
StatusInitStack(SqStack*S);
StatusPush(SqStack*S,Selemtypee);
StatusPop(SqStack*S,Selemtypee);
StatusInitStack(SqStack*S)
{//构造一个空栈S
S->
base=(Selemtype*)malloc(STACK_INIT_SIZE*sizeof(Selemtype));
if(!
S->
base)returnOVERFLOW;
//存储分配失败
top=S->
base;
stacksize=STACK_INIT_SIZE;
returnOK;
}
StatusPush(SqStack*S,Selemtypee)
{//插入元素e为新的栈顶元素
if(S->
top-S->
base>
=S->
stacksize)
{//栈满,追加存储空间
base=(Selemtype*)realloc(S->
base,(S->
stacksize+STACKINCREMENT)*sizeof(Selemtype));
S->
base+S->
stacksize;
stacksize+=STACKINCREMENT;
}
*S->
top++=e;
returnOK;
StatusPop(SqStack*S,Selemtypee)
{//若栈不空值,则删除S的栈顶元素用e返回其值,并返回OK,否则返回ERROR
top==S->
base)returnERROR;
e=*--S->
top;
StatusStackEmpty(SqStack*S)
{//若栈S为空,则返回TRUE,否则返回FALSE
base)
returnTRUE;
else
returnFALSE;
SelemtypeGetTop(SqStack*S)
{//取栈顶元素并用e返回其值
Selemtypee;
e=*(S->
top-1);
returne;
inttransform(charstr1[],charstr2[])
{//将中缀表达式转换为后缀表达式
SqStackOp;
InitStack(&
Op);
//定义一个空栈用来存放运算符
Push(&
Op,'
#'
);
Selemtypetop;
//取得运算符栈的栈顶元素赋值给top
Selemtypee=NULL;
//出栈的栈顶元素赋值给e
inti=0,j=0,m;
chara;
m=strlen(str1);
for(i=0;
i<
m;
i++)
{
a=str1[i];
if('
0'
<
=str1[i]&
&
str1[i]<
='
9'
)//如果是操作数直接放在后缀表达式中
{
str2[j]=str1[i];
j++;
}
else
str2[j]='
'
;
j++;
switch(a)
{
case'
('
:
Push(&
Op,a);
}break;
*'
/'
top=GetTop(&
if((top=='
)||(top=='
))
{
Pop(&
Op,e);
str2[j]=top;
//比其高,现将栈顶运算符出栈,再进栈。
push(&
}
else
Push(&
}break;
+'
-'
if(top=='
||top=='
)
Pop(&
)'
Pop(&
while(top!
{
j++;
top=GetTop(&
}
top=GetTop(&
Pop(&
while(top!
{
str2[j]=top;
j++;
top=GetTop(&
str2[j]=top;
printf("
转化后的后缀表达式为:
%s\n"
str2);
Selemtypecounttem(charstr[])
{//计算后缀表达式的值
SqStackS;
S);
intlen;
len=strlen(str);
SelemtypeA[10];
intm,n,p,e=NULL,k,E;
inti,j,l,ch,zh;
//zh为字符型转化为的整形数
len;
str[i]&
str[i]<
k=i;
j=0;
while('
=str[k]&
str[k]<
)//将字符型的一串数字转化为整形
A[j]=str[k]-48;
j++;
k++;
ch=A[j-1];
E=1;
for(l=1;
l<
j;
l++)
E=10*E;
zh=A[j-l-1]*E;
ch+=zh;
Push(&
S,ch);
i=k-1;
elseif(str[i]=='
||str[i]=='
m=GetTop(&
Pop(&
S,e);
n=GetTop(&
switch(str[i])
case'
p=m+n;
break;
p=n-m;
p=n*m;
p=n/m;
S,p);
elseif(str[i]=='
result=GetTop(&
表达式的运算结果为:
%d\n"
result);
returnresult;
intmain()
请输入表达式(整数的四则运算)"
gets(string1);
//用字符串string1盛放缀表达式
transform(string1,string2);
//用字符串string2盛放后缀表达式
counttem(string2);
实验2
列车车票订票系:
1、问题描述:
能够从若干书名中提取关键词,建立词索引表。
如果要程序正常运行必须包含BookInfo.txt的记事本,记事本中的内容如下:
2、概要设计
(1)词索引表的抽象数据类型
ADTIdxTermType{
基本操作:
*InitIdxList(IdxListType*idxlist
//初始化操作,置索引表为空表,并idxlist.item[0]设一空的子串。
GetLine(FILE*f)
//从文件f中读入一个书目信息到书目缓冲区buf。
ExtractKeyWord(&
wdlist,intbno[])
//从buf中提取关键词到wdlist,书号存入bno。
InsIdxList(*idxlist,&
wdlist,intbno[])、
//将书号bno的书名关键词按词典顺序插入idxlist。
PutText(FILE*g,IdxListType*idxlist)
//将生成的索引表输入到文件g中。
}ADTIdxListType
(2)存储结构的选择
关键词索引表中的每一项包括两个部分内容,其一是关键词,采用顺序存储结构;
其二是书号索引,由于不同的关键词对应的书号数量变化很大,而且是动态生成,因此宜采用链式存储结构。
ADTSeqList{
基本操作:
template<
classType>
classSeqList{
public:
char*item[MaxKeyNum];
//字符串的数组
intlast;
//词表长度
SeqListwdlist(int);
};
//词表类型(顺序表)
如上所说的各类的关系图如下:
(3)其它函数
1)主函数main().
SeqList<
classString>
wdlist;
IdxListType*idxlist=NULL;
FILE*f,*g;
intBookNo[5];
if((f=fopen("
BookInfo.txt"
"
r"
))==NULL)
{cout<
"
ERROR--2"
return0;
if((g=fopen("
BookIdx.txt"
"
w"
cout<
ERROR--3!
idxlist=InitIdxList(idxlist);
//初始化索引表idxlist为空表
while(!
feof(f))
GetLine(f);
//从文件f中读一个书目信息到buf
ExtractKeyWord(wdlist,BookNo);
//从buf中提取数目关键词到词表,书号存入BookNo
InsIdxList(idxlist,wdlist,BookNo);
//将书号为BookNo的关键词插入索引表
cout<
\n"
b<
PutText(g,idxlist);
//将生成的索引表idxlist输入到文件f中
}
3、调试
程序名为suoyinbiao.exe,运行环境为DOS。
程序执行后显示:
3、源代码
iostream.h>
cstdlib>
cstring>
cctype>
fstream>
#defineMaxBookNum1000//假设只对1000本书建索引表
#defineMaxKeyNum2500//索引表的最大容量
#defineMaxLineLen500//书目串的最大长度
#defineMaxWordNum10//词表的最大容量
template<
char*item[MaxKeyNum];
//字符串数组
intlast;
//词表的长度
//索引表类型(顺序表)
TypedefstructLNode{//存放书号的链表
intdata[3];
structLNode*next;
}LNode,*LinkList;
classString{//存放关键词的串
char*ch;
intlength;
classIdxTermType{
String*key;
LinkListbnolist;
//索引项类型
classIdxListType{
IdxTermTypeitem[MaxKeyNum+1];
//索引表类型(有序表)
char*buf;
//书目缓冲区
inti;
//无重复的关键字个数
intb;
//保留i的前一次变换的值
intv;
char*com[10]={"
and"
a"
an"
the"
to"
of"
IdxListType*InitIdxList(IdxListType*idxlist)
//初始化操作,置索引表为空表
//idxlist=(IdxListType*)malloc(sizeof(IdxListType));
idxlist=newIdxListType;
if(idxlist==NULL)
ERROR--1!
exit(-1);
idxlist->
item[0].key=newString;
item[0].bnolist=newLNode;
item[0].key->
ch=NULL;
length=0;
item[0].bnolist->
next=NULL;
last=0;
returnidxlist;
voidGetLine(FILE*f)
//从文件f中读入一个书目信息到书目缓冲区buf
intj=0;
staticchara[100][100];
charc;
c=fgetc(f);
while(((a[v][j++]=tolower(c))!
='
\n'
)&
(c!
=EOF))
c=fgetc(f);
a[v][j-1]='
\0'
buf=a[v++];
cout<
buf;
intExtractKeyWord(SeqList<
String>
//从buf中提取书目关键词到词表wdlist,书号存入bno
intk,s,t=0,j=0;
externintb=i;
char*p=buf,*q,*c[10];
while(!
isalnum(*p))
p++;
while(isdigit(*p)||!
isalpha(*p))
bno[j]=*p-'
j--;
q=p;
while(*q)
while((*q!
*q&
(*q!
q++;
*(q-1)='
c[t++]=p;
p=q;
for(k=0;
k<
t;
++k)
for(s=0;
s<
8;
++s)
if(strcmp(c[k],com[s])==0)
if(s==8)
wdlist.item[i++]=c[k];
wdlist.last=i;
词表长度:
wdlist.last;
书号:
j;
cout<
bno[k]<
endl;
for(k=b;
i;
++k)
关键词"
wdlist.item[k]<
return0;
intInsIdxList(IdxListType*idxlist,SeqList<
wdlist,intbno[])
//将书号bno的书名关键词按词典顺序插入索引表idxlist
intk,t,s,j=0,c=b,d;
LinkListp,q;
if(i>
=MaxKeyNum+1)
超过索引表最大容量,请调整!
exit(-1);
for(t=idxlist->
last;
j<
i-b;
j++,c++)
if(0==t)//第一个关键词插入
ch=wdlist.item[c];
for(d=0;
d<
3;
++d)
data[d]=bno[d];
//书号存储
//链表尾部指向空
last++;
t++;
for(k=0;
k++)
if((s=strcmp(wdlist.item[c],idxlist->
item[k].key->
ch))==0)/插入的关键词已存在
p=newLNode;
//申请个新结点,保存书号
\n插入的书号:
++d)
p->
p->
data[d];
q=idxlist->
item[k].bnolist;
fprintf(stdout,"
%d"
q->
data[d]);
while(q->
next!
=NULL)
q=q->
next;
q->
next=p;
\n关键词%-20s\t\n"
idxlist->
ch);
q=idxlist->
while(q)
if(q->
elseif(s>
0)
if(k==t-1)//插入的关键词大于最后一个已存在的关键词,则插到最后
item[t].key=newString;
item[t].bnolist=newLNode;
item[t].key->
item[t].bnolist->
next=NULL;
for(s=t-1;
s>
=k;
--s)
{//插入的关键字小于当前已有的关键字,则把当前的关键字向后移
item[s+1].key->
ch=idxlist->
item[s].key->
ch;
item