【数据结构实验三】串

版权声明:本文为King_HAW原创文章,未经King_HAW允许不得转载。 https://blog.csdn.net/King_HAW/article/details/71434994

版权申明:

本实验的实验目的,实验内容,实验要求归东北大学所有,未经许可禁止转载。


实验三 串

实验目的

掌握字符串堆分配存储的构造与输出操作;利用求串长、串比较、求子串操作实现串的模式匹配(index)。

实验内容

1. 通过结构体声明堆存储的串,并构造一个串;

2. 实现串的输出;

3. 编写求串长、串比较、求子串操作;

4. 根据3中的操作,实现串的模式匹配;

5. 在主函数中声明两个串,通过模式匹配测试一个串是否存在于另一个串中。

源码:

#include "stdafx.h"
#include <iostream>

using namespace std;
typedef int Status;

/***********类型定义***********/
typedef struct hstring{
	char *ch;  //串指针
	int length;  //串长
}HString;

/***********函数申明***********/
Status StrAssign(HString &T, char *chars);  //构造串
int StrLength(HString S);  //计算串长
int StrCompare(HString S, HString T);  //串比较
Status SubString(HString &Sub, HString S, int pos, int len);  //取子串
int Index(HString S, HString T, int pos);  //模式匹配
void StrPrint(HString S);  //输出

/***********函数实现***********/
/***********构造串***********/
Status StrAssign(HString &T, char *chars){
	int i, j;
	char *c;
	//if(T.ch) free(T.ch);
	for(i = 0, c = chars; *c; ++i, ++c); //测定串长
	if(!i){
		T.ch = NULL;
		T.length = 0;
	}
	else{
		if(!(T.ch = (char *)malloc(i * sizeof(char)))) exit(OVERFLOW);
		for(j = 0; j <= i - 1; ++j){
			T.ch[j] = chars[j];
		}
		T.length = i;
	}
	return OK;
}

/***********输出串***********/
void StrPrint(HString S){
	int j;
	for(j = 0; j <= S.length - 1; ++j)
		cout << S.ch[j];
	cout << endl;
}

/***********串比较***********/
int StrCompare(HString S, HString T){
	int i;
	for(i = 0; i < S.length && i < T.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;
}

/***********取子串***********/
Status SubString(HString &Sub, HString S, int pos, int len){
	int j;
	if(pos < 1 || pos > S.length || len < 0 || len > S.length - pos + 1)
		return ERROR;
	//if(Sub.ch) delete(Sub.ch);
	if(!len){
		Sub.ch = NULL;
		Sub.length = 0;
	}
	else{
		Sub.ch = (char *)malloc(len * sizeof(char));
		for(j = 0; j < len; ++j){
			Sub.ch[j] = S.ch[j + pos - 1];
			Sub.length = len;
		}
	}
	return OK;
}

/***********模式匹配***********/
int Index(HString S, HString T, int pos){
	int n, m ,i;
	HString sub;
	if(pos > 0){
		n = StrLength(S);
		m = StrLength(T);
		i = pos;
	}
	while(i <= n - m + 1){
		SubString(sub, S, i, m);
		if((StrCompare(sub, T)) != 0) ++i;
		else
			return i;
	}
	return 0;
}

/***********主函数***********/
int main()
{
    HString S = {NULL,0}, S1 = {NULL,0}, S2 = {NULL,0}, sub = {NULL,0};
    char *mainstring = "HelloWorld5555";
    //char *substring = "World";
    StrAssign(S1, mainstring);
    //StrAssign(S2, substring);
    StrAssign(S2, "World");
    cout << "S1: ";
    StrPrint(S1);
    cout << "The length of S1 is " << StrLength(S1) << endl;
    cout << "S2: ";
    StrPrint(S2);
    cout << "The length of S2 is " << StrLength(S2) << endl;
    cout << "The location is " << Index(S1, S2, 1) << endl;
    SubString(sub, S1, 6, 7);
    cout << "the SubString is ";
    StrPrint(sub);
    cout << "StrCompare: " << StrCompare(S2, S1) << endl;
    return 0;
}

这里注意一下,取子串和构造串中注释的部分是释放内存语句,可以不要。注意一下主函数中对于S1,S2的初始化和赋值语句的不同。


运行结果:



猜你喜欢

转载自blog.csdn.net/King_HAW/article/details/71434994