Self-created Big Data Division for interview questions

Source: Internet
Author: User

Truth about division of large numbers (integers)

Thick and thin hair, dripping water and stone.
Introduction
Overall debugging is not the best. It moves forward without debugging, and the train of thought is as complete as possible with fewer defects.
Question
This function is used to divide large numbers out of the integer representation range.
Ideas
Use strings to process data and take advantage of the length of strings.
Lab

Code
Test. cpp
#include 
   
    #include 
    
     using namespace std;// extra the class of stringclass String:public string{public:// mode the add of intstatic string ADD_Int(string a,string b){// exception of inputif( a.empty() )return b;else if( b.empty() )return "0";if(!check_all_number(a) || !check_all_number(b)){return "exception of input ADD_Int";}Standardization(a);Standardization(b);if(a[0] != '-' && b[0] != '-')return AddInt(a,b);else if(a[0] != '-'&& b[0] == '-')return MinusInt( a,b.substr( 1,b.size() ) );else if(a[0] == '-'&& b[0] != '-')return MinusInt(b,a.substr(1,a.size()));else return '-'+AddInt(a.substr(1,a.size()),b.substr( 1,b.size() ));}// make a-b mode int a - b;static string MINUS_Int(string a,string b){// exception of inputif( a.empty() )return b;else if( b.empty() )return "0";if(!check_all_number(a) || !check_all_number(b)){return "exception of input Multiplies_Int";}Standardization(a);Standardization(b);if(a[0] != '-' && b[0] != '-')return MinusInt(a,b);else if(a[0] != '-' && b[0] == '-')return AddInt(a,b.substr(1,b.size()));else if(a[0] == '-' && b[0] != '-')return "-"+AddInt(a.substr(1,a.size()),b);else return MinusInt( b.substr(1,b.size()) , a.substr(1,a.size()) );}// make a*b mode int a * b;static string MULT_Int(string a,string b){// exception of inputif( a.empty() )return b;else if( b.empty() )return "0";if(!check_all_number(a) || !check_all_number(b)){return "exception of input Multiplies_Int";}Standardization(a);Standardization(b);string::size_type i = a.size(),j = b.size();string c = "0",d = "";bool fushu = (a[0] == '-' && b[0] != '-')||(a[0] != '-' && b[0] == '-');if(a[0] == '-')a = a.substr(1,a.size());if(b[0] == '-')b = b.substr(1,b.size());int jinwei = 0;for( j = b.size()-1 ; j < b.size() ;j--){// each number of b to * a jinwei = 0;for( i = a.size()-1 ; i < a.size() ;i-- ){d = IntToChar(   ( CharToNumber(a[i]) * CharToNumber(b[j]) + jinwei ) % 10 )+ d ;jinwei = ( CharToNumber(a[i]) * CharToNumber(b[j]) + jinwei ) / 10 ;}if(jinwei)d = IntToChar(jinwei) +d;// add all number resultc = ADD_Int(c,d);d = "";unsigned int zero = 0 ;while( zero < b.size() - j ){d = d + '0';zero ++;}}Standardization(c);if( fushu )return '-'+c;else return c;}// mode the division a/bstatic string DIV_Int(string a,string b){// exception of inputif( a.empty() )return "0";else if( b.empty() )return "e";if(!check_all_number(a) || !check_all_number(b)){return "exception of input DIV_Int";}Standardization(a);Standardization(b);if(b == "0")return "e";bool fushu =  (a[0] == '-' && b[0] != '-')||(a[0] != '-' && b[0] == '-');if( a[0] == '-' )a = a.substr(1,a.size());if( b[0] == '-' )b = b.substr(1,b.size());if( Compare(a,b) == '<' )return "0";string yushu = "";string beichushu = a.substr(0,b.size());string shang = Division( beichushu , b);yushu =  MinusInt( beichushu ,MULT_Int( shang, b) );string c = shang;for(string::size_type i = b.size(); i9 )return "e";return ""+IntToChar(i);}// make a-b mode int a - b;static string MinusInt(string a,string b){// exception of inputif(!check_all_number(a) || !check_all_number(b))return "exception of input MinusInt";Standardization(a);Standardization(b);// particular string of inputif(a.empty()){if(b.empty())return "0";elsereturn "-"+b;}else if(b.empty()){return a;}// normal number a < bstring c = "";bool check = true ;if(Compare(a,b) == '=')return "0";else if(Compare(a,b) == '<'){c = a ;a = b ;b = c ;c = "";check = false ;}// normal number a >= bstring::size_type i = a.size()-1, j = b.size()-1;int jiewei = 0,now;while(i < a.size() && j < b.size()){now = CharToNumber(a[i]) - CharToNumber(b[j]) - jiewei ;if( now < 0 ){jiewei = 1;now = 10 + now ;}else jiewei = 0;c = IntToChar(now)  + c ;i--;j--;}while(i < a.size()){now = CharToNumber(a[i]) - jiewei ;if( now < 0 ){jiewei = 1;now = 10 + now ;}else jiewei = 0;c = IntToChar( now )  + c ;i--;}Standardization(c);if(!check)c = '-' + c;return c; }// mode the add of intstatic string AddInt(string a,string b){// exception of inputif( a.empty() )return b;else if( b.empty() )return "0";if(!check_all_number(a) || !check_all_number(b)){return "exception of input AddInt";}Standardization(a);Standardization(b);string::size_type i = a.size()-1 ,j = b.size()-1 , k = 0 ;string c = "";int jinwei = 0;while( i < a.size() && j < b.size() ){c = IntToChar( ( CharToNumber(a[i]) + CharToNumber(b[j]) + jinwei ) % 10 ) + c;jinwei = ( CharToNumber(a[i]) + CharToNumber(b[j]) + jinwei ) / 10;j--;i--;}while( j < b.size()  ){c =  IntToChar( ( CharToNumber(b[j]) + jinwei ) % 10 ) + c;jinwei = ( jinwei + CharToNumber(b[j]) ) / 10;j--;}while( i < a.size() ){c =  IntToChar( ( CharToNumber(a[i]) + jinwei ) % 10 ) + c;jinwei = ( jinwei + CharToNumber(a[i]) ) / 10;i--;}if( jinwei )c = IntToChar(  jinwei  ) + c;Standardization(c);return c;}// make char to the int numberstatic int CharToNumber(char c){if( c >= '0' && c <= '9' )return int(c - '0');else {cout<
     
      = 0 && i <= 9 ){string c = "";return c+char(i+48);}else{cout<
      
        '9')return false;i++; }return true ;}// compare string a and bstatic char Compare(string a,string b){if(a.empty() || b.empty()){cout<<"error of input compare";return 'e';}else{if(!check_all_number(a) || !check_all_number(b)){return 'e';}Standardization(a);Standardization(b);if(a[0] == '-' && b[0] != '-')return '<';else if( a[0] != '-' && b[0] == '-')return '>';bool fushu = (a[0] == '-');if(a.size() > b.size() )return fushu?'<':'>';else if(a.size() == b.size()){for(string::size_type i = 0;i < a.size(); i++){if(a[i] > b[i])return fushu?'<':'>';if(a[i] < b[i])return fushu?'>':'<';}return '=';}return fushu?'>':'<';}}// make string into standard numberstatic void Standardization(string &a){if(!check_all_number(a)){cout<>a>>b;cout<<"a="<
       

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.