/雨后晴空/ 2020-10-29 12:25 采纳率: 40%
浏览 193

函数deletestring()用于删除字符串LC第pos位起长为len的子串得到新串str,请问我的函数错在哪儿

函数deletestring()用于删除字符串LC第pos位起长为len的子串得到新串str,请问我的函数错在哪儿

int deletestring(Chunk str, Chunk &LC, int pos, int len)//删除LC中的子串得到从第i位起长度为len
{
if(pos < 0 || pos >= str.length || len < 0 || len > str.length - pos)//要删除的子串位置不合理
{
cout<<"子串删除失败!";
return 0;
}
if (str.ch)//删除后的新串
{
delete(str.ch);shn
str.ch = NULL;
}
str.ch = new char[LC.length-len+1];//分配str空间
str.ch = NULL;
int j = 0;
int k;
while(j<pos)
{
str.ch[j] = LC.ch[j];
j++;
}
k = j;
j = j+len-1;//将j移动到第len个字符后
while(k<LC.length-len)//
{
str.ch[k] = LC.ch[j];
++k;
++j;
}
int l = 0;
cout<<"删除后的新串为:";
while(l<LC.length-len)
{
cout<<str.ch[k];
l++;
}
cout<<"\n"<<"-------------"<<endl;
return 1;
}

//源码是
#include
#include
#include
using namespace std;
#define maxsize 100;
typedef struct //堆式顺序存储
{
char *ch;//串中的字符
int length;
}Chunk;

void init(Chunk str)//创建串/ */
{

str->length = 0;//串长为0
str->ch = NULL;

}

void assig(Chunk &str, char *chi)//串赋值 将串chi赋给str
{
if(str.ch)//如果原串中有字符释放原串空间
{
delete str.ch;
}
int len = 0;
char *c = chi;//字符指针c指向串chi的首地址
while(*c)
{
++len;
++c;
}//遍历chi串求长度
if(len == 0)//chi为空串
{
str.ch = NULL;
str.length = 0;
}
else
{
str.ch = new char[len+1];//为ch动态分配空间
if(str.ch == NULL)
{
str.ch = NULL;
//return 0;
}
else//ch创建成功
{
c = chi;//指针c指向字符串首地址
for(int i = 0; i<=len; i++)
{
str.ch[i] = *c;
c++;
}
str.length = len;
}
}
}

int concat(Chunk &str, Chunk str1, Chunk str2) {
if (str.ch) {
free(str.ch);
str.ch = NULL;
}
str.ch = (char*)malloc(sizeof(char)*(str1.length + str2.length + 1));
if (str.ch == NULL)
return 0;
int i = 0;
while (i < str1.length) {
str.ch[i] = str1.ch[i];
i++;
}
int j = 0;
while (j <=str2.length) {
str.ch[i + j] = str2.ch[j];
++j;
}
str.length = str1.length + str2.length;
cout<<"拼接后的字符串是:"<<endl;
for(int i=0; i<str1.length+str2.length; i++)//输出字符串str
{
cout<<str.ch[i];
}
cout<<endl;
cout<<"-------------"<<endl;
return 1;
}

int inser(Chunk &str, Chunk LA1, Chunk LB1, int pos)//将串LB1插入到LA1中pos位前
{
if(str.ch)//如果str中有字符就清空字符串
{
delete str.ch;
str.ch = NULL;//指向空
}
else//str中没有字符
{
str.ch = new char[LA1.length+LB1.length+1];
if(str.ch = NULL)
{

        return 0;
    }
    int i=0;
    while(i<pos-1)//将LA1前pos-1位赋给str
    {
        str.ch[i] = LA1.ch[i];
        i++;
    }
    int j = 0;
    while(j<LB1.length)//将LB1赋给str
    {
        str.ch[i+j] = LB1.ch[j];
        j++;
    }
    while(i<LA1.length)//LA1pos位以后的字符赋给str
    {
        str.ch[i+j] = LA1.ch[i];
        i++;
    }
    str.length = LA1.length+LB1.length;
    cout<<"字符串插入成功,新串是:";
    i = 0;
    while(i<str.length)
    {
        cout<<str.ch[i];
    }
}

}

