串的定长顺序存储结构

头文件 head.h


#include<string.h>
#include<ctype.h>
#include<malloc.h> /* malloc()等 */
#include<limits.h> /* INT_MAX等 */
#include<stdio.h> /* EOF(=^Z或F6),NULL */
#include<stdlib.h> /* atoi() */
#include<io.h> /* eof() */
#include<math.h> /* floor(),ceil(),abs() */
#include<process.h> /* exit() */
/* 函数结果状态代码 */
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
/* #define OVERFLOW -2 因为在math.h中已定义OVERFLOW的值为3,故去掉此行 */
typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
typedef int Boolean; /* Boolean是布尔类型,其值是TRUE或FALSE */

#define MAXSTRLEN 255									//用户可以在255以内定义最大串长
typedef unsigned char SString[MAXSTRLEN + 1];				//0号单元存放串的长度

Status StrAssign(SString T, char *chars);

Status StrCopy(SString T, SString S);

Boolean StrEmpty(SString S);

int StrCompare(SString S, SString T);

int StrLength(SString s);

Status ClearString(SString S);

Status Concat(SString T, SString S1, SString S2);

Status SubString(SString Sub, SString S, int pos, int len);

int Index(SString S, SString T, int pos);

Status Replace(SString S, SString T, SString V);

Status StrInsert(SString S, int pos, SString T);

Status StrDelete(SString S, int pos, int len);

Status DestoryString(SString S);

void StrPrint(SString T);

算法实现

#include"head.h"

Status StrAssign(SString T, char *chars)
{
	//初始条件:chars是字符串常量
	//操作结果:生成一个其值等于chars的串T

	int len;
	len = strlen(chars);
	if (len > MAXSTRLEN)
		return ERROR;

	T[0] = len;
	for (int i = 1; i <= T[0]; i++)
		T[i] = *(chars + i - 1);

	return OK;
}

Status StrCopy(SString T, SString S)
{
	//初始条件:串S存在
	//操作结果:由串S赋值得到串T

	for (int i = 0; i <= S[0]; i++)
		T[i] = S[i];

	return OK;
}

Boolean StrEmpty(SString S)
{
	//初始条件:串S存在
	//操作结果:若S为空串,则返回TRUE, 否则返回FALSE

	if (S[0] == 0)
		return TRUE;
	else
		return FALSE;
}

int StrCompare(SString S, SString T)
{
	//初始条件:串S和T存在
	//操作结果:若S>T,则返回值>0;若S=T,则返回值=0;若S<T,则返回值<0

	for (int i = 1; i <= (S[0] > T[0] ? T[0] : S[0]); i++)
	{
		if (S[i] != T[i])
			return S[i] - T[i];
	}

	return S[0] - T[0];
}

int StrLength(SString S)
{
	//初始条件:串S存在
	//操作结果:返回S的元素的个数,成为串的长度

	return  S[0];
}

Status ClearString(SString S)
{
	//初始条件:串S存在
	//操作结果:将S清为空串

	S[0] = 0;

	return OK;
}

Status Concat(SString T, SString S1, SString S2)
{
	//初始条件:串S1和S2存在
	//操作结果:用T返回由S1和S2连接而成的新串,如若未截断,则返回TRUE,否则,返回FALSE

	//S1[0] + S2[0] <= MAXSTRLEN
	if (S1[0] + S2[0] <= MAXSTRLEN)
	{
		T[0] = S1[0] + S2[0];
		for (int i = 1; i <= S1[0]; i++)
			T[i] = S1[i];
		for (int i = 1; i <= T[0]; i++)
			T[S1[0] + i] = S2[i];

		return TRUE;
	}
	//S1[0] < MAXSTRLEN && S1
	else
	{
		T[0] = MAXSTRLEN;
		for (int i = 1; i <= S1[0]; i++)
			T[i] = S1[0];
		for (int i = 1; i <= MAXSTRLEN - S1[0]; i++)
			T[S1[0] + i] = S2[i];

		return FALSE;
	}
}

Status SubString(SString Sub, SString S, int pos, int len)
{
	//初始条件:串S存在,1<=pos<=StrLength(S)且0<=len<=StrLength(S)-pos+1
	//操作结果:用Sub返回串中的pos个字符起长度为len的子串

	if (pos<1 || pos >S[0] || len<0 || len>S[0] - pos + 1)
		return ERROR;
	Sub[0] = len;
	for (int i = 1; i <= len; i++)
		Sub[i] = S[pos + i - 1];

	return OK;
}

int Index(SString S, SString T, int pos)
{
	//初始条件:串S和T存在,T是非空串, 1<=pos <=S[0]
	//操作结果:若主串S中存在和串T值相同的子串,则返回它在主串S中第pos个字符之后第一次出现的位置;否则函数值为0

	SString sub_temp;

	if (pos < 1 || pos >S[0])
		return 0;

	for (int i = pos; i <= S[0] - T[0] + 1; i++)
	{
		SubString(sub_temp, S, i, T[0]);
		if (StrCompare(sub_temp, T) == 0)
			return i;
	}

	return 0;
}

Status Replace(SString S, SString T, SString V)
{
	//初始条件:串S、T和V存在,T是非空串
	//操作结果:用V替换主串S中出现的所有的与T相等的不重叠的子串
		
	int i = 1;						//从S的第一个字符起查找串T
	Status k;
	
	if (StrEmpty(V))
		return ERROR;

	do
	{
		i = Index(S, T, i);			//结构
		if (i)
		{
			StrDelete(S, i, T[0]);
			k = StrInsert(S, i, V);	//不能完全插入
			if (!k)
				return ERROR;
			i += V[0];
		}
	} while (i);

	return OK;
}

Status StrInsert(SString S, int pos, SString T)
{
	//初始条件:串S和T都存在,1<=pos<=S[0]+1
	//操作结果:在串S的第pos个字符之前插入T

	int i;
	if (pos<1 || pos>S[0] + 1)
		return ERROR;

	if (S[0] + T[0] <= MAXSTRLEN)					//完全插入
	{ 
		for (i = S[0]; i >= pos; i--)				//移动数据
			S[i + T[0]] = S[i];
		for (i = pos; i<pos + T[0]; i++)			//复制数据
			S[i] = T[i - pos + 1];
		S[0] = S[0] + T[0];
		return TRUE;
	}
	else                                           //部分插入,S被截断
	{
		for (i = MAXSTRLEN; i >= pos; i--)
			S[i] = S[i - T[0]];
		for (i = pos; i<pos + T[0] && i<MAXSTRLEN; i++)
			S[i] = T[i - pos + 1];
		S[0] = MAXSTRLEN;
		return FALSE;
	}
}

Status StrDelete(SString S, int pos, int len)
{
	// 初始条件: 串S存在,1≤pos≤StrLength(S)-len+1 
	// 操作结果: 从串S中删除第pos个字符起长度为len的子串 
	int i;
	if (pos<1 || pos>S[0] - len + 1 || len<0)
		return ERROR;

	for (i = pos + len; i <= S[0]; i++)				//移动数据
		S[i - len] = S[i];
	S[0] -= len;

	return OK;
}

Status DestoryString(SString S)
{
	//定长数组,无法销毁
	return OK;
}

void StrPrint(SString T)
{ 
	//输出字符串T。另加

	for (int i = 1; i <= T[0]; i++)
		printf("%c", T[i]);
	printf("\n");
}

测试文件

#include"head.h"

