C++运算符编写CMyCString类
来源:互联网 发布:2017网络热词及解释 编辑:程序博客网 时间:2024/06/06 01:58
#include <string>
#include <iostream>
using namespace std;
struct Node
{
char c;
Node* pNext;
};
Node* phead = NULL;
Node* pEnd = NULL;
void Push_Back(char c) // 在链表的尾部添加
{
//申请一个节点
Node* temp = new Node;
temp->c = c;
temp->pNext = NULL;
// 把这个节点放到链表上
if (!phead)
{
phead = temp;
}
else
{
pEnd->pNext = temp;
}
pEnd = temp;
}
char Pop_Front() //从链表上去掉
{
if (!phead)
{
return 0;
}
else
{
// 记住节点中字符
char c = phead->c;
// 记住要删除的这个头节点
Node* bj = phead;
phead = phead->pNext;
// 删除bj
delete bj;
return c;
}
}
class CMyString
{
private:
char* m_pStr;
public:
friend ostream& operator<<(ostream& os,CMyString& c_str);//重载<<,比较常用
friend CMyString operator+(char* str,CMyString& c_str);//char*变量+CMyString 对象
friend istream& operator>>(istream& is,CMyString& c_str);//重载>>,比较常用
public:
CMyString()
{
m_pStr = new char[1];
*m_pStr = 0;
}
CMyString(char* str)//将字符串给对象赋值
{
m_pStr = new char[strlen(str)+1];
strcpy(this->m_pStr,str);
}
CMyString(const CMyString& c_str)//拷贝构造函数
{
m_pStr = new char[strlen(c_str.m_pStr)+1];
strcpy(this->m_pStr,c_str.m_pStr);
}
~CMyString()
{
delete[] m_pStr;
m_pStr = NULL;
}
CMyString& operator=(char* str)//重载赋值运算符
{
if (this->m_pStr != NULL)//先释放已经申请的空间
{
delete[] this->m_pStr;
this->m_pStr = NULL;
}
this->m_pStr = new char[strlen(str)+1];//重新申请空间
strcpy(this->m_pStr,str);
return *this;
}
CMyString& operator=(CMyString& c_str)//重载=,对象给对象赋值
{
if ( this == &c_str)
{
return *this;
}
char* str = new char[strlen(c_str.m_pStr)+1];
if (this->m_pStr != NULL)
{
delete[] this->m_pStr;
this->m_pStr = NULL;
}
this->m_pStr = str;
strcpy(this->m_pStr,c_str.m_pStr);
return *this;
}
CMyString operator+(char* str)//CMyString对象+char*字符串
{
CMyString temp;
temp.m_pStr = new char[strlen(str)+strlen(this->m_pStr)+1];
strcpy(temp.m_pStr,this->m_pStr);
strcat(temp.m_pStr,str);
return temp;
}
CMyString operator+(CMyString& c_str)
{
CMyString temp;
temp.m_pStr = new char[strlen(c_str.m_pStr)+strlen(this->m_pStr)+1];
strcpy(temp.m_pStr,this->m_pStr);
strcat(temp.m_pStr,c_str.m_pStr);
return temp;
}
bool operator==(char* str)//CMyString和char*字符串是否相等
{
if (!strcmp(this->m_pStr,str))
{
return true;
}
else
{
return false;
}
}
char operator[](int index)
{
if (strlen(this->m_pStr) > index && index >= 0)
{
return this->m_pStr[index];
}
return -1;
}
};
CMyString operator+(char* str,CMyString& c_str)//类外重载操作符
{
CMyString temp;
temp.m_pStr = new char[strlen(c_str.m_pStr)+strlen(str)+1];
strcpy(temp.m_pStr,str);
strcat(temp.m_pStr,c_str.m_pStr);
return temp;
}
ostream& operator<<(ostream& os,CMyString& c_str)
{
os << c_str.m_pStr ;
return os;
}
istream& operator>>(istream& is,CMyString& c_str)
{
if (c_str.m_pStr != NULL)
{
delete[] c_str.m_pStr;
}
char c;
int nLength = 0;
while( (c=getchar()) != '\n')
{
// 把这个字符放到链表里
Push_Back(c);
//统计输入字符的个数
nLength++;
}
c_str.m_pStr = new char[nLength+1];
// 把链表的 字符 放到 数组里
for (int i=0;i<nLength+1;i++)
{
c_str.m_pStr[i] = Pop_Front();
}
return is;
}
int main()
{
CMyString str;
cout << "asdasdasd" << endl;
CMyString str1("Asdasd");// = "Asdasd";
CMyString str2(str);
CMyString str3 = "asdasd";
cout << str << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
cout << "------------------------" << endl;
str = "ccccccc";
str3 = str2 = str;
cout << str << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
cout << "------------------------" << endl;
str3 = str1+"LLLLLL";
str2 = str3+str1;
str1 = "uuuuuu"+str2;
cout << str << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
cout << "------------------------" << endl;
cout << (str=="cccccc") << endl;
cout << str1[10] << endl;
cin >> str1;
cout << str1 << endl;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
struct Node
{
char c;
Node* pNext;
};
Node* phead = NULL;
Node* pEnd = NULL;
void Push_Back(char c) // 在链表的尾部添加
{
//申请一个节点
Node* temp = new Node;
temp->c = c;
temp->pNext = NULL;
// 把这个节点放到链表上
if (!phead)
{
phead = temp;
}
else
{
pEnd->pNext = temp;
}
pEnd = temp;
}
char Pop_Front() //从链表上去掉
{
if (!phead)
{
return 0;
}
else
{
// 记住节点中字符
char c = phead->c;
// 记住要删除的这个头节点
Node* bj = phead;
phead = phead->pNext;
// 删除bj
delete bj;
return c;
}
}
class CMyString
{
private:
char* m_pStr;
public:
friend ostream& operator<<(ostream& os,CMyString& c_str);//重载<<,比较常用
friend CMyString operator+(char* str,CMyString& c_str);//char*变量+CMyString 对象
friend istream& operator>>(istream& is,CMyString& c_str);//重载>>,比较常用
public:
CMyString()
{
m_pStr = new char[1];
*m_pStr = 0;
}
CMyString(char* str)//将字符串给对象赋值
{
m_pStr = new char[strlen(str)+1];
strcpy(this->m_pStr,str);
}
CMyString(const CMyString& c_str)//拷贝构造函数
{
m_pStr = new char[strlen(c_str.m_pStr)+1];
strcpy(this->m_pStr,c_str.m_pStr);
}
~CMyString()
{
delete[] m_pStr;
m_pStr = NULL;
}
CMyString& operator=(char* str)//重载赋值运算符
{
if (this->m_pStr != NULL)//先释放已经申请的空间
{
delete[] this->m_pStr;
this->m_pStr = NULL;
}
this->m_pStr = new char[strlen(str)+1];//重新申请空间
strcpy(this->m_pStr,str);
return *this;
}
CMyString& operator=(CMyString& c_str)//重载=,对象给对象赋值
{
if ( this == &c_str)
{
return *this;
}
char* str = new char[strlen(c_str.m_pStr)+1];
if (this->m_pStr != NULL)
{
delete[] this->m_pStr;
this->m_pStr = NULL;
}
this->m_pStr = str;
strcpy(this->m_pStr,c_str.m_pStr);
return *this;
}
CMyString operator+(char* str)//CMyString对象+char*字符串
{
CMyString temp;
temp.m_pStr = new char[strlen(str)+strlen(this->m_pStr)+1];
strcpy(temp.m_pStr,this->m_pStr);
strcat(temp.m_pStr,str);
return temp;
}
CMyString operator+(CMyString& c_str)
{
CMyString temp;
temp.m_pStr = new char[strlen(c_str.m_pStr)+strlen(this->m_pStr)+1];
strcpy(temp.m_pStr,this->m_pStr);
strcat(temp.m_pStr,c_str.m_pStr);
return temp;
}
bool operator==(char* str)//CMyString和char*字符串是否相等
{
if (!strcmp(this->m_pStr,str))
{
return true;
}
else
{
return false;
}
}
char operator[](int index)
{
if (strlen(this->m_pStr) > index && index >= 0)
{
return this->m_pStr[index];
}
return -1;
}
};
CMyString operator+(char* str,CMyString& c_str)//类外重载操作符
{
CMyString temp;
temp.m_pStr = new char[strlen(c_str.m_pStr)+strlen(str)+1];
strcpy(temp.m_pStr,str);
strcat(temp.m_pStr,c_str.m_pStr);
return temp;
}
ostream& operator<<(ostream& os,CMyString& c_str)
{
os << c_str.m_pStr ;
return os;
}
istream& operator>>(istream& is,CMyString& c_str)
{
if (c_str.m_pStr != NULL)
{
delete[] c_str.m_pStr;
}
char c;
int nLength = 0;
while( (c=getchar()) != '\n')
{
// 把这个字符放到链表里
Push_Back(c);
//统计输入字符的个数
nLength++;
}
c_str.m_pStr = new char[nLength+1];
// 把链表的 字符 放到 数组里
for (int i=0;i<nLength+1;i++)
{
c_str.m_pStr[i] = Pop_Front();
}
return is;
}
int main()
{
CMyString str;
cout << "asdasdasd" << endl;
CMyString str1("Asdasd");// = "Asdasd";
CMyString str2(str);
CMyString str3 = "asdasd";
cout << str << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
cout << "------------------------" << endl;
str = "ccccccc";
str3 = str2 = str;
cout << str << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
cout << "------------------------" << endl;
str3 = str1+"LLLLLL";
str2 = str3+str1;
str1 = "uuuuuu"+str2;
cout << str << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
cout << "------------------------" << endl;
cout << (str=="cccccc") << endl;
cout << str1[10] << endl;
cin >> str1;
cout << str1 << endl;
system("pause");
return 0;
}
0 0
- C++运算符编写CMyCString类
- C语言运算符运算
- 如何用C语言编写幂运算的代码?救急!!!
- C语言中使用位运算编写程序
- 矩形类中运算符重载【C++】
- 矩形类中运算符重载【C++】
- 矩形类中运算符重载【C++】
- 【C++】String类中的运算符重载
- c++_类的运算符重载
- 矩形类中运算符重载【C++】
- C语言运算符
- c 运算符 优先级
- C++-运算符重载
- c 运算符 优先级
- C语言运算符
- C语言运算符
- C语言运算符
- C 中运算符
- 0023 java中的各种数据类型在内存中存储的方式 【入门】
- useradd 命令
- 为什么项目估算偏差如此之大?
- ASN.1、BER、SMI、MIB、PDU的关系——转
- MySQL中的数据类型
- C++运算符编写CMyCString类
- 自动折行布局的实现
- SpringMVC Controller 介绍
- oracle 索引index那些事
- WebRTC音视频引擎研究(1)--整体架构分析
- [BBS] ShowArticleTree.jsp 01 帖子的树形展现-递归 、Session 存 login 否
- java 加密字节数组在数据库中的保存
- 数据库主键设计之思考
- 移动应用的十项设计原则及小提示