Home > Back-end >  Finish problem sets, found himself overloading division algorithm to write too bother sew, can you h
Finish problem sets, found himself overloading division algorithm to write too bother sew, can you h

Time:02-10

Problem sets are such



#include
#include
#include
#include
using namespace std;

The class CHugeInt {
Friend ostream& Operator<(ostream& , CHugeInt&);
Friend istream& Operator> (istream& , CHugeInt&);
Public:
//3 - general constructor argument long
//3 - general constructor argument string
CHugeInt (int);
CHugeInt (const char *);
//# 4 - overloaded + operator parameters this
CHugeInt operator + (const CHugeInt&);
//5 - overloaded *
CHugeInt operator * (const CHugeInt&) Const;
//# 6 - reload/
CHugeInt operator/(const CHugeInt& LHS) const;

//7 to relational operator
Bool operator<=(const CHugeInt&) Const;
Bool operator<(const CHugeInt&) Const;
Bool operator>=(const CHugeInt&) Const;
Bool operator> (const CHugeInt&) Const;
Boolean operator!=(const CHugeInt&) Const;
Boolean operator==(const CHugeInt&) Const;

Private:
The static const int m_maxshort=30;
Int m_len;
Short int m_myshort [m_maxshort];
Short m_ne;//value & lt; 0 indicates a negative number is greater than & gt;=said positive
No.//overload -
CHugeInt operator - (const CHugeInt&) Const;
};

CHugeInt: : CHugeInt (int LHS) {
//each element initial myshort array of 0
for(int i=0; I* (m_myshort + I)=0;

//the incoming LHS save from right to left to decompose to myshort array
Int I=m_maxshort - 1;
Int j=0;
While (lhs> 0 {
* (m_myshort + I)=LHS % 10;
LHS=LHS/10;
- I;
+ + j;
}
//keep myshort effective length to len
M_len=j;
M_ne=1;//default is positive
}
CHugeInt: : CHugeInt (const char * LHS) {
//each element initial myshort array of 0
for(int i=0; I* (m_myshort + I)=0;

//the incoming LHS save from right to left to decompose to myshort array
Int I=m_maxshort - 1;
M_len=strlen (LHS);
M_ne=1;//default is positive
//whether the data crossing the line
If (m_len & gt; M_maxshort) {
Cout<& lt;" The input data is beyond the range, can't say ";
exit(1);
}

Int j=m_len - 1;
While (j>=0 & amp; & I>=0) {
* (m_myshort + I)=* (LHS + j) - '0'.//the char numerical saved to the array character to
- j;
- I;
}
}
//# 4 - overloaded + operator parameters this
CHugeInt CHugeInt: : operator + (const CHugeInt& LHS) {
CHugeInt temp (0);
Short flag=0;//carry flag
Short I=m_maxshort - 1;//an array starting position
Short k=0;

//when the plus sign (left several effective value, or the right number of RMS is not null or carry flag not to 0
While (m_len - k> 0 | | LHS. M_len - k> 0 | | (flag!=0 & amp; & 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 & lt; 0) cout<& lt;" - ";
For (int I=0, j=CHugeInt: : m_maxshort - LHS. M_len; ICout<* (LHS. M_myshort + j);

}
Return cout.
}
Istream& Operator> (istream& Cin, CHugeInt& LHS) {
Char * p=new char [CHugeInt: : m_maxshort + 1];
Cout<& lt;" Please enter no more than 30 numerical data: ";
Cin> Setw (30) & gt;> p;
LHS=p;

Return cin.
}
//5 - overloaded *
CHugeInt CHugeInt: : operator * (const CHugeInt& LHS) const {
Static int I=0;//a loop counter I
The static CHugeInt temp (0);//save the multiplier provisional results
The static CHugeInt sum (0);//add
the result of the multiplier
//before the end of recursion to temp, sum, I the three variable values 0 otherwise it will bring values into the next multiplication
If (LHS. M_len - I==0) {//if the effective bit for multiplier is calculated out directly return
i=0;
CHugeInt TMP=sum;
Sum=0;
Temp=0;
Return the TMP;
}

Int z=LHS. M_maxshort - I;//every time to recalculate the position in the array to save
Int flag=0;//carry value

For (int j=m_maxshort - 1; J>=m_maxshort - m_len | | (flag> 0 & amp; & J>=0); - j) {
Int c=LHS. M_myshort [LHS m_maxshort - I - 1);
If (c==0) continue;//must be c=0 without calculating the value of the column of
C=c * m_myshort [j] + flag;
Z=z - 1;
Temp. M_myshort [z]=c % 10;//save the remainder
Flag=c/10;//business namely carry value
+ + temp. M_len;
}
//if carry values are not zero, display error
If (flag> 0 {
CoutCout<& lt;" Multiplication result overflow, the result is not correct "& lt; exit(1);
}

Sum=sum + temp;//temp and sum up
+ + I;
//empty the temporary number variable
For (int j=temp m_maxshort - 1; J>=temp. M_maxshort - temp. M_len; - j)
* (temp. M_myshort + j)=0.

//must multiplier from right to left to save bits to 29 element in the array save save ten to 2800 to 27 on
//I=0 means bits, 1 said 10 2, one hundred, said he was "on
Temp. M_len=I;

The return operator * (LHS);//recursive
}
//# 6 - reload/
CHugeInt CHugeInt: : operator/(const CHugeInt& LHS) const {
If (LHS==CHugeInt (" 0 ")) {//divisor LHS is 0, display an error
Cout<& lt;" Divisor cannot be zero "& lt; exit(1);
} else if (LHS==CHugeInt (" 1 "))//divisor LHS is 1 return dividend
Return * this;
Else if (LHS==(* this))//divisor is equal to the dividend return 1
Return CHugeInt (1);
Else if (lhs> (* this))//divisor is greater than the dividend return 0
Return CHugeInt (0);

CHugeInt c (" 0 ");//save multiples (commercial)
CHugeInt I (" 10 ");
CHugeInt j (" 1 ");
C=LHS * I;
For (; (* this) & gt; C) {
J=I;
I=I * CHugeInt (" 10 ");
C=LHS * I; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related