![]() |
#2
ma35872008-10-21 20:43
写的乱了点,里面好像有错误,呵呵
![]() #include <iostream> #include <fstream> using namespace std; class AbsCollection{ protected: int *iPtr; //指向数组的指针 int last; //数组最后一个元素的位置 int Max_Size; //数组长度 public: AbsCollection(){}; AbsCollection(int size) { Max_Size=size; iPtr=new int[Max_Size]; last=-1; }; virtual ~AbsCollection(){} void OutSet() { if(last<0) { cout<<"Empty set!"<<endl; return; } cout<<"The number of elements:" <<last+1<<endl; for(int *ip=iPtr;ip<=iPtr+last;) cout<<*ip++<<" "; cout<<endl; } int getlast() const {return last;} int getitem(int i) const {return iPtr[i];} }; class Set : public AbsCollection { public: Set():AbsCollection(100){} Set(const char *, int); ~Set(); Set operator = (const Set &s); void input(int x); //增加一个元素 void erase(int x); //删除一个元素成员函数 bool isInSet(int x); //编写判别元素是否属于集合的成员函数 void intersection(Set &s); //求两个集合交的函数成员 void sub(Set &s); //求两个集合差的成员函数 void sort(); //编写集合排序的成员函数 }; Set::Set(const char * file, int maxsize):AbsCollection(maxsize) { ifstream infile(file); int i = 0; while (infile) { infile >> iPtr[i]; i++; } last = i - 1; } Set::~Set() { ofstream outfile("out.txt"); if (last > 0) { for (int i = 0; i <= last; i++) { outfile << iPtr[i] << " "; } outfile << endl; } delete [] iPtr; } Set Set::operator = (const Set &s) { if (s.getlast() != -1) { for (int i = 0; i < s.getlast(); ++i) { this->iPtr[i] = s.iPtr[i]; } } return *this; } void Set::input(int x) { if (last + 1 > Max_Size) { cout << "超出最大范围" << endl; } else { for (int i = 0; i < last; i++) { if (x == iPtr[i]) { cout << "有相同元素,插入失败" << endl; return; } } iPtr[i] = x; last++; } } void Set::erase(int x) { int flag = 0; for (int i = 0; i < last; i++) { if (x == iPtr[i]) { for (int j = i; j < last; ++j) { iPtr[j] = iPtr[j + 1]; } iPtr[j] = 0; --last; flag = 1; break; } } if (!flag) { cout << "没有这个元素" << endl; } } bool Set::isInSet(int x) { for (int i = 0; i < last; ++i) { if (x == iPtr[i]) { return 1; } } return 0; } void Set::intersection(Set &s) { for (int i = 0; i < s.getlast(); i++) { if (isInSet(s.getitem(i))) { cout << s.getitem(i) << " "; } } } void Set::sub(Set &s) { for (int i = 0; i < s.getlast(); i++) { if (isInSet(s.getitem(i))) { erase(s.getitem(i)); } } } void Set::sort() { int flag = 0; for (int i = 0; i < last; ++i) { for (int j = last-1; j > i; --j) { if (iPtr[j] < iPtr[j - 1]) { iPtr[j] = iPtr[j - 1] + iPtr[j]; iPtr[j - 1] = iPtr[j] - iPtr[j - 1]; iPtr[j] = iPtr[j] - iPtr[j - 1]; flag = 1; } } if (flag == 0) { return; } } } Set operator + (Set &s1, Set &s2) //重载+运算符,用于求两个集合的并 { Set s; for (int i = 0; i < s2.getlast(); ++i) { if (s1.isInSet(s2.getitem(i))) { continue; } else { s1.input(s2.getitem(i)); } } s = s1; return s; } ostream & operator << (ostream &out, Set &s) //重载<<运算符,用于在屏幕上输出集合元素 { if (s.getlast()<0) { out << "error!"; return out; } for (int i = 0; i < s.getlast(); ++i) { out << s.getitem(i) << " "; } out << endl; return out; } int main() { Set sa("in1.txt",100); cout << sa; return 0; } |
在VC++中建立一个Win32 Console Application工程。
实现一个 AbsCollection类,该类包含的成员有:储存元素的整型数组、标志最后一个元素位置的整型last及数组长度、用来对集合中元素进行枚举的一个成员函数,并从AbsCollection类派生出Set类。Set对象中的数组是包含零个或多个无序的非重复元素,f1.dat 、f2.dat 文件中存放初始数据元素,由Set对象构造函数打开文件读入到数组中,经过加工处理后的运算结果存入该对象的数组中,由Set 对象析构函数将数组中元素写入指定的磁盘文件中。已知AbsCollection类定义如下:
class AbsCollection{
protected:
int *iPtr; //指向数组的指针
int last; //数组最后一个元素的位置
int Max_Size; //数组长度
public:
AbsCollection(){};
AbsCollection(int size){
Max_Size=size;
iPtr=new int[Max_Size];
last=-1;
};
~AbsCollection(){}
void OutSet(){
if(last<0){
cout<<"Empty set!"<<endl;
return;
}
cout<<"The number of elements:" <<last+1<<endl;
for(int *ip=iPtr;ip<=iPtr+last;)
cout<<*ip++<<” “;
cout<<endl;
}
};
(1)定义Set类,编写Set类构造函数和析构函数,使它们完成打开文件读入元素到数组中和将数组中元素写入指定的f1.dat磁盘文件中;
(2)编写集合增加一个元素成员函数input(int x);(注:如果集合中已有相同的元素,则不再加入)
(3)编写删除一个元素成员函数erase(int x);
(4)编写判别元素是否属于集合的成员函数isInSet(int x);
(5)编写求两个集合交的函数成员intersection(Set &s);
(6)编写求两个集合差的成员函数;
(7)重载+运算符,用于求两个集合的并;
(8)编写集合排序的成员函数sort();(从大到小排序)
(9)重载<<运算符,用于在屏幕上输出集合元素。