Challenge interview programming: Big integer addition, subtraction, multiplication, division, interview integer
Challenge interview programming: Big integer addition, subtraction, multiplication, division
Everything is limited, even if it seems that infinite time or space is very limited. The built-in addition, subtraction, multiplication, and Division Types in the computer are limited. Let's add, subtract, multiply, and divide an infinite integer.
The following uses the C ++ code for implementation:
# Include <iostream> # include <string> using namespace std; // string add_int (string, string); string sub_int (string, string ); string mul_int (string, string); string div_int (string, string); string mod_int (string, string); string divide_int (string, string, int ); inline int compare (string s1, string s2) {if (s1.size () <s2.size () return-1; else if (s1.size ()> s2.size () return 1; elsereturn s1.compare (s2 );}/* The addition of large integers is essentially only processed: two positive numbers are added, for example, "123" + "234". In other cases, 1 must be converted. positive and Negative => "123" + "-234" = "123"-"234" is converted to subtraction 2. negative positive => "-234" + "123" = "123"-"234" 3. negative plus negative => "-123" + "-234" =-("123" + "234") */string add_int (string s1, string s2) {if (s1 = "0") return s2; if (s2 = "0") return s1; if (s1 [0] = '-') {if (s2 [0] = '-') {return "-" + add_int (s1.erase (0, 1), s2.erase (0, 1 )); // case 3} else {return sub_int (s2, s1.erase (0, 1 )); // Case 2 }}if (s2 [0] = '-') {return sub_int (s1, s2.erase (0, 1 )); // case 1} // processing essence string: size_type I, size1, size2; size1 = s1.size (); size2 = s2.size (); if (size1 <size2) {for (I = 0; I <size2-size1; I ++) // fill in s1 = "0" + s1;} else {for (I = 0; I <size1-size2; I ++) // Add zero s2 = "0" + s2;} int n1, n2; n2 = 0; size1 = s1.size (); size2 = s2.size (); string res; for (I = size1-1; I! = 0; I --) // Add {n1 = (s1 [I]-'0' + s2 [I]-'0' + n2) % 10 from the second bit; // n1 indicates the current bit value n2 = (s1 [I]-'0' + s2 [I]-'0' + n2)/10; // n2 indicates carry res = char (n1 + '0') + res;}/* The above loop cannot handle 0th bits because the type of I is string: size_type, it is non-negative type * // For 0th bits, n1 = (s1 [0]-'0' + s2 [0]-'0' + n2) % 10; n2 = (s1 [0]-'0' + s2 [0]-'0' + n2)/10; res = char (n1 + '0') + res; if (n2 = 1) res = "1" + res; return res;}/* large integer subtraction is essentially only processed: two integers are subtracted, and a large minus one: "1234 "-" 234 "other situations need to be converted 1. small minus big positive => "234"-"1234" =-("1234"-"234") 2. minus positive => "1234"-"-234" = "1234" + "234" 3. minus positive => "-1234"-"234" =-("1234" + "234") 4. negative subtraction => "-1234"-"-234" = "234"-"1234" =-("1234"-"234") */string sub_int (string s1, string s2) {if (s2 = "0") return s1; if (s1 = "0") {if (s2 [0] = '-') return s2.erase (0, 1); return "-" + s2;} if (s1 [0] = '-') {if (s2 [0] = '-') {return sub_int (s2. Erase (0, 1), s1.erase (0, 1); // case 4} return "-" + add_int (s1.erase (0, 1), s2 ); // case 3} if (s2 [0] = '-') return add_int (s1, s2.erase (0, 1 )); // case 2 // adjust the length of s1 and s2 string: size_type I, size1, size2; size1 = s1.size (); size2 = s2.size (); if (size1 <size2) {for (I = 0; I <size2-size1; I ++) // fill in s1 = "0" + s1;} else {for (I = 0; I <size1-size2; I ++) // Add zero s2 = "0" + s2;} int t = s1.compare (s2); if (t <0) // S1 and s2 have the same size, but s1 <s2return "-" + sub_int (s2, s1); if (t = 0) return "0 "; // processing essence: s1> s2string res; string: size_type j; for (I = s1.size ()-1; I! = 0; I --) {if (s1 [I] <s2 [I]) // insufficient. You need to borrow one {j = 1; while (s1 [I-j] = '0') {s1 [I-j] = '9'; j ++ ;} s1 [I-j]-= 1; res = char (s1 [I] + ':'-s2 [I]) + res ;} else {res = char (s1 [I]-s2 [I] + '0') + res ;}} res = char (s1 [0]-s2 [0] + '0') + res; // remove leading zero res. erase (0, res. find_first_not_of ('0'); return res;} string mul_int (string s1, string s2) {if (s1 = "0" | s2 = "0 ") return "0"; // sign is an int sign = 1; if (s1 [0] = '-') {Sign * =-1; s1.erase (0, 1);} if (s2 [0] = '-') {sign * =-1; s2.erase (0, 1);} string: size_type size1, size2; string res, temp; size1 = s1.size (); size2 = s2.size (); // set the maximum length of s1 to if (size1 <size2) {temp = s1; s1 = s2; s2 = temp; size1 = s1.size (); size2 = s2.size ();} int I, j, n1, n2, n3, t; for (I = size2-1; I> = 0; I --) {temp = ""; n1 = n2 = n3 = 0; for (j = 0; j <size2-1-I; j ++) temp = "0" + temp; n3 = s2 [I]-'0'; for (j = size1-1; j> = 0; j --) {t = (n3*(s1 [j]-'0 ') + n2); n1 = t % 10; // the value of the current position of the n1 record n2 = t/10; // n2 record carry value temp = char (n1 + '0') + temp;} if (n2) temp = char (n2 + '0') + temp; res = add_int (res, temp);} if (sign =-1) return "-" + res; return res;} string divide_int (string s1, string s2, int flag) // flag = 1, returns the quotient; flag = 0, returns the remainder {string quotient, residue; if (s2 = "0 ") {quotient = residue = "error"; I F (flag = 1) return quotient; elsereturn residue;} if (s1 = "0") {quotient = residue = "0"; if (flag = 1) return quotient; elsereturn residue;} // sign1 is the quotient symbol, and sign2 is the remainder symbol int sign1, sign2; sign1 = sign2 = 1; if (s1 [0] = '-') {sign1 * =-1; sign2 =-1; s1.erase (0, 1 );} if (s2 [0] = '-') {sign1 * =-1; s2.erase (0, 1);} if (compare (s1, s2) <0) {quotient = "0"; residue = s1;} else if (compare (s1, s2) = 0) {quotient = "1"; Residue = "0";} else {string temp; string: size_type size1, size2; size1 = s1.size (); size2 = s2.size (); int I; if (size2> 1) temp. append (s1, 0, size2-1); for (I = size2-1; I <size1; I ++) {temp = temp + s1 [I]; // Tester for (char c = '9'; c> = '0'; c --) {string t = mul_int (s2, string (1, c )); string s = sub_int (temp, t); if (s = "0" | s [0]! = '-') {Temp = s; quotient = quotient + c; break ;}}residue = temp;} // remove the leading zero quotient. erase (0, quotient. find_first_not_of ('0'); residue. erase (0, residue. find_first_not_of ('0'); if (sign1 =-1) {quotient = "-" + quotient;} if (sign2 =-1) {if (residue. empty () residue = "0"; elseresidue = "-" + residue;} if (flag = 1) return quotient; else return residue;} string div_int (string s1, string s2) {return divide_in T (s1, s2, 1);} string mod_int (string s1, string s2) {return divide_int (s1, s2, 0);} int main (void) {string s1, s2; char op; while (cin> s1> op> s2) {switch (op) {case '+': cout <add_int (s1, s2) <endl; break; case '-': cout <sub_int (s1, s2) <endl; break; case '*': cout <mul_int (s1, s2) <endl; break; case '/': cout <div_int (s1, s2) <endl; break; case '%': cout <mod_int (s1, s2) <endl; break; Default: cout <"The operator is error! "<Endl; break;} return 0 ;}
Then I came to a C code ......
Code download
Big integer addition, subtraction, multiplication, division C ++
Directory of all contents