Subtraction of large numbers 2 -- add the leading zero before the shorter number to align the trim bit

Source: Internet
Author: User

 

 # Define _ Crt_secure_no_warnings # Include <Stdio. h> # Include < String . H> Int  Main (){  Char Str1 [ 255 ], Str2 [ 255  ];  Int Num1 [255 ] = { 0 }, Num2 [ 255 ] = { 0 }; //  Used to store the subtrahend and subtrahend, and the shorter subtrahend Add the leading zeros.      Int Dvalue [ 255 ] = { 0  };  Int  Length1, leng2, length;  Int  I, J;  Int Neg_flag = 0 ; //  A negative flag      Int Flag = 0 ; //  Borrow sign  Gets (str1); gets (str2); length1 = Strlen (str1); leng22. = Strlen (str2); Length = Length1> leng22? Length1: leng22;  //  Store two numbers in num []     For (I = length- 1 , J = length1- 1 ; J> = 0 ; I --, j -- ) {Num1 [I] = Str1 [J]- '  0  '  ;}  For (I = length- 1 , J = leng22- 1 ; J> = 0 ; I --, j --) {Num2 [I] = Str2 [J]- '  0  '  ;}  //  Subtraction from the back to the back      For (I = length- 1 ; I> = 0 ; I -- ) {Dvalue [I] = Flag + num1 [I]- Num2 [I];  If (Dvalue [I] < 0 ) {Dvalue [I] = Dvalue [I] + 10  ; Flag =- 1 ; //  Indicates the borrow position  }  Else  {Flag = 0  ;}}  //  If the above loop is exceeded, flag =-1 indicates that the difference is negative.  // When the result is negative, the subsequent algorithm example is as follows:  //  For example, after the bitwise calculation, the difference value is 123, and the actual difference value is 1123-456.  //  Then, the difference between 667 and 1000 is the actual difference. Set a negative flag to neg_flag, and then use 1000-667.  //  That is, use 10 minus the second bit, and then use 9 minus the rest.      If (Flag =- 1  ) {Neg_flag = 1  ; Dvalue [Length - 1 ] = 10 -Dvalue [length- 1  ];  For (I = length- 2 ; I> = 0 ; I -- ) {Dvalue [I] = 9 - Dvalue [I] ;}}  //  Skip the leading zero before outputting the difference value I = 0  ;  While (Dvalue [I] =0 & (I < Length) {I ++ ;}  //  If the two numbers are equal, all the difference values are 0 and only one 0 is output.      If (I = Length) {putchar (  '  0  '  );  Return   0  ;}  // If it is a negative number, the negative number is output first.      If (Neg_flag = 1  ) {Putchar (  '  -  '  );}  For (; I <length; I ++ ) {Printf (  "  % D  "  , Dvalue [I]);}  Return   0 ;}

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.