目录
一、功能函数定义(func.cpp)
二、主函数调用(main.cpp)
三、头文件声明(before.h)
一、功能函数定义(func.cpp)
#include <iostream>
#include "before.h"
using namespace std;
status AssignString(SString &S,char *input)
{
char *p; //指示字符串
int i; //记录字符串长度
for(i = 0,p = input;*p;i++,p++); //算字符串的长度
if(i > Maxlen) return ERROR; //字符串长度超过数组最大长度
if(!i)
{
S.length = 0;
S.ch[1] = '#';
return OVERFLOW;
}//input为空
else
{
for(int j = 1;j < i+1;j ++)
{
if(input[j-1] == ' ') S.ch[j] = '#'; //注意字符数组是从0开始存储字符
else S.ch[j] = input[j-1]; //注意字符数组是从0开始存储字符
}
S.length = i;
return OK; //建立串S成功
}
}//输入串
void OutString(SString S)
{
if(S.length == 0) cout << "该串为空,无法输出!" << endl;
else
{
for(int i = 1;i < S.length+1;i ++)
{
cout << S.ch[i] << " ";
if(i%7 == 0) cout << endl;
}
cout << endl;
}
}//输出串
status LengthString(SString S)
{
return S.length;
}//返回串的长度
status CompareString(SString S,SString T)
{
for(int i = 1;i < S.length && i < T.length;i ++) if(S.ch[i] != T.ch[i]) return S.ch[i]-T.ch[i];
return S.length-T.length; //若字符都一样,则长度长的串大
}//比较串
status StrCopy(SString &T,SString S)
{
if(S.length == 0) return ERROR;
for(int i = 1;i < S.length+1;i ++) T.ch[i] = S.ch[i];
T.length = S.length;
return OK;
}//串的复制
status StrEmpty(SString S)
{
if(S.length == 0) return OK;
else return ERROR;
}//串的判空
status ClearString(SString &S)
{
S.length = 0;
return OK;
}//清空串
status Concat(SString &T,SString S1,SString S2)
{
int i;
for(i = 1;i < S1.length+1;i ++)
T.ch[i] = S1.ch[i];
for(;i < S1.length+S2.length+1;i ++)
T.ch[i] = S2.ch[i-S1.length];
T.length = S1.length+S2.length;
return OK;
}//串的连接
status SubString(SString &sub,SString S,int pos,int len)
{
if(pos+len-1 > S.length) return OVERFLOW;
if(S.length == 0) return ERROR;
for(int i = pos;i < pos+len;i ++) sub.ch[i-pos+1] = S.ch[i];
sub.length = len;
return OK;
}//查找子串
status Replace(SString &S,SString T,SString V)
{
if(V.length > T.length) return OVERFLOW;
else
{
int index = Index_BF(S,T,0);
if(index == ERROR) return ERROR;
StrDelete(S,index,T);
StrInsert(S,index,V);
}
return OK;
}//串的替换
status StrInsert(SString &S,int pos,SString T)
{
if(pos<0 || pos>S.length+1 || pos+T.length > Maxlen) return OVERFLOW;
for(int i = S.length;i >= pos;i --) S.ch[i+T.length] = S.ch[i];
for(int i = 1;i < T.length+1;i ++) S.ch[pos+i-1] = T.ch[i];