自己的字符串类(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_ */
- 自己的字符串类(Own string class)
- 实现自己的字符串类String
- 将String字符串转换为Class类
- 自己写string 字符串处理类。。。。。。。。。。。。。。
- C++中处理字符串的Class-----string 用法详解
- The String class is provided in the Java library. Provide your own implementation for the following
- 自己写的 string 类
- 自己实现的string类
- 自己实现的string类
- 自己写的String类
- 自己实现的String类
- 《我自己的爱达荷》My Own Private Idaho(美国)
- build-your-own-promise 构建自己的promise
- 字符串string转整数int 实现自己的atoi函数
- 原始类型.Class 及 Class.forName(String class) 与 类名.class 的区别
- js 封装自己的class类
- js-封装自己的class类
- 学会封装自己的class类:
- HDU 1085 Holding Bin-Laden Captive!(母函数)
- thrift 的使用
- Quartzb2D
- MeanShift聚类
- ubuntu右键以root权限打开文件(文件夹)方法分享
- 自己的字符串类(Own string class)
- 关于分布式事务处理
- 条件Lamada树生成器
- svn 常见问题
- 关于水晶报表字段多的解决方案
- OpenCV提取轮廓(去掉面积小的轮廓)
- SQL SERVER 2012 动漫情景剧(中)
- lazy symbol binding failed: Symbol not found: _objc_storeStrong 的错误
- PKCS#1 v2.1 RSA Cryptography Standard (PKCS#1 RSA密码学规范,版本2.1)