習題是這樣的

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
using namespace std;
class CHugeInt{
friend ostream& operator<<(ostream&,CHugeInt&);
friend istream& operator>>(istream&,CHugeInt&);
public:
//3- 一般建構式 引數 long
//3- 一般建構式 引數字串
CHugeInt(int);
CHugeInt(const char*);
//4- 多載+號運算子 引數本類
CHugeInt operator+(const CHugeInt&);
//5-多載*號
CHugeInt operator*(const CHugeInt&)const;
//6-多載/號
CHugeInt operator/(const CHugeInt& lhs)const;
//7-關系運算子
bool operator<=(const CHugeInt&)const;
bool operator<(const CHugeInt&)const;
bool operator>=(const CHugeInt&)const;
bool operator>(const CHugeInt&)const;
bool operator!=(const CHugeInt&)const;
bool operator==(const CHugeInt&)const;
private:
static const int m_maxshort=30;
int m_len;
short int m_myshort[m_maxshort];
short m_ne; //值<0表示負數 大于>=表示正數
//多載-號
CHugeInt operator-(const CHugeInt&)const;
};
CHugeInt::CHugeInt(int lhs){
//myshort陣列每個元素初始為0
for(int i=0;i<m_maxshort;++i)
*(m_myshort+i)=0;
//把傳進來的lhs從右向左分解保存到myshort陣列內
int i=m_maxshort-1;
int j=0;
while(lhs>0){
*(m_myshort+i) = lhs % 10;
lhs=lhs/10;
--i;
++j;
}
//把myshort有效長度保存到len
m_len=j;
m_ne=1; //默認為正數
}
CHugeInt::CHugeInt(const char *lhs){
//myshort陣列每個元素初始為0
for(int i=0;i<m_maxshort;++i)
*(m_myshort+i)=0;
//把傳進來的lhs從右向左分解保存到myshort陣列內
int i=m_maxshort-1;
m_len=strlen(lhs);
m_ne=1; //默認為正數
//判斷資料是否越界
if(m_len> m_maxshort){
cout<<"輸入資料超出有效范圍,無法表示";
exit(1);
}
int j=m_len-1;
while(j>=0 && i>=0){
*(m_myshort+i) = *(lhs+j) - '0'; //把char字符轉成數值保存到陣列中
--j;
--i;
}
}
//4- 多載+號運算子 引數本類
CHugeInt CHugeInt::operator+(const CHugeInt& lhs){
CHugeInt temp(0);
short flag=0; //進位標志
short i=m_maxshort-1; //陣列保存開始位置
short k=0;
// 當加號(左邊數有效值,或者右邊數有效值不為空 或者進位標志不為0
while(m_len-k> 0 || lhs.m_len-k> 0 || (flag !=0 && i>=0)){
short j=*(m_myshort+i) + *(lhs.m_myshort+i)+ flag;
if(j>9){
flag=1;
*(temp.m_myshort+i)= j-10;
}else{
flag=0;
*(temp.m_myshort+i)= j;
}
--i;k++;
++temp.m_len;
}
return temp;
}
ostream& operator<<(ostream& cout,CHugeInt& lhs){
if(lhs.m_ne<0) cout<<"-";
for(int i=0,j=CHugeInt::m_maxshort- lhs.m_len; i<lhs.m_len; ++i,++j){
cout<<*(lhs.m_myshort+j);
}
return cout;
}
istream& operator>>(istream& cin,CHugeInt& lhs){
char *p=new char[CHugeInt::m_maxshort+1];
cout<<"請輸入不超過30個數值資料:";
cin>>setw(30)>>p;
lhs=p;
return cin;
}
//5-多載*號
CHugeInt CHugeInt::operator*(const CHugeInt& lhs)const{
static int i=0; //相當回圈中計數器i
static CHugeInt temp(0); //保存乘數臨時結果
static CHugeInt sum(0); //對乘數結果相加
//結束遞回前必須對temp,sum,i這3個變數值清0 否則會把值帶入下一次乘法
if(lhs.m_len-i ==0){ //如果乘數的有效位都計算完 直接回傳
i=0;
CHugeInt tmp=sum;
sum=0;
temp=0;
return tmp;
}
int z=lhs.m_maxshort-i; //每次都重新計算在陣列中要保存的位置
int flag=0; //進位值
for(int j=m_maxshort-1;j>=m_maxshort-m_len || (flag>0 && j>=0);--j){
int c=lhs.m_myshort[lhs.m_maxshort-i-1];
if(c==0) continue; //必須 c=0時不必計算這列的值
c=c * m_myshort[j] + flag;
z=z-1;
temp.m_myshort[z]= c % 10; //保存余數
flag= c/ 10; //商即進位值
++temp.m_len;
}
//如果進位值不為0,顯示出錯
if(flag>0){
cout<<temp<<endl;
cout<<"乘法結果溢位,結果不正確"<<endl;
exit(1);
}
sum=sum+temp; //把temp和sum相加
++i;
//清空臨時數變數
for(int j=temp.m_maxshort-1;j>=temp.m_maxshort-temp.m_len;--j)
*(temp.m_myshort+j)=0;
//必須 乘數從右往左 個位保存到陣列第個29元素 十位保存到28 百位保存到27類推
//i=0表示個位,1表示十位 2表示百位,類推
temp.m_len=i;
return operator*(lhs); //遞回
}
//6-多載/號
CHugeInt CHugeInt::operator/(const CHugeInt& lhs)const{
if(lhs== CHugeInt("0")){ //除數lhs為0,顯示錯誤
cout<<"除數不能為0"<<endl;
exit(1);
}else if(lhs== CHugeInt("1")) //除數lhs為1 回傳被除數
return *this;
else if(lhs == (*this)) //除數等于被除數 回傳1
return CHugeInt(1);
else if(lhs> (*this)) //除數大于被除數 回傳0
return CHugeInt(0);
CHugeInt c("0"); //保存倍數(商)
CHugeInt i("10");
CHugeInt j("1");
c=lhs*i;
for(;(*this) > c;){
j=i;
i=i*CHugeInt("10");
c=lhs*i;
}
CHugeInt d=0;
do{
c=j+i; //左邊界+右邊界
c=c * CHugeInt(5); //把c值 乘5
//把c的值右移一位
for(int a=c.m_maxshort-1;a>(c.m_maxshort- c.m_len);--a){
c.m_myshort[a]=c.m_myshort[a-1];
}
c.m_myshort[c.m_maxshort-c.m_len]=0; //把最右邊值用0復蓋
--c.m_len; //把有效位數減1
d=lhs * c;
if( d >(*this)){
i=c;
}else if(d <(*this))
j=c;
d=(*this)-d; //用被除數減去 倍數和除數積后的值
}while( d>=lhs || d<0) ; //當d為正數或者為負數繼續回圈(當d為<除數 且 d為正數時跳出do)
return c;
}
bool CHugeInt::operator<=(const CHugeInt& lhs)const{
return !((*this) > lhs);
}
bool CHugeInt::operator<(const CHugeInt& lhs)const{
return !((*this)>= lhs);
}
bool CHugeInt::operator>=(const CHugeInt& lhs)const{
return ((*this) > lhs) || ((*this)== lhs);
}
bool CHugeInt::operator>(const CHugeInt& lhs)const{
if( this->m_ne >0 && lhs.m_ne<0) //如果左數為正數 右數為負數
return true;
else if(this->m_ne<0 && lhs.m_ne>0) //如果左數為負數 右數為正數
return false;
else if(this->m_ne<0 && lhs.m_ne<0){ //如果兩個都是負數
//把兩個數改成正數
CHugeInt a=(*this);
a.m_ne=1;
CHugeInt b=lhs;
b.m_ne=1;
return !(a>b); //呼叫自己比大小
}
//兩個數都是正數的比較
if((*this).m_len < lhs.m_len)
return false;
else if((*this).m_len == lhs.m_len){
for(int i= lhs.m_maxshort - lhs.m_len;i<lhs.m_maxshort;++i){
if((*this).m_myshort[i] > lhs.m_myshort[i])
return true;
else if((*this).m_myshort[i] < lhs.m_myshort[i])
return false;
}
}
return true;
}
bool CHugeInt::operator!=(const CHugeInt& lhs)const{
return !(*this==lhs);
}
bool CHugeInt::operator==(const CHugeInt& lhs)const{
if((*this).m_len != lhs.m_len) return false;
if((*this).m_ne != lhs.m_ne) return false;
for(int i= (*this).m_maxshort - (*this).m_len;i<(*this).m_maxshort;++i){
int x=(*this).m_myshort[i];
int y= lhs.m_myshort[i];
if( x != y)
return false;
}
return true;
}
CHugeInt CHugeInt::operator-(const CHugeInt& lhs)const{
const CHugeInt *a=this;
const CHugeInt *b=&lhs;
CHugeInt temp(0); //臨時變數用于保存回傳的結果
if((*this) < lhs){ //被減數小于減數 交換兩數
const CHugeInt *p=a;
a=b;b=p;
temp.m_ne=-1; //把結果先設成負數
}
int flag=0; //借位標志
for(int i=a->m_maxshort-1;i>= (a->m_maxshort)-(a->m_len);--i){
int x=a->m_myshort[i];
int y=b->m_myshort[i];
if((x-flag-y) <0){
x=x+10-flag-y;
flag=1;
}else{
x=x-y-flag;
flag=0;
}
temp.m_myshort[i]=x;
++temp.m_len;
}
//洗掉最前面為0的數值
for(int i=temp.m_maxshort- temp.m_len;i<temp.m_maxshort;++i){
int x=temp.m_myshort[i];
if(x !=0)
break;
else
--temp.m_len;
}
return temp;
}
int main()
{
//多載乘法
CHugeInt a="5785314568951354621456";
CHugeInt b="516";
CHugeInt c=0;
c=a*b;
cout<<"a*b 的結果="<<c<<endl;
//多載除法
a="301550978545357423501";
b="5000012456";
c=a/b;
cout<<"a/b 的結果="<<c<<endl;
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/258220.html
標籤:新手樂園
上一篇:初學C語言的小白求助各位大佬
下一篇:有關結構體的問題求助
