鱼C论坛

 找回密码
 立即注册
查看: 3236|回复: 3

[技术交流] 字符更换

[复制链接]
发表于 2022-10-15 10:25:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
前段时间,看到坛里有说指定字串中的字符改字母的,今天写了一个,大家看看符合了要求没有.



sshot-1.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-10-15 10:34:36 | 显示全部楼层
这是所有的方法,定义了生成对像的总数统计,深复制 和 赋值重载.
另外,把每个对像的总统计数的当前数,复制一份到自己,做成链表的话,应当非常方便.

  1. #ifndef MYSTRING_H
  2. #define MYSTRING_H

  3. class mystring
  4. {
  5.         static int sum;
  6.         char *p;
  7.         int plen,n;
  8.         public:
  9.                 mystring();
  10.                 ~mystring();
  11.                 mystring(const char*);
  12.                 mystring(const mystring&);
  13.                 void operator=(const mystring&);
  14.                
  15.                 void disp();
  16.                 void rename(const char*);
  17.                 void rename(int index,char b);
  18.         protected:
  19. };

  20. #endif
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-15 10:39:13 | 显示全部楼层
这是实现,使用了cstring中的方法: strlen,strcpy两个,来免除一次次对char数组的遍历,
只要把对char数组的下标控制到不越界,即可.考虑到如果在游戏系统中对初始化玩家姓名,生平,等等,对字符串的要求,暂时只做到这样应该能正常的工作吧?
欢迎大家补充.


  1. #include "mystring.h"
  2. #include <cstring>
  3. #include <stdio.h>
  4. int mystring:: sum=0;
  5. mystring::mystring():plen(7)
  6. {
  7.         p=new char[plen];
  8.         strcpy(p,"deault");
  9.         sum++;
  10.         n=sum;
  11. }

  12. mystring::~mystring()
  13. {
  14.         printf("string %d %p "%s" was deleted (destroy)\n",n,p,p);
  15.         sum--;
  16.         delete [] p;
  17. }

  18. mystring :: mystring(const char* a)
  19. {
  20.         plen = strlen(a)+1;
  21.         p= new char [ plen ];
  22.         strcpy(p,a);
  23.         sum++;
  24.         n=sum;
  25. }

  26. mystring :: mystring(const mystring&a)
  27. {
  28.         delete []p;
  29.         plen= a.plen;
  30.         p=new char [ plen ];
  31.         strcpy(p,a.p);
  32.         sum++;
  33.         n=sum;
  34. }

  35. void mystring::operator=(const mystring&a)
  36. {
  37.         delete []p;
  38.         plen= a.plen;
  39.         p=new char [ plen ];
  40.         strcpy(p,a.p);
  41. }

  42. void mystring::disp()
  43. {
  44.         printf("string %d %p %s (disp)\n",n,p,p);
  45. }

  46. void mystring:: rename(const char*a)
  47. {
  48.         delete [] p;
  49.         plen = strlen(a) + 1;
  50.         p=new char[plen];
  51.         strcpy(p,a);
  52. }

  53. void mystring:: rename(int index,char b)
  54. {
  55.         if(index<0)
  56.         {
  57.                 printf("string %d min length is 0 "%c" can not replace "%c"\n",
  58.                                 n,b,p[0]);
  59.         }
  60.         if(index<plen)
  61.         {
  62.                 char t=p[index];
  63.                 p[index] = b;
  64.                 printf("string %d %p "%c" was replaced to "%c"\n",n,p,t,b);
  65.         }
  66.         else if(index==plen)
  67.         {
  68.                 printf("string %d rear is space "%c" can not replace "%c"\n",
  69.                                                 n,b,p[plen]);
  70.         }
  71.         else
  72.         {
  73.                 printf("string %d max length is %d can not use "%c" to replace\n",
  74.                                                                 n,plen,b);
  75.         }
  76. }

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-15 11:10:41 | 显示全部楼层
本帖最后由 howzyao 于 2022-10-15 11:11 编辑

大家对比一下,这两个功效一样,但少了点返回,是返回的好呢,还是直接在调用者的this中改了不返回好呢?

个人偏向到不返回,本质上,调用者总归是改好了嘛,何必返回.

  1. mystring& operator=(const mystring&);
  2.                 //void operator=(const mystring&);
复制代码

  1. mystring& mystring:: operator=(const mystring&a)
  2. {
  3.         delete []p;
  4.         plen= a.plen;
  5.         p=new char [ plen ];
  6.         strcpy(p,a.p);
  7.         return *this;
  8. }

  9. //void mystring::operator=(const mystring&a)
  10. //{
  11. //        delete []p;
  12. //        plen= a.plen;
  13. //        p=new char [ plen ];
  14. //        strcpy(p,a.p);
  15. //}
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-23 12:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表