#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
#define ElemType char
#define MAXSIZE 40
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -1
typedef struct{
ElemType data[MAXSIZE];
int length;
}Sqstring,*SqString;
//生成串
void StrAssign(SqString &s,char arr[]){
int i;
while(arr[i]!='\0'){
s->data[i]=arr[i];
i++;
}
s->length=i;
}
//销毁串
void DestroyStr(SqString &s){
/*本章的顺序串是直接采用顺序串本身来表示的,而不是顺序串指针
,它的存储空间是由操作系统管理,即由操作系统分配其空间,
并在超出作用域的情况下释放空间 */
}
//串的复制
void CopyStr(SqString &s,SqString t){
int i;
for(i=0;i<t->length;i++){
s->data[i]=t->data[i];
s->length=t->length;
}
}
//判断串相等
bool EqualStr(SqString s,SqString t){
bool same=true;
int i;
if(s->length!=t->length){
return false;
}else{
for(i=0;i<s->length;i++){
if(s->data[i]!=t->data[i]){
same=false;
break;
}
}
}
return same;
}
//求串长
int StrLength(SqString s){
return s->length;
}
//串的连接
SqString ContactStr(SqString s,SqString t){
SqString str;
int i;
str->length=s->length+t->length;
for(i=0;i<s->length;i++){
str->data[i]=s->data[i];
}
for(i=0;i<t->length;i++){
str->data[s->length+i]=t->data[i];
}
return str;
}
//求子串(顺序串逻辑位置第i开始,连续j个字符组成的子串)
SqString SubStr(SqString s){
cout<<"请输入子串起始位置和子串大小:";
int i;
int j;
cin>>i;
cin>>j;
SqString str;
str=new Sqstring;
str->length=0;
if(i<=0||i>s->length||j<0||i+j-1>s->length){
cout<<"输入有误";
return str;
}
int k;
for(k=i-1;k<i+j-1;k++){
str->data[k-i+1]=s->data[k];
}
str->length=j;
return str;
}
//子串的删除(顺序串逻辑位置第i开始,连续j个字符组成的子串)
SqString DestroyStr(SqString s){
SqString str;
int i;
int j;
cout<<"请输入删除子串起始位置和元素个数:";
cin>>i;
cin>>j;
if(i<=0||i>s->length||i+j>s->length+1){
return str;//返回空串
}
int k;
for(k=0;k<i-1;k++){
str->data[k]=s->data[k];
}
for(k=i+j-1;k<s->length;k++){
str->data[k-j]=s->data[k];
}
str->length=s->length-j;
return str;
}
//子串的插入
SqString InsStr(SqString s1,int i,SqString s2){
int j;
i=i+1;
SqString str;
str=new Sqstring;
str->length=0;
if(i<=0||i>s1->length){
return str;
}
for(j=0;j<i-1;j++){
str->data[j]=s1->data[j];
}
for(j=0;j<s2->length;j++){
str->data[i+j-1]=s2->data[j];
}
for(j=i-1;j<s1->length;j++){
str->data[s2->length+j]=s1->data[j];
}
str->length=s1->length+s2->length;
return str;
}
//子串的替换(顺序串逻辑位置第i开始,替换连续j个字符,插入t的子串)
SqString RepStr(SqString s,SqString t){
int k;
SqString str;
str->length=0;
cout<<"请输入起始位置和替换元素个数";
int i;
int j;
cin>>i;
cin>>j;
if(i<=0||i>s->length||i+j-1>s->length){
return str;
}
for(k=0;k<i-1;k++){
str->data[k]=s->data[k];
}
for(k=0;k<t->length;k++){
str->data[i+k-1]=t->data[k];
}
for(k=i+j-1;k<s->length;k++){
str->data[t->length+k-j]=s->data[k];
}
str->length=s->length-j+t->length;
return str;
}
//输出串
void PrintStr(SqString s){
int i;
cout<<"输出:";
if(s->length>0){
int i;
for(i=0;i<s->length;i++){
char c=s->data[i];
printf("%c",c);
}
}
}
int main(){
cout<<"数组:"<<"{abcde}"<<endl;
char arr[]="abcde";
SqString s;
s=new Sqstring;
cout<<"建立串s并且将数组导入s中."<<endl;
StrAssign(s,arr);
cout<<"串s";
PrintStr(s);
cout<<endl;
SqString s2;
s2=new Sqstring;
cout<<endl;
cout<<"建立串s2,s2值复制于s。"<<endl;
CopyStr(s2,s);
cout<<"串s2";
PrintStr(s2);
cout<<endl<<"判断s与s2是否相等:";
if(EqualStr(s,s2)){
cout<<"两串相等。";
} else{
cout<<"两串不等。";
}
cout<<endl<<"串s2的长度为:"<<s2->length<<endl;
cout<<"将串s与串s2相连接."<<endl<<"输出连接串";
SqString Cstring = ContactStr(s,s2);
PrintStr(Cstring);
cout<<endl<<endl<<"连接串的长度为:"<<Cstring->length<<endl;
cout<<"求连接串第2个元素开始,大小为5的子串。"<<endl;
SqString sonstring = SubStr(Cstring);
cout<<"连接串子串";
PrintStr(sonstring);
cout<<endl<<endl;
char arrt[]="12345";
SqString t;
t=new Sqstring;
StrAssign(t,arrt);
PrintStr(t);
cout<<"将{12345}插入连接串第五个元素位置之后开始"<<endl;
SqString Istring;
Istring=new Sqstring;
Istring=InsStr(Cstring,5,t);
PrintStr(Istring);
}