zoukankan      html  css  js  c++  java
  • Problem A: 字符串类(I)

    Problem A: 字符串类(I)

    Time Limit: 1 Sec  Memory Limit: 128 MB
    Submit: 6383  Solved: 3131
    [Submit][Status][Web Board]

    Description

    封装一个字符串类,用于存储字符串和处理的相关功能,支持以下操作:

    1. STR::STR()构造方法:创建一个空的字符串对象。

    2. STR::STR(const char *)构造方法:创建一个字符串对象,串的内容由参数给出。

    3. STR::length()方法:返回字符串的长度。

    4. STR::putline()方法:输出串的内容,并换行。

    -----------------------------------------------------------------------------

    你设计一个字符串类STR,使得main()函数能够正确运行。

    函数调用格式见append.cc。

    append.cc中已给出main()函数。

    -----------------------------------------------------------------------------

    Invalid Word(禁用单词)错误:“string”、“vector”等被禁用。

     

    Input

    输入有若干行,每行一个字符串。

    Output

    每组测试数据对应输出一行,包含两部分内容,首先是一个整数,表示输入串的长度,然后是输入的字符串,两者用一个空格分开。格式见sample。

    Sample Input

    A
    123456789

    Sample Output

    0 
    12 Hello World!
    1 A
    9 123456789
    

      

    HINT

     

    Append Code

    int main()
    {
        STR e;
        STR h("Hello World!");
        char s[100001];
        cout << e.length() << " ";
        e.putline();
        cout << h.length() << " ";
        h.putline();
        while(gets(s) != NULL)
        {
            STR str(s);
            cout << str.length() << " ";
            str.putline();
        }
    }
    

      

    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    #include <cstdio>
    using namespace std;
    int getlength(const char *arr)
    {
        int l=0;
        for(int i=0; arr[i]!=''; i++)
            l++;
        return l;
    }
    void copystring(char *arr, const char *brr)
    {
        int l=getlength(brr);
        for(int i=0; i<l; i++)
            arr[i]=brr[i];
            arr[l]='';
    }
    class STR
    {
    public :
        char *arr;
        int l;
        STR():arr(NULL),l(0){}
        STR(const char *brr)
        {
            l=getlength(brr);
            arr=new char[l+1];
            copystring(arr,brr);
        }
        int length()
        {
            return l;
        }
        void putline()
        {
            for(int i=0; i<l; i++)
                cout<<arr[i];
            cout<<endl;
        }
        ~STR()
        {
            delete []arr;
        }
    };
    int main()
    {
        STR e;
        STR h("Hello World!");
        char s[100001];
        cout << e.length() << " ";
        e.putline();
        cout << h.length() << " ";
        h.putline();
        while(gets(s) != NULL)
        {
            STR str(s);
            cout << str.length() << " ";
            str.putline();
        }
    }
    

      

    作者:7oDo

    仅供参考,请勿抄袭。

    Hang Hang Hang !!!

  • 相关阅读:
    iOS-触摸事件、手势识别、摇晃事件、耳机线控
    iOS-App生命周期
    Foundation框架—时间处理对象NSDate
    Kali linux渗透测试的艺术 思维导图
    数据结构_二叉树遍历
    数据结构_数值转换
    <转载>Mac下,使用sshpass让iterm2支持多ssh登录信息保存
    <转载>iTerm2使用技巧
    Maven打包编译找不到com.sun.crypto.provider.SunJCE类
    MySQL自动设置create_time和update_time
  • 原文地址:https://www.cnblogs.com/Jie-Fei/p/9126028.html
Copyright © 2011-2022 走看看