高精度A+B

 主要思想:进位加一,退0补0,(去前导零)

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string a, b;  
    cin >> a >> b;   //输入字符串数组
    int i = a.length() - 1, j = b.length() - 1, add = 0,x,y;
    string ans="";
    while (i >= 0 || j >= 0 || add != 0)  //add!=0是因为如果加到最高位的时候要进位,有这个条件则能继续循环,否则循环结束
    {
        if(i >= 0) x=a[i] - '0' ;
        else x=0;
        if(j >= 0) y=b[j] - '0' ;
        else y=0;
        int result = x + y + add;    //从个位数开始相加,add表示进位 
        ans.push_back('0' + result % 10);   //a.push_back(5); 表示在a的最后一个向量后插入一个元素,其值为5
        add = result / 10; 
        i--;
        j--;
    }    
    //reverse(a.begin(),a.end()); 对a中的从a.begin()(包括它)到a.end()(不包括它)的元素倒置,但不排列,如a中元素为1,3,2,4,倒置后为4,2,3,1    
    reverse(ans.begin(), ans.end());
    //去除前导0
    int start = 0;
    for (int j = 0; j < ans.length(); j++)
    {
        if (ans[j] == '0')
            start++;
        else
            break;
    }
    if (start == ans.length())
    {
        cout << '0';
        return 0;
    }
    ans = ans.substr(start); 
    cout << ans;
    
    return 0;
}
  1. 首先以字符串的形式输入a和b
  2. 从个位数开始相加,i初始表示a的个位数,j初始表示b的个位数,ans表示最后得到的字符串
  3. 当i>=0或者j>=0时,将两数相加;如果i>=0,将a[i]转为数字,再与y相加;
  4.  if(i >= 0) x=a[i] - '0' ;
            else x=0;
            if(j >= 0) y=b[j] - '0' ;
            else y=0;

    else是因为如果a或者b其中一个数字被加完了之后,再往上的位数就为0了,就不需要再加了,直接用0加,将x和y初始赋值为0也是一样的

  5. ans.push_back('0' + result % 10);
    add = result / 10; 

    表示以字符串的形式将得到的result的个位数加到ans里去;add表示进位

  6.  reverse(a.begin(),a.end()); 表示对a中的从a.begin()(包括它)到a.end()(不包括它)的元素倒置,但不排列,如a中元素为1,3,2,4,倒置后为4,2,3,1 ;因为是从个位数开始相加然后加到ans中的,所以最后要反转倒序ans;

  7. 这道题是不需要 去除前导零的,但以后也会碰到这种题型;start用来计数前面的零的个数;从左往右数,一旦没有了,立马break;

  8. string s = "123456";

    string sub1 = s.substr(3); //只有一个数字3表示从下标为3开始一直到结尾:sub1 = "456"

    string sub2 = s.substr(5, 2); //从下标为5开始截取长度为3位:sub2 = "45"

C++中的高精度运算符重载是指对整数进行大数运算时,通过重载运算符来实现对大数的加减乘除等操作。一般情况下,C++内置的整数类型(如int、long等)有一定的位数限制,无法处理超过其表示范围的大数。而通过运算符重载,我们可以自定义一个类来表示大数,并对其进行各种运算操作。 以下是一个简单的示例,展示了如何实现C++中的高精度运算符重载: ```cpp #include <iostream> #include <vector> using namespace std; class BigInteger { private: vector<int> digits; // 用vector存储大数的每一位 public: BigInteger() {} BigInteger(int num) { while (num > 0) { digits.push_back(num % 10); num /= 10; } } BigInteger operator+(const BigInteger& other) const { BigInteger result; int carry = 0; int i = 0; while (i < digits.size() || i < other.digits.size() || carry != 0) { int sum = carry; if (i < digits.size()) { sum += digits[i]; } if (i < other.digits.size()) { sum += other.digits[i]; } result.digits.push_back(sum % 10); carry = sum / 10; i++; } return result; } friend ostream& operator<<(ostream& os, const BigInteger& num) { for (int i = num.digits.size() - 1; i >= 0; i--) { os << num.digits[i]; } return os; } }; int main() { BigInteger a(123456789); BigInteger b(987654321); BigInteger c = a + b; cout << "a + b = " << c << endl; return 0; } ``` 在上述示例中,我们定义了一个名为BigInteger的类,用于表示大数。通过重载加法运算符`+`,我们可以实现对两个BigInteger对象的相加操作。同时,我们还重载了输出流运算符`<<`,以便能够直接输出BigInteger对象的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值