字符串静态顺序结构C/C++实现(数据结构严蔚敏版)

1、头文件String.h

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
using namespace std;

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2

typedef int Status;

#define MAXSTRLEN 255
typedef unsigned char SString[MAXSTRLEN + 1];  //0号单元存放串的长度


//将字符数组赋值给字符串,并在第一位置记录它的长度 
Status StrAssign(SString& T, char chars[])
{ 
  //生成一个其值等于chars的串T。
  int i; 
  
  if(strlen(chars)>MAXSTRLEN)
	 return ERROR;

  //这里的长度最大只能是255,可以表示成一个字符 
  T[0] = strlen(chars);
  
  for(i = 0; i <= T[0]; i++){
    T[i+1] = chars[i];
    
  }
  
  return OK;
}

//字符串复制 
Status StrCopy(SString& T, const SString& S)
{
 //由串S复制得串T
  int i;
  
  for(i = 1; i <= S[0]; i++){
    T[i] = S[i];
  }
  
  T[0] = S[0];
  
  return OK;
}


//判断否为空字符串 
Status StrEmpty(const SString& S)
{//判空
  if(S[0] == 0)
	  return TRUE;
  else
	  return FALSE;
}

//比较字符串 
Status StrCompare(const SString& S, const SString& T){
 //若S>T,则返回值>0,若S=T,则返回值=0;若S<T,则返回值<0
 //s[0]和T[0]分别表示为字符串长度
  
  int i;
  for(i = 1; i<=S[0] && i<=T[0]; i++){
  	
	    if(S[i] != T[i])
			return S[i] - T[i]; 
		
  }
  
  	return S[0]-T[0];
  
}

//获取字符串的长度 
Status StrLength(const SString& S)
{
 	 return S[0];
}

//连接两个字符串 
Status Concat(SString& T, const SString& S1, const SString& S2){ 
  //用串T返回由S1和S2连接而成的新串。
	int i,j;
	
	//是否被截断 
	Status uncut;
	
	if( S1[0]+S2[0] <= MAXSTRLEN )//未截断
	{
		  T[0] = S1[0] + S2[0];
		  
	      for(i = 1; i <= S1[0]; i++){
		   		T[i] = S1[i];
		   		
		  }
		  
	      for(j = 1; j <= S2[0]; j++){
			  	T[S1[0] + j] = S2[j]; 
			  	
		  }
		  
		  uncut=TRUE;
	  
	}

	else if(S1[0] < MAXSTRLEN)//截断
	{
		  T[0] = MAXSTRLEN;
		  
		  //将S1中的字符复制到T中 
		  for( i = 1; i < S1[0]; i++){
		    T[i] = S1[i];
		    
		  }
		  
		  //将S2的字符复制到T中,直到T满了为止 
		  for(j = 1; j <= MAXSTRLEN-S1[0]; j++){
			 T[S1[0]+j] = S2[j];
			 
		  }
		  
		  uncut=FALSE;
	}
	else// S1[0]>MAXSTRLEN
	{
	   T[0] = MAXSTRLEN;
	   
       for(i = 1; i<= MAXSTRLEN; i++){
	     T[i] = S1[i];
	     
	   }
	   
	   uncut=FALSE;
	}
	
	return uncut;
}


//求子串 
Status SubString(SString& Sub, const SString& S, int pos, int len)
{   //求子串
	//用Sub返回串S的第pos个字符起长度为len的子串
	int i, j;
	
    if(pos < 1 || pos > S[0] || len < 0 || len > S[0] - pos + 1)
	    return ERROR;
	    
    if(len == 0){
	   Sub[0] = 0;
	   
	}else{
		
	 for(i = 1,j = pos; i <= len-1,j <= pos+len-1; i++,j++)
		Sub[i] = S[j];
		
		Sub[0]=len;
	 
	}
	
	return OK;
}

//模式匹配 
Status Index(SString& S, SString& T, int& pos) 
{ 
  //若主串S中存在和串T相同的子串,则返回它在主串S的第pos个
  //字符之后第一次出现的位置,否则函数值为0
	int n,  m, i;
	SString 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;
}

Status StrInsert(SString& S, int pos, const SString& T)
{  //插入函数
   //在串S的第pos个字符之前插入串T。
	int i;
	
	if (pos<1 || pos>S[0])
		return ERROR;
		
	if (S[0]+T[0] < MAXSTRLEN)//完全插入
	{
		for (i = S[0]; i >= pos; i--){
			S[i + T[0]] = S[i];
			
		}
		
		for (i = 1; i <=T[0]; i++){
			S[pos-1+i] = T[i];
			
		}
		
		S[0] = S[0] + T[0];
		
		return TRUE;
	}
	else
	{
		for (i = S[0]; i >= MAXSTRLEN-S[0];i--)
		{
			S[MAXSTRLEN-S[0]+1] = S[i];
		}
		for (i = 1; i <= MAXSTRLEN - S[0];i++)
		{
			S[pos - 1 + i] = T[i];
		}
		return FALSE;
	}
}

Status StrDelete(SString& S, int pos, int len)
{  //删除
   //从串S中删除第pos个字符起长度为len的子串
	int i;
	
	if (pos<1 || pos>S[0])
		return ERROR;
		
	else
		for (i = pos + len ; i<=S[0]; i++)
		{
			S[i-len] = S[i];
		}
		
	S[0] -= len;
	
	return  OK;
}

Status StrPrint(const SString& S)
{//输出函数
	int i;
	
	for (i = 1; i <= S[0]; i++)
	{
		cout << S[i];
	}
	
	cout << endl;
	return 0;
}


2、测试文件test.cpp

#include "String.h"

int main(void)
{    
	SString S, Sub, s, t;
	int pos;
	int len;
	int n;

	char a[100], b[100];
	
	
	
	cout<<"		0----退出"<<endl;
	cout<<"		1----初始化字符串"<<endl;
	cout<<"		2-----判断字符串是否为空"<<endl;
	cout<<"		3-----比较字符串大小,并返回"<<endl;
	cout<<"		4------求字符串的长度"<<endl;
	cout<<"		5-------截取字符串"<<endl;
	cout<<"		6--------查找子串的位置"<<endl;
	cout<<"		7--------插入子串"<<endl;
	cout<<"		8---------删除子串"<<endl;
	cout<<"		9---------打印字符串\n"<<endl; 
	
	cout<<"请输入你的操作"<<endl;
	cin>>n;
	
	while(1){
		
		switch(n){
			case 0: 
				cout<<"你已经退出"<<endl;
				exit(0);
				
			case 1:
				cout<<"请输入字符串"<<endl; 
				cin>>a;
				StrAssign(S, a);
				break; 
				 
			case 2:
				if(!StrEmpty(S)){
					cout<<"字符串不是空串"<<endl;
					
				}else{
					cout<<"字符串是空串"<<endl;
					
				}
				break;
				
			case 3:
				
				cout<<"请输入你要比较的两个字符串"<<endl;
				cin>>a;
				cin>>b;
				
				StrAssign(s, a);
				StrAssign(t, b); 
				
				n = StrCompare(s, t);
				
				if(n > 0){
					cout<<"第一字符串大"<<endl;
					
				}else if(n == 0){
					cout<<"两个字符串大小相等"<<endl;
					
				}else{
					cout<<"第二个字符串大" ;
				}
				break;
				
			case 4:
			
				len  = StrLength(S); 
				cout<<"字符串的长度是:"<<len<<endl;
				break;
			
			case 5:
				cout<<"请输入你要截取的子串的位置"<<endl;
				n  = SubString(Sub, S, pos, len);
				 
				if(n){
					cout<<"你截取的字符串为"<<StrPrint(Sub);
					
				}else{
					cout<<"截取失败"<<endl; 
				}
				
				break;
				
			case 6:
				cout<<"请输入后你查找的子串"<<endl;
				cin>>a;
				StrAssign(t, a);
				Index(S, t, pos);
				cout<<"你查找的子串位置是:"<<pos<<endl;
				break;
				
			case 7:
				cout<<"请输入你要插入的位置和子串"<<endl;
				cin>>pos; 
				cin>>a;
				StrAssign(t, a);
				StrInsert(S, pos, t);
				break;
				
			case 8:
				cout<<"请输入你要删除的子串位置和长度"<<endl;
				cin>>pos;
				cin>>len;
				StrDelete(S, n, len);
				break;
				
			case 9:
				cout<<"字符如下:"<<endl;
				StrPrint(S);	
			
		}
		
	
	
	cout<<"		0----退出"<<endl;
	cout<<"		1----初始化字符串"<<endl;
	cout<<"		2-----判断字符串是否为空"<<endl;
	cout<<"		3-----比较字符串大小,并返回"<<endl;
	cout<<"		4------求字符串的长度"<<endl;
	cout<<"		5-------截取字符串"<<endl;
	cout<<"		6--------查找子串的位置"<<endl;
	cout<<"		7--------插入子串"<<endl;
	cout<<"		8---------删除子串";
	cout<<"		9---------打印字符串\n\n"<<endl; 
	
	cout<<"请输入你的操作\n"<<endl;
	cin>>n;	
	}
	
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

圣诞节不感冒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值