一份测试卷,大家做做看(2)
3.请完成以下集合类模板的设计,集合类对象的创建及使用。
#include<iostream.h>
const maxcard=20;
template<class T>class set{
T elems[maxcard];
int card;
public:
set( ){______1______;}
friend bool operator&(T,set);
friend bool operator==(set,set);
friend bool operator!=(set,set);
friend ____2____ operator+(set,set);
friend set operator+(set,T);
friend set operator-(set,T);
friend set operator*(set,set);
friend ostream&operator<<(_____3_____,const set&s){
for(int i=0;i<s.card;i++)out<<s.elems[i]<<" ";
return out;}
};
template<class T>bool operator&(T e ,set<T> s){
for(int i=0;i<s.card;i++)
if(s.elems[i]==e)
return true;
return _____4_____;}
template<class t>bool operator==(set<t> s1,set<t> s2){
if(s1.card!=s2.card)return false;
for(int i=0;i<s1.card;i++)
if(!(s1.elems[i]&s2))return false;
return true;}
template<class t>bool operator!=(set<t> s1,set<t> s2)
{ return !(s1==s2)?true:false;}
template<class t>set<t> operator+(set<t> s,t e)
{____5_____ res=s;
if(s.card<maxcard)
if(!(e&s))res.elems[res.card++]=e;
return res;}
template<class t>set<t> operator+(set<t> s1,set<t> s2)
{set<t> res=s1;
for(int i=0;i<s2.card;i++)
res=res+s2.elems[i];
return ___6___;}
template<class t>set<t> operator-(set<t> s,t e)
{set<t> res=s;
if(!(e&s))return res;
for(int i=0;i<s.card;i++)
if(s.elems[i]==e)
for(;i<s.card-1;i++)
res.elems[i]=res.elems[i+1];
____7____;
return res;}
template<class t>set<t> operator*(set<t> s1,_______8_______){
set<t> res;
for(_____9_____;i<s1.card;i++)
for(int j=0;j<s2.card;j++)
if(s1.elems[i]==s2.elems[j])
{ res.elems[res.card++]=s1.elems[i];break;}
return res;}
void main( ){
set<int> ______10______;
set<double> s4,s5;
for(int i=0;i<10;i++)
s1=s1+i;
s2=s1;
for(i=0;i<2;i++)
s1=s1-i;
s3=s1*s2;
cout<<s3<<endl;
for(i=0;i<5;i++)
s4=s4+3.5*i;
s5=s4+s5;
cout<<s5<<endl;
}
4.设计一个学生类,其属性有学号、姓名、性别、年龄和计算机原理成绩,并定义相应操作,重载析取符( >> )和插入符( << )以支持I/O操作。然后在main函数中定义学生对象数组,对于学生对象数组从磁盘文件“student.txt” 进行输入,最后以学号n为参数在数组中查找学号为n的学生,并显示该生的全部信息。