Daily Record (C language)--string Implementation Large integer addition

Source: Internet
Author: User

Operating Environment: CentOs 64-bit--vim

Recently, looking at the book "Jian refers to offer", read the front about the ability of the interview, immediately feel that their programming ability is far worse.

Perhaps my understanding of the robust code is not deep, I think the robust code can be used to deal with a variety of different input, can have the corresponding processing, and give the corresponding output.

Here is what I looked at after the big integer addition done some perfect, before the only implementation of the pure numeric characters to sum, even to the null pointer processing is not, good ashamed. I'll use annotations to document my understanding of this algorithm.

1 #include <stdio.h>

2 #include <string.h>

3 int sum (char *a,char *b,char *sum);//summation

4 void Inverse (char *s);//reverse string to sum

5

6 int Change (char *r)//remove non-numeric characters from the string

7 {

8 int i = 0,J;

9 if (r = = NULL)

{printf ("R is null!\n");

One by one return-1;

12}

while (I < strlen (R))

14 {

if (R[i] <= ' 9 ' && r[i] >= ' 0 ')

i++;

+ Else

for (j = I;j < strlen (r); j + +)

R[J] = r[j+1];

20}

printf ("Change =%s\n", r);

22}

23

int sum (char* a,char* b,char* sum)

25 {

if (a = = NULL | | b = = NULL) {

printf ("A or B is null!\n");

return-1;

29}

M=strlen Int (a);

N=strlen Int (b);

printf ("m =%d,n =%d\n", m,n);

int acc = 0;

T,i int;

Inverse (a);

Inverse (b);

Notoginseng printf ("a =%s,b =%s\n", A, b);

(i = 0;i < m | | I < n;i++)

39 {

if (i >= m)//I didn't know why this was minus ' 0 ', and then

t = b[i]-' 0 ' + acc; I checked the ASCII code to know. The string operation is

if (i >= N) the value of the character in the//ascii code, which I passed

t = a[i]-' 0 ' + acc; GDB knows after debugging. The number of characters minus ' 0 ' is

The-else//equals the character to Integer and Integer,

[A[i]-' 0 ' + b[i]-' 0 ' + acc;//Plus ' 0 ' turns the number into a character.

Sum[i] = t% 10 + ' 0 ';

if (T > 9)

The ACC = 1;

$ else

ACC = 0;

51}

if (acc = = 1)

sum[i++] = ' 1 ';

"Sum[i]=";

Inverse (sum);

56}

57

inverse void (char *s)

59 {

int i;

length int;

A. Char t;

Length=strlen (s);

(i=0;i<length-i-1;i++)

65 {

T=s[i];

S[I]=S[LENGTH-I-1];

s[length-i-1]=t;

69}

70}

71

A. int main ()

73 {

a[100]= char "94jg987";

b[100]= char "9t5h624";

sum[100 Char];

("a=%s,b=%s\n", A, b);

(a);

(b);

Sum (a,b,sum);

Bayi printf ("sum=%s\n", sum);

0;

83}

Summary: The algorithm idea is to first reverse the string, to facilitate the subsequent summation, in order to prevent the string will appear non-numeric characters, you can first call the change (char*) function to remove the non-numeric characters in the string. Of course, this algorithm can certainly be optimized, but the small white I am now recorded here, I hope you have any good way to tell me, I will be open-minded to try.

Daily Record (C language)--string Implementation Large integer addition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.