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 { i
2、nt 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 插入元素 void remove(int t);//l 删除元素 void clear();//l 清空集合 void print();//l 集合显示输出 IntSet setunion(const IntSet&
3、);//l 集合并
IntSet setdifference(const IntSet&);//l 集合差
IntSet setintsection(const IntSet&);//l 集合交
};#endif
/* intset.cpp */
#include "stdafx.h"
#include
4、Set::IntSet(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、ize-1;
while (l<=u)
{
int m=(u+l)/2;
if (t 6、x[i-1]) { int 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、cursize--;
x = new 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) 8、 { for (int i=0;i 9、ntSet& anotherset)
{
IntSet r = anotherset;
for (int i=0;i 10、turn r;
}
平时作业(2)
第1题.定义HugeInt类,计算并显示出5000阶乘的值和它的位数。5000!的值是多少?
测试示例主程序
/*********************************************************/
/* f5000.cpp */
/*********************************************************/
#include 11、 12、 hugeint.h */
#include 13、
int m_sign; //符号
int m_len; //长度
char m_num[MAXLEN]; //存储空间
};
/* hugeint.cpp */
#include "stdafx.h"
#include "hugeint.h"
#include 14、gn=0; m_len=0; }
HugeInt::HugeInt(const int &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;
}
els 15、e { m_num[1]=0; m_len=1; m_sign=1; }
}
HugeInt HugeInt::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.L 16、en()) { muti1=this->m_num; muti2=R.m_num; len1=this->m_len; 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[ 17、k]%=10; k++; }
if(carry!=0) { result[k]+=carry; Result.m_len=k; carry=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、n out; }
第2题.改善第一次作业中的IntSet,分别使用运算符+、*、-和<<表达集合并、集合交、集合差和集合输出。(必须上机验证)
/* 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 构造函数
I 19、ntSet(const IntSet&);//l 拷贝构造函数
~IntSet();//l 析构函数
void insert(int t);//l 插入元素
friend ostream& operator<<(ostream&,const IntSet&);
IntSet operator-(const IntSet&);//l 集合差
IntSet operator+(const IntSet&);//l 集合并
IntSet operator*(const IntSet&);//l 集合交
};
#endif
/* intset.cpp 20、 */
#include "stdafx.h"
#include 21、m.cursize; x=new int[maxsize=m.maxsize]; for (int i=0;i 22、d IntSet::insert(int t)
{
if (member(t)) {return;} if (cursize>=maxsize) {exit(1);} x[cursize++]=t;
for (int i=cursize-1;i>0;i--) { if (x[i] 23、size>0)
{
for (int i=0;i 24、x[i]);
return r;
}
IntSet IntSet::operator+(const IntSet& anotherset)
{ IntSet r = anotherset;
for (int i=0;i






