数据结构课程设计报告.docx
- 文档编号:7428684
- 上传时间:2023-01-23
- 格式:DOCX
- 页数:21
- 大小:123.05KB
数据结构课程设计报告.docx
《数据结构课程设计报告.docx》由会员分享,可在线阅读,更多相关《数据结构课程设计报告.docx(21页珍藏版)》请在冰豆网上搜索。
数据结构课程设计报告
数据结构
课程设计报告
信科院软件4班
学号:
2010050704##
姓名:
**
实验一
将中缀表达式转换为后缀表达式并运算
1、问题描述
从键盘输入一个中缀表达式,操作数为实数,支持+、-、*、/运算,操作数和运算符之间用空格分开。
将输入的中缀表达式转换为后缀式,并输出后缀式。
例:
5+(6-8/9)*4
2、实验目的
熟悉各种数据结构,掌握在visualc++6.0环境下的一些算法实现。
学会在实际问题中使用数据结构。
3、算法描述及实验步骤
根据本实验内容的要求,实验要实现的是将一个中缀表达式转换为后缀表达式并运算。
通过设计一个算法来实现。
4、调试过程
运行visualc++6.0。
打开事先编写好的程序。
在visualc++6.0环境下编译、运行,改正错误,直至能够正确运行为止。
5、实验结果
运行后
输入一个中缀表达式后的结果
6、源代码
#include
#include
#include
#defineSTACK_INIT_SIZE100
#defineSTACKINCREMENT10
#defineOK1
#defineOVERFLOW-2
#defineERROR0
#defineTRUE1
#defineFALSE0
typedefintSelemtype;
typedefintStatus;
#defineMAX50
charstring1[MAX];//定义两个字符串分别存放中缀表达式和后缀表达式
charstring2[MAX];
intresult;
typedefstruct
{
Selemtype*base;//在构造之前和销毁之后,base的值为NULL
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;//存储分配失败
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
returnOK;
}
StatusPush(SqStack*S,Selemtypee)
{//插入元素e为新的栈顶元素
if(S->top-S->base>=S->stacksize)
{//栈满,追加存储空间
S->base=(Selemtype*)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(Selemtype));
if(!
S->base)returnOVERFLOW;//存储分配失败
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*S->top++=e;
returnOK;
}
StatusPop(SqStack*S,Selemtypee)
{//若栈不空值,则删除S的栈顶元素用e返回其值,并返回OK,否则返回ERROR
if(S->top==S->base)returnERROR;
e=*--S->top;
returnOK;
}
StatusStackEmpty(SqStack*S)
{//若栈S为空,则返回TRUE,否则返回FALSE
if(S->top==S->base)
returnTRUE;
else
returnFALSE;
}
SelemtypeGetTop(SqStack*S)
{//取栈顶元素并用e返回其值
Selemtypee;
if(S->top==S->base)returnERROR;
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 { 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; case'*': case'/': top=GetTop(&Op); if((top=='*')||(top=='/')) { Pop(&Op,e); str2[j]=top;j++;//比其高,现将栈顶运算符出栈,再进栈。 push(&Op,a); } else { Push(&Op,a); }break; case'+': case'-': top=GetTop(&Op); if(top=='+'||top=='-'||top=='*'||top=='/') { Pop(&Op,e); str2[j]=top;j++; Push(&Op,a); } else { Push(&Op,a); }break; case')': { top=GetTop(&Op); Pop(&Op,e); while(top! ='(') { str2[j]=top; j++; top=GetTop(&Op); Pop(&Op,e); } }break; } } } top=GetTop(&Op); Pop(&Op,e); while(top! ='#') { str2[j]=top; j++; top=GetTop(&Op); Pop(&Op,e); } str2[j]=top; printf("转化后的后缀表达式为: %s\n",str2); } Selemtypecounttem(charstr[]) {//计算后缀表达式的值 SqStackS; InitStack(&S); intlen; len=strlen(str); SelemtypeA[10]; intm,n,p,e=NULL,k,E; inti,j,l,ch,zh;//zh为字符型转化为的整形数 for(i=0;i { if('0' { k=i;j=0; while('0'<=str[k]&&str[k]<='9')//将字符型的一串数字转化为整形 { A[j]=str[k]-48; j++; k++; } ch=A[j-1]; E=1; for(l=1;l { E=10*E; zh=A[j-l-1]*E; ch+=zh; } Push(&S,ch); i=k-1; } elseif(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/') { m=GetTop(&S); Pop(&S,e); n=GetTop(&S); Pop(&S,e); switch(str[i]) { case'+': p=m+n;break; case'-': p=n-m;break; case'*': p=n*m;break; case'/': p=n/m;break; } Push(&S,p); } elseif(str[i]=='') { } elseif(str[i]=='#') { result=GetTop(&S); } } printf("表达式的运算结果为: %d\n",result); returnresult; } intmain() { printf("请输入表达式(整数的四则运算)"); 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 public: char*item[MaxKeyNum];//字符串的数组 intlast;//词表长度 SeqListwdlist(int); };//词表类型(顺序表) 如上所说的各类的关系图如下: (3)其它函数 1)主函数main(). intmain() { SeqList IdxListType*idxlist=NULL; FILE*f,*g; intBookNo[5]; if((f=fopen("BookInfo.txt","r"))==NULL) {cout<<"ERROR--2"; return0; } else { if((g=fopen("BookIdx.txt","w"))==NULL) { cout<<"ERROR--3! "; return0; } else { idxlist=InitIdxList(idxlist);//初始化索引表idxlist为空表 while(! feof(f)) { GetLine(f);//从文件f中读一个书目信息到buf ExtractKeyWord(wdlist,BookNo);//从buf中提取数目关键词到词表,书号存入BookNo InsIdxList(idxlist,wdlist,BookNo);//将书号为BookNo的关键词插入索引表 cout< } PutText(g,idxlist);//将生成的索引表idxlist输入到文件f中 } } } 3、调试 程序名为suoyinbiao.exe,运行环境为DOS。 程序执行后显示: 3、源代码 #include #include #include #include #include #defineMaxBookNum1000//假设只对1000本书建索引表 #defineMaxKeyNum2500//索引表的最大容量 #defineMaxLineLen500//书目串的最大长度 #defineMaxWordNum10//词表的最大容量 template public: char*item[MaxKeyNum];//字符串数组 intlast;//词表的长度 SeqListwdlist(int); };//索引表类型(顺序表) TypedefstructLNode{//存放书号的链表 intdata[3]; structLNode*next; }LNode,*LinkList; classString{//存放关键词的串 public: char*ch; intlength; }; classIdxTermType{ public: String*key; LinkListbnolist; };//索引项类型 classIdxListType{ public: IdxTermTypeitem[MaxKeyNum+1]; intlast; };//索引表类型(有序表) char*buf;//书目缓冲区 inti;//无重复的关键字个数 intb;//保留i的前一次变换的值 intv; char*com[10]={"and","a","an","the","a","an","to","of"}; IdxListType*InitIdxList(IdxListType*idxlist) //初始化操作,置索引表为空表 { //idxlist=(IdxListType*)malloc(sizeof(IdxListType)); idxlist=newIdxListType; if(idxlist==NULL) { cout<<"ERROR--1! "; exit(-1); } idxlist->item[0].key=newString; idxlist->item[0].bnolist=newLNode; idxlist->item[0].key->ch=NULL; idxlist->item[0].key->length=0; idxlist->item[0].bnolist->next=NULL; idxlist->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< } intExtractKeyWord(SeqList //从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-'0'; p++;j++; } j--; while(! isalpha(*p)) p++; q=p; while(*q) { while((*q! ='')&&*q&&(*q! =EOF)) q++; q++; *(q-1)='\0'; c[t++]=p; p=q; } for(k=0;k { for(s=0;s<8;++s) { if(strcmp(c[k],com[s])==0) break; } if(s==8) wdlist.item[i++]=c[k]; } wdlist.last=i; cout<<"词表长度: "< cout<<"书号: "; for(k=0;k cout< for(k=b;k cout<<"关键词"< return0; } intInsIdxList(IdxListType*idxlist,SeqList //将书号bno的书名关键词按词典顺序插入索引表idxlist { intk,t,s,j=0,c=b,d; LinkListp,q; if(i>=MaxKeyNum+1) { cout<<"超过索引表最大容量,请调整! \n"; exit(-1); } for(t=idxlist->last;j { if(0==t)//第一个关键词插入 { idxlist->item[0].key->ch=wdlist.item[c]; for(d=0;d<3;++d) idxlist->item[0].bnolist->data[d]=bno[d];//书号存储 idxlist->item[0].bnolist->next=NULL;//链表尾部指向空 idxlist->last++; t++; } else { for(k=0;k { if((s=strcmp(wdlist.item[c],idxlist->item[k].key->ch))==0)/插入的关键词已存在 { p=newLNode;//申请个新结点,保存书号 cout<<"\n插入的书号: "; for(d=0;d<3;++d) { p->data[d]=bno[d]; cout< } q=idxlist->item[k].bnolist; for(d=0;d<3;++d) fprintf(stdout,"%d",q->data[d]); while(q->next! =NULL) q=q->next; q->next=p; p->next=NULL; fprintf(stdout,"\n关键词%-20s\t\n",idxlist->item[k].key->ch); q=idxlist->item[k].bnolist; while(q) { for(d=0;d<3;++d) fprintf(stdout,"%d",q->data[d]); if(q->next! =NULL) fprintf(stdout,","); q=q->next; } printf("\n"); break; } elseif(s>0) { if(k==t-1)//插入的关键词大于最后一个已存在的关键词,则插到最后 { idxlist->item[t].key=newString; idxlist->item[t].bnolist=newLNode; idxlist->item[t].key->ch=wdlist.item[c]; for(d=0;d<3;++d) idxlist->item[t].bnolist->data[d]=bno[d]; idxlist->item[t].bnolist->next=NULL; idxlist->last++; t++; break; } } else { idxlist->item[t].key=newString; idxlist->item[t].bnolist=newLNode; for(s=t-1;s>=k;--s) {//插入的关键字小于当前已有的关键字,则把当前的关键字向后移 idxlist->item[s+1].key->ch=idxlist->item[s].key->ch; idxlist->item
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 课程设计 报告