自己的字符串类(Own string class)

来源:互联网 发布:windows手机桌面壁纸 编辑:程序博客网 时间:2024/05/16 04:23
/*
* BasicString.h
*
* Created on: Mar 6, 2012
* Author: crteng2
*/

#ifndef BASICSTRING_H_
#define BASICSTRING_H_
#include <iostream>

using namespace std;

class BasicString
{
public:
//default constructor
// explicit
BasicString(const char *src = NULL);
//destructor
virtual
~BasicString();
//copy constructor
BasicString(const BasicString &str);
//assignment operator overload
virtual BasicString&
operator=(const BasicString &str);
//"+" operator overload
virtual BasicString&
operator+(const BasicString &str) const;
//">" operator overload
virtual bool
operator>(const BasicString&str) const;
//"<" operator overload
virtual bool
operator<(const BasicString&str) const;
//"==" operator overload
virtual bool
operator==(const BasicString&str) const;
//"!=" operator overload
virtual bool
operator!=(const BasicString&str) const;

operator char*() const
{
return store_data;
}

char*
getData(void)
{
return store_data;
}

private:
char *store_data;
};

#endif /* BASICSTRING_H_ */


/*
* BasicString.cpp
*
* Created on: Mar 6, 2012
* Author: crteng2
*/

#include "BasicString.h"

BasicString::BasicString(const char *src)
{

if (src == NULL)
{
store_data = new char[1];
*store_data = '\0';
}
else
{
store_data = new char[strlen(src) + 1];
strcpy(store_data, src);
}
}

BasicString::~BasicString()
{
delete[] store_data;
}

BasicString::BasicString(const BasicString &str)
{
store_data = new char[strlen(str.store_data) + 1];
strcpy(store_data, str.store_data);
}

//"=" operator overload
BasicString&
BasicString::operator=(const BasicString &str)
{
if (&str != this)
{
delete[] store_data;
store_data = new char[strlen(str.store_data) + 1];
strcpy(store_data, str.store_data);
}
return *this;
}

//"+" operator overload
BasicString&
BasicString::operator+(const BasicString &str) const
{
char *store = new char[strlen(store_data) + strlen(str.store_data) + 1];
strcpy(store, store_data);
strcat(store, str.store_data);
BasicString *tmp = new BasicString(store);
delete[] store;
store = NULL;
return *tmp;
}

//">" operator overload
bool
BasicString::operator>(const BasicString&str) const
{
if (strcmp(store_data, str.store_data) == 1)
{
return true;
}
else
{
return false;
}
}

//"<" operator overload
bool
BasicString::operator<(const BasicString&str) const
{
if (strcmp(store_data, str.store_data) == -1)
{
return true;
}
else
{
return false;
}
}

//"==" operator overload
bool
BasicString::operator==(const BasicString&str) const
{
   if (strcmp(store_data, str.store_data) == 0)
   {
    return true;
   }
   else
   {
    return false;
   }
}

//"!=" operator overload
bool
BasicString::operator!=(const BasicString&str) const
{
   if (strcmp(store_data, str.store_data) != 0)
   {
      return true;
   }
   else
   {
      return false;
   }
}


//============================================================================
// Name : MainString.cpp
// Author : Carpe_He
// Version :
// Copyright : Your copyright notice
// Description : Own string class, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

#include "BasicString.h"
#include "OtherFunctionDealWith.h"

int main()
{

basicStringTest();
return 0;
}

/*
* OtherFunctionDealWith.cpp
*
* Created on: Mar 6, 2012
* Author: crteng2
*/

#include "OtherFunctionDealWith.h"
#include "DateString.h"

//basic string class verify
void
basicStringTest(void)
{

BasicString str("adfhq3hffn4yiuo");
BasicString str1(str);
BasicString str2;
str2 = str;
BasicString str3 = str + str2;
cout << str2 << endl;

if (str3 == str1)
{
cout << "Equal" << endl;
}
else
{
cout << "Not equal." << endl;
}

SortString(str);

/*
* Note:If a class default constructor
* is explict then must to explicit invoking.
* (Array initialize).
*/

BasicString strArra[7] =
{ "bsdfg", "aasr2", "f2rfs", "dasdfg", "casdt2", "efq3" };

cout << *strArra << endl;

SortString(strArra);

}


/*
* OtherFunctionDealWith.h
*
* Created on: Mar 6, 2012
 * Author: Carpe_He

*/

#ifndef OTHERFUNCTIONDEALWITH_H_
#define OTHERFUNCTIONDEALWITH_H_

#include "BasicString.h"
#include "DateString.h"

//our owner basic string verify
void
basicStringTest(void);
//our owner date string verify

//sort a string object any element.
template<class T>
void
SortString(T& str)
{

#if 1 //Before sort
cout << "Before sort:" << endl;
cout << str << endl;
#endif
char *dest = str.getData();
char temp;
for (unsigned int i = 0; i < strlen(str.getData()); i++)
{
for (unsigned int j = 0; j < strlen(str.getData()) - 1; j++)
{
if (dest[j] > dest[j + 1])
{
temp = dest[j];
dest[j] = dest[j + 1];
dest[j + 1] = temp;
}
}
}
#if 1 //After sort
cout << "After sort:" << endl;
cout << dest << endl;
#endif
}

//sort a owner write string Array
template<class T, const unsigned int size>
void
SortString(T(&point)[size])
{
cout << size << endl;
#if 1 //Before sort print everyone element from array.
cout << "Before sort:" << endl;

for (int i = 0; i < 7; i++)
cout << point[i] << " ";
cout << endl;
#endif

for (unsigned int i = 0; i < size; i++)
{
for (unsigned int j = 0; j < size - 1; j++)
{
if (point[j] > point[j + 1])
{
T temp = point[j];
point[j] = point[j + 1];
point[j + 1] = temp;
}
}
}

#if 1//After sort print everyone element from array.
cout << "After sort:" << endl;
for (int i = 0; i < 7; i++)
cout << point[i] << " ";
cout << endl;
#endif
}

#endif /* OTHERFUNCTIONDEALWITH_H_ */

原创粉丝点击