面試題之自創 實現兩個整型大數(都是正數)的相加操作

來源:互聯網
上載者:User

test.cpp(更新過)
#include <iostream>#include <string>using namespace std;// extra the class of stringclass String:public string{public:// mode the add of intstatic string Add(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";}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;return c;}// make char to the int numberstatic int CharToNumber(char c){if( c >= '0' && c <= '9' )return int(c - '0');else {cout<<"exception of input CharToNumber "<<endl;system("pause");return 0;}}// make int to the model charstatic char IntToChar(int i){if( i >= 0 && i <= 9 ){return char(i+48);}else{cout<<i<<" exception of input IntToChar"<<endl;system("pause");}}// check whether the string is legal static bool check_all_number(string a){if(a.empty())return true ;string::size_type L = a.size(),i = 0;while( i < L ){if( a[i] < '0' || a[i] > '9')return false;i++; }return true ;}// make a-b mode int a - b;static string Multiplies(string a,string b){// exception of inputif(!check_all_number(a) || !check_all_number(b))return "exception of input Multiplies";Standardization(a);Standardization(b);// particular string of inputif(a.empty()){if(b.empty())return "0";elsereturn "-"+b;}else if(b.empty()){return a;}// normal numberstring c = "";bool check = true ;if(Compare(a,b) == '=')return "0";else if(Compare(a,b) == '<'){c = a ;a = b ;b = c ;c = "";check = false ;}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; }// compare string a and bstatic char Compare(string a,string b){if(a.empty() || b.empty()){cout<<"error of imput compare";return 'e';}else{if(a.size() > b.size())return '>' ;else if(a.size() == b.size()){for(string::size_type i = 0;i < a.size(); i++){if(a[i] > b[i])return '>';if(a[i] < b[i])return '<';}return '=';}return '<';}}static void Standardization(string &a){if(!check_all_number(a)){cout<<a<<" exception of input Standardization"<<endl;}string::size_type i = 0;while(i < a.size()){if(a[i] != '0')break;i++;}a = a.substr(i,a.size());}};// main functionint main(){string a ;string b ;cin>>a>>b;cout<<"a="<<a<<",b="<<b<<endl;cout<<String::Add(a,b)<<endl;system("pause");return 0;}




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.