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

starcat 2009-09-09 12:06:03
自学
人笨
年纪大
不容易啊!
...全文
100 点赞 收藏 25
写回复
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
恭喜了
回复
发动态
发帖子
新手乐园
创建于2007-09-28

3.2w+

社区成员

C/C++ 新手乐园
申请成为版主
社区公告
暂无公告