收藏 分销(赏)

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

上传人:二*** 文档编号:4808123 上传时间:2024-10-13 格式:DOC 页数:15 大小:49.54KB 下载积分:5 金币
下载 相关 举报
面向对象技术C++平时作业.doc_第1页
第1页 / 共15页
本文档共15页,全文阅读请下载到手机保存,查看更方便
资源描述
平时作业共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 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&);//l 集合并 IntSet setdifference(const IntSet&);//l 集合差 IntSet setintsection(const IntSet&);//l 集合交 };#endif /* intset.cpp */ #include "stdafx.h" #include <iostream> #include <cstdlib> using namespace std; #include "intset.h" 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) { cursize=m.cursize; x=new int[maxsize=m.maxsize]; for (int i=0;i<cursize;i++) { x[i]=m.x[i]; } } bool IntSet::member(int t) const { int l=0; int u=cursize-1; while (l<=u) { int m=(u+l)/2; if (t<x[m]) u=m-1; else if (t>x[m]) l=m+1; else return true; } return false; } void 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]<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<<"该集合中不存在"<<t<<"这个元素,删除失败。"<<endl; }else { 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() { if (cursize<=0) {return;} x = new int[maxsize]; cursize =0; } void IntSet::print() { cout << "{"; if (cursize>0) { for (int i=0;i<cursize;i++) { cout <<x[i]; if (i!=cursize-1) { cout <<','; } } } cout << "}"; } IntSet IntSet::setdifference(const IntSet& anotherset) { IntSet r; for (int i=0;i<cursize;i++) if(!anotherset.member(x[i])) r.insert(x[i]); return r; } IntSet IntSet::setunion(const IntSet& anotherset) { IntSet r = anotherset; for (int i=0;i<cursize;i++) if(!anotherset.member(x[i])) r.insert(x[i]); return r; } IntSet IntSet::setintsection(const IntSet& anotherset) { IntSet r; for (int i=0;i<cursize;i++) if(anotherset.member(x[i])) r.insert(x[i]); return r; } 平时作业(2) 第1题.定义HugeInt类,计算并显示出5000阶乘的值和它的位数。5000!的值是多少? 测试示例主程序 /*********************************************************/ /* f5000.cpp */ /*********************************************************/ #include <iostream> #include <iomanip> using namespace std; #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 <iostream> const int MAXLEN=202300; 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; //长度 char m_num[MAXLEN]; //存储空间 }; /* hugeint.cpp */ #include "stdafx.h" #include "hugeint.h" #include <iostream> #include <memory> #include <cmath> #include <cstring> using namespace std; HugeInt::HugeInt() { memset(m_num,0,sizeof(char)*MAXLEN); m_sign=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; } else { 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.Len()) { 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[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<<R.m_num[i]+0;} out<<std::endl; return 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 构造函数 IntSet(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 */ #include "stdafx.h" #include <iostream> #include <cstdlib> using namespace std; #include "intset.h" 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) { cursize=m.cursize; x=new int[maxsize=m.maxsize]; for (int i=0;i<cursize;i++) { x[i]=m.x[i]; } } bool IntSet::member(int t) const { int l=0; int u=cursize-1; while (l<=u) { int m=(u+l)/2; if (t<x[m]) u=m-1; else if (t>x[m]) l=m+1; else return true; } return false; } void 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]<x[i-1]) { int temp=x[i]; x[i]=x[i-1]; x[i-1]=temp; } else{ break;}} } ostream& operator<<(ostream& os, const IntSet& is) { cout << "{"; if (is.cursize>0) { for (int i=0;i<is.cursize;i++) { os <<is.x[i]; if (i!=is.cursize-1) { os <<','; } } } cout << "}"; return os; } IntSet IntSet::operator-(const IntSet& anotherset) { IntSet r; for (int i=0;i<cursize;i++) if(!anotherset.member(x[i])) r.insert(x[i]); return r; } IntSet IntSet::operator+(const IntSet& anotherset) { IntSet r = anotherset; for (int i=0;i<cursize;i++) if(!anotherset.member(x[i])) r.insert(x[i]); return r; } IntSet IntSet::operator*(const IntSet& anotherset) { IntSet r; for (int i=0;i<cursize;i++) if(anotherset.member(x[i])) r.insert(x[i]); return r;}
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服