• 面试题:实现大数加减乘除四则运算


    #include <iostream>
    #include <cstdlib>
    #include <vector>
    #include <sstream>
    #include <algorithm>
    using namespace std;

    class Solution {
    public:
    string addTwoString(string nums1, string nums2) {
    int n1 = nums1.size();
    int n2 = nums2.size();
    //便于操作,统一转换为长数加短数
    if (n1 < n2)
    return addTwoString(nums2, nums1);

    int minlen = n2;
    int maxlen = n1;
    string result;
    int carry = 0;
    for (int i = 0; i < minlen; i++) {
    int sum = nums1[--n1] - '0' + nums2[--n2] - '0' + carry;
    carry = sum / 10;
    int a = sum % 10;
    result.push_back(a + '0');
    }
    for (int i = minlen; i < maxlen; i++) {
    int sum = nums1[--n1] - '0' + carry;
    carry = sum / 10;
    int a = sum % 10;
    result.push_back(a + '0');
    }

    if (carry != 0)
    result.push_back(carry + '0');

    reverse(result.begin(), result.end());
    return result;
    }

    string multiplyTwoString(string nums1, string nums2) {
    string result = "0";
    if (nums1 == "0" || nums2 == "0")
    return result;

    vector<string> tmpResult;
    int n1 = nums1.size();
    int n2 = nums2.size();

    int carry = 0;
    reverse(nums1.begin(), nums1.end());
    reverse(nums2.begin(), nums2.end());

    for (int i = 0; i < n1; i++) {
    string str;
    for (int j = 0; j < n2; j++) {
    int sum = (nums1[i] - '0') * (nums2[j] - '0') + carry;
    carry = sum / 10;
    int a = sum - carry * 10;
    str.push_back(a + '0');
    }
    if (carry != 0)
    str.push_back(carry + '0');
    carry = 0;
    reverse(str.begin(), str.end());
    for (int k = 0; k < i; k++) {
    str.push_back('0');
    }
    tmpResult.push_back(str);
    }

    int size = tmpResult.size();

    for (int i = 0; i < size; i++) {
    result = addTwoString(result, tmpResult[i]);
    }
    return result;
    }

    string substractTwoString(string nums1, string nums2) {
    string result;
    int n1 = nums1.size();
    int n2 = nums2.size();

    //判断符号为正负
    char sign = '+';
    if (n1 < n2) {
    sign = '-';
    nums1.swap(nums2);

    } else if (n1 == n2) {
    for (int i = 0; i < n1; i++) {
    if (nums1[i] > nums2[i]) {
    break;
    } else if (nums1[i] < nums2[i]) {
    sign = '-';
    nums1.swap(nums2);
    break;
    }
    }
    }
    int borrow = 0;
    reverse(nums1.begin(), nums1.end());
    reverse(nums2.begin(), nums2.end());

    n1 = nums1.size();
    n2 = nums2.size();

    for (int i = 0; i < n2; i++) {
    int r = nums1[i] - nums2[i] - borrow;
    borrow = 0;
    if (r < 0) {
    r = r + 10;
    borrow = 1;
    }
    result.push_back(r + '0');
    }

    for (int i = n2; i < n1; i++) {
    int r = nums1[i] - '0' - borrow;
    borrow = 0;
    if (r < 0) {
    r = r + 10;
    borrow = 1;
    }
    result.push_back(r + '0');
    }

    for (int i = n1 - 1; i >= 0; i--) {
    if (result[i] == '0')
    result.erase(result.begin() + i);
    else
    break;
    }
    reverse(result.begin(), result.end());
    if (sign == '-') {
    result.insert(result.begin(), '-');
    }
    return result;
    }


    string divideTwoString(string nums1, string nums2) {
    string result;
    result = substractTwoString(nums1, nums2);
    if (result[0] == '-') {
    result = "0";
    return result;
    }
    result = "";
    int n1 = nums1.size();

    stringstream stream;
    string dividend;

    for (int j = 0; j < n1; j++) {
    dividend.push_back(nums1[j]); //被除数
    //从9到0,试探性的找商,当余数不为负数时,为当前位的值
    for (int i = 9; i >= 0; i--) {
    stream.str("");
    stream << i;
    string s = stream.str();
    string sj = multiplyTwoString(nums2, s);
    string remain = substractTwoString(dividend, sj);

    if(remain[0] != '-'){
    result.push_back(i+'0');
    dividend = remain;
    break;
    }
    }
    }
    //去掉商前面的零位
    int size = result.size();
    for(int i=0;i<size;i++){
    if(result[0]=='0'){
    result.erase(result.begin());
    }else {
    break;
    }
    }
    //四舍五入
    string s = substractTwoString(multiplyTwoString(dividend,"2"),nums2);
    if(s[0] == '-') addTwoString(result,"1");

    return result;
    }
    };

    int main() {
    string nums1 = "123456";
    string nums2 = "789";
    Solution s;
    cout << s.addTwoString(nums1, nums2) << endl;
    cout << s.multiplyTwoString(nums1, nums2) << endl;
    cout << s.substractTwoString(nums1, nums2) << endl;
    cout << s.divideTwoString(nums1, nums2) << endl;
    return 0;
    }

  • 相关阅读:
    css
    数据库
    手机编码
    火锅开2了
    Linux 与 Virtual PC(VirtualBox)
    切图
    [zz] Loading Master Data without using the PSA in SAP BI 7.0
    Notes: CRM Analytics–BI from a CRM perspective (2)
    Notes: CRM Analytics–BI from a CRM perspective (3)
    Sharing: hints, tcodes, commands that may help when solving support package implementation problems
  • 原文地址:https://www.cnblogs.com/H-xiaofeng/p/12730992.html
Copyright © 2020-2023  润新知