Actually, this class has mixed together the basic functionalities of a String and those of a StringBuffer...
Header:
#ifndef STRING_H_
#define STRING_H_
#include <iostream>
using namespace std;
class String{
friend ostream& operator<<(ostream& output, const String& str);
private:
char* buffer;
int initSize;
int incrementSize;
int strLength;
int bufferSize;
public:
String(const char* initStr = new char[128], int initSize = 128, int incrementSize = 128);
String(const String& str);
virtual ~String(){
delete[] buffer;
}
String operator+(const String& str) const;
String operator+(const char* str) const;
String& operator+=(const String& str);
String& operator+=(const char* str);
String operator=(const String& str) const;
String operator=(const char* str) const;
bool operator==(const String& str) const;
int length() {
if(strLength == -1)
strLength = strlen(buffer);
return strLength;
}
};
#endif
Implementation:
#include "String.h"
using namespace std;
String::String(const char* initStr, int initSize, int incrementSize):
initSize(initSize), incrementSize(incrementSize){
int strLen = strlen(initStr) + 1;
if(strLen > 1)
initSize = (strLen / incrementSize + 1) * incrementSize;
buffer = new char[initSize];
memcpy(buffer, initStr, strLen);
strLength = -1;
bufferSize = initSize;
}
String::String(const String& str){
bufferSize = str.bufferSize;
buffer = new char[bufferSize];
strcpy(buffer, str.buffer);
initSize = str.initSize;
incrementSize = str.incrementSize;
strLength = -1;
}
String String::operator+(const String& str) const{
String tempStr = *this;
char* paramStr = str.buffer;
//传进来的String对象的长度
int strSize = strlen(paramStr);
//如果传进来的是一个空字符串
if(!strSize)
return tempStr;
//如果当前字符串缓存是buffer[128],之前保存了100个字符
//那(tempStr.bufferSize - tempStr.length())就表示当前缓存还剩下28个字符的空间
int excessSize = strSize - (tempStr.bufferSize - tempStr.length());
char* oldBuffer = tempStr.buffer;
if(excessSize > 0){
tempStr.bufferSize = tempStr.bufferSize + (excessSize / tempStr.incrementSize + 1) * tempStr.incrementSize;
char* newBuffer = new char[tempStr.bufferSize];
strcpy(newBuffer, oldBuffer);
strcat(newBuffer, paramStr);
delete [] oldBuffer;
oldBuffer = newBuffer;
}else{
strcat(oldBuffer, paramStr);
}
tempStr.strLength = -1;
return tempStr;
}
String String::operator+(const char* str) const{
String tempStr = *this;
//传进来的char*对象的长度
int strSize = strlen(str);
//如果传进来的是一个空字符串
if(!strSize)
return tempStr;
//如果当前字符串缓存是buffer[128],之前保存了100个字符
//那(tempStr.bufferSize - tempStr.length())就表示当前缓存还剩下28个字符的空间
int excessSize = strSize - (tempStr.bufferSize - tempStr.length());
char* oldBuffer = tempStr.buffer;
if(excessSize > 0){
tempStr.bufferSize = tempStr.bufferSize + (excessSize / tempStr.incrementSize + 1) * tempStr.incrementSize;
char* newBuffer = new char[tempStr.bufferSize];
strcpy(newBuffer, oldBuffer);
strcat(newBuffer, str);
delete [] oldBuffer;
oldBuffer = newBuffer;
}else{
strcat(oldBuffer, str);
}
tempStr.strLength = -1;
return tempStr;
}
String& String::operator+=(const String& str) {
char* temp = str.buffer;
//传进来的String对象的长度
int tempSize = strlen(temp);
//如果传进来的是一个空字符串,则返回原来的字符串
if(!tempSize)
return *this;
//如果当前字符串缓存是buffer[128],之保存了100个字符
//那(bufferSize - length())就表示当前缓存还剩下28个字符的空间
int excessSize = tempSize - (bufferSize - length());
if(excessSize > 0){
bufferSize = bufferSize + (excessSize / incrementSize + 1) * incrementSize;
char* newBuffer = new char[bufferSize];
strcpy(newBuffer, buffer);
strcat(newBuffer, temp);
delete [] buffer;
buffer = newBuffer;
}else{
strcat(buffer, temp);
}
this->strLength = -1;
return *this;
}
String& String::operator+=(const char* str) {
//传进来的char*的长度
int tempSize = strlen(str);
//如果传进来的是一个空字符串,则返回原来的字符串
if(!tempSize)
return *this;
//如果当前字符串缓存是buffer[128],之保存了100个字符,
//那(bufferSize - length())就表示当前缓存还剩下28个字符的空间
int excessSize = tempSize - (bufferSize - length());
cout << "excessSize:" << excessSize << endl;
if(excessSize > 0){
bufferSize = bufferSize + (excessSize / incrementSize + 1) * incrementSize;
char* newBuffer = new char[bufferSize];
strcpy(newBuffer, buffer);
strcat(newBuffer, str);
delete [] buffer;
buffer = newBuffer;
}else{
strcat(buffer, str);
}
cout << "buffer size:" << bufferSize << endl;
this->strLength = -1;
return *this;
}
String String::operator=(const String& str) const{
return str;
}
String String::operator=(const char* str) const{
return String(str);
}
bool String::operator==(const String& str) const{
return strcmp(buffer, str.buffer) == 0 ? true : false;
}
ostream& operator<<(ostream& output, const String& str) {
output << str.buffer;
return output;
}
分享到:
相关推荐
You can significantly minimize the number of header files you need to include in your own header files by using forward declarations. For example, if your header file uses the File class in ways that ...
Were you aware that grade schools and high schools have begun implementing C Programming in their curriculum's? Are you wanting a simple way to understand a step by step action to learning C ...
replaceinfiles.zip Search and Replace in multiple files Add-in for Visual C++ 6.0(83KB)<END><br>76,RescueAgent.zip A Code Rescue Add-in for Visual C++ Developers(118KB)<END><br>77,stripem.zip...
Implementing Automation in MFC .ODLay Hee-Hoo! About the Sample Other OLE Applications Debugging OLE Applications Automation and ActiveX Controls Custom COM Interfaces The Rubber Hits the Road ...
All computer code is included, allowing developers to extend and reuse the software modules for their own project work. The book’s tutorial-like approach empowers students and practitioners with the ...
Rolling Your Own Servers in Python Part IV: Assorted Topics Chapter 16. Databases and Persistence Section 16.1. "Give Me an Order of Persistence, but Hold the Pickles" Section 16.2. ...
Its very well documented and you should be able to adapt it for writing your own addins<END><br>45,msgbox32_dll.zip This code compiles to a DLL and when registered will install itself as a VB Add-...
1.2.8 Programming in Go......................................................................................10 1.2.9 Summary.............................................................................
1.2.8 Programming in Go......................................................................................10 1.2.9 Summary.............................................................................