字符串的动态顺序结构(C/C++语言)

所有实现代码在头文件中,只要在测试代码加上头文件,就可以测试了。测试代码你觉得不好用,可以再修改。
#include "str.h"

1.头文件str.h

#include<iostream>
using namespace std;
#include<string.h>
#include<malloc.h> 
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
 
#define MAXQSIZE 5
//Status是函数的类型,其值是函数结果状态码
typedef int Status;

//字符串定义 
typedef struct{
    char *ch;                    //若是非空串,则按串长分配存储区,否则 ch 为 NULL
    int length;                    //字符串长度
}HString;

//初始化给字符串赋值 
Status StrAssign(HString &T, char * chars){
  	//if(T.ch)  free(T.ch);

  //若串存储空间不为NULl 释放原有空间
      int i = strlen(chars);    //获取chars长度
      
      if(!i){
         T.ch = NULL;    //串常量chars长度为0时串为空串长度0存储区间指向NULL
          T.length = 0;
          
      }else{
          T.ch = (char *)malloc(i * sizeof(char));    //申请空间
          
          if(!T.ch)
			  exit(OVERFLOW);
             
             
          for(int j = 0; j < i; j++){
          		T.ch[j] = chars[j];        //写入chars串
		  }
              
          T.length = i;
      }
      
      return OK;
}

//返回字符串的长度
int StrLength(HString &S){
    return S.length;
}


//字符串复制,将字符串T复制给S 
Status StrCopy(HString& S, const HString& T){
	int i;
	//如果字符串不为空,就释放它的空间 
	if(!S.ch) free(S.ch);
	
	//如果字符串是空串 
	if(!T.length){
		S.ch = NULL;
		S.length;
		
	} else{
		//给字符串S申请 
		S.ch = (char*)malloc(sizeof(char) * T.length);
		//如果分配地址失败,就退出 
		if(!S.ch) exit(OVERFLOW);
		
		//将字符串T赋值给S,长度是T的长度 
		for(int i=0; i<T.length; i++){
			S.ch[i] = T.ch[i];
		}
		S.length = T.length;
		
	}
	
	return OK;
} 