int substring(Chunk &subsstr, Chunk str, int pos, int len)
{
if (pos < 0 || pos >= str.length || len < 0 || len > str.length - pos)
return 0;
if (subsstr.ch) {
free(subsstr.ch);
subsstr.ch = NULL;
}
if (len == 0) {
subsstr.ch = NULL;
subsstr.length = 0;
cout<<"取得子串为空!";
return 1;
}
else
{
subsstr.ch = (char*)malloc(sizeof(char)*(len + 1));
int i = pos;
int j = 0;
while (i < pos + len) {
subsstr.ch[j] = str.ch[i];
++i;
++j;
}
subsstr.ch[j] = '\0';
subsstr.length = len;
cout<<"子串为:";
j = 0;
while(j<len)
{
cout<<subsstr.ch[j];
++j;
}
cout<<"\n"<<"-------------"<<endl;
}
return 1;
}

int deletestring(Chunk str, Chunk &LC, int pos, int len)//删除LC中的子串得到从第i位起长度为len
{
if(pos < 0 || pos >= str.length || len < 0 || len > str.length - pos)//要删除的子串位置不合理
{
cout<<"子串删除失败!";
return 0;
}
if (str.ch)//删除后的新串
{
delete(str.ch);
str.ch = NULL;
}
str.ch = new char[LC.length-len+1];//分配str空间
str.ch = NULL;
int j = 0;
int k;
while(j<pos)
{
str.ch[j] = LC.ch[j];
j++;
}
k = j;
j = j+len-1;//将j移动到第len个字符后
while(k<LC.length-len)//
{
str.ch[k] = LC.ch[j];
++k;
++j;
}
int l = 0;
cout<<"删除后的新串为:";
while(l<LC.length-len)
{
cout<<str.ch[k];
l++;
}
cout<<"\n"<<"-------------"<<endl;
return 1;
}

void BF(Chunk &str, Chunk &LD, int pos)//主串LD模式串str
{
int i = pos-1;
int j = 0;
int iLD = strlen(LD.ch);//主串长度
int jstr = strlen(str.ch);//模式串长度
while(j {
if(str.ch[i] = str.ch[j])
{
++i;
++j;
}
else
{
i = i-j+1;
j = 0;
}
}
if(j>=jstr)//模式串匹配成功
{
i = i-j+1;
cout<<"模式串第一次出现的位置是第"<<i<<"位!"<<endl;
}
else
{
cout<<"没有匹配的模式串!";
}
}
int main()
{
Chunk asstr, costr, instr, sustr, destr, BFstr, strcat, strinser, strsub, strdele, s1,s2;//asstr用于串赋值,costr用于串拼接,sustr用于取子串
//instr用于串插入,destr用于串删除
char ch1[10] = "abcde";
char ch2[10] = "fghij";
int i = 0;
init(&asstr);
init(&costr);
init(&instr);
init(&sustr);
init(&destr);
init(&s1);
init(&s2);
init(&BFstr);//创建字符串
assig(asstr,ch1);//ch1赋给asstr
cout<<"复制后的字符串是:";
while(i<asstr.length)
{
cout<<asstr.ch[i];
i++;
}
cout<<"\n"<<"赋值完成!"<<endl;
assig(s1,ch1);
assig(s2,ch2);
assig(costr,"abcde");
assig(instr,"ABCDE");
assig(sustr,"0123456789");
assig(destr,"ABCDEFGHIJ");
assig(BFstr,"asdfghij");//将若干字符字符赋给字符串(功能A)
concat(strcat, s1, s2);//将字符串asstr拼接到costr后(功能B)
//inser(strinser, instr,s1 ,3);//将字符串costr插入到inser的第3位
substring(strsub,sustr, 3, 6);//取串sustr第3位起长为6的字符串
cout<<"ok1";
deletestring(strdele, destr, 3, 3);//删除串destr第3位起长度为3的子串
cout<<"ok2";
return 0;
}

//运行截图
图片说明

  • 写回答

1条回答 默认 最新

  • 小学狗喵喵叫 2020-10-29 13:46
    关注

    函数里的参数弄乱了,前面str是删除串,后面又用作新串

    评论

报告相同问题?

悬赏问题

  • ¥15 无源定位系统的时差估计误差标准差
  • ¥15 请问这个代码哪里有问题啊
  • ¥20 python--version在命令端输入结果Python is not defined怎么办?还有pip不是exe格式是不是没安装成功?
  • ¥15 通过GaussianView进行结构微调消除虚频
  • ¥15 调用transformers库
  • ¥15 由于导出的数据名字中带有/,导致Matlab打不开,怎么办?
  • ¥15 新硬盘安装的程序总是崩溃,提示遇到错误
  • ¥15 openpcdet自制数据集评估bev精度和3d精度相同
  • ¥15 excel 上下按钮 显示行
  • ¥20 云卓h12pro 数传问题