庆祝写“类”的交集程序成功,散分!

starcat 2009-09-09 12:06:03
自学
人笨
年纪大
不容易啊!
...全文
145 25 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoliang_c 2009-09-11
  • 打赏
  • 举报
回复
接分!
chb610 2009-09-09
  • 打赏
  • 举报
回复
恭喜接分
jinwei1984 2009-09-09
  • 打赏
  • 举报
回复
jf
  • 打赏
  • 举报
回复
恭喜恭喜
andy0409 2009-09-09
  • 打赏
  • 举报
回复
嗯,好东西。
my578009030 2009-09-09
  • 打赏
  • 举报
回复
jf...
飞天御剑流 2009-09-09
  • 打赏
  • 举报
回复
万里征程始于足下!
liuysheng 2009-09-09
  • 打赏
  • 举报
回复
类的交集,做什么用的?谢谢
amossavez 2009-09-09
  • 打赏
  • 举报
回复
aopha 2009-09-09
  • 打赏
  • 举报
回复
继续努力
starcat 2009-09-09
  • 打赏
  • 举报
回复
大部分是copy例题的。
#include <iostream>
using namespace std;

const int MaxSize = 100;

class Set {
int len;
char members[MaxSize];

int find(char ch);

public:
Set() { len = 0; }

int getLength() { return len; }

void showset();
bool isMember(char ch);

Set operator+(char ch);
Set operator-(char ch);
Set operator&(char ch);

Set operator+(Set ob2);
Set operator-(Set ob2);

Set operator&(Set ob2);
};

int Set::find(char ch) {
int i;

for(i = 0; i < len; i++)
if(members[i] == ch) return i;

return -1;
}

void Set::showset() {
cout << "{ ";
for(int i = 0; i < len; i++)
cout << members[i] << " ";

cout << "}\n";
}

bool Set::isMember(char ch) {
if(find(ch) != -1) return true;
return false;
}

Set Set::operator+(char ch) {
Set newset;

if(len == MaxSize) {
cout << "Set is full.\n";
return *this;
}

newset = *this;

if(find(ch) == -1) {
newset.members[newset.len] = ch;
newset.len++;
}
return newset;
}

Set Set::operator-(char ch) {
Set newset;
int i = find(ch);

for(int j = 0; j < len; j++)
if(j != i) newset = newset + members[j];

return newset;
}

Set Set::operator+(Set ob2) {
Set newset = *this;

for(int i = 0; i < ob2.len; i++)
newset = newset + ob2.members[i];

return newset;
}

Set Set::operator-(Set ob2) {
Set newset = *this;

for(int i = 0; i < ob2.len; i++)
newset = newset - ob2.members[i];

return newset;
}

Set Set::operator&(Set ob2) {
Set newset;

for(int i = 0; i < len; i++)
if(ob2.find(members[i]) != -1)
newset = newset + members[i];

return newset;
}

int main() {
Set s1;
Set s2;
Set s3;

s1 = s1 + 'A';
s1 = s1 + 'B';
s1 = s1 + 'C';

cout << "s1 after adding A B C: ";
s1.showset();

cout << "\n";

cout << "Testing for membership using isMember().\n";
if(s1.isMember('B'))
cout << "B is a member of s1.\n";
else
cout << "B is not a member of s1.\n";

if(s1.isMember('T'))
cout << "T is member of s1.\n";
else
cout << "T is not a member of s1.\n";

cout << "\n";

s1 = s1 - 'B';
cout << "s1 after s1 = s1 - 'B': ";
s1.showset();

s1 = s1 - 'A';
cout << "s1 after s1 = s1 - 'A': ";
s1.showset();

s1 = s1 - 'C';
cout << "s1 after s1 = s1 - 'C': ";
s1.showset();

cout << "\n";

s1 = s1 + 'A';
s1 = s1 + 'B';
s1 = s1 + 'C';
cout << "s1 after adding A B C: ";
s1.showset();

cout << "\n";

s2 = s2 + 'A';
s2 = s2 + 'X';
s2 = s2 + 'W';

cout << "s2 after adding A X W: ";
s2.showset();

cout << "\n";

s3 = s1 + s2;
cout << "s3 after s3 = s1 + s2: ";
s3.showset();

s3 = s3 - s1;
cout << "s3 after s3 - s1: ";
s3.showset();

cout << "\n";

cout << "s2 after s2 = s2 - s2: ";
s2 = s2 - s2;
s2.showset();

cout << "\n";

s2 = s2 + 'C';
s2 = s2 + 'B';
s2 = s2 + 'A';

cout << "s2 after adding C B A: ";
s2.showset();

cout << "\n";

s1 = s1 - s1;
s1 = s1 + 'X';
s1 = s1 + 'A';
cout << "s1 after adding X A: ";
s1.showset();
cout << "\n";
s3 = s1 & s2;
cout << "The intersection of s1 and s2:\n";
s3.showset();

return 0;
}

wanjingwei 2009-09-09
  • 打赏
  • 举报
回复
接分
acdbxzyw 2009-09-09
  • 打赏
  • 举报
回复
接分。
cphj 2009-09-09
  • 打赏
  • 举报
回复
程序啥样?发上来晒一晒啊
mstlq 2009-09-09
  • 打赏
  • 举报
回复
还没毕业就说自己年纪大,不太好哦^_^
dclchj 2009-09-09
  • 打赏
  • 举报
回复
接分,祝贺楼主!
whg01 2009-09-09
  • 打赏
  • 举报
回复
纯接分。
kernelshell 2009-09-09
  • 打赏
  • 举报
回复
一起努力。搂主也是高手了。
fallening 2009-09-09
  • 打赏
  • 举报
回复
楼主有否看过stl的set的交集实现?
lintaonv 2009-09-09
  • 打赏
  • 举报
回复
恭喜了
加载更多回复(5)

33,321

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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