学习数据结构写了一个String的类,贴出来求指教
#ifndef SimpleString_h_h
#define SimpleString_h_h
#include <IOSTREAM.H>
#include <STDLIB.H>
#include <STRING.H>
#define defaultSize 128
class SimpleString{
private:
char *ch;
int curLength; //实际长度
int maxSize;
public:
SimpleString();
SimpleString(int ms) {ch=new char[ms]; curLength=0; maxSize=ms;}
//maxSize为传入参数的String
SimpleString(const char * init);
SimpleString(const SimpleString & A); //拷贝构造函数
~SimpleString() {delete[] ch;}
int getLength() {return curLength;}
bool isEmpty() {return curLength==0;} //空则返回1
void empty() {delete [] ch; curLength=0; maxSize=0;}
int Search(SimpleString& A) const;
int Search(const char* p) const;
void makeUpper();
void makeLower();
void makeReverse();
void TrimAll();
void TrimLeft();
void TrimRight();
void Insert(int nIndex,char c);
void Delete(int n);
void DeleteFromTo(int m,int n);
SimpleString& operator = (char * p);
SimpleString& operator = (SimpleString& A);
SimpleString operator + (const SimpleString& s) const;
SimpleString operator + (const char * p) const;
SimpleString& operator += (const SimpleString& ob);
SimpleString& operator += (const char * p);
// friend void operator<(const SimpleString& A,const SimpleString& B);
friend ostream& operator<<(ostream& out,SimpleString& ou);
friend istream& operator>>(istream& in,SimpleString& i);
operator const char*() {return (const char*)ch;}
char& operator[](int i); //取第i个字符
int operator == (SimpleString& A) const {return strcmp(ch,A.ch)==0;}
//判断串是否相等,不相等返回1,相等返回0;
friend bool operator<(const SimpleString& A,const SimpleString& B);
friend bool operator>(const SimpleString& A,const SimpleString& B);
};
SimpleString::SimpleString(){
ch=new char[1];
ch[0]='\0';
curLength=0;
maxSize=1;
}
SimpleString::SimpleString(const char * init){
int len=strlen(init);
ch=new char[len+1];
strcpy(ch,init);
curLength=len;
maxSize=len+1;
}
SimpleString::SimpleString(const SimpleString & A){
ch=new char[A.maxSize];
int i=0;
while(A.ch[i]!='\0'){
ch[i]=A.ch[i];
i++;
}
ch[i]='\0';
curLength=i;
maxSize=A.maxSize;
}
int SimpleString::Search(SimpleString& A) const{
int m=curLength,n=A.curLength;
int i=0;
for(i;i<=m-n;i++){
for(int j=0;ch[i+j]==A.ch[j]&&A.ch[j];j++);
if(j<n) continue;
return i;
}
return -1;
}
int SimpleString::Search(const char* p) const{
int m=curLength,n=strlen(p);
int i=0;
for(i;i<m-n;i++){
for(int j=0;ch[i+j]==p[j]&&p[j];j++);
if(j<n) continue;
return i;
}
return -1;
}
void SimpleString::makeUpper(){
if (isEmpty()) return;
for(int i=0;i<curLength;i++){
if (ch[i]<='z'&&ch[i]>='a')
{
ch[i]+=('A'-'a'); //-32
}
}
}
void SimpleString::makeLower(){
if(isEmpty()) return;
for(int i=0;i<curLength;i++){
if (ch[i]>='A'&&ch[i]<='Z')
{
ch[i]+=32;
}
}
}
void SimpleString::makeReverse(){
if(isEmpty()) return;
char *temp=new char[curLength];
for(int k=0;ch[k]!='\0';temp[k]=ch[k],k++);
for(int i=0;i<curLength;i++){
ch[i]=temp[curLength-1-i];
}
delete[]temp;
}
void SimpleString::TrimAll(){
if(isEmpty()) return;
int m=0;
char *temp=new char[curLength];
for(int k=0;ch[k]!='\0';temp[k]=ch[k],k++);
for(int i=0;i<curLength;i++){
if(temp[i]!=' ')
ch[m++]=temp[i];
}
ch[m]='\0';
curLength=m;
delete[]temp;
}
void SimpleString::TrimLeft(){
if(isEmpty()) return;
int i=0,m=0;
char *temp=new char[curLength+1];
for(int k=0;ch[k]!='\0';temp[k]=ch[k],k++);
temp[k]='\0';
while(1){
if(temp[i]!=' '){
while(temp[i]!='\0'){
ch[m++]=temp[i++];
}
break;
}
i++;
}
curLength=m;
delete[]temp;
}
void SimpleString::TrimRight(){
if(isEmpty()) return;
int i=0;
char *temp=new char[curLength+1];
strcpy(temp,ch);
while(temp[i]!='\0'){
ch[i]=temp[i];
if(temp[i+1]==' ') break;
++i;
}
ch[i+1]='\0';
curLength=i+1;
// delete[]temp;
}
void SimpleString::Insert(int nIndex,char c){
if(curLength==0) {
//空串 且maxSize=1 curLength=0
delete[]ch;
ch=new char[2];
ch[0]=c;
ch[1]='\0';
curLength=1;
maxSize=2;
return;
}
if(nIndex>=curLength&&curLength==maxSize-1){
//插入位置大于curLength 且没有空间插入
char* temp=ch;
delete[]ch;
ch=new char[curLength+1];
strcpy(ch,temp);
ch[curLength+1]=c;
curLength++;
maxSize=curLength+1;
return;
}
if(nIndex>=curLength&&curLength<maxSize-1){
//插入位置大于curLength 但是maxSize比较大
ch[curLength]=c;
ch[curLength+1]='\0';
return;
}
if (nIndex<curLength&&nIndex>0&&curLength==maxSize-1)
{
//插入位置在字符串中,但是没有空间
char* temp=new char[curLength+1];
strcpy(temp,ch);
delete[]ch;
ch=new char[maxSize+1];
int m=0;
for(int i=0;i<curLength;i++){
if(i==nIndex) ch[m++]=c;
ch[m++]=temp[i];
}
ch[m]='\0';
curLength=m;
maxSize++;
delete[]temp;
return;
}
if (nIndex<curLength&&nIndex>0) {
//插入位置在中间,但是有空间
char* temp=new char[curLength+1];
strcpy(temp,ch);
int m=0;
for(int i=0;i<curLength;i++){
if(i==nIndex) ch[m++]=c;
ch[m++]=temp[i];
}
ch[m]='\0';
curLength=m;
delete[]temp;
return;
}
}
void SimpleString::Delete(int n){
char* temp=new char[curLength+1];
strcpy(temp,ch);
int m=0;
for(int i=0;i<curLength;i++){
if(i==n) continue;
ch[m++]=temp[i];
}
ch[m]='\0';
curLength=m;
delete[]temp;
return;
}
void SimpleString::DeleteFromTo(int m,int n){
char* temp=new char[curLength+1];
strcpy(temp,ch);
int mm=0;
for(int i=0;i<curLength;i++){
if(i>=m&&i<=n) continue;
ch[mm++]=temp[i];
}
ch[mm]='\0';
curLength=mm;
delete[]temp;
return;
}
SimpleString& SimpleString::operator = (char * p){
int len=strlen(p);
delete[] ch;
ch=new char[len+1];
strcpy(ch,p);
curLength=len;
maxSize=len+1;
return *this;
}
SimpleString& SimpleString::operator = (SimpleString& A){
if (maxSize < A.curLength+1)
{
delete[] ch;
ch=new char[A.curLength+1 ];
strcpy(ch,A.ch);
curLength=A.curLength;
maxSize=A.maxSize;
}else{
strcpy(ch,A.ch);
curLength=A.curLength;
}
return *this;
}
SimpleString SimpleString::operator + (const SimpleString& s) const{
char *p=new char[curLength+s.curLength+1];
strcpy(p,ch);
strcat(p,s.ch);
SimpleString C(p);
delete[] p;
return C;
}
SimpleString SimpleString::operator + (const char * p) const{
int len=strlen(p);
char *q=new char[curLength+len+1];
strcpy(q,ch);
strcat(q,p);
SimpleString C(q);
delete[] q;
return C;
}
SimpleString& SimpleString::operator += (const SimpleString& ob){
char * temp = ch;
int n = curLength+ob.curLength;
int m = (maxSize>n)?maxSize:n;
ch=new char[m+1];
if(ch==NULL) {cerr<<"wrong ch!\n";exit(1);}
maxSize=m+1;
curLength=n;
strcpy(ch,temp);
strcat(ch,ob.ch);
delete[] temp;
return *this;
}
SimpleString& SimpleString::operator += (const char * p){
char * temp=ch;
int len=strlen(p);
int n=curLength+len;
int m=(maxSize>n)?maxSize:n;
ch=new char[m+1];
if(ch==NULL) {cerr<<"wrong ch!\n";exit(1);}
maxSize=m+1;
curLength=n;
strcpy(ch,temp);
strcat(ch,p);
delete[] temp;
return *this;
}
ostream& operator<<(ostream& out,SimpleString& ou){
int i=0;
while(ou.ch[i]!='\0'){
out<<ou.ch[i]<<" ";
//if(i%5==0) out<<endl;
i++;
}
out<<endl;
return out;
}
istream& operator>>(istream& in,SimpleString& i){
char a;
int k=0;
i.ch=new char[defaultSize];
cout<<"input string end with #"<<endl;
while(1){
in>>a;
if(a=='#') break;
if(k==(defaultSize-1)) { cout<<"need more space!"<<endl;break;}
i.ch[k++]=a;
}
i.ch[k]='\0';
i.curLength=k-1;
i.maxSize=defaultSize;
return in;
}
char& SimpleString::operator[](int i){
//下标从零开始
if (i<0||i>curLength)
{
cout<<"下标越界!"<<endl;exit(1);
}
return ch[i];
}
bool operator<(const SimpleString& A,const SimpleString& B){
int i=0;
while(A.ch[i++]!='\0'){
if (A.ch[i]>=B.ch[i])
{
return false;
}
}
return true;
}
bool operator>(const SimpleString& A,const SimpleString& B){
int i=0;
while(A.ch[i++]!='\0'){
if (A.ch[i]<=B.ch[i])
{
return false;
}
}
return true;
}
#endif
主函数的实现
// SimpleString.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <IOSTREAM.H>
#include <STRING.H>
#include "SimpleString.h"
int main(int argc, char* argv[])
{
SimpleString A; //空构造函数
// A.Insert(1,'a');
// A.Delete(2);
// A.DeleteFromTo(1,3);
SimpleString B("bbbb"); //接收一个字符串指针(char *)的构造函数
SimpleString C(B); //拷贝构造函数
SimpleString D;
A="abcd"; //= 重载
D=A;
B=B+D; //+ 重载
B=B+"EbbB";
B+=C; //+= 重载
B+="dddd";
A<B;
A>A;
SimpleString E;
cin>>E; //流插入
cout<<E; //流提取
SimpleString F("ABCDEF");
const char*p=(const char*)F; //强制类型转换运算符
cout<<F[2]<<endl; //下标运算符
int pos=B.Search(E); //字符串模式匹配
cout<<pos<<endl;
B.makeUpper();
B.makeLower();
B.makeReverse();
SimpleString str(" abcde ");
str.TrimLeft();
str.TrimRight();
str.makeUpper();
str.makeReverse();
cout<<str.Search("CB")<<endl;
str.Insert(8,'b');
return 0;
}