版权声明:本文参考了严蔚敏的《 数据结构》。未经作者允许,严禁用于商业出版,否则追究法律责任。网络转载请注明出处,这是对原创者的起码的尊重!!!
1 串的主要操作
计算串长、复制、比较、取子串、匹配
2 代码
#include<iostream>
using namespace std;
#define ok 1
#define error 0
#define yes 1
#define no 0
#define NULL 0
struct str //定义串,就是动态顺序表的定义
{
char *ch;
int length;
};
void init(str &str1)
{
str1.ch = NULL;
str1.length = 0;
}
int getlength(str str1)//取串的长度
{
return str1.length;
}
int getarraylength(char str1[])//取数组的长度
{
int len = 0;
while (str1[len] != NULL)
len++;
return len;
}
int strcopy1(str str1, str &str2)//将str1复制给str2
{
if (str2.ch != NULL)
{
free(str2.ch);
str2.ch = NULL;
str2.length = 0;
}
if (str1.length == 0)
{
str2.ch = NULL;
str2.length = 0;
return ok;
}
else
{
str2.ch = (char*)malloc(sizeof(char)*(str1.length + 1));
if (str2.ch == NULL)
return error;
else
{
for (int i = 0; i <= str1.length; i++)//要把最后的"\0"复制过去
{
str2.ch[i] = str1.ch[i];
}
str2.length = str1.length;
return ok;
}
}
}
int strcopy2(char str1[], str &str2)//将数组str1复制给str2
{
if (str2.ch)
{
free(str2.ch);
str2.ch = NULL;
str2.length = 0;
}
int len = getarraylength(str1);
if (len == 0)
{
str2.ch = NULL;
str2.length = 0;
return ok;
}
else
{
str2.ch = (char*)malloc(sizeof(char)*(len + 1));
if (str2.ch == NULL)
return error;
else
{
for (int i = 0; i <= len; i++)//要把最后的"\0"复制过去
{
str2.ch[i] = str1[i];
}
str2.length = len;
return ok;
}
}
}
int strcompare(str str1, str str2)//比较str1,str2的大小
{
for (int i = 0; i < str1.length&&i < str2.length; i++)
if (str1.ch[i] != str2.ch[i])
return str1.ch[i] - str2.ch[i];
return str1.length - str2.length;
}
int strcontact(str str1, str str2, str &str3)//将str2链接在str1后面
{
int i,j;
if (str3.ch != NULL)
{
free(str3.ch);
str3.length = 0;
}
str3.ch = (char*)malloc(sizeof(char)*(str1.length + str2.length + 1));
if (str3.ch == NULL)
return error;
else
{
for (i = 0; i < str1.length; i++)//不要把最后的"\0"复制过去
str3.ch[i] = str1.ch[i];
for (j = 0; j <= str2.length;j++)//要把最后的"\0"复制过去
str3.ch[j+i] = str2.ch[j];
str3.length = str1.length + str2.length;
return ok;
}
}
int substring(str str1, int pos,int len , str &substr)//求str1的从位置pos开始长度为len的子串substr
{
if (pos < 1 || len < 0 || len + pos - 1>str1.length || pos>str1.length)
return error;
if (substr.ch != NULL)
{
free(substr.ch);
substr.ch = NULL;
substr.length = 0;
}
substr.ch = (char*)malloc(sizeof(char)*(len + 1));
if (substr.ch == NULL)
return error;
else
{
for (int i = pos - 1; i < pos + len - 1; i++)
substr.ch[i-pos + 1] = str1.ch[i];
substr.ch[len] = NULL;
substr.length = len;
return ok;
}
}
int match(str str1, int &pos, str substr)//查找substr在str1中的位置
{
int i = 0, j = 0, k = 0;
if (substr.length > str1.length)
{
pos = 0;
return error;
}
while (i < str1.length && j < substr.length)
if (str1.ch[i] != substr.ch[j])
{
i = ++k;
j = 0;
}
else
{
i++; j++;
}
if (j == substr.length)
{
pos = i - j + 1;
return ok;
}
else
return n
}
void main() //演示串的各种操作
{
int pos = 3, len = 4, pos1;
char a[] = "i love you";
char b[] = " forever!";
str string1, string2, string3;
init(string1); init(string2); init(string3);
if (strcopy2(b, string1))
if (strcopy1(string1, string2))
if (strcopy2(a, string1))
{
if (strcontact(string1, string2, string3))
cout <<"“"<<string1.ch << "”"<< "和" << "“" << string2.ch << "”" << "连接起来是" << "“" << string3.ch << "”" << endl;
else
cout << "连接失败" << endl;
if (substring(string1, pos, len, string3))
{
cout << "“" << string1.ch << "”" << "从位置"<<pos<<"开始长度为"<<len<<"的子串为" << "“" << string3.ch << "”" << endl;
if (match(string1, pos1, string3))
cout <<"匹配成功! " << "“" << string3.ch << "”" << "在" << "“" << string1.ch << "”" << "中的位置是:" << pos1 << endl;
else
cout << "串匹配失败" << endl;
int c = strcompare(string1, string3);
if (c == 0)
cout << "“" << string1.ch << "”" << "=" << "“" << string3.ch << "”" << endl;
else if (c<0)
cout << "“" << string1.ch << "”" << "<" << "“" << string3.ch << "”" << endl;
else
cout << "“" << string1.ch << "”" << ">" << "“" << string3.ch << "”" << endl;
}
else
cout << "取子串失败" << endl;
}
else
cout << "数组复制到串失败" << endl;
else
cout << "串复制到串失败" << endl;
else
cout << "数组制到串失败" << endl;
}
3、运行结果
版权声明:本文参考了严蔚敏的《 数据结构》。未经作者允许,严禁用于商业出版,否则追究法律责任。网络转载请注明出处,这是对原创者的起码的尊重!!!