← Back to topics
Topic

big nums

b
bour1992
I have seen many problems that deals with big integers.
I use C++ and it can handle only some digits.

Can you help me on how to represent and use them?
g
goran_f2
make a structure :


struct bignum{
int n; //number of digits
char digits[MAX];
};

and put the digit X with value X*10^i in digits[i].
Mathematical operations are done like you would do them on paper.
b
bour1992
But how can I do operations on chars?
Does it need any conversion?
g
goran_f2
No, you can use them like ints.
You just need to convert them to ints when you print them.
You can use ints instead of chars but then you use 4 times more memory.
b
bour1992
but how can i do an operation like:
'5'+'3'
?
s
s-lime
always subtract zero (or add when outputing)
cout << (('5'-'0')+('3'-'0'))+'0';

int char2num(char a) {
return a-'0';
}

char num2char(int a) {
return a+'0';
}
cout << num2char(char2num('3')+char2num('5'));
b
bour1992
Thank you, that was very helpful!
b
bour1992
I tried to make a code that will add two big number, but for some reason it doesn't work.

Can you check my code and tell me if i have any mistake?
#include <iostream>
#include <string.h>
using namespace std;


struct bignums{
int n;
char digits[200];
};



void init(bignums a, int def) //transfer the digits of the smallest number so they will be under the digits of the biggest number
{
a.n+=def;
for (int i=a.n-1;i>=0;i--)
{
a.digits[i+def]=a.digits[i];
a.digits[i]='0';
}
}


void addition(bignums a, bignums b, bignums c)
{
int carry=0,temp,z=1;
if (a.n-b.n>0)
init(b,a.n-b.n);
if (b.n-a.n>0)
init(a,b.n-a.n);
for (int i=a.n-1;i>=0;i--)
{
temp=(a.digits[i]-'0')+(b.digits[i]-'0')+carry;
c.digits[i+1]=(temp % 10)+'0';
carry=temp/10;
}
if (carry!=0)
c.digits[0]=carry+'0';
if (carry==0)
while(c.digits[z]!='0')
c.digits[z-1]=c.digits[z];
}

int main()
{
bignums a,b,c;
cin >> a.digits >> b.digits;
a.n=strlen(a.digits);
b.n=strlen(b.digits);
addition(a,b,c);
for (int i=0;i<c.n;i++)
cout << c.digits[i];
return 0;
}
b
boris4
if ( c.digits[ 1 ] != '0' )
this is infinity while:
while(c.digits[z]!='0')
c.digits[z-1]=c.digits[z];

you need to increase z

but anyway i think that is now what you want

let's say you have numbers
2200 and 3303
you will get
05503
and after while you will get
55503
instead of 5503

Please correct me if I'm wrong
b
bour1992
i did it:

void addition(bignums a, bignums b, bignums c)
{
int carry=0,temp,z=1;
if (a.n-b.n>0)
init(b,a.n-b.n);
if (b.n-a.n>0)
init(a,b.n-a.n);
for (int i=a.n-1;i>=0;i--)
{
temp=(a.digits[i]-'0')+(b.digits[i]-'0')+carry;
c.digits[i+1]=(temp % 10)+'0';
carry=temp/10;
}
if (carry!=0)
c.digits[0]=carry+'0';
if (carry==0)
while(c.digits[z]!='0')
{
c.digits[z-1]=c.digits[z];
z++;
}
}

but it still doesn't output anything.
m
matteo123
put scanf and printf instead of cin and cout
b
bour1992
I don't think that the problem is really there in my case.
b
boris4
it is because you need & in your function
so your code should look like

#include <iostream>
#include <string.h>
using namespace std;


struct bignums
{
int n;
char digits[200];
};



void init( bignums& a, int def )
{
a.n+=def;
for (int i=a.n-1;i>=0;i--)
{
a.digits[i+def]=a.digits[i];
a.digits[i]='0';
}
}


void addition(bignums a, bignums b, bignums& c)
{
int carry=0,temp,z=1;
if (a.n-b.n>0)
init(b,a.n-b.n);
if (b.n-a.n>0)
init(a,b.n-a.n);
for (int i=a.n-1;i>=0;i--)
{
temp=(a.digits[i]-'0')+(b.digits[i]-'0')+carry;
c.digits[i+1]=(temp % 10)+'0';
carry=temp/10;
}
if (carry!=0)
{
c.digits[0]=carry+'0';
c.n = a.n + 1;
}
if (carry==0)
while( z <= a.n )
{
c.digits[z-1]=c.digits[z];
c.n = a.n;
z++;
}
}

int main()
{
bignums a,b,c;
cin >> a.digits >> b.digits;
a.n=strlen(a.digits);
b.n=strlen(b.digits);
addition(a,b,c);
for (int i=0;i<c.n;i++)
cout << c.digits[i];
cout << endl;
return 0;
}


and i changed your while a little bit
b
bour1992
Thank you boris, you helped me very much!
b
boris4
you are welcome :)
M
MilosRadic
and what should be done about this in pascal?