面试题之自创 大数相加减
2014-01-01 21:56
253 查看
大数相加减
这是我补前面大正整数相加减的博客,正数很常用,但是如果出现负数,我们肿么办?
#include <iostream> #include <string> using namespace std; // extra the class of string class String:public string { public: // mode the add of int static string 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 MultipliesInt( a,b.substr( 1,b.size() ) ); else if(a[0] == '-'&& b[0] != '-') return MultipliesInt(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 MUL_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 MultipliesInt(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 MultipliesInt( b.substr(1,b.size()) , a.substr(1,a.size()) ); } private: // make a-b mode int a - b; static string MultipliesInt(string a,string b) { // exception of input if(!check_all_number(a) || !check_all_number(b)) return "exception of input Multiplies"; 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 static string 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; return c; } // make char to the int number static 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 char static 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; if(a[0] == '-') i++; while( i < L ) { if( a[i] < '0' || a[i] > '9') return false; i++; } return true ; } // compare string a and b static 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 '<'; } } // make string into standard number static void Standardization(string &a) { if(!check_all_number(a)) { cout<<a<<" exception of input Standardization"<<endl; } 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()); if( fushu && a != "0") a = '-' + a; } }; // main function int main() { string a ; string b ; cin>>a>>b; cout<<"a="<<a<<",b="<<b<<endl; cout<<"add = "<<String::ADD_Int(a,b)<<endl; cout<<"mul = "<<String::MUL_Int(a,b)<<endl; system("pause"); return 0; }
相关文章推荐
- 面试题之自创 实现两个整型大数(都是正数)的相加操作
- 面试题之自创 实现两个整型大数(都是正数)的相减操作
- 面试题大数相加相减
- 剑指offer之面试题12 大数相加 实现任意两个整数的加法
- 一道PHP面试题 大数相加 溢出的处理
- 面试题之自创 大数相乘
- 一道PHP面试题 大数相加 溢出的处理
- JavaScript 面试题 - 大数相加
- 面试题之自创 大数相除
- 大数相加(1+2+...+N)
- [leetcode] 2. Add Two Numbers(链表、大数相加)
- 栈的应用2——大数相加
- C/C++和JAVA 实现大数相加
- [ACM Steps]2.3.1 A+B Problem Ⅱ 大数相加
- HDoj1047Integer Inquiry(大数连续相加)
- 高精度问题之大数相加
- hdoj1002A + B Problem II(大数相加)
- leetcode_415(两个数字字符串相加,模拟大数相加)
- [各种面试题] 大数乘法
- HDU 1047。多个大数相加