实验6:Problem H: 字符串类(II)

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

A 123456789

Sample Output

12 Hello World! 0 12 Hello World! 12 Hello World! 12 Hello World! 10 A123456789 1 A 9 123456789 10 123456789A 1 A

HINT

 

Append Code

 1 #include <bits/stdc++.h>
 2 using namespace::std;
 3 
 4 class STR
 5 {
 6 public:
 7     STR() : len_(0), siz_(0), s_(NULL) { }
 8     STR(const char* str) : len_(strlen(str)), siz_(2 * len_)
 9     {
10         s_ = new char[siz_];
11         strcpy(s_, str);
12     }
13     ~STR() { delete[] s_; }
14 
15     void putline() const
16     {
17         for(int i=0;i<len_;i++)
18             cout<<s_[i];
19         cout << endl;
20     }
21 
22     int length() { return len_; }
23     int len_, siz_;
24     char *s_;
25 
26     friend STR operator+(const STR&, const STR&);
27 
28     STR& operator+=(const STR& s)
29     {
30         int len = len_ + s.len_;
31         siz_ = len * 2;
32         char *str = new char[siz_];
33         for(int i = 0; i < len_; i++)
34             str[i] = s_[i];
35         for(int i = len_; i <= len; i++)
36             str[i] = s.s_[i - len_];
37         delete[] s_;
38         s_ = str;
39         len_ = len;
40         return *this;
41     }
42 };
43 
44 STR operator+(const STR& s, const STR& t)
45 {
46     STR res;
47     res.len_ = s.len_ + t.len_;
48     res.siz_ = res.len_ * 2;
49     res.s_ = new char[res.siz_];
50     int i;
51     for(i = 0; i < s.len_; i++)
52         res.s_[i] = s.s_[i];
53     for(int j = i; j <= i + t.len_; j++)
54         res.s_[j] = t.s_[j - s.len_];
55     return res;
56 }
57 int main()
58 {
59     STR e;
60     STR h("Hello World!");
61     STR he = e + h;
62     cout << he.length() << " ";
63     he.putline();
64     cout << e.length() << " ";
65     e.putline();
66     cout << h.length() << " ";
67     h.putline();
68     e += h;
69     cout << e.length() << " ";
70     e.putline();
71     cout << h.length() << " ";
72     h.putline();
73 
74     char s1[100001], s2[100001];
75     while(gets(s1) != NULL && gets(s2) != NULL)
76     {
77         STR str1(s1), str2(s2);
78         STR str = str1 + str2;
79         cout << str.length() << " ";
80         str.putline();
81         cout << str1.length() << " ";
82         str1.putline();
83         cout << str2.length() << " ";
84         str2.putline();
85         str2 += str1;
86         cout << str2.length() << " ";
87         str2.putline();
88         cout << str1.length() << " ";
89         str1.putline();
90     }
91 }
向代码最深处出发~!
原文地址:https://www.cnblogs.com/auto1945837845/p/5384104.html