Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Large integer subtraction c language
Large integer subtraction c language
# include & ltstdio.h & gt

# include & ltstdlib.h & gt

# include & ltstring.h & gt

char a[ 1000],b[ 1000];

int c[ 100 1];

int x,y,I,j,k;

int blen = 0;

int alen = 0;

char flag = '+'; //Negative number used to subtract the mark.

Intsubtract (char * a, char * b, int len){// In order to avoid code duplication, the calculation of * * * is extracted and written as a function.

int I;

for(I = len- 1; I & gt=0; I-) {

if(a[I]& lt; b[i]) {

c[I]= a[I]+ 10-b[I];

b[I- 1]++;

} Otherwise {

c[I]= a[I]-b[I];

}

}

Returns 0;

}

Int subtraction () {

for(I = 0; I< 100; i++)

{

scanf("%c ",& ampb[I]);

if(b[i]=='\n ')

Break;

blen++;

}

printf(" = ");

if(alen & gt; blen)

{

x =(alen-blen);

for(I = blen- 1; I & gt=0; I-)

{

alen-;

b[alen]= b[I];

}

for(I = 0; I & ltx;; i++)

b[I]= ' 0 ';

alen = blen+x;

blen = alen

} else if(blen & gt; alen) {

x =(blen-alen);

for(I = alen- 1; I & gt=0; I-)

{

blen-;

a[blen]= a[I];

}

for(I = 0; I & ltx;; i++)

a[I]= ' 0 ';

blen = alen+x;

alen = blen

}

for(I = alen- 1; I & gt=0; I-) {

a[I]= a[I]--0;

b[I]= b[I]--0;

}

if (strcmp(a,b)>0) {

Subtract (a, b, alen);

} Otherwise {

flag = '-';

Subtract (b, a, alen);

}

Returns 0;

}

Int addition () {

for(I = 0; I< 100; i++)

{ scanf("%c ",& ampb[I]);

if(b[i]=='\n ')

Break;

blen++;

}

printf(" = ");

if(alen & gt; blen)

{

x =(alen-blen);

for(I = blen- 1; I & gt=0; I-)

{

alen-;

b[alen]= b[I];

}

for(I = 0; I & ltx;; i++)

b[I]= ' 0 ';

alen = blen+x;

} else if(blen & gt; alen) {

x =(blen-alen);

for(I = alen- 1; I & gt=0; I-)

{

blen-;

a[blen]= a[I];

}

for(I = 0; I & ltx;; i++)

a[I]= ' 0 ';

blen = alen+x;

alen = blen

}

for(I = alen- 1; I & gt=0; I-) {

a[I]= a[I]--0;

b[I]= b[I]--0;

if(c[I+ 1]+a[I]+b[I]& gt; = 10)

{

c[I]=(c[I+ 1]+a[I]+b[I])/ 10;

c[I+ 1]=(c[I+ 1]+a[I]+b[I])% 10;

}

other

{

c[I+ 1]+=(a[I]+b[I]);

}

}

Returns 0;

}

int main()

{

for(I = 0; I< 100; i++)

a[I]= 0;

for(I = 0; I< 100; i++)

b[I]= 0;

for(I = 0; I< 10 1; i++)

c[I]= 0;

for(I = 0; I< 100; i++)

{

scanf("%c ",& ampa[I]);

if(a[i]=='+')

{

Addition ();

goto output 1;

}

if(a[i]=='-')

{

Subtraction ();

goto output2

}

alen++;

}

Output 1:

{

for(I = 0; I<= aleni++)

printf("%d ",c[I]);

Returns 0;

}

Output 2:

{

Printf("%c ",logo);

for(I = 0; I< Allen; i++)

printf("%d ",c[I]);

Returns 0;

}

}

Code running result:

123456789- 12345

=+ 123444444

Terminates with a return code of 0.

Press any key to continue. ...

123456+ 1 1 1 1 1 1

=0234567

Terminates with a return code of 0.

Press any key to continue. ...

1000000- 1

=+0999999

Terminates with a return code of 0.

Press any key to continue. ...

1- 1000000

=-0999999

Terminates with a return code of 0.

Press any key to continue. ...

//What did you do to your code! I hope it will be helpful to your study.