P88 例4-3 编写一个程序,测试BFIndex()函数的正确性。

P88 例4-3 编写一个程序,测试BFIndex()函数的正确性。

头文件:DString.h

#include<stdio.h>
#include<stdlib.h> 
#include<string.h>

typedef struct
{
	char* str;
	int maxLength;
	int length;
}DString;

void Initiate(DString* S, int max, char* string)
{
	int i;
	int size = max + 1;
	S->str = (char*)malloc(sizeof(char) * size);
	if (S->str != NULL)
	{
		S->maxLength = max;
		S->length = (int)strlen(string);
		for (i = 0; i < S->length; i++)
			S->str[i] = string[i];
		S->str[i] = '\0';
	}
}

int Insert(DString* S, int pos, DString T)
{
	if (pos < 0)
	{
		printf("参数pos出错!");
		return 0;
	}
	else
	{
		int i;
		char* buffer = (char*)malloc(sizeof(char) * (S->length + 1));
		buffer[S->length] = '\0';
		for (i = 0; i < S->length; i++)
		{
			buffer[i] = S->str[i];
		}

		if (S->length + T.length > S->maxLength)
		{
			free(S->str);
			int size = S->length + T.length + 1;
			S->str = (char*)malloc(sizeof(char) * size);
			S->str[size - 1] = '\0';
			S->maxLength = S->length + T.length;
		}
		for (i = S->length - 1; i >= pos; i--)
			S->str[i + T.length] = buffer[i];
		for (i = 0; i < T.length; i++)
			S->str[pos + i] = T.str[i];
		S->length += T.length;
		S->str[S->length + T.length] = '\0';
		free(buffer);
		return 1;
	}
}

int Delete(DString* S, int pos, int len)
{
	int i;
	if (S->length <= 0)
	{
		printf("数组中未存放字符无元素可删!\n");
		return 0;
	}
	else if (pos < 0 || len<0 || pos + len>S->length)
	{
		printf("参数pos和len不合法!\n");
		return 0;
	}
	else
	{
		for (i = pos + len; i <= S->length - 1; i++)
			S->str[i - len] = S->str[i];
		S->length = S->length - len;
		return 1;
	}
}

int SubString(DString* S, int pos, int len, DString* T)
{
	int i;
	if (pos < 0 || len<0 || pos + len>S->length)
	{
		printf("参数pos和len出错!\n");
		return 0;
	}
	if (len > T->maxLength)
	{
		T->str = (char*)malloc((len + 1) * sizeof(char));
		T->maxLength = len;
	}
	for (i = 0; i < len; i++)
		T->str[i] = S->str[pos + i];
	T->length = len;
	T->str[i] = '\0';
	return 1;
}

void Destroy(DString* S)
{
	free(S->str);
	S->maxLength = 0;
	S->length = 0;
}

源文件:例4-3.c

#include"DString.h"

int BFIndex(DString S,int start,DString T)
{
	int i=start,j=0,v;
	
	while(i<S.length && j<T.length)
	{
		if(S.str[i]==T.str[j])
		{
			i++;
			j++;
		}
		else
		{
			i=i-j+1;
			j=0;
		}
	}
	
	if(j==T.length)
		v=i-T.length;
	else
		v=-1;
	return v;
}

int main()
{
	DString myString1,myString2;
	int max1=29,max2=9;
	int pos=0;
	Initiate(&myString1,max1,"Dada Structure Data Structure");
	Initiate(&myString2,max2,"Structure");
	
	pos=BFIndex(myString1,pos,myString2);
	printf("第一次查找时 pos=%d\n",pos);
	
	pos=BFIndex(myString1,pos+1,myString2);
	printf("第二次查找时 pos=%d\n",pos);
	Destroy(&myString1); 
	Destroy(&myString2); 
	return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值