- 19
- 0
- 约3.99千字
- 约 5页
- 2016-09-01 发布于重庆
- 举报
数据结构实验报告--串
北京建筑大学
理学院《数据结构与算法》课程 实验报告
课程名称《数据结构与算法》 实验名称 串的定义以及应用 实验地点 基C-419 日期_2015-5-12
姓名 李若万 班级 信131 学号 201307010135 指导教师 毕靖 成绩
【实验目的】 1.熟悉并写出字符出的逻辑结构表示
2.实现字符串表示
3.实现字符串的操作 【实验内容】
串的定义以及应用
【实验要求】
要求:
在实验报告中写出串的ADT表示;
在实验报告中给出数据类型定义和核心算法和程序;
在实验报告中罗列实验过程中出现的问题和解决的方法;
打包上交调试后的完整程序,提交实验报告;
实验之前写出实验报告的大概框架,实验过程中填写完整。
实验时携带需要上机调试的程序;
实验评分:实验之前预习占20%,实验报告书写情况占50%,运行情况30%。
【实验步骤】
实验中出现的问题以及解决方法:
1.
【实验结果】
1、ADT表示:
ADT String{
StrAssign(T, chars)
StrEmpty(S)
StrLength(S)
Index(S, T, pos)
StrInsert(S, pos, T)
StrDelete(S, pos, len)
Replace(S,T, V)
StrCompare( S, T)
Concat(T, S1,S2)
}
2、数据类型定义:
typedef struct{
char *ch;
int length;
}HString;
3、核心算法程序:
# include stdlib.h
# include stdio.h
# include string.h
# include conio.h
#define OK 1
#define ERROR 0
#define OVERFLOW -1
typedef struct{
char *ch;
int length;
}HString;
int StrAssign(HString T, char* chars){
int len = strlen(chars);
if(T.ch) free(T.ch);
if(!len){
T.ch = NULL;
T.length = 0;
}
else{
if(!(T.ch = (char *)malloc((len+1)* sizeof(char))))
exit(0);
strcpy(T.ch,chars);
T.length = len;
}
return 0;
}
int StrCompare(HString S,HString T){
for(int i=0;iS.length iT.length;++i)
if(S.ch[i]!=T.ch[i]) return S.ch[i]-T.ch[i];
return S.length-T.length;
}
int StrLength(HString S){
return S.length;
}
int Concat(HString T,HString S1,HString S2){
int i;
if(T.ch) free(T.ch);
if(!(T.ch=(char *)malloc((S1.length+S2.length)*sizeof(char))))
exit(0);
for(i=0;iS1.length;i++)
T.ch[i]=S1.ch[i];
for(i=0;iS2.length;i++)
T.ch[S1.length+i]=S2.ch[i];
T.length=S1.length+S2.length;
return OK;
}
void SubString(HString Sub, HString S, int pos, int len){
if(pos 1 || pos S.length || len 0 || len S.length - pos + 1)
exit(0);
if(Sub.ch) free(Sub.ch);
if(!len){
Sub.ch = NULL;
原创力文档

文档评论(0)