1、平时作业共2次 平时作业(1) 定义、实现并测试表达由整型数元素构成集合类型IntSet。 需提供操作至少应涉及: l 构造函数 l 析构函数 l 拷贝构造函数 l 插入元素 l 删除元素 l 清空集合 l 集合并 l 集合交 l 集合差 l 集合显示输出 集合显示输出格式为{元素1,元素2,…},空集输出为{}。 /* intset.h */ #ifndef INTSET_H #define INTSET_H class IntSet { int cu
2、rsize,maxsize; int *x; bool member(int t) const; public: IntSet(int m = 100);//l 构造函数 IntSet(const IntSet&);//l 拷贝构造函数 ~IntSet();//l 析构函数 void insert(int t);//l 插入元素 void remove(int t);//l 删除元素 void clear();//l 清空集合 void print();//l 集合显示输出 IntSet setunion(const IntSet&);//l 集
3、合并
IntSet setdifference(const IntSet&);//l 集合差
IntSet setintsection(const IntSet&);//l 集合交
};#endif
/* intset.cpp */
#include "stdafx.h"
#include
4、tSet(int m) { if (m<1) exit(1);cursize=0;x=new int[maxsize=m];}
IntSet::~IntSet() { delete x;}
IntSet::IntSet(const IntSet& m)
{
cursize=m.cursize;x=new int[maxsize=m.maxsize];for (int i=0;i 5、e (l<=u)
{
int m=(u+l)/2;
if (t 6、 temp=x[i];x[i]=x[i-1];x[i-1]=temp;} else{ break;}}
}
void IntSet::remove(int t)
{
int flag = 0; int pos;
for (int i = 0;i < cursize;i++) { if (t==x[i]) { flag = 1;pos = i;} }
if (flag == 0)
{
cout<<"该集合中不存在"< 7、w int[cursize];
for (int j = 0;j < pos;j++) { x[j] = temp[j];}
for (int i = pos;i < cursize;i++) { x[i] = temp[i+1];} }
}
void IntSet::clear()
{
if (cursize<=0) {return;} x = new int[maxsize];cursize =0;
}
void IntSet::print()
{
cout << "{";if (cursize>0) { for (int i=0;i 8、) { cout < 9、 r = anotherset;
for (int i=0;i 10、geInt类,计算并显示出5000阶乘值和它位数。5000!值是多少?
测试示例主程序
/*********************************************************/
/* f5000.cpp */
/*********************************************************/
#include 11、
#include "hugeint.h"
int main()
{ HugeInt product =1;
long N;
cout << "enter n :" ;
cin>>N; //运营时输入5000
for (long idx=1;idx<=N;idx++) product = product*idx;
cout << endl << N << "!= " << product << endl;
return 0;
}
/* hugeint.h */
#include 12、const int MAXLEN=00;
class HugeInt
{
public:
HugeInt();
HugeInt(const int& iOperand);
friend std::ostream& operator <<(std::ostream& out,HugeInt &R);
HugeInt operator *(HugeInt &R);
HugeInt operator *(int R);
int Len(){return m_len;}
private:
int m_sign;//符号
int m_len;//长度
ch 13、ar m_num[MAXLEN];//存储空间
};
/* hugeint.cpp */
#include "stdafx.h"
#include "hugeint.h"
#include 14、ioperand)
{
memset(m_num,0,sizeof(char)*MAXLEN);
if(ioperand!=0)
{
if(ioperand>0)
m_sign=1;
else
m_sign=-1;
int i=0,k=1;
int abs_R=abs(ioperand);
do { i++;m_num[i]=abs_R%10;abs_R/=10;}while(abs_R);
m_len=i;
}
else { m_num[1]=0;m_len=1;m_sign=1;}
}
HugeInt Huge 15、Int::operator *(int R) { HugeInt hInt=R;return (*this)*hInt;}
HugeInt HugeInt::operator *(HugeInt &R)
{
HugeInt Result=0;
Result.m_sign=this->m_sign*R.m_sign;
char *muti1,*muti2,*result=Result.m_num;
int len1,len2;
if(this->m_len>R.Len()) { muti1=this->m_num;muti2=R.m_num;len1=this->m_le 16、n;len2=R.m_len;}
else { muti1=R.m_num;muti2=this->m_num;len2=this->m_len;len1=R.m_len;}
int i=1,j=1,k=1,carry=0;
while(j<=len2)
{
i=1; k=j;
while(i<=len1) { result[k]+=muti1[i++]*muti2[j]+carry;carry=result[k]/10;result[k]%=10;k++;}
if(carry!=0) { result[k]+=carry;Result.m_len=k;ca 17、rry=0;} else { Result.m_len=k-1;}
j++;
}
return Result;
}
std::ostream& operator <<(std::ostream &out,HugeInt &R) { int i;if(R.m_sign==-1) { out<<"-";} for(i=R.m_len;i!=0;i--) { out< 18、
/* intset.h */
#ifndef INTSET_H
#define INTSET_H
class IntSet {
int cursize,maxsize; int *x; bool member(int t) const;
public:
IntSet(int m = 100);//l 构造函数
IntSet(const IntSet&);//l 拷贝构造函数
~IntSet();//l 析构函数
void insert(int t);//l 19、 插入元素
friend ostream& operator<<(ostream&,const IntSet&);
IntSet operator-(const IntSet&);//l 集合差
IntSet operator+(const IntSet&);//l 集合并
IntSet operator*(const IntSet&);//l 集合交
};
#endif
/* intset.cpp */
#include "stdafx.h"
#include 20、m>
#include 21、 }
}
bool IntSet::member(int t) const
{
int l=0; int u=cursize-1;
while (l<=u)
{
int m=(u+l)/2;
if (t 22、);} x[cursize++]=t;
for (int i=cursize-1;i>0;i--) { if (x[i] 23、ize-1)
{
os <<',';
}
}
}
cout << "}";
return os;
}
IntSet IntSet::operator-(const IntSet& anotherset)
{ IntSet r;
for (int i=0;i






