【东华大学oj】19 统计字符串个数(面向对象)

19 统计字符串个数

#include <iostream>
#include <iomanip>
#include <cstring>

using namespace std;
class MyString
{
private:
    char *str;
    static int count;
public:

    MyString(unsigned n,char c)
    {
        count++;
        str=new char[n+1];
        for(int i=0; i<n; i++)
            str[i]=c;

        str[n]='\0';
    }

    MyString(char *p)
    {
        count++;
        if(p)
        {
            str=new char[strlen(p)+1];
            strcpy(str,p);
        }
        else
        {
            str=new char[1];
            str[0]='\0';
        }
    }

    MyString()
    {
        count++;
        str=new char[1];
        str[0]='\0';
    }

    MyString(const MyString&other)
    {
        count++;
        str=new char[strlen(other.str)+1];
        strcpy(str, other.str);

    }

    ~MyString()
    {
        count--;
        delete[] str;
    }
    static int GetCount()
    {
        return count;
    }

    void ShowStr() const
    {
        cout<<str;
    }

    MyString&operator=(const MyString &other)
    {
        if(this !=&other)
        {
            delete[] str;
            str=new char[strlen(other.str)+1];
            strcpy(str,other.str);
        }
        return *this;
    }

};
int MyString::count=0;

void fun1(int n)
{
    MyString strArr[n];
    MyString *pStr;
    cout<<strArr[0].GetCount()<<endl;
}
void fun2(int n)
{
    MyString *pStr=new MyString[n];
    cout<<MyString::GetCount()<<endl;
}
void fun3(MyString &s1, MyString s2)
{
    MyString s3;
    s3=s1=s2;
    cout<<MyString::GetCount()<<endl;
    s3.ShowStr();
    cout<<endl;
}
int main( )
{
    MyString s1;
    MyString s2;
    int n;
    char charArr[20];
    int op;

    while (cin >> op)
    {
        switch (op)
        {
        case 1:
        {
            cin>>n;
            fun1(n);
            break;
        }
        case 2:
        {
            cin>>n;
            fun2(n);
            break;
        }
        case 3:
        {
            int m;
            char ch;
            cin>>m>>ch;
            s2=MyString(m,ch);
            fun3(s1,s2);
            s1.ShowStr();

            cout<<endl;

            break;
        }
        case 4:
        {
            cin >> charArr;
            s1=MyString(charArr);
            cout<<MyString::GetCount()<<endl;
            s1.ShowStr();
            cout<<endl;
            break;
        }
        }
    }

    return 0;
}

作者: Turbo时间限制: 1S章节: 类与对象

问题描述 :

实验目的:静态成员、构造函数、析构函数、等于号的使用。

实验内容:

实现一个自己的字符串类MyString,要求包含以下函数:

构造函数:

MyString(unsigned n,char c):将字符c重复n次得到字符串

MyString(char *p):根据传入的字符数组得到字符串

根据需要,还需要设计默认构造函数、拷贝构造函数等其它函数。

其中,MyString类需要提供一个静态方法GetCount,功能为获取到本程序在运行的过程中某时刻有多少个MyString的对象。

本次实验提供了部分代码,请使用以下代码调用MyString类的功能完成输入、处理、输出操作:

void fun1(int n)
{
    MyString strArr[n];
    MyString *pStr;
    cout<<strArr[0].GetCount()<<endl;
}
void fun2(int n)
{
    MyString *pStr=new MyString[n];
    cout<<MyString::GetCount()<<endl;
}
void fun3(MyString &s1, MyString s2)
{
    MyString s3;
    s3=s1=s2;
    cout<<MyString::GetCount()<<endl;
    s3.ShowStr();
    cout<<endl;
}
int main( )
{
 MyString s1;
 MyString s2;
    int n;
    char charArr[20];
 int op;

    while (cin >> op)
    {
        switch (op)
        {
            case 1:
            {
                cin>>n;
                fun1(n);
                break;
            }
            case 2:
            {
                cin>>n;
                fun2(n);
                break;
            }
            case 3:
            {
                int m;
                char ch;
                cin>>m>>ch;
                s2=MyString(m,ch);
                fun3(s1,s2);
                s1.ShowStr();

                cout<<endl;

                break;
            }
            case 4:
            {
                cin >> charArr;
                s1=MyString(charArr);
                cout<<MyString::GetCount()<<endl;
                s1.ShowStr();
                cout<<endl;
                break;
            }
        }
    }

    return 0;
}

输入说明 :

可输入多组测试数据,每组测试数据包含两行:

第一行输入一个操作的种类,

第二行输入所需要的参数:

对于第1个操作和第2个操作,第二行输入一个整数。

对于第3个操作,第二行输入一个整数及一个字符。

对于第4个操作,第二行输入一个字符串。

输出说明 :

具体输出见函数中的输出语句及输出范例。

  • 22
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ixll625

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

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

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

打赏作者

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

抵扣说明:

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

余额充值