Мне нужна помощь с числами, которые больше, чем int, я начинающий программист. Пожалуйста, помогите, если вы можете, любая помощь высоко ценится!
Я использую C ++ в Unix. шпатлевка
вот код
#include <iostream>
#include <string>
const bool debugging = false;
const int size = 30; // I want to make it easy to work with.
// Change to something big later.
struct bignum
{
int digit[size];
char sign; // '+' or '-'
bool ok;
};
// to store the number one thousand six hundred and seventy nine,
// 1679, digit[0]=9, digit[1]=7, digit[2]=6, digit[3]=1, all the rest are zero
void init(bignum & B)
{ for (int i = 0; i < size; i += 1)
B.digit[i] = 0;
B.sign = '+';
B.ok = true; }
void print(bignum B)
{ if (! B.ok)
cout << "BAD";
cout << B.sign;
bool printed = false;
for (int i = size - 1; i >= 0; i -= 1)
{ int dig = B.digit[i];
if (debugging)
cout << "|";
if (dig != 0 || printed || debugging)
{ cout << dig;
printed = true; } }
if (! printed)
cout << "0"; }
void addsmall_is(bignum & B, int N) // _is means "ignoring the + or - sign"// N must be small.
{ int toadd = N;
int position = 0;
while (toadd > 0)
{ int sum = B.digit[position] + toadd;
if (sum < 10)
{ B.digit[position] = sum;
toadd = 0; }
else
{ B.digit[position] = sum % 10;
toadd = sum / 10; }
position += 1;
if (position >= size)
{ B.ok = false;
break; } } }
void add_is(bignum & B, bignum C)
{ int position = 0;
int toadd = 0;
// while(true) how to use while loop here?? how to stop it?
for (int position = 0; position < size; position++)
{ int sum = B.digit[position] + C.digit[position] + toadd;
if (sum < 10)
{ B.digit[position] = sum;
toadd = 0;
}
else
{ B.digit[position] = sum % 10;
toadd = sum/10;
}
// position +=1;
if (position >= size)
{ B.ok = false;
break;
}
}
}
void main()
{ bignum X, Y;
init(X);
init(Y);
while (true)
{ print(X); //how to init two bignum??
cout<<endl;
char c, cc;
int n, nn;
cin >> c >> n >> cc >> nn;
if (cin.fail())
break;
if (c == '+')
addsmall_is(X, n);
if (cc == '+')
addsmall_is(Y, nn);
add_is(X, Y);
}
}
Задача ещё не решена.
Других решений пока нет …