当前位置: 首页 > news >正文

10.2 作业

1. 编程

#include <iostream>
#include <string.h>
using namespace std;class MyString
{
private:int size;int capacity;char *str;public:// 构造函数MyString();MyString(const char *);// 拷贝构造MyString(const MyString &);// 赋值运算符MyString &operator=(const MyString &);MyString &operator=(const char *);// 长度int len();// 拼接void strcat(const MyString &other);MyString const operator+(const MyString &) const;// 比较int strcmp(const MyString &);// 输入void in();// 输出void out();// 内存管理~MyString();
};MyString::MyString() : size(0), capacity(128), str(new char[capacity]) { memset(str, 0, capacity); }
MyString::MyString(const char *s) : size(0), capacity(128), str(new char[capacity])
{memset(str, 0, capacity);while (*s != 0 && size < capacity){str[size] = *s;size++;s++;}
}MyString::MyString(const MyString &other) : size(other.size), capacity(other.capacity), str(new char[capacity])
{memset(str, 0, capacity);for (int i = 0; i < size; ++i){str[i] = other.str[i];}
}MyString &MyString::operator=(const MyString &other)
{if (this == &other) return *this;delete []str;size = other.size;capacity = other.capacity;str = new char[capacity];memset(str, 0, capacity);for (int i = 0; i < size; ++i){str[i] = other.str[i];}return *this;
}MyString &MyString::operator=(const char *s)
{size = 0;memset(str, 0, capacity);while (*s != 0 && size < capacity){str[size] = *s;size++;s++;}
}int MyString::len()
{return size;
}void MyString::strcat(const MyString &other)
{int tmp = size + other.size;for (int i = 0; size < tmp && size < capacity; ++size, ++i){str[size] = other.str[i];}
}MyString const MyString::operator+(const MyString &other) const
{MyString ret(*this);ret.strcat(other);return ret;
}int MyString::strcmp(const MyString &other)
{for (int i = 0; i < size && i < other.size; ++i){if (str[i] != other.str[i]){return str[i] - other.str[i];}}if (size == other.size)return 0;else if (size > other.size)return str[other.size];else if (size < other.size)return other.str[size];
}void MyString::in()
{char s[128];cin >> s;size = 0;memset(str, 0, capacity);while (s[size] != 0 && size < capacity){str[size] = s[size];size++;}
}void MyString::out()
{for (int i = 0; i < size; ++i){cout << str[i];}cout << endl;
}MyString::~MyString()
{delete[] str;
}int main()
{//无参构造 MyString s1;s1.out();//有参构造MyString s2("hello");s2.out();//拷贝构造MyString s3(s2);s3.out();//输入s3.in();s3.out();//拷贝赋值s3 = s2;s3.out();//赋值运算符s3 = " world";s3.out();//拼接MyString s4 = s2 + s3;s4.out();//长度cout << s4.len() << endl;//比较s1 = "abc", s2 = "abd";cout << s1.strcmp(s2) << endl;cout << s2.strcmp(s1) << endl;
}

2. 刷题

http://www.dtcms.com/a/435921.html

相关文章:

  • 用c 做网站可以吗wordpress下一页代码
  • JAVA之判断数字是否存在
  • 字符串操作与方法深度解析
  • 在线ppt制作网站有哪些百度站长查询工具
  • 门户网站html模板下载制作的网站
  • 南京公司网站建设怎么收费南充楼盘网
  • 郑州seo网站有优化wordpress 企业网站教程
  • 如何做网站使用手册wordpress分类目录发不了文章
  • 如何推广微信视频号网站排名优化软件
  • 保定 网站企业信息公示管理系统山东
  • 【操作系统】cpu执行程序流程
  • CMake编译命令格式区别解析
  • 建设中学校园网站的目的出名的网络公司有哪些
  • 免费手机端网站模板口碑营销的前提及好处有哪些
  • 赣州网站优化公司秒玩大型游戏的网站
  • 网站设计制作步骤胶州网站建设平台
  • 浦口区城乡建设集团网站网站建设 商业价值
  • 欧洲数据保护法律与法规核心概念
  • 做快递网站难吗教育培训机构管理系统
  • 百度 网站描述拘束 wordpress
  • 新圩做网站公司注册城乡规划师成绩查询
  • TISAX 汽车行业信息安全标准
  • 网站维护包括的内容简单的dw制作网页步骤
  • 电子商城网站模板wordpress xmlrcp
  • Typora v1.11.6便携中文版_Markdown编辑器
  • 广州网站建设策划想学网络运营怎么开始
  • 嘉兴英文网站建设建设部网站查询
  • QoS之流量监管配置方法
  • 领码方案:超越授权的边界——重塑数字时代权限管控的艺术与科学
  • 怎么通过做网站来赚钱济南正宗网站建设报价