Home > Back-end >  C language to realize high precision integer addition and subtraction
C language to realize high precision integer addition and subtraction

Time:06-04

 implemented in C language, the best don't pointer, because a pointer can not learn,

CodePudding user response:

CodePudding user response:

Only addition and subtraction, nothing technical content, simulation to calculate steps, with array implementation, online check, slightly self-reliance

CodePudding user response:

refer to the second floor and 蘤 old crepe reply:
only addition and subtraction, no technical content, simulate the hand count steps, with array implementation, online check, slightly self-reliance

CodePudding user response:

# include & lt; Stdio. H>
# include & lt; String. H>
# define MAX 100
Int main () {
Char a [MAX + 2)={' 0 '}, b=[MAX + 2] {' 0 '};
int x;

Printf (" do Input a and b: \ n ");
The scanf (" % s % s ", & amp; (a [1]), & amp; (b) [1]));//control the first position reserved for possible carry
Printf (" array status: \ na \ n \ nb==% s % s ", a, b);
Int len1=strlen (a); The length of the//a
Int len2=strlen (b); The length of a/b
Int len=len1 & lt; Len2? Len2: len1; How long is the longest in//a and b

If (len1 & gt; Len2) {//mobile b and a tail alignment
for (x=0; X<=len2; X++)//move
B] [len - x=b [len2 - x];
for (x=0; XB=[x + 1] '0'.
Printf (" After Move B: \ na \ n \ nb==% s % s ", a, B);
} else if (len1 & lt; Len2) {
for (x=0; X<=len1; X++)
A] [len - x=a, [len1 - x].
for (x=0; XA [x + 1]='0'.
Printf (" After Move A: \ \ na=% s nb=% s \ n ", A, b);
};
Printf (" Begin the Add... \n");
Int t, c=0;
For (x=len - 1; X>=0; X -) {
T=a [x] - '0' + b [x] - '0' + c;//c is carry
C=t>=10? 1:0;
T %=10;
A [x]=t + '0'.
};

Printf (" \ n \ nResult in a is: % s ", a);
If (a [0]=='0')
Printf (" the Result is: % s \ n ", & amp; A, [1]).
The else
Printf (" the Result is: % s \ n ", a);
return 0;
}
No pointer is an array, where is also not clear

CodePudding user response:

Fun
reference 4 floor response:
# include & lt; Stdio. H>
# include & lt; String. H>
# define MAX 100
Int main () {
Char a [MAX + 2)={' 0 '}, b=[MAX + 2] {' 0 '};
int x;

Printf (" do Input a and b: \ n ");
The scanf (" % s % s ", & amp; (a [1]), & amp; (b) [1]));//control the first position reserved for possible carry
Printf (" array status: \ na \ n \ nb==% s % s ", a, b);
Int len1=strlen (a); The length of the//a
Int len2=strlen (b); The length of a/b
Int len=len1 & lt; Len2? Len2: len1; How long is the longest in//a and b

If (len1 & gt; Len2) {//mobile b and a tail alignment
for (x=0; X<=len2; X++)//move
B] [len - x=b [len2 - x];
for (x=0; XB=[x + 1] '0'.
Printf (" After Move B: \ na \ n \ nb==% s % s ", a, B);
} else if (len1 & lt; Len2) {
for (x=0; X<=len1; X++)
A] [len - x=a, [len1 - x].
for (x=0; XA [x + 1]='0'.
Printf (" After Move A: \ \ na=% s nb=% s \ n ", A, b);
};
Printf (" Begin the Add... \n");
Int t, c=0;
For (x=len - 1; X>=0; X -) {
T=a [x] - '0' + b [x] - '0' + c;//c is carry
C=t>=10? 1:0;
T %=10;
A [x]=t + '0'.
};

Printf (" \ n \ nResult in a is: % s ", a);
If (a [0]=='0')
Printf (" the Result is: % s \ n ", & amp; A, [1]).
The else
Printf (" the Result is: % s \ n ", a);
return 0;
}
No pointer is an array, where also don't know
always show the answer wrong aye

CodePudding user response:

?? What is your test data???
Give a test of several

CodePudding user response:

Fun
reference 6 floor response:
?? What is your test data???
Give a test of several

Only 1 1 and 2 2 are given, the background data

CodePudding user response:

//ok, so you can enter 1 1/2 2, get, 2/4
# include & lt; Stdio. H>
# include & lt; String. H>
# define MAX 100
Int main () {
Char a [MAX + 2)={' 0 '}, b=[MAX + 2] {' 0 '};
int x;

//printf (" do Input a and b: \ n ");
While (2==the scanf (" % s % s ", & amp; A [1], & amp; B) [1]) {//empty out the first position, reserved for the highest possible carry
//printf (" array status: \ na \ n \ nb==% s % s ", a, b);//just input hand an array of
Int len1=strlen (a); The length of the//a
Int len2=strlen (b); The length of a/b
Int len=len1 & lt; Len2? Len2: len1; How long is the longest in//a and b

If (len1 & gt; Len2) {//mobile b and a tail alignment
for (x=0; X<=len2; X++)//move
B] [len - x=b [len2 - x];
for (x=0; XB=[x + 1] '0'.
} else if (len1 & lt; Len2) {//a no long, b here to move to align b tail
for (x=0; X<=len1; X++)
A] [len - x=a, [len1 - x].
for (x=0; XA [x + 1]='0'.
};
//printf (" After Move A: \ \ na=% s nb=% s \ n ", A, b);//tail aligned array after

//printf (" Begin the Add... \n");//start to do addition, storing the results directly in a array of
Int t, c=0;
For (x=len - 1; X>=0; X -) {
T=a [x] - '0' + b [x] - '0' + c;//c is carry
C=t>=10? 1:0;
T %=10;
A [x]=t + '0'.
};

//printf (" \ n \ nResult in a is: % s ", a);//after the addition to perform a array
the condition of theIf (a [0]=='0')//highest didn't carry a + b
//printf (" the Result is: % s \ n ", & amp; A, [1]).//starting from 1 output array
nullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related