一时兴起,编了一个类,可以进行集合运算
一时兴起,编了一个类,可以进行集合运算,
把集合封装成类CJiHe,类的函数完成交集、并集、差集等运算。
“集合”是一个很广的数学概念,数学中对它的研究非常多,
但是我这次主要是练习编程,而不是去研究集合,
所以我找了一个简单的集合:小写字母集,集合的元素是小写字母。
因为集合本身有“不重复”的特性,所以最多只有26个成员,
集合本身也有“无顺序”特性,所以对元素没有排序要求。
至于集合的运算,因为使用最简单的莫过于+、-、*之类的了,
所以我的程序主要进行了运算符重载。
这样虽然编程比较复杂,但是应用中却不用记忆函数名了。
//----------JiHe.h----------已略去不重要的内容----------
class CJiHe
{
public:
CJiHe(void);//创建空的集合
virtual ~CJiHe();
CJiHe(const char *str);//从字符串创建,自动去除非法字符与重复字符
CJiHe(const CJiHe &other);//拷贝构造
CJiHe &operator = (const CJiHe &other);//赋值号重载
CJiHe &operator = (const char *str);//赋值号重载,从字符串赋值
CJiHe operator + (const CJiHe &other) const;//加法重载,并集
CJiHe operator - (const CJiHe &other) const;//减法重载,差集
CJiHe operator * (const CJiHe &other) const;//乘法重载,交集
CJiHe &operator += (const CJiHe &other);//+=重载,并集
CJiHe &operator -= (const CJiHe &other);//-=重载,差集
CJiHe &operator *= (const CJiHe &other);//*=重载,交集
CJiHe operator + (const char c) const;//加法重载,加一个元素
CJiHe operator - (const char c) const;//减法重载,减一个元素
void View(void) const;//显示所有成员
private:
char m_data[27];//存放集合元素,为了运算方便,不足27个时,后面全部为\0,而不只一个\0
};
//----------JiHe.cpp----------已略去不重要的内容----------
int FindInString(const char *str, const char c)
{
//全局函数
//从字符串中查找字符
//有则返回1,无则返回0
for (const char *p=str; *p!=NULL; p++)
{
if (*p == c) return 1;
}
return 0;
}
CJiHe::CJiHe(void)
{
//创建空的集合
memset(m_data, 0, 27);
}
CJiHe::~CJiHe()
{
}
CJiHe::CJiHe(const char *str)
{
//从字符串创建,去除非法字符与重复字符
int i = 0;//指向m_data开头
memset(m_data, 0, 27);
for (const char *p=str; *p!=NULL; p++)
{
if (*p < 'a') continue;//非法字符
if (*p > 'z') continue;//同上
if (!FindInString(m_data, *p)) m_data[i++] = *p;//去除重复元素
}
}
CJiHe::CJiHe(const CJiHe &other)
{
//拷贝构造
memcpy(m_data, other.m_data, 27);
}
CJiHe &CJiHe::operator = (const CJiHe &other)
{
//赋值号重载
//可以没有返回值,有返回值是方便“a=b=c=d;”这样的运算
if (&other == this) return *this;//自身赋值
memcpy(m_data, other.m_data, 27);
return *this;
}
CJiHe &CJiHe::operator = (const char *str)
{
//赋值号重载,从字符串赋值
int i = 0;//指向m_data开头
memset(m_data, 0, 27);
for (const char *p=str; *p!=NULL; p++)
{
if (*p < 'a') continue;//非法字符
if (*p > 'z') continue;//同上
if (!FindInString(m_data, *p)) m_data[i++] = *p;//去除重复元素
}
return *this;
}
CJiHe CJiHe::operator + (const CJiHe &other) const
{
//加法重载,并集
char temp[27];
memcpy(temp, m_data, 27);//照自身复制
int i = strlen(temp);//指向第一个\0
for (const char *p=other.m_data; *p!=NULL; p++)
{
if (!FindInString(temp, *p)) temp[i++] = *p;
}
return CJiHe(temp);
}
CJiHe CJiHe::operator - (const CJiHe &other) const
{
//减法重载,差集
char temp[27];
memset(temp, 0, 27);
int i = 0;//指向开头
for (const char *p=m_data; *p!=NULL; p++)
{
if (!FindInString(other.m_data, *p)) temp[i++] = *p;
}
return CJiHe(temp);
}
CJiHe CJiHe::operator * (const CJiHe &other) const
{
//乘法重载,交集
char temp[27];
memset(temp, 0, 27);
int i = 0;//指向开头
for (const char *p=m_data; *p!=NULL; p++)
{
if (FindInString(other.m_data, *p)) temp[i++] = *p;
}
return CJiHe(temp);
}
CJiHe &CJiHe::operator += (const CJiHe &other)
{
//+=重载,并集
int i=strlen(m_data);//指向\0
for (const char *p=other.m_data; *p!=NULL; p++)
{
if (!FindInString(m_data, *p)) m_data[i++] = *p;
}
return *this;
}
CJiHe &CJiHe::operator -= (const CJiHe &other)
{
//-=重载,差集
char temp[27];
memset(temp, 0, 27);
int i = 0;
for (const char *p=m_data; *p!=NULL; p++)
{
if (!FindInString(other.m_data, *p)) temp[i++] = *p;
}
memcpy(m_data, temp, 27);
return *this;
}
CJiHe &CJiHe::operator *= (const CJiHe &other)
{
//*=重载,交集
char temp[27];
memset(temp, 0, 27);
int i = 0;
for (const char *p=m_data; *p!=NULL; p++)
{
if (FindInString(other.m_data, *p)) temp[i++] = *p;
}
memcpy(m_data, temp, 27);
return *this;
}
CJiHe CJiHe::operator + (const char c) const
{
//加法重载,加一个元素
char temp[27];
memcpy(temp, m_data, 27);//照自身复制
int i=strlen(temp);
if (!FindInString(temp, c)) temp[i] = c;//防止重复
return CJiHe(temp);
}
CJiHe CJiHe::operator - (const char c) const
{
//减法重载,减一个元素
char temp[27];
memset(temp, 0, 27);//设为空
int i = 0;
for (const char *p=m_data; *p!=NULL; p++)
{
if (*p != c) temp[i++] = *p;
}
return CJiHe(temp);
}
void CJiHe::View(void) const
{
//显示所有成员
printf("%s\n", m_data);
}