Home > Back-end >  Everybody look at me how the C algorithm of large number is wrong
Everybody look at me how the C algorithm of large number is wrong

Time:10-17

 # include & lt; Iostream> 
#include


using namespace std;
The class BInt//statement of large number
{
Public:
BInt () {};
BInt (char [] s);
BInt (int I);//use plain integer structure of large number
Public:
Int operator=(int bn2);//overloaded assignment operations, with average number to allow large assignment
Char * operator=(char * s1);//overloaded assignment operations, allows for a string of large Numbers for the assignment
Public:
Friend cost & amp; The operator & gt;> (cost & amp; Input, BInt& Bn) {input & gt;> Bn. M_s; Return the input; }//overload input operator
Friend ostream & amp; The operator & lt; <(ostream & amp; The output, BInt& Bn) {output & lt; Friend BInt operator + (const BInt& Bn1, const BInt& Bn2);//reloading the addition operator
Friend BInt operator - (const BInt& Bn1, const BInt& Bn2);//reloading the subtraction operator
Friend Boolean operator & lt; (const BInt& Bn1, const BInt& Bn2);//overloaded Friend Boolean operator & lt;=(const BInt& Bn1, const BInt& Bn2);//overloaded less than or equal to operator
Friend Boolean operator & gt; (const BInt& Bn1, const BInt& Bn2);
Friend Boolean operator & gt; (const BInt& Bn1, int bn2);
Friend Boolean operator & gt;=(const BInt& Bn1, const BInt& Bn2);
Friend Boolean operator==(const BInt& Bn1, const BInt& Bn2);
Friend Boolean operator==(const BInt& Bn1, int bn2);
Private:
Const int size () {return m_s. The size (); }//for large Numbers of digits
String m_s;//internal use string to store large Numbers
};

BInt: : BInt (int I)//implementation with the average number of large
{
Char a, [2]={0}.
If (I==0)
{
M_s="0";
return;
}

For (int TMP. I> 0; I/=10)
{
TMP=I % 10;
A [0]=TMP + '0'.
M_s=a + m_s;
}
}

BInt: : BInt (char []) s: m_s (s)//implementation using string structure of large number
{
}

Int BInt: : operator=(int bn2)//assignment operation is overloaded
{
Char a, [2]={0}.
M_s="";
If (bn2==0)
{
M_s="0";
Return bn2;
}

For (int TMP. Bn2 & gt; 0; Bn2/=10)
{
TMP %=bn2 10;
A [0]=TMP + '0'.
M_s=a + m_s;
}
Return bn2;
}

Char * BInt: : operator=(char * s1)
{
M_s=s1;
Return s1.
}


Bool operator<(const BInt& Bn1, const BInt& Bn2)//implementation is less than the overloading operation
{
If (bn1. The size () & lt; Bn2. The size ()
{
return true;
}
Else if (bn1. The size () & gt; Bn2. The size ()
{
return false;
}
For (int I=0, length=bn1. The size (); i{
If (bn1 m_s [I] {
return true;
}
Else if (bn1 m_s [I] & gt; Bn2. M_s [I])
{
return false;
}
}
return false;
}

Boolean operator==(const BInt& Bn1, const BInt& Bn2)//to operate is equal to the overloaded
{
If (bn1. The size ()!=bn2. The size ()
{
return false;
}
For (int I=0, length=bn1. The size (); i{
If (bn1 m_s [I]!=bn2. M_s [I])
{
return false;
}
}
return true;
}

Bool operator<=(const BInt& Bn1, const BInt& Bn2)//realize small and equal to the overloaded
{
If (bn1 & lt; Bn2 | | bn1==bn2)
{
return true;
}
The else
{
return false;
}
}

Boolean operator==(const BInt& Bn1, int bn2)//that can compare with normal number of large
{
BInt I (bn2);
If (bn1==I)
{
return true;
}
return false;


}


Bool operator> (const BInt& Bn1, const BInt& Bn2)
{
If (bn1 & lt; Bn2==false& & (bn1==bn2)==false)
{
return true;
}
The else
{
return false;
}
}

Bool operator> (const BInt& Bn1, int bn2)
{
BInt b (bn2);

If (bn1 & gt; B)
{
return true;
}
The else
{
return false;
}
}

Bool operator>=(const BInt& Bn1, const BInt& Bn2)
{
If (bn1 & gt; Bn2 | | bn1==bn2)
{
return true;
}
The else
{
return false;
}
}

BInt operator + (const BInt& Bn1, const BInt& Bn2)
{
BInt sum;
Char a, [2].
A [0]='0'.
A [1]=0;
For (int I=bn1. M_s. The size () - 1, j=bn2 m_s. The size () - 1; I>=0 | | j>=0; - I, j)
{
int tmp;
If (i<0)
{
TMP=bn2. M_s [j] - '0' + a [0] - '0';
}
Else if (j<0)
{
TMP=bn1. M_s [I] - '0' + a [0] - '0';
}
The else
{
TMP=bn1. M_s. [I] - '0' + bn2 m_s [j] - '0' + a [0] - '0';
}
A [0]=10 + TMP % '0'.
The sum. M_s=a + sum. M_s;
A [0]=10 + TMP/' 0 '.
}
If (a [0]!='0')
{
The sum. M_s=a + sum. M_s;

}
return sum;
}

Int main ()
{
BInt a. (1234567);//test in integer structure of large number
BInt b (" 123456789123456789123456789 ");//test to string structure of large number
Cout<& lt;" A: "& lt; Cout<& lt;" B: "& lt;
A=42335;//test to integer number assignment
B="15365423432435343";//test as a string to the assignment of large number
Cout<& lt;" A: "& lt; Cout<& lt;" B: "& lt;
Cout<& lt;" A and b in a larger number is: "& lt; <(a> b? A: b) & lt;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related