//插入子串,将字符串T插入到字符串S的pos位置 
Status StrInsert(HString& S, const int& pos, const HString& T){
	//判断插入配置是否合理 
	if(pos < 0 || pos > S.length) return ERROR;
	
	if(T.ch){
		//重新改变字符串的长度 
		S.ch = (char*)realloc(S.ch, (S.length+ T.length) * sizeof(char));
		if(!S.ch) return ERROR;
		
		//将pos位置后的数据后移,腾出空间给T 
		for(int i = S.length-1; i >= pos; i--){
			S.ch[i+T.length] = S.ch[i];
		}
		
		//字符串插入 
		for(int i = 0; i < T.length; i++){
			S.ch[pos+i] = T.ch[i];
		}
		
		S.length = S.length + T.length;
	}
	
	return OK;
} 
//比较两个字符串的大小,若 S>T 返回值>0。相等 返回0 ,否则返回 <0
int StrCompare(const HString& S, const HString& T){
	
    for(int 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;
}

//清空串S为空串,并释放所占空间
Status ClearString(HString &S){
    if(S.ch){
        free(S.ch);
        S.ch = NULL;
    }
    S.length = 0;
    return OK;
}

//连接两个字符串,生成一个新的字符串
Status Concat(HString &T, const HString& S1, const HString& S2){
	
    if(T.ch) free(T.ch);
    
    //申请内存空间 
    T.ch = (char *)malloc( (S1.length + S2.length) * sizeof(char) );
    
    if(!T.ch) exit(OVERFLOW);
    
    //长度等于两个字符串的长度和 
    T.length = S1.length + S2.length;
    
    //复制S1的字符串 
    for(int i = 0; i < S1.length; i++)
        T.ch[i] = S1.ch[i];

	//复制S2的字符串 
    for(int i = 0;i < S2.length; i++)
        T.ch[i + S1.length] = S2.ch[i];

    return OK;
}

//字符串截取,返回截取的子串
Status SubString(HString& sub, const HString& S, const int& pos, const int& len){
	
	//判断截取是否合法 
    if( pos < 1 || pos > S.length || len < 0 || (S.length - pos + 1 ) < len  )
        return ERROR;
     
	 //释放就空间 
    if(sub.ch) free(sub.ch);
    
    //分配新地址空间 
    sub.ch=(char *)malloc( len * sizeof(char) );   
	 
    if(!sub.ch) exit(OVERFLOW);
    
    //复制字符串 
    for(int i = 0; i < len; i++)
        sub.ch[i] = S.ch[i + pos - 1];
        
    sub.length = len;
    
    return OK;
}

//删除子串
Status StrDelete(HString& S, int pos, int len){
	//判断删除的位置和长度是否合法 
	if(pos < 0 || len < 0 ||pos+len > S.length)
		return ERROR;
	
	//将pos+len位置后面的数据前移 
	for(int i = pos+len; i <= S.length-1; i++){
		S.ch[i-len] = S.ch[i];
	} 
	
	//删除pos到pos+len位置的数据 
	S.ch = (char*)realloc(S.ch, (S.length-len)*sizeof(char));
	S.length = S.length - len;
	
	return OK;
} 

//字符串的定位,查找字符串T字主串S中的位置,并返回给pos 
Status Index(const HString& S, const HString& T, int& pos){
	int i = 0, j = 0;
	
	while(i < S.length && j < T.length){
		
		//如果两个字符相同,就继续比较下一个字符 
		if(S.ch[i] == T.ch[j]){
			i++;
			j++;
			
		}else{ 
			// 遇到不匹配的字符,就重新进行匹配,从主串第i-j+1位置重新跟T进行匹配 
			i = i-j+1;
			j = 0;
		}
	}
	
	//匹配完成 
	if(j == T.length){
		pos = i-T.length;
		return OK;
		
	}else{
		//没完全匹配,后面剩余一些字符没匹配 
		return ERROR;
	}
} 


//置换子串操作
Status StrReplace(HString&S, const HString& T, const HString& V) {
	int pos;
	while(Index(S, T, pos)){//判断T是否为S的子串 
	
		StrDelete(S, pos, T.length);//删除S中的子串T 
		StrInsert(S, pos, V);//在S中插入子串V 
		return OK;
	} 
	
	//没有在S中查到子串T 
	return ERROR;
}

//打印字符串 
Status Print(const HString& S){

    for(int i = 0; i < S.length; i++){
    	cout<<S.ch[i];
    }   
	return OK; 
}

2.测试文件test.cpp

#include "str.h"
int  main(void){
   HString S, T, s1, s2, V;
   int n, length, index;
   char s[30];
   S.ch = NULL;
   T.ch = NULL;
   
   
   cout<<"请输入你的操作"<<endl;
   cout<<"		0-------------退出"<<endl;
   cout<<"		1-------------初始化字符串"<<endl;
   cout<<"		2-------------获取字符串的长度"<<endl;
   cout<<"		3-------------清空字符串"<<endl;
   cout<<"		4--------------字符串比较"<<endl;
   cout<<"		5--------------获取子串"<<endl;
   cout<<"		6---------------打印子串"<<endl; 
   cout<<"		7---------------连接子串\n"<<endl;
   cout<<"		8---------------替换子串\n"<<endl;
     
   
   cin>>n;
   getchar();
   
   while(1){
   	
   	switch(n){
   		
   		case 0:
   			cout<<"你已经退出"<<endl;
   			exit(0);
   			
   		case 1:
   			cout<<"请输入你要输入的字符串"<<endl;
   			gets(s);
			StrAssign(S, s);
			cout<<"初始化成功"<<endl;
			break;
			
		case 2: 
				
				length = S.length;
				cout<<"你输入的字符串的长度是"<<endl;
				cout<<length<<endl;
				break;	
			
		case 3:
			ClearString(S);
			cout<<"字符串已经清空"<<endl;
			break;    
   		
   		case 4:
   			cout<<"请再输入一个你要比较的另一个字符串"<<endl;
   			gets(s);
   			StrAssign(T, s);
   			 index = StrCompare(S, T);
   			if(index > 0){
   					cout<<"第一个字符串大"<<endl; 
			   }else{
			   		cout<<"第二个字符串大"<<endl; 
			   }
   	
   			break;
   			
   		case 5:
   			cout<<"请输入你要获取子串的位置和长度"<<endl;
   			cin>>n;
   			cin>>length;
   			SubString(T, S, n, length);
   			cout<<"你获取的字符串子串如下:"<<endl;
   			Print(T);
   			break;
   			
   		case 6:
		   cout<<"你输入字符串信息如下: "<<endl;
		   Print(S);
		   break;
		   
		case 7:
			char a[10];
			cout<<"请输入你要连接的两个子串"<<endl;
			gets(a);
			StrAssign(s1, a);
			getchar();
			gets(s);
			StrAssign(s2, s);
			
			Concat(T, s1, s2);
			cout<<"你连接的字符串如下: "<<endl; 
			Print(T);
			break;
		
		case 8:
			char s[10];
			char t[10];
			char v[10];
			cout<<"请输入字符串、替换字符串、替换后的字符串"<<endl;
			
			cin>>s;
			StrAssign(S, s);
			
			cin>>t;
			StrAssign(T, t);
			
			cin>>v;
			StrAssign(V, v);
			StrReplace(S, T, V);
			
			Print(S);
			break;
			 
		    
	   }
   	
	   cout<<"\n\n请输入你的操作"<<endl;
	   cout<<"		0-------------退出"<<endl;
	   cout<<"		1-------------初始化字符串"<<endl;
	   cout<<"		2-------------获取字符串的长度"<<endl;
	   cout<<"		3-------------清空字符串"<<endl;
	   cout<<"		4--------------字符串比较"<<endl;
	   cout<<"		5--------------获取子串"<<endl;
	   cout<<"		6---------------打印子串"<<endl; 
	   cout<<"		7---------------连接子串\n"<<endl;
	   cout<<"		8---------------替换子串\n"<<endl;
	   
	   cin>>n;
	   getchar();
	   
    
   	
   }
   
 
    
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

圣诞节不感冒

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

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

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

打赏作者

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

抵扣说明:

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

余额充值