Problem G: 字符串类(II)
来源:互联网 发布:手机淘宝所在地怎么改 编辑:程序博客网 时间:2024/06/08 07:25
字符串类,嗯。禁用string头文件,嗯。没啥好说的。
Description
封装一个字符串类,用于存储字符串和处理的相关功能,支持以下操作:
1. STR::STR()构造方法:创建一个空的字符串对象。
2. STR::STR(const char *)构造方法:创建一个字符串对象,串的内容由参数给出。
3. STR::length()方法:返回字符串的长度。
4. STR::putline()方法:输出串的内容,并换行。
5. 运算符“+”和“+=”,表示两个字符串的连接运算,规则为:
c = a + b 表示串c中的字符是a和b的连接:“a+b”的结果是一个新的字符串,串a和串b的内容不变。
a += b 表示串a中的字符是a和b的连接:串b中的内容不变
-----------------------------------------------------------------------------
你设计一个字符串类STR,使得main()函数能够正确运行。
函数调用格式见append.cc。
append.cc中已给出main()函数。
-----------------------------------------------------------------------------
Invalid Word(禁用单词)错误:“string”、“vector”等被禁用。
Input
输入有若干行,每行一个字符串。
Output
每组测试数据对应输出一行,包含两部分内容,首先是一个整数,表示输入串的长度,然后是输入的字符串,两者用一个空格分开。格式见sample。
Sample Input
A123456789
Sample Output
12 Hello World!0 12 Hello World!12 Hello World!12 Hello World!10 A1234567891 A9 12345678910 123456789A1 A
HINT
下面附上ac代码:#include <iostream>#include <cstdio> //支持gets输入using namespace std;//代替string里的strlen函数int len(const char *a){ int i; for(i = 0; a[i]; i++); return i;}
//代替string里的strcpy函数void cpy(char *a,const char *b){ int i; for(i = 0; b[i]; i++) a[i] = b[i]; a[i] = 0;}
class STR{private: char *s; int l, siz;public: STR() : l(0), siz(0), s(NULL) { } STR(const char* c) { l = len(c); siz = 2 * l; s = new char[siz]; cpy(s,c); } ~STR() { delete[] s; } int length() { return l; } void putline() const { for(int i = 0; i < l; i++) cout<<s[i]; cout<<endl; } friend STR operator+(const STR&,const STR&); STR& operator+=(const STR &t) { int len = l + t.l; siz = len * 2; char *ss = new char[siz]; for(int i = 0; i < l; i++) ss[i] = s[i]; for(int j = l; j <= len; j++) ss[j] = t.s[j - l]; delete[] s; s = ss; l = len; return *this; }};STR operator+(const STR& s,const STR& t){ STR r; int i; r.l = s.l + t.l; r.siz = r.l * 2; r.s = new char[r.siz]; for(i = 0; i < s.l; i++) r.s[i] = s.s[i]; for(int j = i; j <= i + t.l; j++) r.s[j] = t.s[j - s.l]; return r;}int main(){ STR e; STR h("Hello World!"); STR he = e + h; cout << he.length() << " "; he.putline(); cout << e.length() << " "; e.putline(); cout << h.length() << " "; h.putline(); e += h; cout << e.length() << " "; e.putline(); cout << h.length() << " "; h.putline(); char s1[100001], s2[100001]; while(gets(s1) != NULL && gets(s2) != NULL) { STR str1(s1), str2(s2); STR str = str1 + str2; cout << str.length() << " "; str.putline(); cout << str1.length() << " "; str1.putline(); cout << str2.length() << " "; str2.putline(); str2 += str1; cout << str2.length() << " "; str2.putline(); cout << str1.length() << " "; str1.putline(); }}
0 0
- Problem G: 字符串类(II)
- Problem G: 字符串类(II)
- Problem B: 字符串类(II)
- A + B Problem II(利用字符串进行四则运算)
- Problem D: 数组类(II)
- Problem D: 数组类(II)
- Problem G: 动态的字符串排序
- Problem G: 逃脱(搜索)
- Problem G
- Problem G
- Problem G
- Problem G
- Problem G
- Problem G
- Problem G
- problem G
- Problem G
- Problem G
- java面向对象第6章 异常1
- R语言数据可视化:ggplot2中的qplot()函数
- 关闭回显
- 多线程中的单例模式
- Fabric架构解析
- Problem G: 字符串类(II)
- mybatis org.apache.ibatis.ognl.TokenMgrError: Lexical error at line 1, column 24. Encountered: "\u30
- Chapter 6 Interfaces and Inner Classes.md
- Flume-将数据写入动态分区表
- C++模板实现的队列多线程安全
- MATLAB简单矩阵的输入方法
- Ecshop模板开发(十八):侧边栏目分类列表
- ZOJ3212-K-Nice
- 飞行姿态计算