目录
一、main
#include <iostream>
//#include <bits/stdc++.h>
#include "com.h"
using namespace std ;
int main(int argc, char** argv)
{
int choice , pos , len ;
SString S , Sub , T , V , S1 , S2 ;
char chars[MAXLEN*2] ;
do
{
/*
串的输入 串的输出
串的长度 串是否为空
串的子串 串的复制
串的插入 串的删除
模式匹配 串的替换
串的清空 串的销毁
串的比较 串的连接
*/
printf("\t\t\t==================================================\n");
printf("\t\t\t| |\n");
printf("\t\t\t| 串的基本操作 |\n");
printf("\t\t\t| |\n");
printf("\t\t\t|=================================================|\n");
printf("\t\t\t| 请选择要操作的命令 |\n");
printf("\t\t\t|-------------------------------------------------|\n");
printf("\t\t\t| |\n");
printf("\t\t\t| 1.串的输入 2.串的输出 |\n");
printf("\t\t\t| 3.串的长度 4.串是否空 |\n");
printf("\t\t\t| 5.串的子串 6.串的复制 |\n");
printf("\t\t\t| 7.串的插入 8.串的删除 |\n");
printf("\t\t\t| 9.模式匹配 10.串的替换 |\n");
printf("\t\t\t| 11.串的清空 12.串的销毁 |\n");
printf("\t\t\t| 13.串的比较 14.串的连接 |\n");
printf("\t\t\t| 0.退出整个系统 |\n");
printf("\t\t\t| |\n");
printf("\t\t\t==================================================\n");
printf("选择(0---14):\t");
cin >> choice ;
switch(choice)
{
case 0 : //0.退出整个系统
cout << "退出成功" ;
return 0 ;
case 1 : //1.串的输入
cout << "请输入字符串:" ;
cin >> chars ;
if(StrAssign(S , chars) == OK) cout << "输入成功" ;
cout << endl ;
cout << "现字符串为:" ;
if(PrintStr(S) == ERROR) cout << "空串" ;
cout << endl ;
break ;
case 2 : // 2.串的输出
cout << "现字符串为:" ;
if(PrintStr(S) == ERROR) cout << "空串" ;
cout << endl ;
break ;
case 3 : //3.串的长度
cout << "现字符串为:" ;
if(PrintStr(S) == ERROR) cout << "空串" ;
cout << endl ;
cout << "串的长度为:" << StrLength(S) ;
break ;
case 4 : //4.串是否空
cout << "现字符串为:" ;
if(PrintStr(S) == ERROR) cout << "空串" ;
cout << endl ;
if(StrEmpty(S)) cout << "该串为空串" ;
else cout << "该串不为空" ;
cout << endl ;
break ;
case 5 : // 5.串的子串
cout << "现主串为:" ;
if(PrintStr(S) == ERROR) cout << "空串" ;
cout << endl ;
cout << "请输入子串在主串的初始位置:" ;
cin >> pos ;
cout << "请输入子串的长度:" ;
cin >> len ;
if(SubString(Sub , S , pos , len) == ERROR) cout << "输入不合法" ;
else
{
cout << "该子串为:" ;
if(PrintStr(Sub) == ERROR) cout << "空串" ;
}
cout << endl ;
break ;
case 6 : //6.串的复制
cout << "现主串为:" ;
if(PrintStr(S) == ERROR) cout << "空串" ;
cout << endl ;
if(StrCopy(T , S) == ERROR) cout << "主串为空,操作失败" ;
else
{
cout << "复制得到的串T为:" ;
if(PrintStr(T) == ERROR) cout << "空串" ;
}
cout << endl ;
break ;
case 7 : //7.串的插入
cout << "现字符串为:" ;
if(PrintStr(S) == ERROR) cout << "空串" ;
cout << endl ;
cout << "请输入要插入的子串T:" ;
cin >> chars ;
StrAssign(T , chars) ;
cout << "请输入要插入的位置:" ;
cin >> pos ;
if(StrInsert(S , pos , T) == ERROR) cout << "输入不合法,操作失败" ;
else
{
cout << "插入成功" << endl ;
cout << "现字符串为:" ;
if(PrintStr(S) == ERROR) cout << "空串" ;
}
cout << endl ;
break ;
case 8 : //8.串的删除
cout << "现字符串为:" ;
if(PrintStr(S) == ERROR) cout << "空串" ;
cout << endl ;
cout << "请输入删除初位置:" ;
cin >> pos ;
cout << "请输入删除长度" ;
cin >> len ;
if(StrDelete(S ,pos , len) == ERROR) cout << "输入不合法,操作失败" ;
else
{
cout << "删除成功" << endl ;
cout << "现字符串为:" ;
if(PrintStr(S) == ERROR) cout << "空串" ;
}
cout << endl ;
break ;
case 9 : //9.模式匹配
cout << "现字符串为:" ;
if(PrintStr(S) == ERROR) cout << "空串" ;
cout << endl ;
cout << "请输入子串T :" ;
cin >> chars ;
StrAssign(T , chars) ;
cout << "请输入T在S中第一次出现的位置pos:" ;
cin >> pos ;
if(Index_BF(S , T , pos) == ERROR) cout << "输入有误" ;
else if(Index_BF(S , T , pos) == OVERFLOW) cout << "没在S中查寻到T" ;
else cout << "子串T在主串S中的位置为:" << Index_BF(S , T , pos) ;
cout << endl ;
break ;
case 10 : // 10.串的替换
cout << "现字符串为:" ;
if(PrintStr(S) == ERROR) cout << "空串" ;
cout << endl ;
cout << "请输入被替换子串T:" ;
cin >> chars ;
StrAssign(T , chars) ;
cout << "请输入替换子串V:" ;
cin >> chars ;
StrAssign(V , chars) ;
if(Replace(S , T , V) == ERROR) cout << "T为空,操作失败" ;
else
{
cout << "替换成功" << endl ;
cout << "现字符串为:" ;
if(PrintStr(S) == ERROR) cout << "空串" ;
}
cout << endl ;
break ;
case 11 : // 11.串的清空
if(ClearString(S) == OK) cout << "该串已被清空" ;
cout << endl ;
break ;
case 12 : // 12.串的销毁
if(DestroyString(S) == OK) cout << "该串已被销毁" ;
cout << endl ;
break ;
case 13 : //13.串的比较
cout << "请输入字符串S1:" ;
cin >> chars ;
StrAssign(S1 , chars) ;
cout << "请输入字符串S2:" ;
cin >> chars ;
StrAssign(S2 , chars) ;
cout << "两串的比较结果为:" ;
if(StringCompare(S1 , S2) == 0) cout << "S1 = S2" ;
else if(StringCompare(S1 , S2) > 0) cout << "S1 > S2" ;
else cout << "S1 < S2" ;
cout << endl ;
break ;
case 14 : //14.串的连接
cout << "请输入字符串S1:" ;
cin >> chars ;
StrAssign(S1 , chars) ;
cout << "请输入字符串S2:" ;
cin >> chars ;
StrAssign(S2 , chars) ;
cout << "两串的连接后的主串T为:" ;
if(Concat(T , S1 , S2) == ERROR) cout << "子串为空,操作失败" ;
else PrintStr(T) ;
cout << endl ;
break ;
// Status Replace(SString &S , SString T , SString V)
// SubString(SString &Sub , SString S , int pos , int len)
// printf("\t\t\t| 5.串的子串 6.串的复制 |\n");
// printf("\t\t\t| 7.串的插入 8.串的删除 |\n");
// printf("\t\t\t| 9.模式匹配 10.串的替换 |\n");
// printf("\t\t\t| 11.串的清空 12.串的销毁 |\n");
// printf("\t\t\t| 13.串的比较 14.串的连接 |\n");
// printf("\t\t\t| 15.退出整个系统 |\n");
/*
case 4 : //4.串的比较
SString S1 , S2 ;
cout << "请输入字符串S1:" ;
cin >> chars ;
StrAssign(S1 , chars) ;
cout << "请输入字符串S2:" ;
cin >> chars ;
StrAssign(S2 , chars) ;
cout << "两串的比较结果为:" ;
if(StringCompare(S1 , S2) == 0) cout << "S1 = S2" ;
else if(StringCompare(S1 , S2) > 0) cout << "S1 > S2" ;
else cout << "S1 < S2" ;
cout << endl ;
break ;
case 5 : //5.模式匹配BF算法
SString s , t ;
cout << "请输入主串S :" ;
cin >> chars ;
StrAssign(s , chars) ;
cout << "请输入子串T :" ;
cin >> chars ;
StrAssign(t , chars) ;
cout << "请输入T在S中第一次出现的位置pos:" ;
cin >> pos ;
if(Index_BF(s , t , pos) == ERROR) cout << "输入有误" ;
else if(Index_BF(s , t , pos) == OVERFLOW) cout << "没在S中查寻到T" ;
else cout << "子串T在主串S中的位置为:" << Index_BF(s , t , pos) ;
cout << endl ;
break ;*/
default :
cout << "输入不合法,请重新输入" ;
cout << endl ;
}
}while(choice != 0) ;
return 0;
}
二、function
#include <stdlib.h>
#include "com.h"
#include <bits/stdc++.h>
using namespace std;
// 函数
//1.串的输入
Status StrAssign(SString &S , char chars[])
{
int i = 0 ;
while(chars[i] != '\0' && i+1 < MAXLEN) // 不能超出存储范围
{
S.ch[i+1] = chars[i] ; // S是从 1 开始存储的
i ++ ;
}
S.length = strlen(chars) ; // 长度
return OK ;
}
// 2.串的输出
Status PrintStr(SString S)
{
if(S.length == 0)
{
return ERROR ; //空串
}
int i = 1 ;
for(i = 1 ; i <= S.length ; i ++)
cout << S.ch[i] ;
return OK ;
}
//3.串的长度
int StrLength(SString S)
{
return S.length ;
}
//4.串是否为空
bool StrEmpty(SString S)
{
if(S.length == 0) return true ;
else return false ;
}
// 5.取子串
Status SubString(SString &Sub , SString S , int pos , int len)
{
if(pos < 1 || pos > S.length || pos > S.length-len) return ERROR ; // 输入不合法
int i ;
for(i = 1 ; i <= len ; i ++)
Sub.ch[i] = S.ch[pos+i-1] ;
Sub.length = len ;
return OK ;
}
//6串的复制
Status StrCopy(SString &T , SString &S) // 将串S复制给串T
{
if(StrEmpty(S)) return ERROR ; // S为空串
int i = 1 ;
for(i = 1 ; i <= S.length ; i ++)
T.ch[i] = S.ch[i] ;
T.length = S.length ;
return OK ;
}
// 7串的插入
Status StrInsert(SString &S , int pos , SString T)
{
int i ;
if(pos < 1 || pos > S.length + 1) return ERROR ; // pos值不合法
for(i = S.length + T.length ; i >= pos + T.length ; i --)
S.ch[i] = S.ch[i-T.length] ;
for(i = 1 ; i <= T.length ; i ++)
S.ch[pos+i-1] = T.ch[i] ;
S.length += T.length ;
return OK ;
}
// 8串的删除
// 在串S中从pos位置开始删除,删除长度为
Status StrDelete(SString &S , int pos , int len)
{
if(pos < 1 || pos > S.length-len+1 || len > S.length) return ERROR ; // 输入不合法
int i ;
for(i = len+pos ; i <= S.length ; i ++)
S.ch[i-len] = S.ch[i] ;
S.length -= len ;
return OK ;
}
//9.模式匹配BF算法
Status Index_BF(SString S , SString T , int pos)
{
if(pos < 1 || pos > S.length) return ERROR ; //输入有误
int i = pos , j = 1 ;
while(i <= S.length && j <=T.length)
{
if(S.ch[i] == T.ch[j])
{
i ++ ; // 后移
j ++ ;
}
else
{
i = i - j + 2 ;
j = 1 ;
}
}
if(j > T.length) return i - T.length ;
else return OVERFLOW ;
}
// 10.串的替换
Status Replace(SString &S , SString T , SString V) // T非空
{
if(StrEmpty(T)) return ERROR ; // T为空
int i = 1 ;
i = Index_BF(S , T , i) ; // T出现的位置
while(i != OVERFLOW && i != ERROR)
{
// 2、删除 T (从i位置开始,删除长度为T.length)
StrDelete(S , i , StrLength(T)) ;
// 3、插入V(在 i 位置插入串 V)
StrInsert(S , i , V) ;
// 4、再从i+T.length位置开始查找下一个T,重复操作
i += StrLength(V) ;
// 1、找到 T 依次出现的位置
i = Index_BF(S , T , i) ; // T出现的位置
}
return OK ;
}
// 11串的清空
Status ClearString(SString &S)
{
int i = 1 ;
for(i = 1 ; S.ch[i] != '\0' ; i ++)
S.ch[i] = '\0' ;
S.length = 0 ;
return OK ;
}
// 12串的销毁
Status DestroyString(SString &S)
{
ClearString(S) ;
// delete S.ch ;
return OK ;
}
//13.串的比较
// 返回 = 0 , S1 = S2
// 返回 < 0 , S1 < S2
// 返回 > 0 , S1 > S2
Status StringCompare(SString S1 , SString S2)
{
int i = 1 ;
for(i = 1 ; i <= S1.length && i <= S2.length ; i ++)
{
if(S1.ch[i] != S2.ch[i])
return S1.ch[i] - S2.ch[i] ; // ASCII码值
}
// 如果它们的前面部分都相等,就比长度
return S1.length - S2.length ;
}
// 14.串的连接
Status Concat(SString &T , SString S1 , SString S2)
{
if(S1.length == 0 && S2.length == 0) return ERROR ;
int i , j ;
if(S1.length + S2.length <= MAXLEN) // 存的下
{
for(i = 1 ; S1.ch[i] != '\0' ; i ++)
T.ch[i] = S1.ch[i] ;
for(i = 1 ; S2.ch[i] != '\0' ; i ++)
T.ch[S1.length+i] = S2.ch[i] ;
T.length = S1.length + S2.length ;
T.ch[T.length+1] = '\0' ;
}
else if(S1.length <= MAXLEN) // 只能装的下S1
{
for(i = 1 ; S1.ch[i] != '\0' ; i ++)
T.ch[i] = S1.ch[i] ;
for(i = 1 ; i <= MAXLEN-S1.length ; i ++)
T.ch[S1.length+i] = S2.ch[i] ;
T.ch[i] = '\0' ;
T.length = MAXLEN ;
}
else if(S1.length > MAXLEN)
{
for(i = 1 ; i <= MAXLEN ; i ++)
T.ch[i] = S1.ch[i] ;
T.ch[i] = '\0' ;
T.length = MAXLEN ;
}
return OK ;
}
三、com
#ifndef _FUNC_H
#define _FUNC_H
#define OK 0
#define ERROR -1
#define OVERFLOW -2
typedef int Status ;
#define MAXLEN 255
typedef struct{
char ch[MAXLEN+1] ;
int length ;
}SString;
extern Status StrAssign(SString &S , char chars[]) ;
extern Status PrintStr(SString S) ;
extern int StrLength(SString S) ;
extern bool StrEmpty(SString S) ;
extern Status SubString(SString &Sub , SString S , int pos , int len) ;
extern Status StrCopy(SString &T , SString &S) ;
extern Status StrInsert(SString &S , int pos , SString T) ;
extern Status StrDelete(SString &S , int pos , int len) ;
extern Status Index_BF(SString S , SString T , int pos) ;
extern Status Replace(SString &S , SString T , SString V) ;
extern Status ClearString(SString &S) ;
extern Status DestroyString(SString &S) ;
extern Status StringCompare(SString S1 , SString S2) ;
extern Status Concat(SString &T , SString S1 , SString S2) ;
#endif