Problem B: 字符串类(II)

Problem B: 字符串类(II)

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 3652   Solved: 1577
[ Submit][ Status][ Web Board]

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

Append Code

#include <iostream>
#include <iomanip>
#include <cstdio>
using namespace::std;

int len(const char *a)
{
    int i;
    for(i=0;a[i];i++);
    return i;
}
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:
    int len_, siz_;
    char *s_;
public:
    STR() : len_(0), siz_(0), s_(NULL) { }
    STR(const char* str) : len_(len(str)), siz_(2 * len_)
    {
        s_ = new char[siz_];
        cpy(s_, str);
    }
    ~STR() { delete[] s_; }

    void putline() const
    {
        for(int i=0;i<len_;i++)
            cout<<s_[i];
        cout << endl;
    }

    int length() { return len_; }

    friend STR operator+(const STR& s, const STR& t)
    {
        STR res;
        res.len_ = s.len_ + t.len_;
        res.siz_ = res.len_ * 2;
        res.s_ = new char[res.siz_];
        int i;
        for(i = 0; i < s.len_; i++)
            res.s_[i] = s.s_[i];
        for(int j = i; j <= i + t.len_; j++)
            res.s_[j] = t.s_[j - s.len_];
        return res;
    }

    STR& operator+=(const STR& s)
    {
        int len = len_ + s.len_;
        siz_ = len * 2;
        char *str = new char[siz_];
        for(int i = 0; i < len_; i++)
            str[i] = s_[i];
        for(int i = len_; i <= len; i++)
            str[i] = s.s_[i - len_];
        delete[] s_;
        s_ = str;
        len_ = len;
        return *this;
    }
};


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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Usher_Ou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值