2012综合实验四-参考答案(7)

2019-07-31 09:31

********************************************************/ //if the program is judged by OJ, please follow C++ head file standard #include #include

#define Max_size 10000 //may be omitted if class string is employed

class BigInteger {

private: int * m_num; //the array to store digits int m_size; //the actual number (size) of the digits int m_capacity; //the capacity to store digits int comp(const BigInteger & B); //compare this number with B void initString(char * s); //initialize the digits with string void trimSize(int start); //set actual size void clean(); //set all digits to zero

public: BigInteger(); BigInteger(int capacity); BigInteger(int * n, int size); BigInteger(char * s); BigInteger(const BigInteger & B); ~BigInteger(); void operator = (const BigInteger & B); void operator = (char * s); bool operator < (const BigInteger & B); bool operator > (const BigInteger & B); bool operator <= (const BigInteger & B); bool operator >= (const BigInteger & B); bool operator == (const BigInteger & B); bool operator != (const BigInteger & B); BigInteger operator + (const BigInteger & B); BigInteger operator - (const BigInteger & B); BigInteger operator * (const BigInteger & B); friend ostream & operator << (ostream & c, BigInteger & B); friend istream & operator >> (istream & c, BigInteger & B); };

BigInteger::BigInteger() { m_capacity=20; m_num=new int[m_capacity]; m_size=0; }

BigInteger::BigInteger(int capacity) { int n=capacity/10; if(capacity!=n*10) capacity=(n+1)*10; m_capacity=capacity; m_num=new int[m_capacity]; m_size=0; }

void BigInteger::trimSize(int start) { int i; for(i=start; i>0; i--) //no need to check i==0 if(m_num[i]) break; m_size=i+1; //actual size }

void BigInteger::clean() { int i; for(i=0; i

BigInteger::BigInteger(int * n, int size) { m_capacity=(size/10+1)*10; if(m_capacity<20) m_capacity=20; m_num=new int[m_capacity]; int i; for(i=0; i

}

void BigInteger::initString(char * s) { m_capacity=(strlen(s)/10+1)*10; if(m_capacity<20) m_capacity=20; m_num=new int[m_capacity]; int i; int n=strlen(s); for(i=0; i='0'&&s[i]<='9') //in case input is illegal m_num[n-1-i]=s[i]-'0'; else m_num[n-1-i]=0; trimSize(--i); }

BigInteger::BigInteger(char * s) { initString(s); }

BigInteger::BigInteger(const BigInteger & B) { m_capacity=B.m_capacity; m_num=new int[m_capacity]; m_size=B.m_size; int i; for(i=0; i

BigInteger::~BigInteger() { delete [] m_num; }

void BigInteger::operator =(const BigInteger & B) { delete [] m_num; m_capacity=B.m_capacity; m_num=new int[m_capacity]; m_size=B.m_size;

int i; for(i=0; i

void BigInteger::operator = (char * s) { delete [] m_num; initString(s); }

int BigInteger::comp(const BigInteger & B) { if(m_size>B.m_size) return 1; if(m_size=0; i--) if(m_num[i]>B.m_num[i]) return 1; else if(m_num[i]

bool BigInteger::operator < (const BigInteger & B) { return (-1==comp(B)); }

bool BigInteger::operator > (const BigInteger & B) { return (1==comp(B)); }

bool BigInteger::operator <= (const BigInteger & B) { int r=comp(B); return (-1==r||0==r); }

bool BigInteger::operator >= (const BigInteger & B) {

int r=comp(B); return (1==r||0==r); }

bool BigInteger::operator == (const BigInteger & B) { return (0==comp(B)); }

bool BigInteger::operator != (const BigInteger & B) { return (0!=comp(B)); }

ostream & operator << (ostream & c, BigInteger & B) { int i; for(i=B.m_size-1; i>=0; i--) c<

istream & operator >> (istream & c, BigInteger & B) { char s[Max_size]; //better replaced with class string c>>s; B=s; return c; }

BigInteger BigInteger::operator + (const BigInteger & B) { int a=0, i, minSize, maxSize, *p; if(m_size


2012综合实验四-参考答案(7).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:“小学生学习对联,提高语文素养” 实验报告

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

马上注册会员

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