一时兴起,编了一个类,可以进行集合运算

toumao 2005-11-03 12:06:06
一时兴起,编了一个类,可以进行集合运算,
把集合封装成类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);
}
...全文
101 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Snow_Ice11111 2005-11-04
  • 打赏
  • 举报
回复
共享精神!赞楼主一个!!
收藏了,谢谢你!!
toumao 2005-11-03
  • 打赏
  • 举报
回复
后来又增加了这两个运算符:
CJiHe &operator += (const char c);//-=重载,加一个元素
CJiHe &operator -= (const char c);//*=重载,减一个元素
alen_ghl 2005-11-03
  • 打赏
  • 举报
回复
e
toumao 2005-11-03
  • 打赏
  • 举报
回复
晕,这个贴子怎么在列表里找不到?不列出来?

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