• C++ string 实现大整数相加减


    随意两个大整数的加减算法。可自己主动推断正负号。代码例如以下:

    #include <iostream>
    #include <vector>
    #include <cstring>
    #include <algorithm>
    #include <string>
    
    using namespace std;
    
    string BigInegerAdd(string s1, string s2) // s1+s2;
    {
    	int len = s1.size()>s2.size()?

    s1.size()+1:s2.size()+1; string res(len, '0'); int i = s1.size() - 1, j = s2.size() - 1, k = len - 1; int borrow = 0; while(i >=0 && j >= 0) { int sum = s1[i] - '0' + s2[j] - '0' + borrow; if (sum >= 10) { borrow = 1; sum -= 10; res[k--] = sum + '0'; } else {res[k--] = sum + '0'; borrow = 0;} i--;j--; } while(i >= 0) { int sum = s1[i] - '0' + borrow; if (sum >= 10) { borrow = 1; sum -= 10; res[k--] = sum + '0'; } else {res[k--] = sum + '0'; borrow = 0;} i--; } while(j >= 0) { int sum = s2[j] - '0' + borrow; if (sum >= 10) { borrow = 1; sum -= 10; res[k--] = sum + '0'; } else {res[k--] = sum + '0'; borrow = 0;} j--; } if (borrow == 1) { res[k] = '1'; } else res[k] = '0'; if (res[0] == '0')//ignore the prefix '0's { return res.substr(1, res.size()-1); } else return res; } //negative == true means s1 < s2 string BigInegerMinus(string s1, string s2, bool negative) // s1-s2; { if (s1.size() < s2.size()) { return BigInegerMinus(s2, s1, true); } if (s1.size() == s2.size()) { int i = 0; while(i < s1.size() && s1[i] == s2[i]) i++; if (s1[i] < s2[i]) { return BigInegerMinus(s2, s1, true); } } string res(s1.size(), '0'); int i = s1.size() -1, j = s2.size() - 1; int k = i; int borrow = 0; while(i >= 0 && j >= 0) { int sum = s1[i] - '0' - borrow - (s2[j] - '0'); //cout<<sum<<endl; if (sum < 0) { borrow = 1; sum += 10; res[k--] = sum + '0'; } else{ borrow = 0; res[k--] = sum + '0'; } i--;j--; } while(i >= 0) { int sum = s1[i--] - '0' - borrow; if (sum < 0) { borrow = 1; sum += 10; res[k--] = sum + '0'; } else{ borrow = 0; res[k--] = sum + '0'; } } if (res[0] == '0') { //ignore the prefix '0's int index = 1; while(index < res.size() && res[index] == '0') index++; if (negative) { return "-" + res.substr(index, res.size() - index); } else return res.substr(index, res.size() - index); } else { if (negative) { return "-" + res; } else return res; } } string BigIneger(string s1, string s2) { if (s1 == "") { return s2; } if (s2 == "") { return s1; } char sign1 = '+', sign2 = '+'; if (s1[0] == '-') { sign1 = '-'; } if (s2[0] == '-') { sign2 = '-'; } if (sign1 == '+' && sign2 == '+') { return BigInegerAdd(s1, s2); } else if (sign1 == '+' && sign2 == '-') { return BigInegerMinus(s1, s2.substr(1, s2.size()-1),false); } else if (sign1 == '-' && sign2 == '+') { return BigInegerMinus(s2, s1.substr(1, s1.size()-1), false); } else { string tmp = BigInegerAdd(s1.substr(1, s1.size()-1), s2.substr(1, s2.size()-1)); if (tmp[0] == '0') { tmp = tmp.substr(1, tmp.size()- 1); } string res = "-" + tmp; return res; } } int main() { string s1 = "-789546321", s2 = "-15462897444"; cout<<BigIneger(s1,s2)<<endl; return 0; }



  • 相关阅读:
    WBS概念
    Cognos 8体系结构:请求流程
    Cognos8.4安装配置指南
    Cognos 8体系结构:层级结构
    程序猿媛都偷偷深爱的九大习惯
    Cookie是存储在客户端上的一小段数据
    Web前端必须规避的8个误区
    WEB前端开发常用的优化技巧汇总
    微信小程序怎么开发(小程序开发文档)
    入门 IT 行业,该具备哪些技能?
  • 原文地址:https://www.cnblogs.com/slgkaifa/p/6878195.html
Copyright © 2020-2023  润新知