A:全面的MyString

描述

程序填空,输出指定结果

#include <cstdlib>
#include <iostream>
using namespace std;
int strlen(const char * s)
{    int i = 0;for(; s[i]; ++i);return i;
}
void strcpy(char * d,const char * s)
{int i = 0;for( i = 0; s[i]; ++i)d[i] = s[i];d[i] = 0;}
int strcmp(const char * s1,const char * s2)
{for(int i = 0; s1[i] && s2[i] ; ++i) {if( s1[i] < s2[i] )return -1;else if( s1[i] > s2[i])return 1;}return 0;
}
void strcat(char * d,const char * s)
{int len = strlen(d);strcpy(d+len,s);
}
class MyString
{
// 在此处补充你的代码
};int CompareString( const void * e1, const void * e2)
{MyString * s1 = (MyString * ) e1;MyString * s2 = (MyString * ) e2;if( * s1 < *s2 )return -1;else if( *s1 == *s2)return 0;else if( *s1 > *s2 )return 1;
}
int main()
{MyString s1("abcd-"),s2,s3("efgh-"),s4(s1);MyString SArray[4] = {"big","me","about","take"};cout << "1. " << s1 << s2 << s3<< s4<< endl;s4 = s3;s3 = s1 + s3;cout << "2. " << s1 << endl;cout << "3. " << s2 << endl;cout << "4. " << s3 << endl;cout << "5. " << s4 << endl;cout << "6. " << s1[2] << endl;s2 = s1;s1 = "ijkl-";s1[2] = 'A' ;cout << "7. " << s2 << endl;cout << "8. " << s1 << endl;s1 += "mnop";cout << "9. " << s1 << endl;s4 = "qrst-" + s2;cout << "10. " << s4 << endl;s1 = s2 + s4 + " uvw " + "xyz";cout << "11. " << s1 << endl;qsort(SArray,4,sizeof(MyString),CompareString);for( int i = 0;i < 4;i ++ )cout << SArray[i] << endl;//s1的从下标0开始长度为4的子串cout << s1(0,4) << endl;//s1的从下标5开始长度为10的子串cout << s1(5,10) << endl;return 0;
}

输入

输出

1. abcd-efgh-abcd-
2. abcd-
3. 
4. abcd-efgh-
5. efgh-
6. c
7. abcd-
8. ijAl-
9. ijAl-mnop
10. qrst-abcd-
11. abcd-qrst-abcd- uvw xyz
about
big
me
take
abcd
qrst-abcd-样例输入

样例输出

1. abcd-efgh-abcd-
2. abcd-
3.
4. abcd-efgh-
5. efgh-
6. c
7. abcd-
8. ijAl-
9. ijAl-mnop
10. qrst-abcd-
11. abcd-qrst-abcd- uvw xyz
about
big
me
take
abcd
qrst-abcd-

