1. c++ 大数据整数处理 自己C++库更新版
#ifndef _BIG_DATA_STRING_H #define _BIG_DATA_STRING_H #include<iostream> #include <string> #include<Windows.h> #include<fstream> using namespace std; // FUNCTION: deal big number for int overflow mainly // 类中函数说明:每个函数名第一个标示符是大写字母,则是对外窗口,否则不是 // extra the class of string class IntString:public string { private: // function 17: go larger // input: a int keyword with string , and the int keyword key // output: a double number // 功能: 尝试找到一个区间使得 a^2 <= key <= b^2 static string _make_qujian(string key,int weishu); // function 18: binary search // input: two int numbers s and b, and the int keyword key and weishu // output: a double number with string // 功能: 实现sqrt函数,找到 r^2 == key static string _binary_search(string s,string b,string key,int weishu); // function 19: get perfect double // input: two strings with data and key ,weishu // output: a string with result(data < result < data+1) ,and result^2 <= key // 功能: 实现整数开方精确到小数部分 static string _get_perfect(string data,string key,int weishu); // function 19: get more number // input: two string data and key ,(bigger) // output: a pair with <bool,string>,if(true) perfect string,or bad string // 功能: 进一步精确小数的位数 static std::pair<bool,string> _get_more_number(string data,string key); public: // 约化分子分母 static std::pair<string,string> YuehuaFenshu(string a,string b); // 求出两个数的最大公约数 static string MaxYueshu(string a,string b); // 功能: 实现参数两个整数的相加操作,结果存在返回的字符串里 static string ADD_Int(string a,string b); // 功能: 实现参数两个整数的相减操作,结果存在返回的字符串里 static string SUB_Int(string a,string b); // 功能: 实现参数两个整数的相乘操作,结果存在返回的字符串里 static string MULT_Int(string a,string b); // 功能: 实现参数两个整数的相除操作,结果存在返回的字符串里 static string DIV_Int(string a,string b); // 功能: 实现参数两个整数的a^b操作,结果存在返回的字符串里 static string Pow_Int(string a,string b); // 功能: 将整数a转换成对应的字符串格式 static string Int_To_String(int x); // 对于正整数相除 static string Division(string a,string b); // 对于正整数相减 static string SubInt(string a,string b); // 对于正整数相加 static string AddInt(string a,string b); // 把一个字符(0-9)转成对应的整数 static int CharToNumber(char c); // 把一个一位整数(0-9)转成对应的字符串 static string IntToChar(int i); // 检查字符串里的数据是否是一个整数(可以是负数) static bool Check_all_number(string a); // 功能: 实现参数两个整数的a和b比较操作,结果< , = , >存在返回的字符里 static char Compare(string a,string b); // 把一个整数字符串规整化 static bool Standardization(string &a); // 功能: 将存在字符串里的整数取出来,放在整形容器里,然后返回,根据返回的结果可以判定是否转换成功 static std::pair<bool,int> String_into_intNumber(string &a); // 功能: 实现 sqrt 函数 开方函数 ,weishu 为小数长度 static string Sqrt_data( string data ,int weishu); // 功能: 求出a%b(a>0 && b>0) static string Qiuyu(string a,string b); // 功能: 实现取反操作,相反数 static string Qiu_fanshu(string a); // 功能 : 把一个十进制数转换成二进制数,bool值为二进制数的符号位 static std::pair<bool,string> Data_to_Binary_system(string data); // 功能:计算一个整数有多少位数 static int IntData_length(int N); // 功能:一个数组,整数的每一位,还有和放在数组的最后面 和它的长度 static std::pair<int*,int> IntToArray(int a); }; // mode the add of int string IntString::ADD_Int(string a,string b) { // exception of input if( 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 SubInt( a,b.substr( 1,b.size() ) ); else if(a[0] == '-'&& b[0] != '-') return SubInt(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; string IntString::SUB_Int(string a,string b) { // exception of input if( 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 SubInt(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 SubInt( b.substr(1,b.size()) , a.substr(1,a.size()) ); }; // make a*b mode int a * b; string IntString::MULT_Int(string a,string b) { // exception of input if( 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 result c = ADD_Int(c,d); d = ""; unsigned int zero = 0 ; while( zero < b.size() - j ) { d = d + '0'; zero ++; } } Standardization(c); if( fushu && c != "0" ) return '-'+c; else return c; }; // mode the division a/b string IntString::DIV_Int(string a,string b) { // exception of input if( 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 input a = "+a+" ,b= "+b; } 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 = SubInt( beichushu ,MULT_Int( shang, b) ); string c = shang; for(string::size_type i = b.size(); i<a.size(); i++) { beichushu = yushu+ a[i] ; shang = Division( beichushu , b); c = c + shang; yushu = SubInt( beichushu ,MULT_Int( shang, b) ); } Standardization(c); return fushu?('-'+c):c; }; // function: pow number x,y string IntString::Pow_Int(string a,string b) { // exception of input if( 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); string result = "1" ; if(Compare(b,"0") != '<') for(string i ="0" ;Compare(i,b) == '<' ;i = AddInt(i,"1")) { result = MULT_Int(result,a); } else for(string i ="0" ;Compare(i,b) == '>' ;i = SUB_Int(i,"1")) { result = DIV_Int(result,a); } return result ; }; // function : int To string string IntString::Int_To_String(int x) { bool fushu = false; string result=""; if(x < 0 ) { fushu = true ; x = -x; } else if( x == 0 ) return "0"; while(x) { result = IntToChar(x % 10) + result; x = x/10; } if(fushu) result = "-"+result; return result; }; // static char division a/b string IntString::Division(string a,string b) { // exception of input if( a.empty() ) return "0"; else if( b.empty() ) return "e"; if(!Check_all_number(a) || !Check_all_number(b)) { cout<<"exception of input Division"<<endl; return "e"; } Standardization(a); Standardization(b); int i = 0; while( i<=9 ) { // if a - b*i < b if( Compare( SUB_Int( a , MULT_Int(IntToChar(i),b) ) , b ) == '<' ) break; i++; } if( i>9 ) return "e"; return ""+IntToChar(i); }; // make a-b mode int a - b; string IntString::SubInt(string a,string b) { // exception of input if(!Check_all_number(a) || !Check_all_number(b)) return "exception of input MinusInt"; Standardization(a); Standardization(b); // particular string of input if(a.empty()) { if(b.empty()) return "0"; else return "-"+b; } else if(b.empty()) { return a; } // normal number a < b string 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 >= b string::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 int string IntString::AddInt(string a,string b) { // exception of input if( 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 number int IntString::CharToNumber(char c) { if( c >= '0' && c <= '9' ) return int(c - '0'); else { cout<<c<<" exception of input CharToNumber "<<endl; return 0; } }; // make int to the model char string IntString::IntToChar(int i) { if( i >= 0 && i <= 9 ) { string c = ""; return c+char(i+48); } else { cout<<i<<" exception of input IntToChar"<<endl; return "e"; } }; // check whether the string is legal bool IntString::Check_all_number(string a) { if(a.empty()) return true ; string::size_type L = a.size(),i = 0; if(a[0] == '-') i++; while( i < L ) { if( a[i] < '0' || a[i] > '9') return false; i++; } return true ; }; // compare string a and b char IntString::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 string number bool IntString::Standardization(string &a) { if(!Check_all_number(a)) { cout<<a<<" exception of input Standardization"<<endl; return false; } string::size_type i = 0 ; bool fushu = false ; if( a[0] == '-' ) { fushu = true ; i = 1 ; } while(i < a.size()) { if(a[i] != '0') break; i++; } if(i == a.size()) i--; a = a.substr(i,a.size()-i) ; if( fushu && a != "0") a = '-' + a ; return true ; }; // make string(>0) into standard int number std::pair<bool,int> IntString::String_into_intNumber(string &a) { if(Standardization(a)) { int max_int = INT_MAX ; string max = Int_To_String(max_int); bool fushu = false; if(a[0] == '-') { fushu = true ; a = a.substr(1,a.length()); } if(Compare(a,max) != '<') { cout<<"溢出 String_into_intNumber"<<endl; return std::make_pair(false,0); } int result = 0 ; for(size_t i =0;i<a.length();i++) { result = result * 10 + CharToNumber(a[i]); } if(fushu) result = - result; return std::make_pair(true,result); } else { cout<<"exception of function String_into_intNumber input"<<endl; return std::make_pair(false,0); } }; // function: get data // input: a int number data with string // output: a double number b with string // 功能: 实现 sqrt 函数 string IntString::Sqrt_data( string data ,int weishu) { if(weishu < 0) weishu = 0; if(! IntString::Check_all_number(data)) { cout<<"exception of function _sqrt_data input"<<endl; return "NULL"; } IntString::Standardization(data); char r = IntString::Compare(data,"0"); if(r == '=') return "0"; else if(r == '<') { cout<<"exception of function _sqrt_data input"<<endl; } return _make_qujian(data,weishu); } // function: go larger // input: two int number a and b, and the int keyword key // output: a double number with string // 功能: 尝试找到一个区间使得 a^2 <= key <= b^2 string IntString::_make_qujian(string key,int weishu) { if( IntString::Compare(key,"0") == '>' && weishu >=0 ) { string s,b; int length = key.length()/2; // 偶数 if(key.length() % 2 == 0) { s = IntString::Pow_Int("10",IntString::Int_To_String(length-1)); b = IntString::Pow_Int("10",IntString::Int_To_String(length)); } // 奇数 else { s = IntString::Pow_Int("10",IntString::Int_To_String(length)); b = IntString::Pow_Int("10",IntString::Int_To_String(length+1)); } return _binary_search(s,b,key,weishu);// binary search } else { cout<<"exception of function _go_larger input"<<endl; return "NULL"; } } // function: binary search // input: two int numbers s and b, and the int keyword key // output: a double number with string // 功能: 实现sqrt函数,找到 r^2 == key string IntString::_binary_search(string s,string b,string key,int weishu) { if( IntString::Compare(s,"1")!= '<' && IntString::Compare(b,s) != '<' && weishu >= 0 ) { // 检查是否是范围临界值 string s_2 = IntString::MULT_Int(s,s); string b_2 = IntString::MULT_Int(b,b); if(IntString::Compare(key,s_2) == '=') return s; if(IntString::Compare(key,b_2) == '=') return b; // 核心算法:求出中间值 string zhong ,zhong_2,zhong2_2; char r ; while(IntString::Compare(s,b) != '>') { // 求出中间值 zhong = IntString::DIV_Int(IntString::ADD_Int(s,b) ,"2"); zhong_2 = IntString::MULT_Int(zhong,zhong); r = IntString::Compare(zhong_2,key); // 中间的平方大于key值时,这时候最大值减小 if(r == '>') b = IntString::SUB_Int(zhong,"1"); // 出口是,相等时 else if( r == '=' ) return zhong; // 中间的平方小于key值时,中间值要变大,要怎么增大呢 else if(r == '<') { zhong2_2 = IntString::MULT_Int(IntString::ADD_Int(zhong,"1"),IntString::ADD_Int(zhong,"1")); r = IntString::Compare(zhong2_2,key); // 中间值过大时,则要取小数部分 if(r == '>') return _get_perfect(zhong,key,weishu); // 中间值不够大,最小值变大 else s = IntString::ADD_Int(zhong,"1"); } } return "no result"; } else { cout<<"输入异常 _binary_search input"<<endl; return "error"; } } // function: get perfect double // input: two strings with data and key // output: a string with result(data < result < data+1) ,and result^2 <= key // 功能: 实现整数开方精确到小数部分 string IntString::_get_perfect(string data,string key,int weishu) { int i = weishu; string d = data; string k = key; string result; std::pair<bool,string> r ; while(i > 0) { r = _get_more_number(d,k); if(r.first == true) break; else { d = r.second; k += "00"; } i--; } if(weishu != 0) result = data+"."+d.substr(data.length(),d.length()-data.length()); else result = data; return result; } // function: get more number // input: two string data and key ,(bigger) // output: a pair with <bool,string>,if(true) perfect string,or bad string // 功能: 进一步精确小数的位数 std::pair<bool,string> IntString::_get_more_number(string data,string key) { char s = '1',e ='9',zhong,guanxi1,guanxi2 ; key += "00" ; string r1,r2 ; r1 = IntString::MULT_Int((data+'9'),(data+'9')) ; guanxi1 = IntString::Compare(r1,key) ; if(guanxi1 == '<') return std::make_pair(false,data+'9'); else if(guanxi1 == '=') return std::make_pair(true,data+'9'); r1 = IntString::MULT_Int((data+'1'),(data+'1')); guanxi1 = IntString::Compare(r1,key); if(guanxi1 == '=') return std::make_pair(true,data+'1'); else if(guanxi1 == '>') return std::make_pair(false,data+'0'); while(s <= e) { zhong = (s+e)/2; r1 = IntString::MULT_Int((data+zhong),(data+zhong)); guanxi1 = IntString::Compare(r1,key); if(guanxi1 == '=') { return std::make_pair(true,data+zhong); } else if(guanxi1 == '>') e = zhong -1; else if(guanxi1 == '<') { r2 = IntString::MULT_Int((data+char(zhong+1)),(data+char(zhong+1))); guanxi2 = IntString::Compare(r2,key); if(guanxi2 == '>') return std::make_pair(false,data+zhong); else s = zhong+1; } } } // function: 求余操作( 对两个正整数 ) // input: two int number within string // output: a int number within string // 功能: 求出a%b string IntString::Qiuyu(string a,string b) { if(IntString::Compare(a,"0") != '>' || IntString::Compare(b,"0") != '>') return "error of function Qiuyu input"; string shang = IntString::DIV_Int(a,b); string chengji = IntString::MULT_Int(shang,b); return IntString::SUB_Int(a,chengji); } // function: 求反数即对符号去反 // input: a int number within string // output: a int number within string // 功能: 实现取反操作 string IntString::Qiu_fanshu(string a) { if(IntString::Check_all_number(a)) { IntString::Standardization(a); if(a[0] == '-') return a.substr(1,a.length()-1); else return ("-"+a) ; } else { cout<<"exception of function Qiu_fanshu input"<<endl; return "error"; } } // function: 把一个十进制整数转换成二进制数 // input: a int number within string // output: a fuhao wei within bool and a binary_system result with string // 功能 : 把一个十进制整数转换成二进制数 std::pair<bool,string> IntString::Data_to_Binary_system(string data) { if(IntString::Check_all_number(data)) { IntString::Standardization(data); string result = ""; if(IntString::Compare(data,"0") == '=') return std::make_pair(false,"0"); bool fushu = false; if(IntString::Compare(data,"0") == '<') { data = IntString::Qiu_fanshu(data); fushu = true; } while(IntString::Compare(data,"0") == '>') { result = IntString::Qiuyu(data,"2") + result ; data = IntString::DIV_Int(data,"2"); } return std::make_pair(fushu,result); } else { cout<<"exception of function Data_to_Binary_system input"<<endl; return make_pair(true,"error"); } } // function 23: 计算一个整数有多少位数 // 输入: 一个整数 // 输出:整数的位数 int IntString::IntData_length(int N) { int data = N; int length = 0; while(data != 0) { data = data/10; length ++; } return length ; } // function 24: 把一个整型数变成一个数组 // 输入: 一个整数 // 输出:一个数组和它的长度 std::pair<int *,int> IntString::IntToArray(int a) { // 数据初始化 int * data = new int[100] ; int length = 0 ; int sum = 0 ; // 异常输入 if( a<0 ) return std::make_pair(data,-1); // 特殊输入 if(a == 0) { data[0] = 0; length = 1 ; data[length] = 0; } // 正常输入,一般一个整数位数不会超过 100 // 整数的低位存在数组的前面,整数的高位存在数组的后面 while( a>0 ) { data[length] = a%10 ; sum += data[length] ; length ++ ; a = a/10 ; } data[length] = sum; // 返回结果 return std::make_pair(data,length); } // 约化分子分母 std::pair<string,string> IntString::YuehuaFenshu(string a,string b) { string c = MaxYueshu(a,b); a = DIV_Int(a,c); b = DIV_Int(b,c); return std::make_pair(a,b); } // 求出两个数的最大公约数 string IntString::MaxYueshu(string a,string b) { // 异常输入 if(Check_all_number(a) != true || Check_all_number(b) != true || Compare(a,"0") != '>' || Compare(b,"0") != '>') return "error of String::MaxYueshu input a= "+a+" ,b= "+b; // 正常输入,规划a和b,使得a >= b if(Compare(a,b) == '<') { string c = a ; a = b ; b = c ; } // 判断余数 string yushu = IntString::Qiuyu(a,b); while(Compare(yushu,"0") != '=') { a = b; b = yushu ; yushu = Qiuyu(a,b); } // 返回结果b return b; } #endif
时间: 2024-10-12 14:58:59