Addition, subtraction, multiplication, division
High PrecisionString kuangbin
# Include <stdio. h> # Include < String > # Include < String . H> # Include <Iostream> Using Namespace STD; // Compare comparison function: equal return 0, greater than return 1, less than return-1 Int Compare ( String Str1, String Str2 ){ If (Str1.length ()> str2.length ()) Return 1 ; Else If (Str1.length () <str2.length ()) Return - 1 ; Else Return Str1.compare (str2 );} // High-Precision Addition // Only two positive numbers can be added. String Add ( String Str1, String Str2) // High-Precision Addition { String STR; Int Len1 =Str1.length (); Int Len2 = Str2.length (); // Add 0 in front to make it the same length If (Len1 < Len2 ){ For ( Int I = 1 ; I <= len2-len1; I ++ ) Str1 = " 0 " +Str1 ;} Else { For ( Int I = 1 ; I <= len1-len2; I ++ ) Str2 = " 0 " + Str2;} len1 = Str1.length (); Int Cf = 0 ; Int Temp; For ( Int I = len1- 1 ; I> = 0 ; I -- ) {Temp = Str1 [I]- ' 0 ' + Str2 [I]- ' 0 ' + CF; cf = Temp/10 ; Temp % = 10 ; Str = Char (Temp + ' 0 ' ) + STR ;} If (Cf! = 0 ) STR = Char (CF + ' 0 ' ) +STR; Return STR ;} // High Precision Subtraction // Only two positive numbers can be subtracted, and a large reduction is required. String Sub ( String Str1, String Str2) // High Precision Subtraction { String STR; Int TMP = str1.length ()- Str2.length (); Int Cf = 0 ; For ( Int I = str2.length ()- 1 ; I> = 0 ; I -- ){ If (Str1 [TMP + I] <str2 [I] + CF) {Str = Char (Str1 [TMP + I]-str2 [I]-CF + ' 0 ' + 10 ) + STR; cf = 1 ;} Else {Str = Char (Str1 [TMP + I]-str2 [I]-CF + ' 0 ' ) + STR; cf = 0 ;}} For ( Int I = TMP- 1 ; I> = 0 ; I -- ){ If (Str1 [I]-CF> = ' 0 ' ) {Str = Char (Str1 [I]-CF) + STR; cf = 0 ;} Else {Str = Char (Str1 [I]-CF + 10 ) + STR; cf = 1 ;} Str. Erase ( 0 , Str. find_first_not_of ( ' 0 ' )); // Remove redundant leading 0 in the result Return STR ;} // High-Precision Multiplication // Only two positive numbers can be multiplied. String Mul ( String Str1, String Str2 ){ String STR; Int Len1 = Str1.length (); Int Len2 =Str2.length (); String Tempstr; For ( Int I = len2- 1 ; I> = 0 ; I -- ) {Tempstr = "" ; Int Temp = str2 [I]- ' 0 ' ; Int T = 0 ; Int Cf = 0 ; If (Temp! = 0 ){ For ( Int J = 1 ; J <= len2- 1 -I; j ++ ) Tempstr + =" 0 " ; For ( Int J = len1- 1 ; J> = 0 ; J -- ) {T = (Temp * (str1 [J]- ' 0 ' ) + CF) % 10 ; Cf = (Temp * (str1 [J]-' 0 ' ) + CF )/ 10 ; Tempstr = Char (T + ' 0 ' ) + Tempstr ;} If (Cf! = 0 ) Tempstr = Char (CF + ' 0 ' ) + Tempstr;} Str = Add (STR, tempstr);} Str. Erase ( 0 , Str. find_first_not_of ( ' 0 ' )); Return STR ;} // High Precision Division // Division of two positive numbers, quotient, and Residue // Requires High-Precision subtraction and multiplication Void Div ( String Str1, String Str2, String & Quotient, String & Residue) {quotient = Residue = "" ; // Clear If (Str2 = " 0 " )// Determine whether the divisor is 0 {Quotient = Residue = " Error " ; Return ;} If (Str1 = " 0 " ) // Determine whether the divisor is 0 {Quotient = Residue = " 0 " ; Return ;} Int Res = Compare (str1, str2 ); If (RES < 0 ) {Quotient = " 0 " ; Residue =Str1; Return ;} Else If (RES = 0 ) {Quotient = " 1 " ; Residue = " 0 " ; Return ;} Else { Int Len1 = Str1.length (); Int Len2 = Str2.length (); String Tempstr; tempstr. append (str1, 0 , Len2- 1 ); For ( Int I = len2-1 ; I <len1; I ++ ) {Tempstr = Tempstr + Str1 [I]; tempstr. Erase ( 0 , Tempstr. find_first_not_of ( ' 0 ' )); If (Tempstr. Empty () tempstr = " 0 " ; For ( Char Ch = ' 9 ' ; Ch> = ' 0 ' ; Ch --) // Trial Operator { String STR, TMP; Str = STR + Ch; TMP =Mul (str2, STR ); If (Compare (TMP, tempstr) <= 0 ) // Trial provider successful {Quotient = Quotient + Ch; tempstr = Sub (tempstr, TMP ); Break ; }}} Residue = Tempstr;} quotient. Erase ( 0 , Quotient. find_first_not_of ( ' 0 ' )); If (Quotient. Empty () quotient = " 0 " ;} Int Main (){ String Str1, str2; String Str3, str4; While (CIN> str1> Str2) {cout <Add (str1, str2) < Endl; cout <Sub (str1, str2) < Endl; cout <MUL (str1, str2) < Endl; Div (str1, str2, str3, str4); cout <Str3 < " " <Str4 < Endl ;} Return 0 ;}