📜  两个大浮点数之和

📅  最后修改于: 2021-09-02 06:28:55             🧑  作者: Mango

给定两个大字符串str1str2形式的非常大的浮点数,任务是将给定的两个数字相加。

例子:

方法:
为了找到无法存储在内置数据类型中的两个大整数加法,我们将使用一个数组来存储数字的位数,然后从 LSB 开始逐位执行加法运算。

使用这个概念,我们还可以找到大浮点数的总和。

将两个给定浮点数相加的步骤:

  1. 将给定的浮点数以字符串的形式相对于小数点分开,以分隔数字的小数部分和整数部分。
  2. 将两个数的小数部分和整数部分分别相加,并将小数加法的最后进位部分转发到整数部分。
    例如:
    str1 = "23.94" and str2 = "34.23"
    
    For fractional part:
    f1[] = {4, 9}
    f2[] = {3, 2}
    --------------
    Sum  = {7, 1, 1}
    Therefore, Carry = 1
    
    For Integer part:
    Carry = 1
    I1[] = {3, 2}
    I2[] = {4, 3}
    --------------
    Sum  = {8, 5}
    
  3. 用小数“.”连接为整数和小数部分存储的数字获得所需的总和两个大浮点数。
    From Integer part = 58
    From fractional part = 17
    
    Sum = 58.17
    

下面是上述方法的实现:

// C++ program to find Sum of two
// large Floating-point numbers
  
#include 
using namespace std;
  
// Function to make fractional part
// with equal digits
void makeEqualAtFront(vector& A,
                      vector& B)
{
    int n = A.size();
    int m = B.size();
    int diff = abs(n - m);
  
    if (n < m) {
        for (int i = 0; i < diff; i++) {
            A.insert(A.begin(), 0);
        }
    }
    else {
        for (int i = 0; i < diff; i++) {
            B.insert(B.begin(), 0);
        }
    }
}
  
// Function to make Integral part
// with equal digits
void makeEqualAtback(vector& A,
                     vector& B)
{
    int n = A.size();
    int m = B.size();
    int diff = abs(n - m);
  
    if (n < m) {
        for (int i = 0; i < diff; i++) {
            A.push_back(0);
        }
    }
    else {
        for (int i = 0; i < diff; i++) {
            B.push_back(0);
        }
    }
}
  
// Function to add the given large
// floating point number string
void findSum(string s1, string s2)
{
  
    int i;
  
    // To store the integer and
    // fractional part of numbers
    vector Ints1, Ints2;
    vector Fracs1, Fracs2;
  
    // Separating integer and
    // fractional part of s1
    for (i = s1.length() - 1; i > -1; i--) {
  
        // If decimal occurs break
        if (s1[i] == '.') {
            break;
        }
        Fracs1.push_back(s1[i] - '0');
    }
  
    i--;
    for (; i > -1; i--) {
        Ints1.push_back(s1[i] - '0');
    }
  
    // Separating integer and
    // fractional part of s2
    for (i = s2.length() - 1; i > -1; i--) {
  
        // If decimal occurs break
        if (s2[i] == '.') {
            break;
        }
        Fracs2.push_back(s2[i] - '0');
    }
  
    i--;
    for (; i > -1; i--) {
        Ints2.push_back(s2[i] - '0');
    }
  
    // Making number of digits in
    // fractional and Integer
    // part equal
    makeEqualAtFront(Fracs1, Fracs2);
    makeEqualAtback(Ints1, Ints2);
  
    // Adding fractional parts of
    // s1 and s2
    int n = Fracs1.size();
    int m = Fracs2.size();
    i = 0;
    int carry = 0;
  
    while (i < n && i < m) {
  
        // Traverse the Fracs1[] and
        // Fracs2[] and add the digit
        // and store the carry
        int sum = Fracs1[i]
                  + Fracs2[i]
                  + carry;
  
        Fracs1[i] = sum % 10;
        carry = sum / 10;
        i++;
    }
  
    int N = Ints1.size();
    int M = Ints2.size();
    i = 0;
  
    // Adding integer part of
    // s1 and s2
    while (i < N && i < M) {
        int sum = Ints1[i]
                  + Ints2[i]
                  + carry;
        Ints1[i] = sum % 10;
        carry = sum / 10;
        i++;
    }
    if (carry != 0)
        Ints1.push_back(carry);
  
    // Print the result by appending
    // Integer and decimal part stored
    // in Ints1[] and Fracs1[]
    for (int i = Ints1.size() - 1; i > -1; i--) {
        cout << Ints1[i];
    }
    cout << '.';
    for (int i = Fracs1.size() - 1; i > -1; i--) {
        cout << Fracs1[i];
    }
}
  
// Driver Code
int main()
{
    string str1
        = "584506134.87368350839565308";
    string str2
        = "30598657.0330473560587475634983";
  
    findSum(str1, str2);
  
    return 0;
}
输出:
615104791.9067308644544006434983

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live