void main()
{
	int i, j;
	Status k;
	char s, c[MAXSTRLEN + 1];
	SString t, s1, s2;
	printf("请输入串s1: ");
	gets_s(c, _countof(c));
	k = StrAssign(s1, c);
	if (!k)
	{
		printf("串长超过MAXSTRLEN(=%d)\n", MAXSTRLEN);
		exit(0);
	}
	printf("串长为%d 串空否?%d(1:是 0:否)\n", StrLength(s1), StrEmpty(s1));
	StrCopy(s2, s1);
	printf("拷贝s1生成的串为: ");
	StrPrint(s2);
	printf("请输入串s2: ");
	gets_s(c, _countof(c));
	k = StrAssign(s2, c);
	if (!k)
	{
		printf("串长超过MAXSTRLEN(%d)\n", MAXSTRLEN);
		exit(0);
	}
	i = StrCompare(s1, s2);
	if (i < 0)
		s = '<';
	else if (i == 0)
		s = '=';
	else
		s = '>';
	printf("串s1%c串s2\n", s);
	k = Concat(t, s1, s2);
	printf("串s1联接串s2得到的串t为: ");
	StrPrint(t);
	if (k == FALSE)
		printf("串t有截断\n");
	ClearString(s1);
	printf("清为空串后,串s1为: ");
	StrPrint(s1);
	printf("串长为%d 串空否?%d(1:是 0:否)\n", StrLength(s1), StrEmpty(s1));
	printf("求串t的子串,请输入子串的起始位置,子串长度: ");
	scanf_s("%d,%d", &i, &j);
	k = SubString(s2, t, i, j);
	if (k)
	{
		printf("子串s2为: ");
		StrPrint(s2);
	}
	printf("从串t的第pos个字符起,删除len个字符,请输入pos,len: ");
	scanf_s("%d,%d", &i, &j);
	StrDelete(t, i, j);
	printf("删除后的串t为: ");
	StrPrint(t);
	i = StrLength(s2) / 2;
	StrInsert(s2, i, t);
	printf("在串s2的第%d个字符之前插入串t后,串s2为:\n", i);
	StrPrint(s2);
	i = Index(s2, t, 1);
	printf("s2的第%d个字母起和t第一次匹配\n", i);
	SubString(t, s2, 1, 1);
	printf("串t为:");
	StrPrint(t);
	Concat(s1, t, t);
	printf("串s1为:");
	StrPrint(s1);
	Replace(s2, t, s1);
	printf("用串s1取代串s2中和串t相同的不重叠的串后,串s2为: ");
	StrPrint(s2);

	system("pause");
}


结果:

请输入串s1: ABCD
串长为4 串空否?0(1:是 0:否)
拷贝s1生成的串为: ABCD
请输入串s2: 123456
串s1>串s2
串s1联接串s2得到的串t为: ABCD123456
清为空串后,串s1为:
串长为0 串空否?1(1:是 0:否)
求串t的子串,请输入子串的起始位置,子串长度: 3,7
子串s2为: CD12345
从串t的第pos个字符起,删除len个字符,请输入pos,len: 4,4
删除后的串t为: ABC456
在串s2的第3个字符之前插入串t后,串s2为:
CDABC45612345
s2的第3个字母起和t第一次匹配
串t为:C
串s1为:CC
用串s1取代串s2中和串t相同的不重叠的串后,串s2为: CCDABCC45612345
请按任意键继续. . .



在C语言中,(字符)通常被看作是一个字符数组,如果需要对其进行定长顺序存储,可以预先定义一个固定大小的缓冲区来存储字符。动态数组在这种情况下对应的是通过`malloc`函数动态分配内存的字符数组。 定长顺序存储结构的C语言实现大致如下: ```c #define MAX_LENGTH 50 // 定义字符最大度 // 结构体表示定长字符 typedef struct { char str[MAX_LENGTH]; // 字符数组作为数据成员 int length; // 存储字符实际度,包括结束符'\0' } FixedLengthString; // 分配并初始化字符 FixedLengthString* create_fixed_length_string(char* initial) { FixedLengthString* s = (FixedLengthString*) malloc(sizeof(FixedLengthString)); if (initial) { strncpy(s->str, initial, MAX_LENGTH - 1); // 避免超过最大度 s->length = strlen(initial); } else { s->length = 0; } return s; } // 释放动态分配的空间 void destroy_fixed_length_string(FixedLengthString* s) { free(s); } // 使用示例 int main() { FixedLengthString* myStr = create_fixed_length_string("Hello, world!"); // ... destroy_fixed_length_string(myStr); return 0; } ``` 然而,这种方式有局限性,因为一旦定义了最大度,就无法扩展到更大的字符,如果输入的字符超过了这个度,可能会导致数据溢出。动态数组(如`char str[MAX_LENGTH]`)在C语言中通常是固定的,如果需要动态调整大小,可以考虑使用`malloc`或`calloc`动态分配内存。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值