来源Guo Wei

 1     char *p;
 2 public:
 3     //构造函数
 4     MyString(const char *a){
 5         //if(p)delete[]p;
 6         p=new char[strlen(a)+1];
 7         strcpy(p,a);
 8     }
 9     MyString(){
10         p=new char[1];
11         p[0]='\0';
12     }
13     //copy
14     MyString(const MyString&s){
15         p=new char[strlen(s.p)+1];
16         strcpy(p,s.p);
17     }
18     //destruct
19     ~MyString(){if(p)delete[]p;}
20     //cout
21     friend ostream&operator<<(ostream&os,MyString &a){
22         os<<a.p;
23         return os;
24     }
25     //class+class
26     friend MyString operator+(const MyString &a,const MyString &b){
27         char *res=new char[strlen(b.p)+strlen(a.p)+1];
28         strcpy(res,a.p);
29         strcat(res,b.p);
30         return MyString(res);
31     }
32     //[]
33     char&operator[](int x){
34         return p[x];
35     }
36     //class=class
37     MyString& operator=(const MyString&a){
38         if(p)delete[]p;
39         p=new char[strlen(a.p)+1];
40         strcpy(p,a.p);
41         return *this;
42     }
43     //class=char
44     MyString& operator=(char *s){
45         if(p)delete[]p;
46         p=new char[strlen(s)+1];
47         strcpy(p,s);
48         return *this;
49     }
50     //class+=char
51     MyString operator +=(const char *s){
52         *this=*this+MyString(s);
53         return *this;
54     }
55     //char+class
56     friend MyString operator +(char *s,MyString&a){
57         return MyString(s)+a;
58     }
59     //class+char
60     MyString operator +(char*s){
61         return *this+MyString(s);
62     }
63     //<
64     friend bool operator<(MyString&a,MyString&b){
65         return (strcmp(a.p,b.p)==-1);
66     }
67     //>
68     friend bool operator>(MyString&a,MyString&b){
69         return (strcmp(a.p,b.p)==1);
70     }
71     //=
72     friend bool operator==(MyString&a,MyString&b){
73         return (strcmp(a.p,b.p)==0);
74     }
75     //()
76     char*operator()(int x,int y){
77        char *ptr=new char[y+1];
78        for(int i=0;i<y;i++){
79            ptr[i]=p[i+x];
80        }
81        ptr[y]='\0';
82        return ptr;
83     }

View Code

去掉了if(p)delete[]p;一行就过了??emmm……不知道是什么情况

以及vs里不好调……空手调bug

加个分析

 1 int main()
 2 {
 3     MyString s1("abcd-"),s2,s3("efgh-"),s4(s1);//构造函数和复制函数
 4     MyString SArray[4] = {"big","me","about","take"};
 5     cout << "1. " << s1 << s2 << s3<< s4<< endl;//输出流重载
 6     s4 = s3;
 7     s3 = s1 + s3;//加号重载 连接字符串
 8     cout << "2. " << s1 << endl;
 9     cout << "3. " << s2 << endl;
10     cout << "4. " << s3 << endl;
11     cout << "5. " << s4 << endl;
12     cout << "6. " << s1[2] << endl;//重构[] 返回引用
13     s2 = s1;//重载赋值
14     s1 = "ijkl-";//不知可否用构造函数直接解决
15     s1[2] = 'A' ;
16     cout << "7. " << s2 << endl;
17     cout << "8. " << s1 << endl;
18     s1 += "mnop";//重构+=
19     cout << "9. " << s1 << endl;
20     s4 = "qrst-" + s2;//全局函数重载+
21     cout << "10. " << s4 << endl;
22     s1 = s2 + s4 + " uvw " + "xyz";//又一个全局函数重载+ 并且重载都返MyString& 不对 临时不能返&
23     cout << "11. " << s1 << endl;
24     qsort(SArray,4,sizeof(MyString),CompareString);//重载<>==
25     for( int i = 0;i < 4;i ++ )
26     cout << SArray[i] << endl;
27     //s1的从下标0开始长度为4的子串 重载()
28     cout << s1(0,4) << endl;
29     //s1的从下标5开始长度为10的子串
30     cout << s1(5,10) << endl;
31     return 0;
32 }

View Code

B:继承自string的MyString

描述

程序填空,输出指定结果

#include <cstdlib>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class MyString:public string
{
// 在此处补充你的代码
};int main()
{MyString s1("abcd-"),s2,s3("efgh-"),s4(s1);MyString SArray[4] = {"big","me","about","take"};cout << "1. " << s1 << s2 << s3<< s4<< endl;s4 = s3;s3 = s1 + s3;cout << "2. " << s1 << endl;cout << "3. " << s2 << endl;cout << "4. " << s3 << endl;cout << "5. " << s4 << endl;cout << "6. " << s1[2] << endl;s2 = s1;s1 = "ijkl-";s1[2] = 'A' ;cout << "7. " << s2 << endl;cout << "8. " << s1 << endl;s1 += "mnop";cout << "9. " << s1 << endl;s4 = "qrst-" + s2;cout << "10. " << s4 << endl;s1 = s2 + s4 + " uvw " + "xyz";cout << "11. " << s1 << endl;sort(SArray,SArray+4);for( int i = 0;i < 4;i ++ )cout << SArray[i] << endl;//s1的从下标0开始长度为4的子串cout << s1(0,4) << endl;//s1的从下标5开始长度为10的子串cout << s1(5,10) << endl;return 0;
}

