#include <iostream>
using namespace std;
#include<stdio.h>
#include<stdlib.h>
#include"com_h.h"
int main()
{
int choose;
SString S;
SString Y;
SString T;
SString S1;
SString S2;
SString Sub,V;
char chars[MAXLEN*2];
menu();
cout<<">>>>请输入操作数:"<<endl;
cin>>choose;
do{
switch(choose)
{
case 1:
InitString(S);
cout<<"初始化成功!"<<endl;
break;
case 2:
char str[100];
cout<<">>>>>请输入字符串:"<<endl;
scanf("%s",str);
Input(S,str);
cout<<"输入完成!"<<endl;
break;
case 3:
int n;
n=Length(S);
cout<<"串的长度为:"<<n<<endl;
break;
case 4:
if(S.length==0)
{
cout<<"该串为空串!"<<endl;
}
else
{
cout<<"输出的串为:"<<endl;
show(S);
cout<<endl;
}
break;
// case 5://比较两个串
// {
// InitString(Y);//再次初始化一个串
// cout<<"完成串的初始化!"<<endl;
// //char str[100];
// cout<<">>>>>请输入另一个字符串:"<<endl;
// scanf("%s",str);
// Input(Y,str);
// cout<<"输入完成!"<<endl;
// if(compare(S,Y)==1)
// {
// cout<<"两个串的相等!"<<endl;
// }
// else
// {
// cout<<"两个串不相等!"<<endl;
// }
// }
// break;
case 6://模式匹配BF算法
{
int pos;
InitString(T);//初始化字串
cout<<"子串初始化完成!"<<endl;
char str[100];
cout<<"请输入子串字符串:"<<endl;
scanf("%s",str);
Input(T,str);
cout<<"输入完成!"<<endl;
cout<<"请输入从主串的什么位置开始匹配:"<<endl;
cin>>pos;
Index_BF(S,T,pos);
if(Index_BF(S,T,pos)==0)
{
cout<<"匹配失败!"<<endl;
}
else
{
cout<<"匹配成功的位置是:"<<Index_BF(S,T,pos)<<endl;
cout<<"匹配成功!"<<endl;
}
}
break;
case 7://
{
calu(S);
}
break;
case 8://将所有正数排在负数的前面
{
int n;
cout<<"请输入数组的元素的个数:"<<endl;
cin>>n;
int a[n];//输入的数组元素
int b[n];//用于存放正数
int c[n];//用于存放负数或0
int k=0,j=0;//正、负空数组下表
int ct=0;
cout<<"注意>>>>若输入超过"<<n<<"个元素,则自动忽略多余元素!"<<endl;
cout<<"请输入数组元素:"<<endl;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int i=0;i<=n;i++)
{
if(a[i]>0)
{
b[k]=a[i];
k++;
}
else
{
c[j]=a[i];
j++;
}
}
for(int i=0;i<n;i++)
{
if(i<k)
{
a[i]=b[i];
}
else
{
a[i]=c[i-k];
}
}
cout<<"输出正负排序后的数组元素为:"<<endl;
printf(a,n);
}
break;
case 9://判断串是否为空
{
if(StrEmpty(S)==1)
{
cout<<"该串为空!"<<endl;
}
else
{
cout<<"该串不为空!"<<endl;
}
break;
}
case 10:
{
if(StrEmpty(S)==1)
{
cout<<"该串已经为空!"<<endl ;
}
else
{
ClearString(S);
cout<<"串的清空操作完成!"<<endl;
}
break;
}
case 11:
{
DestroyString(S);
cout<<"销毁一个串完成!"<<endl;
}
case 12:
{
int pos;
int len;
cout<<"请输入开始删除的位置:"<<endl;
cin>>pos;
cout<<"请输入删除的长度:"<<endl;
cin>>len;
if(StrDelete(S,pos,len)==0)
{
cout<<"输入数据不合理!"<<endl;
}
else
{
cout<<"删除后的字符为:"<<endl;
show(S);
cout<<endl;
}
break;
}
case 13://串的复制
InitString(T);
if(StrCopy(T , S) == ERROR)
{
cout << "主串为空,操作失败" ;
}
else
{
cout << "复制得到的串为:" ;
if(show(T)==1)
{
cout<<endl;
cout << "输出成功!" ;
}
else
{
cout << "空串!" ;
}
}
cout << endl ;
break ;
case 14:
InitString(S1);
InitString(S1);
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 15://取子串
int pos;
int len;
InitString(Sub);
cout << "请输入子串在主串的初始位置:" ;
cin >> pos ;
cout << "请输入子串的长度:" ;
cin >> len ;
if(SubString(Sub , S , pos , len) == 0)
{
cout << "输入不合法" ;
}
else
{
cout << "该子串为:" ;
if(show(Sub) == 0)
{
cout << "空串" ;
}
}
cout << endl ;
break ;
case 16://插入子串
cout << "现字符串为:" ;
if(show(S) == 0)
{
cout << "空串" ;
}
cout << endl ;
cout << "请输入要插入的子串T:" ;
cin >> chars ;
StrAssign(T , chars) ;
cout << "请输入要插入的位置:" ;
cin >> pos ;
if(StrInsert(S , pos , T) == 0)
{
cout << "输入不合法,操作失败" ;
}
else
{
cout << "插入成功" << endl ;
cout << "现字符串为:" ;
if(show(S) == 1) cout << "空串" ;
}
cout << endl ;
break ;
case 17:
InitString(V);
cout << "现字符串为:" ;
if(show(S) == 0) cout << "空串" ;
cout << endl ;
cout << "请输入被替换子串T:" ;
cin >> chars ;
StrAssign(T , chars) ;
cout << "请输入替换子串V:" ;
cin >> chars ;
StrAssign(V , chars) ;
if(Replace(S , T , V) == 0)
{
cout << "T为空,操作失败" ;
}
else
{
cout << "替换成功" << endl ;
cout << "现字符串为:" ;
if(show(S) == 0)
{
cout << "空串" ;
}
}
cout << endl ;
break ;
case 18:
cout << "请输入字符串S1:" ;
cin >> chars ;
StrAssign(S1 , chars) ;
cout << "请输入字符串S2:" ;
cin >> chars ;
StrAssign(S2 , chars) ;
cout << "两串的连接后的主串T为:" ;
if(Concat(T , S1 , S2) == ERROR) cout << "子串为空,操作失败" ;
else
{
show(T);
}
cout << endl ;
break ;
case 0:
exit(0);
break;
default:
cout<<"请输入正确的操作数!"<<endl;
break;
}
cout<<">>>>请输入操作数:"<<endl;
cin>>choose;
}while(choose!=0);
return 0;
}
#include<stdio.h>
#include<iostream>
using namespace std;
#include<string.h>
#include"com_h.h"
/*
1)定义串的定长顺序存储结构SString,在不调用任何库函数的情况下,实现串的如下算法,并设计菜单调用。
①串的输入、串的输出、串的长度、串的比较、模式匹配BF算法等。(必做)
②统计串中各个不同字符的出现频度(串中允许包含字符'A'~'Z'和'0'~'9')。(选做)
*/
//初始化
Status InitString(SString &S)
{
S.length = 0;
return OK;
}
//串的长度
Status Length(SString S)
{
return S.length;//返回串的长度
}
//输入字符串
Status Input(SString &S,char str[])
{
int i=0;
while(str[i])
{
S.str[++S.length] = str [i];//从位置一开始存储
i++;
}
// S.length = i;
return OK;
}
//串的输入
Status StrAssign(SString &S , char chars[])
{
int i = 0 ;
while(chars[i] != '\0' && i+1 < MAXLEN) // 不能超出存储范围
{
S.str[i+1] = chars[i] ; // S是从 1 开始存储的
i ++ ;
}
S.length = strlen(chars) ; // 长度
return OK ;
}
Status show(SString S)
{
for(int i=1;i<=S.length;i++)
{
cout<<S.str[i];
}
return OK;
}
比较两个串
//Status compare(SString S,SString Y)
//{
// if(S.length==Y.length)
// {
// for(int i=1;i<=S.length;i++)
// {
// if(S.str[i]!=Y.str[i])
// {
// return ERROR;
// }
// }
// return OK;
// }
// else if(S.length>Y.length)
// {
// return 1;
// }
// else
// {
// return -1;
// }
// return ERROR;
// }
int Index_BF(SString S,SString T,int pos)//pos指定从主串的第pos个位置开始查找
{
int i,j=1;
i=pos;
while(i<=S.length&&j<=T.length)//两个串均未比较到串尾
{
if(S.str[i]==T.str[j])//比较的两个字符相等 ,同时相加
{
++i;
++j;
}
else
{
i=i-j+2;//回溯到i的下一个位置
j=1;//并初始化字串比较的位置为1
}
}
if(j>T.length)
{
return i-T.length;//匹配成功
}
else
{
return ERROR;//匹配失败
}
}
//统计串的字符频度
int calu(SString S)
{
int i,b[36]={0};//总共有36个字符
for(i=1;i<=S.length;i++)
{
if('0'<=S.str[i]&&S.str[i]<'9')
{
b[int(S.str[i])-48]++;//字符‘0’的ASCII为48
}
else if('A'<=S.str[i]&&S.str[i]<='Z')
{
b[int(S.str[i])-55]++;//字符‘A’的ASCII为65 ,为55是因为数组前面有10个了
}
}
for(i=0;i<10;i++)
{
if(b[i]>0)
{
cout<<char(i+48)<<":"<<b[i]<<endl; //输出A~Z统计结果
}
}
for(i=10;i<36;i++)
{
if(b[i]>0)
{
cout<<char(i+55)<<":"<<b[i]<<endl; //
}
}
}
//输出正负数排列
void printf(int m[],int n)
{
for(int i=0;i<n;i++)
{
cout<<m[i]<<" ";
}
cout<<endl;
}
//判断串是否为空
Status StrEmpty(SString S)
{
if(S.length == 0)
{
return OK;
}
else
{
return ERROR;
}
}
//串的清空
Status ClearString(SString &S)
{
int i = 1 ;
for(i = 1 ; S.str[i] != '\0' ; i ++)
{
S.str[i] = '\0' ;
}
S.length = 0 ;
return OK ;
}
// 12串的销毁
Status DestroyString(SString &S)
{
ClearString(S) ;
// delete S.ch ;
return OK ;
}
// 在串S中从pos位置开始删除,删除长度为 len
Status StrDelete(SString &S , int pos , int len)
{
if(pos < 1 || pos > S.length-len+1 || len > S.length)
{
return ERROR ;
} // 输入不合法
else
{
int i ;
for(i = len+pos ; i <= S.length ; i ++)
{
S.str[i-len] = S.str[i] ;
}
S.length -= len ;
return OK ;
}
}
//串的复制
Status StrCopy(SString &T , SString &S) // 将串S复制给串T
{
if(StrEmpty(S))
{
return ERROR ; // S为空串
}
else
{
int i = 1 ;
for(i = 1 ; i <= S.length ; i ++)
{
T.str[i] = S.str[i] ;
}
}
T.length = S.length ;
return OK ;
}
Status StringCompare(SString S1 , SString S2)
{
int i = 1 ;
for(i = 1 ; i <= S1.length && i <= S2.length ; i ++)
{
if(S1.str[i] != S2.str[i])
{
return S1.str[i] - S2.str[i] ; // ASCII码值
}
}
// 如果它们的前面部分都相等,就比长度
return S1.length - S2.length ;
}
// 取子串
Status SubString(SString &Sub , SString S , int pos , int len)
{
if(pos < 1 || pos > S.length || pos > S.length-len+1)
{
return ERROR ; // 输入不合法
}
else
{
int i ;
for(i = 1 ; i <= len ; i ++)
Sub.str[i] = S.str[pos+i-1] ;
}
Sub.length = len ;
return OK ;
}
//串的插入
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.str[i] = S.str[i-T.length] ;
}
for(i = 1 ; i <= T.length ; i ++)
{
S.str[pos+i-1] = T.str[i] ;
}
S.length += T.length ;
return OK ;
}
// 1串的替换
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 != 0)
{
// 2、删除 T (从i位置开始,删除长度为T.length)
StrDelete(S , i , Length(T)) ;
// 3、插入V(在 i 位置插入串 V)
StrInsert(S , i , V) ;
// 4、再从i+T.length位置开始查找下一个T,重复操作
i += Length(V) ;
// 1、找到 T 依次出现的位置
i = Index_BF(S , T , i) ; // T出现的位置
}
return OK ;
}
//串的连接
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.str[i] != '\0' ; i ++)
T.str[i] = S1.str[i] ;
for(i = 1 ; S2.str[i] != '\0' ; i ++)
T.str[S1.length+i] = S2.str[i] ;
T.length = S1.length + S2.length ;
T.str[T.length+1] = '\0' ;
}
else if(S1.length <= MAXLEN) // 只能装的下S1
{
for(i = 1 ; S1.str[i] != '\0' ; i ++)
T.str[i] = S1.str[i] ;
for(i = 1 ; i <= MAXLEN-S1.length ; i ++)
T.str[S1.length+i] = S2.str[i] ;
T.str[i] = '\0' ;
T.length = MAXLEN ;
}
else if(S1.length > MAXLEN)
{
for(i = 1 ; i <= MAXLEN ; i ++)
T.str[i] = S1.str[i] ;
T.str[i] = '\0' ;
T.length = MAXLEN ;
}
return OK ;
}
void menu()
{
cout<<"\t\t\t请选择操作"<<endl;
cout<<"\t\t\t1.串的初始化"<<endl;
cout<<"\t\t\t2.输入串"<<endl;
cout<<"\t\t\t3.串的长度"<<endl;
cout<<"\t\t\t4.输出串"<<endl;
// cout<<"\t\t\t5.比较两个串(判断两个字符是否相等)"<<endl;
cout<<"\t\t\t6.模式匹配BF算法"<<endl;
cout<<"\t\t\t7.统计字符出现的频度"<<endl;
cout<<"\t\t\t8.将数组正数放负数前面"<<endl;
cout<<"\t\t\t9.判断该串是否为空"<<endl;
cout<<"\t\t\t10.清空一个串"<<endl;
cout<<"\t\t\t11.销毁一个串"<<endl;
cout<<"\t\t\t12.在串S中从pos位置开始删除,删除长度为 len"<<endl;
cout<<"\t\t\t13.串的复制"<<endl;
cout<<"\t\t\t14.比较两个串"<<endl;
cout<<"\t\t\t15.取子串"<<endl;
cout<<"\t\t\t16.插入字串"<<endl;
cout<<"\t\t\t17.替换"<<endl;
cout<<"\t\t\t18.连接两个字符串"<<endl;
cout<<endl;
}
#define MAXLEN 255//串的最大长度
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef struct{
char str[MAXLEN+1];//存储串的一维数组
int length;//串的实际长度
}SString;
extern Status InitString(SString &S);
extern Status Input(SString &S,char str[]);
extern Status Length(SString S);
extern void menu();
extern Status show(SString S);
//extern Status compare(SString S,SString Y);
extern int Index_BF(SString S,SString T,int pos);
extern int calu(SString S);
//数组元素排序
extern void printf(int m[],int n);
//书上86~87也相关操作
//串的判空
extern Status StrEmpty(SString S);
//串的清空
extern Status ClearString(SString &S) ;
//串的销毁
extern Status DestroyString(SString &S);
//在串S中从pos位置开始删除,删除长度为 len
Status StrDelete(SString &S , int pos , int len);
//串的复制
Status StrCopy(SString &T , SString &S);
//比较
Status StringCompare(SString S1 , SString S2);
Status StrAssign(SString &S , char chars[]);
//取子串
Status SubString(SString &Sub , SString S , int pos , int len);
//串的插入
Status StrInsert(SString &S , int pos , SString T);
// 串的替换
Status Replace(SString &S , SString T , SString V); // T非空
Status Concat(SString &T , SString S1 , SString S2);
字符串的基本操作。