#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
#define MaxSize 100
typedef char ElemType;
typedef struct
{
char data[MaxSize];
int length;
} SqString;
void StrAssign(SqString &s, char cstr[])
{
int i;
for (i = 0; cstr[i] != '\0'; i++)
s.data[i] = cstr[i];
s.length = i;
}
void DestroyStr(SqString &s)
{
}
void StrCopy(SqString &s, SqString t)
{
int i;
for (i = 0; i < t.length; i++)
s.data[i] = t.data[i];
s.length = t.length;
}
bool StrEqual(SqString s, SqString t)
{
bool same = true;
int i;
if (s.length != t.length)
same = 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 Contact(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;
}
SqString SubStr(SqString s, int i, int j)
{
int k;
SqString str;
str.length = 0;
if (i <= 0 || i > s.length || j < 0 || i + j - 1 > s.length)
return str;
for (k = i - 1; k < i + j - 1; k++)
str.data[k - i + 1] = s.data[k];
str.length = j;
return str;
}
SqString InsStr(SqString s1, int i, SqString s2)
{
int j;
SqString str;
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;
}
SqString DelStr(SqString s, int i, int j)
{
int k;
SqString str;
str.length = 0;
if (i <= 0 || i > s.length || i + j > s.length + 1)
return str;
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 RepStr(SqString s, int i, int j, SqString t)
{
int k;
SqString str;
str.length = 0;
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 DisStr(SqString s)
{
int i;
if (s.length > 0)
{
for (i = 0; i < s.length; i++)
printf("%c", s.data[i]);
printf("\n");
}
}
int main(int argc, char *argv[])
{
SqString s, s1, s2, s3, s4;
printf("顺序串的基本运算如下:\n");
printf("(1)建立串s=\"abcdefghefghijklmn\"和串s1=\"xyz\"\n");
StrAssign(s, "abcdefghefghijklmn");
StrAssign(s1, "xyz");
printf("(2)输出串s:");
DisStr(s);
printf("(3)输出串s的长度:%d\n", StrLength(s));
printf("(4)在串s的第9个字符位置插入s1而产生串s2\n");
s2 = InsStr(s, 9, s1);
printf("(5)输出串s2:");
DisStr(s2);
printf("(6)删除串s的第2个字符开始的5个字符而产生串s2\n");
s2 = DelStr(s, 2, 5);
printf("(7)输出串s2:");
DisStr(s2);
printf("(8)将串s的第2个字符开始的5个字符替换成串s1而产生串s2\n");
printf("(9)输出串s2:");
s2 = RepStr(s, 2, 5, s1);
DisStr(s2);
printf("(10)提取串s的第2个字符开始的10个字符而产生串s3\n");
s3 = SubStr(s, 2, 10);
printf("(11)输出串s3:");
DisStr(s3);
printf("(12)将串s1和串s2连接起来而产生串s4\n");
s4 = Contact(s1, s2);
printf("(13)输出串s4:");
DisStr(s4);
return 0;
}
输出:
PS E:\vscode> cd "e:\vscode\C++" ; if ($?) { g++ 第6次实训.cpp -o 第6次实训 } ; if ($?) { .\第6次实训 }
第6次实训.cpp: In function 'int main(int, char**)':
第6次实训.cpp:144:38: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
StrAssign(s, "abcdefghefghijklmn");
^
第6次实训作业.cpp:145:24: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
StrAssign(s1, "xyz");
^
顺序串的基本运算如下:
(1)建立串s="abcdefghefghijklmn"和串s1="xyz"
(2)输出串s:abcdefghefghijklmn
(3)输出串s的长度:18
(4)在串s的第9个字符位置插入s1而产生串s2
(5)输出串s2:abcdefghxyzefghijklmn
(6)删除串s的第2个字符开始的5个字符而产生串s2
(7)输出串s2:aghefghijklmn
(8)将串s的第2个字符开始的5个字符替换成串s1而产生串s2
(9)输出串s2:axyzghefghijklmn
(10)提取串s的第2个字符开始的10个字符而产生串s3
(11)输出串s3:bcdefghefg
(12)将串s1和串s2连接起来而产生串s4
(11)输出串s4:xyzaxyzghefghijklmn