没有名字的c 2022-01-01 14:47 采纳率: 88.2%
浏览 73
已结题

可不可以解释一下这串代码,整个内容看不大懂。😭

在头文件“MiniString.h”中声明了一个字符串类String,将成员字符串中第pos个位置后的字符替换为指定字符串(函数参数)。若成员字符串为“123456789”,参数字符串为“abcde”;当pos为3时,替换后的成员字符串为“123abcde9”;当pos为15时,替换后的成员字符串为“123456789abcde”。请将源程序文件“MiniString.cpp”中的成员函数replace补充完整,补充内容填写在”//333”与”//666”两行之间。

对于main函数中的测试数据,程序的输出应该是:

 原字符串为:123456789

 替换后的字符串为:123abcde9

 原字符串为:123456789

 替换后的字符串为:123abcdefghijk

 原字符串为:123456789

 替换后的字符串为:123456789abcde

[程序]

// MiniString.h

#include

using namespace std;

class String{

public:

     String(char*s=NULL);                       // 用参数初始化str

     String(String&);                           // 拷贝构造函数

     ~String();                                 // 释放动态空间

     void replace(char *p,int pos);             // 将str中第pos个位置后的字符替换为参数字符串

     void show();                               // 输出str

private:

     char *str;                                  // 成员字符串

};

void writeToFile(const string); // 不用考虑此语句的作用

// MiniString.cpp

#include"MiniString.h"

#pragma warning(disable:4996)

String::String(char*s)

{

     if(s==NULL)

               str=NULL;

     else{

               str=new char[strlen(s)+1];

               strcpy(str,s);

     }

}

String::String(String & t)

{

     if(t.str==NULL)

               str=NULL;

     else{

               str=new char[strlen(t.str)+1];

               strcpy(str,t.str);

     }

}

String::~String()

{

     if(str)delete[]str;

}

void String::replace(char *p,int pos)

{

     char *s,*s1;

     int n,i;

     if(pos<=strlen(str)){

               n=pos+strlen(p);

               if(n<=strlen(str)){

                        s1=str+pos;

                        while(*p){

                                 *s1=*p;

                                 s1++;

                                 p++;

                        }

               }

               else{

                        //********333********

                       

                       

                        //********666********

               }

     }

     else{

               s=new char[strlen(str)+strlen(p)+1];

               strcpy(s,str);

               strcat(s,p);

               delete []str;

               str=s;

     }

}

void String::show()

{

     if(str)cout<<str<<endl;

}

//main.cpp

#include"MiniString.h"

int main()

{

     String test1("123456789"),test2(test1),test3(test1);

     cout<<"原字符串为:";

     test1.show();

     test1.replace("abcde",3);

     cout<<"替换后的字符串为:";

     test1.show();

     cout<<"原字符串为:";

     test2.show();

     test2.replace("abcdefghijk",3);

     cout<<"替换后的字符串为:";

     test2.show();

     cout<<"原字符串为:";

     test3.show();

     test3.replace("abcde",15);

     cout<<"替换后的字符串为:";

     test3.show();

     system("pause");

     return 0;

正确答案:

s=new char[n+1];

i=0;

while(i<pos){

s[i]=str[i];

i++;

}

s[i]='\0';

strcat(s,p);

delete []str;

str=s

  • 写回答

1条回答 默认 最新

  • 书山客 2022-01-01 15:03
    关注
    
                            s1=str+n;
                            while(*p){
                                     *s1=*p;
                                     s1++;
                                     p++;
                            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月12日
  • 已采纳回答 1月4日
  • 创建了问题 1月1日

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么