这是放假之前写的很乱的代码,主要解决字符串操作的问题,如串赋值、求串长、求子串等等
#include <cstdio>
#include <malloc.h>
#include <math.h>
#include <string.h>
#include <cstdlib>
#include <iostream>
using namespace std;
typedef struct HString//定义字符串结构
{
char *ch;
int length;
}HString;
int StrAssign(HString *T,char *chars)//字符串赋值
{
int i,j;
if(T->ch)
free(T->ch);
i=strlen(chars);
if(!i)
{
T->ch=NULL;
T->length=0;
}
else
T->ch=(char*)malloc(i*sizeof(char));
if(!T->ch)//分配空间失败
exit(OVERFLOW);
for(j=0;j<i;j++)
T->ch[j]=chars[j];
T->length=i;
return 1;
}
int Length(HString T)//求字符串的长度
{
return T.length;
}
void InitString(HString *T)//初始化一个字符串
{
T->length=0;
T->ch=NULL;
}
void StrPrint(HString T)//所有的操作后的字符放到了T.ch中
{
int i;
for(i=0;i<T.length;i++)
printf("%c",T.ch[i]);
printf("\n");
}
int StrCopy(HString *T,HString t)//字符串复制
{
int i;
if(T->ch)
free(T->ch);
T->ch=(char*)malloc((t.length)*sizeof(char));
if(!T->ch)
exit(OVERFLOW);
for(i=0;i<t.length;i++)
T->ch[i]=t.ch[i];
T->length=t.length;
return 1;
}
void StrCat(HString *s,HString t,HString r)//字符串连接
{
int i;
if(s->ch)
free(s->ch);
s->length=t.length+r.length;
s->ch=(char *)malloc(s->length*sizeof(char));
if(!s->ch)
exit(OVERFLOW);
for(i=0;i<t.length;i++)
s->ch[i]=t.ch[i];
for(i=0;i<r.length;i++)
s->ch[t.length+i]=r.ch[i];
}
int SubString(HString *Sub,HString t,int pos,int len)//求子串
{//sub返回串s中第pos 个字符起长度为len的字符串;
int i;
if(pos<1||pos>t.length||len<0||len>t.length-pos+1)
exit(OVERFLOW);
if(Sub->ch)
free(Sub->ch);
if(!len)
{
Sub->ch=NULL;
Sub->length=0;
}
else
{
Sub->ch=(char *)malloc(len*sizeof(char));
//if(!Sub->ch)
//exit(OVERFLOW);
for(i=0;i<=len-1;i++)
{
Sub->ch[i]=t.ch[pos-1+i];
}
Sub->length=len;
}
return 0;
}
int StrComp(HString t,HString r)
{//串比较
if(t.length>r.length)
return 1;
else
return 0;
}
int StrCompare(HString s,HString t)
{//比较两串是否相等,返回两串的差值
int i;
for(i=0;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;
}
int StrIndex(HString s,HString t,int pos)
{//求串的定位,若t是母串s的子串,返回t在s首次出现的位置,否则返回0
int n,m,i;
HString sub;
InitString(&sub);
if(pos>0)
{//其实更应该注意参数的合法性,这样才能保证程序的健壮性
n=Length(s);
m=Length(t);
i=pos;
while(i<=n-m+1)//note:下标的取值范围为什么我自己不能想出来
{//从母串的第i个位置取得长度为m的字串
SubString(&sub,s,i,m);
if(StrCompare(sub,t)!=0)//将模式串与子串一一比较
++i;//why is not i++?
else
return i;
}
}
return 0;
}
int StrInsert(HString *s,int pos,HString t)
{//插入串为t,原串为s,t在s的第pos 位置之前插入
//程序执行起来有点毛病
int i;
if(pos<1||pos>s->length+1)
printf("pos 位置不合法!!!:");
if(t.length)
{
s->ch=(char*)realloc(s->ch,(s->length+t.length)*sizeof(char));
if(!s->ch)
exit(OVERFLOW);
for(i=s->length-1;i>=pos-1;--i)
s->ch[i+t.length]=s->ch[i];
for(i=0;i<t.length;i++)
s->ch[pos-1+i]=t.ch[i];
s->length=t.length;
}
return 0;
}
int StrEmpty(HString s)
{
if(s.length==0)
printf("此串为空串");
else
printf("此串不为空串!");
}
void StrDelete(HString *s,int pos,int len)
{
int i;
if(s->length<pos+len-1)
{
printf("参数出错");
exit(OVERFLOW);
}
for(i=pos-1;i<s->length;i++)
s->ch[i]=s->ch[i+len];
s->length-=len;
s->ch=(char*)realloc(s->ch,s->length*sizeof(char));
}
int StrReplace(HString *s,HString t,HString v)
{//从第几个位置替换:用V替换主串S中出现的所有与T相等的不重叠的子串
int i=1;// 从串S的第一个字符起查找串T
if(StrEmpty(t))
printf("出错了,");
do
{
i=StrIndex(*s,t,i);//结果为从上一个I之后找到子串T的位置
if(i)//串S存在串T
{
StrDelete(s,i,Length(t));//删除该串T
StrInsert(s,i,v);//在原串T的位置插入串V
i+=Length(v);
}
}while(i);
return i;
}
int StrClear(HString *s)
{
int i=0;
/*while(s->ch[i])
{
free(s->ch[i]);
i++;
}
s->length=0;*/
//这是我一开始写的的代码,不能放弃任何学习的机会
while(s->ch)
{
free(s->ch);
s->ch=NULL;// 这个很容易忘记
}
s->length=0;
return 1;
}
void StrDestory()
{
}
int menu_select()
{
char c;
int n;
printf("\n\n");
printf(" 实现字符串操作的的部分功能! \n");
printf(" ");
printf("\n ********** 于波&&学号2012070152 ***\n\n");
printf(" *卍卍卍卍卍卍卍卍卍卍卍卍卍卍卍卍卍卍卍卍卍* \n");
printf(" ㊣ ㊣ \n");
printf(" ㊣ 菜 单 ㊣ \n");
printf(" ㊣ ㊣ \n");
printf(" ㊣ ㊣ \n");
printf(" ㊣ (1)--- 初始化 ㊣ \n");
printf(" ㊣ (2)--- 串赋值 ㊣ \n");
printf(" ㊣ (3)--- 求串长 ㊣ \n");
printf(" ㊣ (4)--- 求子串 ㊣ \n");
printf(" ㊣ (5)--- 串连接 ㊣ \n");
printf(" ㊣ (6)--- 串删除 ㊣ \n");
printf(" ㊣ (7)--- 串比较 ㊣ \n");
printf(" ㊣ (*0*)-退出系统 ㊣ \n");
printf(" ㊣ (*9*)-显示菜单 ㊣ \n");
printf(" ㊣ 注意:目前仅支持字母,其他...*\n");
printf(" *========= 请在上述序号中选择一个并输入:==* ");
}
int main()
{
int i,m,n;
int flags=1;
char c,p[200];
HString t,s,r,Sub,k,a;
system("cls");
InitString(&r);
InitString(&a);
do{
if(flags==1)
menu_select();
scanf("%d",&i);
switch(i){
//HString类型必须初始化
case 1:{
InitString(&t);
flags=0;
printf("初始化完成,请继续选择:\n");
break;
}
case 2:{
printf("请输入一串字符串,少于200字\n");
cin.get();//消除换行符带来的影响
cin.getline(p,200);
StrAssign(&t,p);//现在t有内容
printf("串t为:");
StrPrint(t);
printf("串赋值完成,请继续选择:\n");
flags=0;
// system("pause");
break;
}
case 3:{
i=Length(t);
printf("串t长为%d\n",i);
//system("pause");
printf("求串长已完成,请继续选择\n");
break;
}
case 4:{
printf("请输入t的第i个字符,长度为len的,注意参数范围:\n");
printf("1<=i<=串长,0<=len-i+1\n");
scanf("%d%d",&m,&n);
printf(" t的子串sub为");
SubString(&Sub,t,m,n);//假设从t字符串的第5个位置起长度为5的S //ubString
StrPrint(Sub);
printf("求子串已完成,请继续");
flags=0;
//system("pause");
break;
}
case 5:{
printf("请输入一个字符串,少于200字\n");
InitString(&s);
cin.get();//消除换行符带来的影响
cin.getline(p,200);
StrAssign(&s,p);//现在t有内容
printf("串t为:");
StrPrint(t);
StrCat(&t,t,s);
printf("连接后的串为:");
StrPrint(t);
printf("求子串已完成,请继续");
flags=0;
//system("pause");
}
case 6:{ printf("请输入删除串的位置和长度\n");
printf("%d %d",m,n);
StrDelete(&t,m,n);
StrPrint(t);
flags=0;
break;
}
case 9:{
flags=1;
}
// printf("%d\ns的长度为",i);
// i=Length(s);
// printf("%d\n下面是比较s和t:",i);
/* i=StrCompare(s,r);
printf("%d\n",i);
printf("%d是串r从第一个字符起,和串s相同的第一\n",StrIndex(s,r,1));
printf("串r插入到串t中从四个位置,总长度为r 的大小:\n");
// StrPrint(t);
//StrPrint(r);
StrInsert(&t,4,r);
StrPrint(t);
InitString(&k);
printf("串k是否空");
StrEmpty(k);
StrAssign(&k,e);
printf("字符串e删除从第3个位置开始,长度为3,剩下的字符串为:\n");
StrReplace(&s,r,t);
StrPrint(s);
StrClear(&s);
printf("字符串s为");
StrPrint(s);*/
}
}while(i!=0);
return 0;
}