分享
 
 
 

IntegerSet解答

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

// File Name: IntegerSet.h

// Assignment: IntegerSet

// Description: head of IntegerSet Class

#ifndef INTEGERSET_H

#define INTEGERSET_H

class IntegerSet{

public:

IntegerSet();

//default constructor;

IntegerSet(int* set,int size);

//constructor from an int array;

IntegerSet(const IntegerSet& ia);

//copy constructor;

//three ways to initialize the instance;

friend std::istream& operator>>(std::istream & cin,IntegerSet& ia);

//input overload

friend std::ostream& operator<<(std::ostream & cout,IntegerSet& ia);

//output overload

void printSet(std::ostream &ostr=std::cout );

//output the instance;

IntegerSet operator+(IntegerSet &addedSet);

//overload union

IntegerSet operator-(IntegerSet &subbedSet);

//overload intersection

void unionOf(const IntegerSet &set1, const IntegerSet &set2 );

void intersectionOf( const IntegerSet &set1, const IntegerSet &set2 );

bool isEqualTo( const IntegerSet &set );

void insertElement( int num );

void deleteElement( int num );

bool isInSet( int num );

private:

bool integer[101];

};

/*std::ostream& operator<<(std::ostream& cout,IntegerSet& ia){

cout<<"{";

for(int index=0;index<=100;index++){

if(ia.integer[index]==true)

cout<<ia.integer[index]<<",";

}

cout<<"}";

return cout;

}*/

#endif

//IntegerSet.cpp

// Assignment: IntegerSet

// Description: detail of IntegerSet Class

#include<iostream>

#include "IntegerSet.h"

IntegerSet::IntegerSet(){

//initialize the set having no integer

for(int index=0;index<=100;index++)

integer[index]=false;

}

IntegerSet::IntegerSet(const IntegerSet & ia){

//initialize the set using copy constructor

for(int index=0;index<=100;index++)

integer[index]=false;

for(index=0;index<=100;index++)

if(ia.integer[index]) this->integer[index]=true;

}

IntegerSet::IntegerSet(int* set,int size){

//initialize the set from a integer array

for(int index=0;index<=100;index++)

integer[index]=false;

for( index=0;index<size;index++)

integer[set[index]]=true;

}

void IntegerSet::unionOf(const IntegerSet &set1, const IntegerSet &set2 ){

for(int index=0;index<=100;index++){

if(set1.integer[index]==true||set2.integer[index]==true)

this->integer[index]=true;

else

this->integer[index]=false;

}

}

void IntegerSet::intersectionOf( const IntegerSet &set1, const IntegerSet &set2 ){

for(int index=0;index<=100;index++){

if(set1.integer[index]==true&&set2.integer[index]==true)

this->integer[index]=true;

else

this->integer[index]=false;

}

}

bool IntegerSet::isEqualTo( const IntegerSet &set ){

int index;

for(index=0;index<=100;index++)

{

if(integer[index]!=set.integer[index])

return false;

}

return true;

}

void IntegerSet::printSet( std::ostream &ostr ) {

ostr<<"{";

for(int index=0;index<=100;index++){

if(this->isInSet(index)) ostr<<index<<",";

}

ostr<<'\b';

ostr<<"}"<<std::endl;

}

void IntegerSet::insertElement( int num ){

if(num<=100&&num>=0)

this->integer[num]=true;

}

void IntegerSet::deleteElement( int num ){

if(num<=100&&num>=0)

this->integer[num]=false;

}

bool IntegerSet::isInSet( int num ){

return this->integer[num];

}

IntegerSet IntegerSet::operator+(IntegerSet &addedSet){

IntegerSet temp;

for(int index=0;index<=100;index++){

if(this->integer[index]==true||addedSet.integer[index]==true)

temp.integer[index]=true;

else

temp.integer[index]=false;

}

return temp;

}

IntegerSet IntegerSet::operator-(IntegerSet &subbedSet){

IntegerSet temp;

for(int index=0;index<=100;index++){

if(this->integer[index]==true&&subbedSet.integer[index]==true)

temp.integer[index]=true;

else

temp.integer[index]=false;

}

return temp;

}

// File Name: test.cpp

// Assignment: IntegerSet

// Description: test of IntegerSet Class

#include <iostream>

#include "IntegerSet.h"

using namespace std;

istream& operator >> (istream & cin, IntegerSet& ia);

ostream& operator << (ostream & cout, IntegerSet& ia);

//Integer Class's friend to cin and cout IntegerSet

//cin and cout overload

int main(){

IntegerSet intset1,intset2,clear;

//three ways to initialize in fact

//use default constructor

char flag='y';

//flag to decide whether want quit

do{

intset1.intersectionOf(intset1,clear);

intset2.intersectionOf(intset2,clear);

cout << "Enter set1:";

cin >> intset1;

cout << "Enter set2";

cin >> intset2;

cout << "set1 is:" << intset1;

cout << "set2 is:";

intset2.printSet();

//two ways to output;

cout << "set1==set2:" << intset1.isEqualTo(intset2) << endl;

IntegerSet temp;

//temp instance to hold the output of intersection and union

//temp.intersectionOf(intset1,intset2);

cout << "Intersection of both sets is" << intset1-intset2;

//temp.unionOf(intset1,intset2);

cout << "Union of both sets is " << intset1+intset2;

int index;

char ch;

//temp variable for input

cout << "Enter numbers to remove from Set 1:";

do{

cin >> index;

ch=cin.get();

if( index<=100&&index>=0 )

intset1.deleteElement(index);

}while( ch!='\n' );

//use int and ' ' format to input

//take enter as finish sign

cout << "Enter numbers to insert into Set 2:";

do{

cin >> index;

ch=cin.get();

if( index<=100&&index>=0 )

intset2.insertElement(index);

}while( ch!='\n' );

//use int and ' ' format to input

//take enter as finish sign

cout << "set1 is:"<<intset1;

cout << "set2 is:" << intset2;

cout << "set1==set2:" << intset1.isEqualTo(intset2) << endl;

temp.intersectionOf(intset1,intset2);

cout << "Intersection of both sets is" << temp;

temp.unionOf(intset1,intset2);

cout << "Union of both sets is " << temp;

cout << "another try?y/n:";

flag=cin.get();

}while(flag!='n');

return 0;

}

ostream& operator << ( ostream & cout, IntegerSet& ia ){

cout << "{";

for( int index=0;index<=100;index++ ){

if( ia.integer[index]==true )

cout << index<< ',';

}

cout << '\b';

cout << "}" << endl;

return cout;

}

istream& operator >> ( istream & cin, IntegerSet& ia ){

int index;

char ch;

cout << "input:";

do{

cin >> index;

ch=cin.get();

if( index<=100&&index>=0 )

ia.insertElement(index);

}while( ch!='\n' );

return cin;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有