c++问题,求高手解答..........分少见谅。
自定义一个String 类用来存储字符串,串中的字符存放在动态分配的数组中,该类
拥有以下成员函数:
构造函数:为对象分配动态存储空间,完成初始化,要考虑多种参数的重载形式,例如
无参数、参数类型为char 或char*;
析构函数:将占用的动态存储空间释放;
拷贝构造函数:实现String 对象之间的深拷贝构造;
IsSubstring 函数:判断参数是否为当前串的子串,参数可以为char*或String 类型;
Str2Num 函数:将数字字符串转成数值;
UpperStr 函数:将字符串中的小写字母均转为大写字母;
ReplaceStr 函数:替换String 中的某个字符串;
ReplaceChar 函数:替换String 中的某个字符;
DelStr 函数:删除String 中的某个字符串;
DelChar 函数:删除String 中的某个字符;
重载了以下运算符:
= 运算符:实现String 对象之间的深拷贝赋值;
+ 运算符:将两个String 对象首尾相连;
[] 运算符:访问String 对象中某个下标位置的字符;
<<运算符:输出String 对象;
自行设计类中的数据成员和其它内容,并编写测试代码使用该Sting 类。
答案:这是string.h头文件
#ifndef String_H
#define String_H
#include <iostream>
using namespace std;
class String
{
public:
/*默认构造函数*/
String():capacity(5),index(0)
{
strArray = new char[capacity];
strArray[0] = '\0';
}
/*参数类型为char的构造函数*/
String(char );
/*参数类型为char*的构造函数*/
String(const char*);
/*拷贝构造函数*/
String(const String&);
/*析构函数*/
~String();
/*返回字符串长度*/
int size() const ;
/*重载输出操作符*/
friend ostream& operator<<(ostream& out,const String &str);
/*重载=操作符*/
String operator=(const String& );
/*重载[]操作符*/
char& operator[](int ) const;
/*重载+操作符*/
String operator+(const String&);
/*判断参数是否为当前串的子串*/
int isSubstring(const char *);
int isSubstring(const String&);
/*将数字字符串转换为数值,暂时不能处理小数的情况*/
long Str2Num();
/*替换String中的某个字符串*/
void ReplaceStr(const String&,const String&);
void ReplaceStr(const String&,const char*);
/*替换String中的某个字符*/
void ReplaceChar(const char ,const String&);
void RepalceChar(const char ,const char*);
/*删除String中的某个字符串*/
void DelStr(const String&);
/*删除String中的某个字符*/
void DelChar(const char);
private:
/*表示字符数组可以容纳的最大字符数*/
int capacity;
/*字符串的当前长度*/
int index;
char *strArray;
/*将字符数组的容量翻倍*/
void extendCapacity(int );
/*KMP匹配算法辅助函数*/
void getNext(const String&,int []);
/*实现替换字符串*/
void doReplaceStr(int,const String&,const String&);
};
#endif
-------------------------------------------------------
这是string.cpp文件
#include "String.h"
/*参数类型为char的构造函数*/
String::String(char sourceChar)
{
capacity = 5; index = 0;
strArray = new char[capacity];
if ((index + 1) >= capacity)
{
this->extendCapacity(capacity + 1);
}
strArray[index++] = sourceChar;
strArray[index] = '\0';
}
/*参数类型为char*的构造函数*/
String::String(const char *sourceStr)
{
capacity = 5; index = 0;
strArray = new char[capacity];
int sourceLength = 0;
for (int i = 0;sourceStr[i] != '\0';++i)
{
++sourceLength;
}
if ((index + sourceLength) >= capacity)
{
this->extendCapacity(index + sourceLength);
}
for (int i = 0;i < sourceLength;++i)
{
strArray[index++] = sourceStr[i];
}
strArray[index] = '\0';
}
/*拷贝构造函数实现深拷贝*/
String::String(const String &sourceStr)
{
capacity = 5; index = 0;
strArray = new char[capacity];
if (sourceStr.size() >= capacity)
{
this->extendCapacity(sourceStr.size());
}
for (int i = 0;i < sourceStr.size();++i)
{
strArray[index++] = sourceStr[i];
}
strArray[index] = '\0';
}
/*析构函数*/
String::~String()
{
delete strArray;
}
/*返回字符数组的长度*/
int String::size() const
{
return index;
}
/*判断参数是否为当前串的子串*/
int String::isSubstring(const char* subStr)
{
/*构造一个String对象然后调用参数为String类型的判断函数*/
String newSubStr = String(subStr);
return this->isSubstring(newSubStr);
}
int String::isSubstring(const String &subStr)
{
if (!strArray || subStr[0] == '\0' || strArray[0] == '\0')
return -1;
int len = subStr.size();
int *next = new int[len + 1];
getNext(subStr,next);
int i = 0,j = 0;
int iter = 0;
while (strArray[i] != '\0' && subStr[j] != '\0')
{
if (strArray[i] == subStr[j])
{
++i;
++j;
}
else
{
iter += j - next[j];
if (next[j] != -1)
{
j = next[j];
}
else
{
j = 0;
++i;
}
}
}
delete []next;
if (subStr[j] == '\0')
return iter;
else
return -1;
}
/*将数字字符串转换为数值,暂时不能处理小数的情况*/
long String::Str2Num()
{
long retNum = 0;
for (int i = 0;i < index;++i)
{
retNum = retNum * 10 + (strArray[i] - '0');
}
return retNum;
}
/*替换String中的某个字符串*/
void String::ReplaceStr(const String &replacedStr, const String &targetStr)
{
int ret;
while ((ret = this->isSubstring(replacedStr)) != -1)
{
this->doReplaceStr(ret,replacedStr,targetStr);
}
}
void String::ReplaceStr(const String &replacedStr, const char *target)
{
String targetStr(target);
this->ReplaceStr(replacedStr,targetStr);
}
/*替换String中的某个字符*/
void String::RepalceChar(const char replaced, const char *target)
{
String replacedStr(replaced);
this->ReplaceStr(replacedStr,target);
}
void String::ReplaceChar(const char replaced,const String &targetStr)
{
String replacedStr(replaced);
this->ReplaceStr(replacedStr,targetStr);
}
/*删除String中的某个字符串*/
void String::DelStr(const String &delStr)
{
this->ReplaceStr(delStr,"");
}
/*删除String中的某个字符串*/
void String::DelChar(const char delChar)
{
this->ReplaceChar(delChar,"");
}
void String::doReplaceStr(int startPos,const String &replacedStr,const String &targetStr)
{
if (index + replacedStr.size() - targetStr.size() >= this->capacity)
{
this->extendCapacity(index + replacedStr.size() - targetStr.size());
}
char *tempArr = this->strArray;
strArray = new char[this->capacity];
int iter = 0;
for (int i = 0;i < startPos;++i)
{
strArray[iter++] = tempArr[i];
}
for (int i = startPos;i < startPos+targetStr.size();++i)
{
strArray[iter++] = targetStr[i - startPos];
}
for (int i = startPos + replacedStr.size();i <= index;++i)
{
strArray[iter++] = tempArr[i];
}
strArray[iter] = '\0'; index = iter;
}
/*KMP算法匹配辅助函数*/
void String::getNext(const String& subStr,int next[])
{
next[0] = -1;
int k = -1,j = 0;
while (subStr[j] != '\0')
{
if (k != -1 && subStr[k] != subStr[j])
k = next[k];
++j; ++k;
if (subStr[k] == subStr[j])
next[j] = next[k];
else
next[j] = k;
}
}
/*扩展容量*/
void String::extendCapacity(int newCapacity)
{
char *tempStr = strArray;
while (capacity < newCapacity)
{
capacity *= 2;
}
strArray = new char[capacity];
for (int i = 0;i <= index;++i)
{
strArray[i] = tempStr[i];
}
delete tempStr;
}
/*重载<<操作符*/
ostream& operator<<(ostream &out,const String&str)
{
for (int i = 0;i < str.size();++i)
{
out << str.strArray[i];
}
return out;
}
/*重载=操作符*/
String String::operator =(const String &sourceStr)
{
/*调用拷贝构造函数返回一个新的String对象*/
return String(sourceStr);
}
/*重载[]操作符*/
char& String::operator [](int position) const
{
return strArray[position];
}
/*重载+操作符*/
String String::operator +(const String &addStr)
{
int addLength = addStr.size(),iter = 0;
if ((addLength + index) >= capacity)
{
this->extendCa
上一个:C语言中有输出格式控制,C++怎么实现?
下一个:请高手给个C++所有头文件及其说明。