字符串& String :: Concat(const char Str [])

时间:2016-06-15 16:44:10

标签: c++ visual-c++

我决定在夏季编写上学期c ++课程的所有作业,以便更好地为c ++ 3做准备,但我不了解如何通过String类或者甚至需要哪些步骤才能连接两个字符串并在主cpp文件中显示结果。 在我的Main.cpp中:

#include <iostream>
using namespace std;

#include "String.h"

int main()
{
    String Str1;
    String Str2("this is a test");
    String Str3(Str2);
    String Str4("bruh");
    int result;

    cout << "Testing Display: " << endl;
    Str2.Display();
    cout << endl;
    cout << "Testing displayLine: " << endl;
    Str2.displayLine();
    cout << endl;

    result = Str2.Compare(Str3);
    if (result < 0)
    {
        Str2.Display();
        cout << " comes before " << endl;
        Str3.Display();
        cout << endl;
    }

    else
        if (result > 0)
        {
            Str3.Display();
            cout << " comes before " << endl;
            Str2.Display();
        }
        else
        {
            Str3.Display();
            cout << " is equal to " << endl;
            Str2.Display();
        }
    cout << endl;

    result = Str2.Compare("wxyz");
    Str1.Copy(Str3);

    cout << "Str1 contains " << Str1.length() <<" characters"<< endl;


    cout << "Concatenation: ";
    Str2.Concat(Str4);
        cout << endl;

    return 0;
}

在我的String.cpp中:

#include <iostream>
using namespace std;
#include <string.h>
#include "String.h"

#pragma warning(disable:4996)

String::String()
{
    NumChars = 0;
    MaxSlots = 0;
    pChar = new char[NumChars+1];
    pChar[0] = '\0';
}

String::String(const char Str[])
{
    NumChars = strlen(Str);
    pChar = new char[NumChars + 1];
    strcpy(pChar, Str);
}

String::String(const String & Str)
{
    NumChars = Str.NumChars;
    pChar = new char[NumChars + 1];
    strcpy(pChar, Str.pChar);
}

String::~String()
{
    delete[] pChar;
}

int String::Compare(const String & Str) const
{
    return strcmp(pChar, Str.pChar);        //case sensitive
}

int String::Compare(const char Str[]) const
{
    return strcmp(pChar, Str);          //case sensitive
}

String& String::Copy(const String & Str)
{
    if (this != &Str)
    {
        if (MaxSlots < Str.NumChars)
        {
            delete[]pChar;
            MaxSlots = Str.NumChars;
            pChar = new char[NumChars + 1];
        }
        else;
        NumChars = Str.NumChars;
        strcpy(pChar, Str.pChar);
    }
    else;
    return *this;
}

String& String::Copy(const char Str[])
{
    delete[] pChar;
    NumChars = strlen(Str);
    MaxSlots = NumChars;
    pChar = new char[MaxSlots + 1];
    return *this;
}

String& String::Concat(const String & Str)
{
    pTemp = new char[NumChars+1];
    strcpy(pTemp, pChar);
    strcat(pTemp, Str.pChar);
    delete[]pChar;
    pChar = pTemp;
    return *this;
}

String & String::Concat(const char Str[])
{
    return *this;
    /*
    NumChars = strlen(Str);
    MaxSlots = NumChars;


     delete[] pChar;
    MaxSlots = MaxSlots + NumChars;
    NumChars = NumChars + strlen(Str);
    pChar = new char[MaxSlots + 1]; */
}

void    String::Display() const
{
    cout << pChar;
}

void    String::displayLine() const
{
    cout << pChar;
}

在我的String.h中:

#ifndef STRING_H
#define STRING_H

class String
    {
    public:
                    String(); //default constructor
                    String(const char[]); 
                    String(const String &); //copy constructor
                    ~String();
        int         Compare(const String &) const;
        int         Compare(const char[])const;
        String&     Copy(const String&);
        String&     Copy(const char[]);
        String&     Concat(const String&);
        String&     Concat(const char[]);
        void        Display()const;
        void        displayLine() const;
        int         length() const;

    private:
        char    * pChar;
        char    *pTemp;
        int     NumChars;
        int     MaxSlots;
};

inline int String::length() const
{
    return NumChars;
};

#endif

2 个答案:

答案 0 :(得分:1)

您希望连接字符串的长度是两个字符串长度的总和。因此:

String& String::Concat(const String & Str)
{
    pTemp = new char[NumChars + Str.NumChars + 1];
    strcpy(pTemp, pChar);
    strcat(pTemp, Str.pChar);
    delete[]pChar;
    pChar = pTemp;
    return *this;
}

你可以通过strcat() - 但是strcpy() - 两次(第二次添加到pTemp的偏移量)来进一步优化,因为你已经知道了字符串的长度。

答案 1 :(得分:0)

我不确定您是否理解MaxSlots的使用情况;这是分配pChar(减1)的大小?如果是这样,代码中的某些点会忘记设置/更新/使用。

我不明白你对“如何通过String类”的意思是什么,但是关于“为了连接两个字符串甚至需要哪些步骤”,你忘记了计算数字已存在于对象中的字符。

首先,我建议创建e Reserve()方法;类似[警告:代码未经过测试]

String& String::Reserve (int n)
{
  if ( n > MaxSlots )
   {
      MaxSlots = n;
      pTemp    = new char[MaxSlots+1];
      strcpy(pTemp, pChar);
      delete[]pChar;
      pChar = pTemp;
   }
}

接下来,以这种方式重写您的Concat()方法

String& String::Concat(const String & Str)
{
   NumChars += Str.NumChars;
   Reserve(NumChars);
   strcat(pChar, Str.pChar);
   return *this;
}

String & String::Concat(const char * Str)
{
   if ( Str )
   {
      NumChars += strlen(Str);
      Reserve(NumChars);
      strcat(pChar, Str);
   }
   return *this;
}

p.s:抱歉我的英语不好