输入

输出

1. abcd-efgh-abcd-
2. abcd-
3. 
4. abcd-efgh-
5. efgh-
6. c
7. abcd-
8. ijAl-
9. ijAl-mnop
10. qrst-abcd-
11. abcd-qrst-abcd- uvw xyz
about
big
me
take
abcd
qrst-abcd-样例输入

样例输出

1. abcd-efgh-abcd-
2. abcd-
3.
4. abcd-efgh-
5. efgh-
6. c
7. abcd-
8. ijAl-
9. ijAl-mnop
10. qrst-abcd-
11. abcd-qrst-abcd- uvw xyz
about
big
me
take
abcd
qrst-abcd-

提示提示 1:如果将程序中所有 "MyString" 用 "string" 替换,那么除
了最后两条红色的语句编译无法通过外,其他语句都没有问题,而且输出和前
面给的结果吻合。也就是说,MyString 类对 string 类的功能扩充只体现在最
后两条语句上面。

提示 2: string 类有一个成员函数 string substr(int start,int 
length); 能够求从 start 位置开始,长度为 length 的子串

提示 3: C++中,派生类的对象可以赋值给基类对象,因为,一个派生
类对象,也可看作是一个基类对象(大学生是学生)。反过来则不行(学生未
必是大学生) 同样,调用需要基类对象作参数的函数时,以派生类对象作为实参,也是没有问题的来源Guo Wei

 1 public:
 2     //construct
 3     MyString():string("\0"){}
 4     MyString(const char*s):string(s){}
 5     MyString(const MyString&a):string(a){}
 6     //+
 7     MyString operator+(const char *b){
 8         string str=*this;
 9         str+=b;
10         return MyString(str.c_str());
11     }
12     MyString operator+(const MyString&a){
13         string str1=*this,str2=a;
14         str1+=a;
15         return MyString(str1.c_str());
16     }
17
18     friend MyString operator+(const char*s,const MyString &a){
19         string str1=a,str2=s;
20         str2+=str1;
21         return MyString(str2.c_str());
22     }
23     //+=
24     MyString& operator+=(const char*s){
25         *this=*this+s;
26         return *this;
27     }
28     //()
29     string operator()(int x,int y){
30         string str;
31         for(int i=0;i<y;i++)
32             str+=(*this)[x+i];
33         return MyString(str.c_str());
34     }

View Code

早点看到提示就好了……

转载于:https://www.cnblogs.com/yalphait/p/8618195.html

