静态串的存储结构以及增删改查

时间:2021-09-09 08:31:47

/**

*数据结构串的操作,有增删改查

*串的初始化以及串的赋值等操作必须注意,在新学的时候必须注意值得传递,指针的指向以及初始化的结果

*/

//操作效果图:

静态串的存储结构以及增删改查

代码:

//串的静态存储结构
/**
静态数组存储结构
typedef struct{
char str[MaxSize];
int length;
}String;
*/

/**
动态的数组存储结构
typedef struct{
char *str; //表示动态数组的名
int MaxLength;//表示动态数组元素的最大个数
int length ;//表示当前的动态数组的当前长度
}DString;
*/

/**
*定义一个静态数组结构体
*/
#include <iostream>
#define MaxSize 100
using namespace std;

typedef struct {
char str[MaxSize];
int length;
}String;

/**
*初始化操作
*/
void Initiate(String *s){
s->length = 0;
}
/**
*插入操作
*在S的i的位置下插入字串T
*/

int Insert(String *s,int i,String T){
//cout<<"length1.................."<<T.length<<endl;
if(i <0 ||i> s->length){
cout<<"参数出错哦!"<<endl;
return 0;
}else if(s->length + T.length >MaxSize){
cout<<"数组空间不足,无法插入!"<<endl;
}else{
/**
*将原串向后移位
*/
for(int j = s->length-1;j >=i;j--){
s->str[j + T.length] = s->str[j];
}
/**
*插入新的串T
*/
for(int j = 0; j < T.length;j++){
s->str[i +j] = T.str[j];
}
s->length= s->length+ T.length;//产生新的串长度
// cout<<"length2..................."<<s->length<<endl;
/* int count = 0;
for(int i = 0;i <s->length ;i++){
cout<<s->str[i];
count++;
}
cout<<"长度"<<count<<endl;
cout<<endl;*/
cout<<"插入成功!"<<endl;
return 1;
}
}

/**
*
*删除串的操作
*删除串s从位置pos到后的长度的子串值
*/

int Delete(String *s,int pos ,int len){
//cout<<"length2.................."<<s->length<<endl;
cout<<"删除过程中....."<<endl;
if(s->length<= 0){
cout<<"数组中无字符串可以删除!"<<endl;
}else if(pos <0 || len <0 || pos + len > s->length){
cout<<"参数pos或len 不合法!"<<endl;
}else{
for(int i = pos + len;i < s->length-1;i++){
s->str[i -len] = s->str[i];
}
s->length= s->length - len;
// cout<<"length2.................."<<s->length<<endl;
/* int count = 0;
for(int i = 0;i <s->length-1 ;i++){
cout<<s->str[i];
count++;
}
cout<<"长度"<<count<<endl;
cout<<endl;*/
return 1;
}
}

/**
*
*取子串操作
*取串从pos开始的len长度的串给T
*/

int SubString(String *s,int pos ,int len,String *T){
if(pos < 0|| pos+ len >s->length|| len <0){
cout<<"参数pos或len有错误!"<<endl;
return 0;
}else{
for(int i = 0 ;i <len;i++){
T->str[i] = s->str[pos +i];
}
T->length = len ;
return 1;
}

}

int main(){
/**
*初始化串
*/

int count = 0;
String *p;
Initiate(p);
char str1[100] = "Hello world!";
int length1 = strlen(str1);
//cout<<"length1<<<<<<<<<<<<<<<<<<<<<<<"<<length1<<endl;
char str2[20] = "I Love You!";
int length2 = strlen(str2);
//cout<<"length2<<<<<<<<<<<<<<<<<<<<<<<"<<length2<<endl;
String myString1 = {"I Love You",length1},myString2 = {"All world!",length2};
cout<<"初始化的长度"<<p->length<<endl;
cout<<"插入操作,将str1插入str2中,从0位置开始:"<<endl;
Insert(&myString2,0,myString1);
cout<<"长度:"<<myString1.length<<"\t"<<myString2.length<<endl;
for(int i = 0;i <myString2.length;i++){
cout<<myString2.str[i];
count++;
}
cout<<endl;
cout<<"长度:"<<count<<"\t"<<myString2.length<<endl;
cout<<endl;

cout<<"删除:"<<endl;
Delete(&myString2,16 ,2);
count = 0;
cout<<"删除后的字符串:"<<endl;
for(int i = 0;i <myString2.length-1;i++){
cout<<myString2.str[i];
count++;
}
cout<<endl;
cout<<"删除后长度:"<<count<<"\t"<<myString2.length<<endl;
cout<<endl;
cout<<"取字符串:"<<endl;
count = 0;
String *p1;
p1 = (String *)malloc(sizeof(String));
SubString(&myString2,5 ,14,p1);
cout<<"未被获取的字符串是:"<<endl;
for(int i = 0;i < myString2.length - p1->length-1;i++){
cout<<myString2.str[i];
}
cout<<endl;

cout<<"获取的字符串是:"<<endl;
for(int i = 0;i <10;i++){
cout<<p1->str[i];
count++;
}
cout<<endl;
cout<<"满字符串是:"<<endl;
for(int i = 0;i < myString2.length-1 ;i++){
cout<<myString2.str[i];
}
cout<<endl;
cout<<"所取字符串长度:"<<count<<"主串的长度:"<<myString2.length<<endl;

return 0;
}