- HString.h 头文件
#include <stdio.h>
#include <stdlib.h>
//定义函数返回的类型
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
//定义数据元素的类型为字符型
typedef int Status;
typedef char ElemType;
//定义单链表的存储结构
typedef struct {
char* ch;
int length;
}HString;
Status StrAssign(HString &T, char* chars) {
// if(T.ch != NULL) {
// free(T.ch);
// }
int i;
char* c;
for(i=0,c=chars; *c; i++, c++);
if(!i) {
T.ch = NULL;
T.length = 0;
} else {
if(!(T.ch=(char*)malloc(i*sizeof(char)))) {
exit(OVERFLOW);
}
for(int j=0; j<i; j++) {
T.ch[j] = chars[j];
}
T.length = i;
}
return OK;
}
int StrLength(HString S) {
return S.length;
}
Status SubString(HString &Sub, HString S, int pos, int len) {
if(pos<1 || pos>S.length || len<0 || len>S.length-pos+1) {
return ERROR;
}
// if(Sub.ch != NULL) {
// free(Sub.ch);
// }
if (!len) {
Sub.ch = NULL;
Sub.length = 0;
} else {
Sub.ch = (char*)malloc(len*sizeof(char));
for (int j = 0,i = pos-1; j<len; j++, i++) {
Sub.ch[j] = S.ch[i];
Sub.length = len;
}
}
return OK;
}
Status Concat(HString &T, HString S1, HString S2) {
if(!(T.ch = (char*)malloc((S1.length+S2.length)*sizeof(char)))) {
exit(OVERFLOW);
}
for(int i = 0; i<S1.length; i++) {
T.ch[i] = S1.ch[i];
}
T.length = S1.length + S2.length;
for(int i = S1.length, j = 0; i<T.length; i++, j++) {
T.ch[i] = S2.ch[j];
}
return OK;
}
Status StrDelete(HString S, HString &s2, int pos, int len) {
HString temp;
if(!(temp.ch = (char*)malloc((S.length-len)*sizeof(char)))) {
exit(OVERFLOW);
}
temp.length = S.length-len;
int i;
for(i=0; i<pos-1; i++) {
temp.ch[i] = S.ch[i];
}
for(int j=i+len; j<S.length; j++,i++) {
temp.ch[i] = S.ch[j];
}
s2 = temp;
return OK;
}
int Index_KMP(HString S, HString T, int pos,int next[]) {
//利用模式串T的next函数求T在主串中第pos个字符之后的位置的KMP算法。
int i = pos;
int j = 0;
while (i<S.length&&j<T.length)
{
if (j == -1 || S.ch[i] == T.ch[j]) { ++i; ++j; }
else
{
j = next[j];
}
}
if (j >= T.length)return i - T.length+1; //匹配成功
else return 0; //输出-1失败
}
exp-1 主程序:测试基本功能
#include "HString.h"
int main() {
HString s;
HString s1;
printf("%s\n", "(1)建立串");
char str1[] = "abcdefghxyzhijklmn";
char str2[] = "xyz";
StrAssign(s, str1);
StrAssign(s1, str2);
printf("%s%s\n", "(2)输出串s:", s.ch);
printf("%s%d\n", "(3)输出串s的长度:", StrLength(s));
//将串s分为两个子串
HString s_sub1;
HString s_sub2;
SubString(s_sub1, s, 1, 8);
SubString(s_sub2, s, 9, StrLength(s)-8);
printf("%s\n", "(4)在串s的第9个字符位置插入串s1而产生s2");
//将s1先与前部分的子串连接,得到temp,再将temp和后部分子串连接,从而实现插入操作
HString s2;
HString temp;
Concat(temp, s_sub1, s1);
Concat(s2, temp, s_sub2);
printf("%s%s\n", "(5)输出串s2:", s2.ch);
printf("%s\n", "(6)删除s的第2个字符开始的5个字符而产生串s2");
StrDelete(s, s2, 2, 5);
printf("%s%s\n", "(7)输出串s2:", s2.ch);
printf("%s\n", "(8)删除s的第2个字符开始的5个字符替换成s1而产生串s2");
HString s2_sub1;
HString s2_sub2;
SubString(s2_sub1, s2, 1, 1);
SubString(s2_sub2, s2, 2, s2.length-1);
Concat(temp, s2_sub1, s1);
Concat(s2, temp, s2_sub2);
printf("%s%s\n", "(9)输出串s2:", s2.ch);
printf("%s\n", "(10)提取串s的第2个字符开始的10个字符而产生s3");
HString s3;
SubString(s3, s, 2, 10);
printf("%s%s\n", "(11)输出串s3:", s3.ch);
printf("%s\n", "(12)将串s1和s2连接起来而产生串s4");
HString s4;
Concat(s4, s1, s2);
printf("%s%s\n", "(13)输出串s4:", s4.ch);
int next[10] = { -1,0,0,1,1,2,0,1 };
int leng = Index_KMP(s, s1, 1, next);
printf("%s%d\n", "(14)输出串s1在主串s中的位置,如不存在输出0, 首元素的位置为:",leng);
return 0;
}
- 测试结果
- exp-2 主程序:利用该结构进行字符加密解密
#include "HString.h"
HString A;
HString B;
char a[] = "abcdefghijklmnopqrstuvwxyz";
char b[] = "ngzqtcobmuhelkpdawxfyivrsj";
HString EnCrypt(HString p) {
HString enCrypt_p;
enCrypt_p.ch = (char*)malloc(p.length*sizeof(char));
enCrypt_p.length = p.length;
for (int i=0; i<p.length; i++) {
for (int j=0; j<A.length; j++) {
if (p.ch[i] == A.ch[j]) {
enCrypt_p.ch[i] = B.ch[j];
}
}
}
return enCrypt_p;
}
HString UnEncrypt(HString q) {
HString unCrypt_q;
unCrypt_q.ch = (char*)malloc(q.length*sizeof(char));
unCrypt_q.length = q.length;
for (int i=0; i<q.length; i++) {
for (int j=0; j<B.length; j++) {
if (q.ch[i] == B.ch[j]) {
unCrypt_q.ch[i] = A.ch[j];
}
}
}
return unCrypt_q;
}
int main() {
HString p;
StrAssign(A, a);
StrAssign(B, b);
char aa[] = "encrypt";
StrAssign(p, aa);
HString enCrypt_p = EnCrypt(p);
printf("%s%s\n", "p串被加密后: ", enCrypt_p.ch);
HString unCrypt_p = UnEncrypt(enCrypt_p);
printf("%s%s\n", "q串被解密后: ", unCrypt_p.ch);
return 0;
}
- 测试结果