18.03.21 继承作业相关推荐

  1. Linux系列在线培训五月盛情开幕!!(5月9日,10日,16日,17日,23日,24日)18:30 - 21:30,

     尊敬的合作伙伴,        坚持就是胜利,您怎可放弃?微软邀您学 Linux,四月仍在继续,五月即将来临!五月日程新鲜出炉,请速速打开添加到您的日历中吧! 微软合作伙伴学院 FY17Q4向合 ...

  2. 封装和继承作业(java)(一)

    封装和继承作业 一.    选择题   1. 使用权限修饰符( B   )修饰的类的成员变量和成员方法,可以被当前包中所有类访问,也可以被它的子类(同一个包以及不同包中的子类)访问.(选择一项) A ...

  3. 2022.03.21飞扬的小鸟

    2022.03.21飞扬的小鸟 (题目来源:https://www.luogu.com.cn/problem/P1941 ) 题目描述 Flappy Bird 是一款风靡一时的休闲手机游戏.玩家需要不 ...

  4. 【预告】世界智能网联汽车大会10月18日-21日在京举行

    由工业和信息化部与北京市政府联合主办的"世界智能网联汽车大会",将于10月18日-21日在北京国家会议中心举行.本次大会的会议规模.嘉宾规格.内容深度.展商规模.观众数量和配套服务 ...

  5. AI公开课:19.03.21钱诚/雷鸣等教授或专家《寒武纪-深度学习处理器》课堂笔记以及个人感悟

    AI公开课:19.03.21钱诚/雷鸣/汪玉/侯晓林等教授或专家<寒武纪-深度学习处理器>课堂笔记以及个人感悟 导读:如果大家去创业的话,一定要看清未来的大趋势,看对赛道,当然,还有人才. ...

  6. 奥鹏南开大学18春学期计算机作业,南开18春1709、1803学期《办公自动化基础》在线作业答案.doc...

    [奥鹏][南开大学]18春(1709.1803)学期<办公自动化基础>在线作业 试卷总分:100 得分:100 第1题,"边框和底纹"对话框中的底纹,其设置单位是(). ...

  7. [18/11/29] 继承(extends)和方法的重写(override,不是重载)

    一.何为继承?(对原有类的扩充) 继承让我们更加容易实现类的扩展. 比如,我们定义了人类,再定义Boy类就只需要扩展人类即可.实现了代码的重用,不用再重新发明轮子(don't  reinvent  w ...

  8. 18春计算机辅助设计作业答案,18春福师《计算机辅助设计—MAYA》在线作业二答案...

    福师<计算机辅助设计-MAYA>在线作业二-0004 试卷总分:100    得分:0 一. 单选题 (共 31 道试题,共 62 分) 1.以下哪个应用程序不能与Maya整合( ) A. ...

  9. 东师18春计算机在线作业答案,东师计算机应用基础18春在线作业3.docx

    东师计算机应用基础18春在线作业3 1.D 2.C 3.D 4.B 5.C 一.单选题共25题,62.5分 1.I/O设备直接( ) A与主机相连接 B与CPU相连接 C与主存储器相连接 D与I/O接 ...

最新文章

  1. springboot中java泛型使用
  2. DL:关于深度学习常用数据集中训练好的权重文件(Deeplab v3、MobileNet、InceptionV3、VGG系列、ResNet、Mask R-CNN )下载地址集合(持续更新)
  3. 回溯法——打印子集树
  4. LVS负载均衡-NAT模式
  5. 去哪儿-01-EnvironmentalPre
  6. 如何pspice模型转成saber模型
  7. Eclipse 中如何设置字体大小与样式
  8. 从零基础入门Tensorflow2.0 ----八、39.3. gpu3
  9. 从零开始的异世界生活(前缀和)
  10. 王道考研操作系统笔记(第二章)附:王道考研408所有PPT和思维导图
  11. 倾斜摄影数据OSGB进入到ArcGIS平台相关问题小结
  12. 第十一章.软件工程(上)
  13. MySQL功能大全(细品)
  14. VS2010:X64和X86冲突问题
  15. 2022-2028年中国铝合金行业市场运营格局及前景战略分析报告
  16. ME525做网络收音机和学外文用了……(安卓4.4.4系统,20190817更新)
  17. QAxObject类操作Excel过程总结
  18. 计算机控制系统的输入输出信号,工业控制系统的输入与输出信号
  19. oracle cpu使用率高怎么排查解决,OracleCPU占用率较高的处理方法
  20. 利用SPSS做数据分析②之数据处理2

热门文章

  1. Java使用SWFTools转换PDF为SWF
  2. 如何将PDF转换成SWF
  3. 义隆I/O口系列EM78P153EM78P153BEM78P153B-SOP8
  4. 优质软件、网站等推荐(持续更新)
  5. hdu 1206 劲乐团
  6. 报错:attributes are not compatible with the provided attributes
  7. Fedora 22 for arndale octa board
  8. 论文笔记 |【AAAI2022】SCSNet
  9. 马斯克“超级高铁”成功测速355km|h,不过仅比我国“复兴号”快5km|h
  10. Windows常用快捷键,及控制台cmd的常用命令