用C++实现字符串类MString

     C++面试中遇到的题目,下面是我编译通过的代码,有不足之处,可以交流指出。

mstring.h

#ifndef MSTRING_H
#define MSTRING_H
#include <iostream>

class MString
{
    friend std::ostream& operator<<(std::ostream& os,  MString& str);
    friend std::istream& operator>>(std::istream& os,  MString& str);
public:
    MString(const char*str = NULL);
    MString(const MString&str);
    ~MString(void);
    MString& operator=(const MString& other);
    MString& operator+(const MString&other);
    bool operator==(const MString& other);
    int len(void);

private:
    char* m_data;
};



#endif // MSTRING_H

mstring.cpp:

#include "mstring.h"
#include<stdio.h>
#include<string.h>

MString::MString(const char *str)
{
    if(str == NULL)
    {
        this->m_data=NULL;
    }
    else
    {
        this->m_data = new char[strlen(str)+1];
        strcpy(this->m_data,str);
    }
}

MString::MString(const MString &str)
{
    if(str.m_data == NULL)
    {
        this->m_data=NULL;
    }
    else
    {
        this->m_data = new char[strlen(str.m_data)+1];
        strcpy(this->m_data,str.m_data);
    }
}

MString::~MString()
{
    if(this->m_data)
    {
        delete[] this->m_data;
        this->m_data=NULL;
    }
}

MString &MString::operator=(const MString &other)
{
    if(this!=&other)
    {
        if(this->m_data!=NULL)
        {
            delete[] this->m_data;
            this->m_data=NULL;
        }

        if(other.m_data==NULL)
        {
            this->m_data=NULL;
        }
        else
        {
            this->m_data = new char[strlen(other.m_data)+1];
            strcpy(this->m_data,other.m_data);
        }
    }

    return *this;
}

MString& MString::operator+(const MString &other)
{
    MString *newString =new MString();

    if(other.m_data==NULL)
    {
        return *this;
    }
    else if(this->m_data ==NULL)
    {
        *newString = other;
    }
    else
    {
        newString->m_data= new char[strlen(other.m_data)+strlen(this->m_data)+1];
        strcpy(newString->m_data,this->m_data);
        strcat(newString->m_data,other.m_data);
    }

    return *newString;
}

bool MString::operator==(const MString &other)
{
    if(this->m_data!=other.m_data)
    {
        if(this->m_data==NULL||other.m_data==NULL)
        {
            return false;
        }
        else if(strlen(this->m_data)!=strlen(other.m_data))
        {
            return false;
        }
        else
        {
            return strcmp(this->m_data,other.m_data)?false:true;
        }
    }
    return true;
}

int MString::len()
{
    return strlen(m_data);
}


std::ostream& operator<<(std::ostream& os,   MString& str)
{
    os<<str.m_data;
    return os;
}

std::istream& operator>>(std::istream& os,   MString& str)
{
    os>>str.m_data;
    return os;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我来帮您用Java完成这道题目。 您可以将原始数据定义为一个字符串型,然后通过字符串分割和字符转换等操作,将其转换为整数型的数组,再根据题目要求实现三个功能。 下面是Java代码实现: ``` public static void main(String[] args) { String mstring = "56,,73,66,81,,,91,,114,77,"; String[] strArr = mstring.split(","); int[] intArr = new int[strArr.length]; for (int i = 0; i < strArr.length; i++) { if (strArr[i].equals("")) { intArr[i] = 0; System.out.println("第" + (i + 1) + "个月的数据缺失"); } else { intArr[i] = Integer.parseInt(strArr[i]); System.out.println("第" + (i + 1) + "个月的使用资金为" + intArr[i]); } } int maxMonth = 0, minMonth = 0, totalAmount = 0, maxAmount = Integer.MIN_VALUE, minAmount = Integer.MAX_VALUE; for (int i = 0; i < intArr.length; i++) { if (intArr[i] > maxAmount) { maxAmount = intArr[i]; maxMonth = i + 1; } if (intArr[i] < minAmount && intArr[i] != 0) { minAmount = intArr[i]; minMonth = i + 1; } totalAmount += intArr[i]; } System.out.println("金额最大的月份为第" + maxMonth + "个月,使用资金为" + maxAmount); System.out.println("金额最小的月份为第" + minMonth + "个月,使用资金为" + minAmount); System.out.println("2019年度使用资金总额为" + totalAmount); } ``` 首先将原始数据字符串分割成一个字符串数组,然后遍历数组将空字符串转换为0,非空字符串转换为整数,并根据题目要求输出缺失项和有数值的项。 接下来遍历整数数组,找到最大值、最小值和总和,并记录对应的月份。 最后输出金额最大的月份、金额最小的月份和2019年度使用资金总额。 希望这个Java解答能够帮到您。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

chengjieno1

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

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

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

打赏作者

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

抵扣说明:

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

余额充值