面向对象技术C++平时作业

2018-12-04 21:57

平时作业共2次

平时作业(1)

定义、实现并测试表示由整型数元素组成的集合类型IntSet。 需提供的操作至少应包括:

? 构造函数 ? 析构函数 ? 拷贝构造函数 ? 插入元素 ? 删除元素 ? 清空集合 ? 集合并 ? 集合交 ? 集合差

? 集合显示输出

集合显示输出的格式为{元素1, 元素2,…},空集的输出为{}。

/* intset.h */ #ifndef INTSET_H #define INTSET_H class IntSet {

int cursize,maxsize; int *x; bool member(int t) const; IntSet(int m = 100);// 构造函数 IntSet(const IntSet&);// 拷贝构造函数 ~IntSet();//

析构函数

插入元素 删除元素

void insert(int t);// void remove(int t);// public:

void clear();// 清空集合 void print();// 集合显示输出

IntSet setunion(const IntSet&);// 集合并 IntSet setdifference(const IntSet&);// IntSet setintsection(const IntSet&);//

集合差 集合交

};#endif

/* intset.cpp */ #include \ #include #include using namespace std; #include \

IntSet::IntSet(int m) { if (m<1) exit(1); cursize=0; x=new int[maxsize=m]; } IntSet::~IntSet() { delete x; } IntSet::IntSet(const IntSet& m) { }

bool IntSet::member(int t) const { }

void IntSet::insert(int t) { }

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<<\该集合中不存在\<

if (member(t)) {return;} if (cursize>=maxsize) {exit(1);} x[cursize++]=t; for (int i=cursize-1;i>0;i--) {

if (x[i]

int l=0; int u=cursize-1; while (l<=u) { }

return false;

int m=(u+l)/2; if (t

u=m-1; l=m+1; return true; else if (t>x[m]) else

cursize=m.cursize; x=new int[maxsize=m.maxsize]; for (int i=0;i

x[i-1]=temp; } else{ break;}}

}

int *temp = x; 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() { }

void IntSet::print() { }

IntSet IntSet::setdifference(const IntSet& anotherset) { }

IntSet IntSet::setunion(const IntSet& anotherset) { }

IntSet IntSet::setintsection(const IntSet& anotherset) {

}

IntSet r;

for (int i=0;i

if(anotherset.member(x[i]))

r.insert(x[i]);

IntSet r = anotherset; for (int i=0;i

if(!anotherset.member(x[i]))

r.insert(x[i]);

IntSet r;

for (int i=0;i

if(!anotherset.member(x[i]))

r.insert(x[i]);

cout << \; if (cursize>0) { for (int i=0;i

if (cursize<=0) {return;} x = new int[maxsize]; cursize =0;

return r;

return r;

return r;

平时作业(2)

第1题.定义HugeInt类,计算并显示出5000阶乘的值和它的位数。5000!的值是多少? 测试示例主程序

/*********************************************************/ /* f5000.cpp */ /*********************************************************/ #include #include using namespace std; #include \int main()

{ HugeInt product =1; long N;

cout << \

cin>>N; //运行时输入5000

for (long idx=1; idx<=N;idx++) product = product*idx; cout << endl << N << \ return 0; }

/* hugeint.h */ #include const int MAXLEN=200000; class HugeInt { public:

private:

int m_sign; //符号 int m_len; //长度 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;}

char m_num[MAXLEN]; //存储空间 };

/* hugeint.cpp */ #include \ #include \ #include #include #include #include using namespace std;

HugeInt::HugeInt() { memset(m_num,0,sizeof(char)*MAXLEN); m_sign=0; m_len=0; }

HugeInt::HugeInt(const int &ioperand) { }

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.Len()) { muti1=this->m_num; muti2=R.m_num; len1=this->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) { }

return Result;

i=1;

k=j;

while(i<=len1) { result[k]+=muti1[i++]*muti2[j]+carry; carry=result[k]/10; if(carry!=0) { result[k]+=carry; Result.m_len=k; carry=0; } else j++;

memset(m_num,0,sizeof(char)*MAXLEN); if(ioperand!=0) { }

else { m_num[1]=0; m_len=1; m_sign=1; }

if(ioperand>0)

m_sign=1; m_sign=-1; else

int i=0,k=1;

int abs_R=abs(ioperand);

do { i++; m_num[i]=abs_R; abs_R/=10; }while(abs_R); m_len=i;

len2=R.m_len; }

result[k]%=10; k++; } { Result.m_len=k-1; }

}

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<


面向对象技术C++平时作业.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:CDMA路测中5个比较重要的参数

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: