📄 bignum.cpp
字号:
for (i = 0; i < SIZE; i++)
tempResult[i] = 0;
int len = maximum(len1, len2);
//compute tempResult
if (numA[0] < numB[0]) {
tempResult[0] = numA[0] + 10 - numB[0];
carry[0] = 1;
}
else {
tempResult[0] = numA[0] - numB[0];
carry[0] = 0;
}
for (i = 1; i < len; i++) {
if (numA[i] < carry[i-1] + numB[i]) {
tempResult[i] = numA[i] + 10 - carry[i-1] - numB[i];
carry[i] = 1;
}
else {
tempResult[i] = numA[i] - carry[i-1] - numB[i];
carry[i] = 0;
}
}
//convert the final product in digit form into character form
for (i = 0; i < len; i++)
result[i] = represent(tempResult[i]);
//remove unnecessary '0's at the beginning of the array
j = len - 1;
while ((result[j] == '0') && (j != -1))
j--;
if (j == -1)
clear(result);
else
result[j+1] = '\0';
reverse();
b.reverse();
//reverse the final result in character form to obtain the correct order
bigInt returnResult(result, negativeFlagOfResult, zeroFlagOfResult);
returnResult.reverse();
return returnResult;
}
//=================================================================================================//
const bigInt& bigInt::operator/(bigInt& b)
{
bigInt tempDividend, quotient;
char tempDividendResult[SIZE] = "", quotientResult[SIZE] = "";
char c;
int i = 0, j, k;
while (i < getLength()) {
if (tempDividend < b) {
c = getDigit(i);
tempDividend.getValue(tempDividendResult);
k = strlen(tempDividendResult);
tempDividendResult[k] = c;
tempDividendResult[k+1] = '\0';
tempDividend.fixArray(tempDividendResult);
tempDividend.setValue(tempDividendResult);
if (tempDividend < b) {
k = strlen(quotientResult);
quotientResult[k] = '0';
quotientResult[k+1] = '\0';
quotient.fixArray(quotientResult);
quotient.setValue(quotientResult);
i++;
}
}
else {
j = 0;
while (tempDividend >= b) {
tempDividend = tempDividend - b;
j++;
}
quotient.getValue(quotientResult);
k = strlen(quotientResult);
quotientResult[k] = represent(j);
quotientResult[k+1] = '\0';
quotient.fixArray(quotientResult);
quotient.setValue(quotientResult);
i++;
}
}
return quotient;
}
//=================================================================================================//
const bigInt& bigInt::operator%(bigInt& b)
{
bigInt tempDividend, quotient;
char tempDividendResult[SIZE] = "", quotientResult[SIZE] = "";
char c;
int i = 0, j, k;
while (i < getLength()) {
if (tempDividend < b) {
c = getDigit(i);
tempDividend.getValue(tempDividendResult);
k = strlen(tempDividendResult);
tempDividendResult[k] = c;
tempDividendResult[k+1] = '\0';
tempDividend.fixArray(tempDividendResult);
tempDividend.setValue(tempDividendResult);
if (tempDividend < b) {
k = strlen(quotientResult);
quotientResult[k] = '0';
quotientResult[k+1] = '\0';
quotient.fixArray(quotientResult);
quotient.setValue(quotientResult);
i++;
}
}
else {
j = 0;
while (tempDividend >= b) {
tempDividend = tempDividend - b;
j++;
}
quotient.getValue(quotientResult);
k = strlen(quotientResult);
quotientResult[k] = represent(j);
quotientResult[k+1] = '\0';
quotient.fixArray(quotientResult);
quotient.setValue(quotientResult);
i++;
}
}
return tempDividend;
}
//=================================================================================================//
const void bigInt::operator+=(bigInt& b)
{
bigInt result;
result = *this + b;
*this = result;
}
const void bigInt::operator*=(bigInt& b)
{
bigInt result;
result = *this * b;
*this = result;
}
const void bigInt::operator-=(bigInt& b)
{
bigInt result;
result = *this - b;
*this = result;
}
const void bigInt::operator/=(bigInt& b)
{
bigInt result;
result = *this / b;
*this = result;
}
const void bigInt::operator%=(bigInt& b)
{
bigInt result;
result = *this % b;
*this = result;
}
const bool bigInt::operator>(bigInt& b)
{
int len1 = getLength(), len2 = b.getLength();
if ((!negative) && b.getNegative())
return true;
else if (negative && (!b.getNegative()))
return false;
else if ((!negative) && (!b.getNegative())) {
if (len1 > len2)
return true;
else if (len1 < len2)
return false;
else {
int j = 0;
while ((a[j] == b.a[j]) && (j != len1))
j++;
if (j == len1)
return false; // a == b
else {
if (value(getDigit(j)) > value(b.getDigit(j)))
return true;
else
return false;
}
}
}
else if (negative && b.getNegative()) {
if (len1 > len2)
return false;
else if (len1 < len2)
return true;
else {
int j = 0;
while ((a[j] == b.a[j]) && (j != len1))
j++;
if (j == len1)
return false; // a == b
else {
if (value(getDigit(j)) > value(b.getDigit(j)))
return false;
else
return true;
}
}
}
}
const bool bigInt::operator<(bigInt& b)
{
int len1 = getLength(), len2 = b.getLength();
if ((!negative) && b.getNegative())
return false;
else if (negative && (!b.getNegative()))
return true;
else if ((!negative) && (!b.getNegative())) {
if (len1 > len2)
return false;
else if (len1 < len2)
return true;
else {
int j = 0;
while ((a[j] == b.a[j]) && (j != len1))
j++;
if (j == len1)
return false; // a == b
else {
if (value(getDigit(j)) > value(b.getDigit(j)))
return false;
else
return true;
}
}
}
else if (negative && b.getNegative()) {
if (len1 > len2)
return true;
else if (len1 < len2)
return false;
else {
int j = 0;
while ((a[j] == b.a[j]) && (j != len1))
j++;
if (j == len1)
return false; // a == b
else {
if (value(getDigit(j)) > value(b.getDigit(j)))
return true;
else
return false;
}
}
}
}
const bool bigInt::operator==(bigInt& b)
{
int len1 = getLength(), len2 = b.getLength();
if (len1 != len2)
return false;
else {
int j = 0;
while ((a[j] == b.a[j]) && (j != len1))
j++;
if (j == len1)
return ((negative && b.getNegative()) || (!negative && (!b.getNegative()))); // a == b
else
return false;
}
}
const bool bigInt::operator!=(bigInt& b)
{
return !(*this == b);
}
const bool bigInt::operator>=(bigInt& b)
{
return ((*this > b) || (*this == b));
}
const bool bigInt::operator<=(bigInt& b)
{
return ((*this < b) || (*this == b));
}
//========================================== MAIN FUNCTION ========================================//
void main(void)
{
bigInt a("123");
bigInt b("45");
bigInt c;
c = a % b;
c.print();
cout << endl;
/**/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -