c++ - 我的代码无法计算非常大的金额,我不知道为什么

标签 c++

这是我的代码。本练习的目的是创建可以计算非常大的总和的代码。我正在反转两个字符串,添加,然后反转生成的字符串。

// Basic mechanism: 
//reverse both strings
//reversing the strings works because ex. 12+12 is the same as 21+21=42->reverse->24
//add digits one by one to the end of the smaller string
//dividing each sum by 10 and attaching the remainder to the end of the result-> gets us the carry over value 
// reverse the result.
//ex. 45+45 ->reverse = 54+54 -> do the tens place -> 0->carry over -> 4+4+1 -> result= 09 -> reverse -> 90.
#include <iostream>
#include <cstring>
using namespace std;

    string strA;
    string strB;
    string ResStr = ""; // empty result string for storing the result 
    int carry =0;
    int  sum; //intermediary sum
    int n1; //length of string 1
    int n2; // length of string 2
    int rem; // remainder
int main()
{
    cout << "enter" << endl;
    cin >> strA;
    cout << "enter" << endl; // I didn't know how to write this program to use argv[1] and argv[2] so this was my solution 
    cin >> strB;
  
    // turning the length of each string into an integer 
    int n1 = strA.length(), n2 = strB.length();
 
    // Reversing both of the strings so that the ones, tens, etc. positions line up (the computer reads from left to right but we want it to read from right to left)
    reverse(strA.begin(), strA.end());
    reverse(strB.begin(), strB.end());
 
  
    for (int i=0; i<n1; i++)//start at 0, perform this operation until the amount of times reaches the value of n1
    {
        //get the sum of current digits 
        int sum = ((strA[i]-'0')+(strB[i]-'0')+carry);
        int rem=(sum%10);
        ResStr+=(rem+'0'); //this gets the remainder and adds it to the next row

        // Calculate carry for next step. Thus works because carry is an integer type, so it will truncate the quotient to an integer, which is what we want
        carry = sum/10;
    }
 
    // Add the remaining digits of the larger number 
    for (int i=n1; i<n2; i++) //start at n1, perform this operation until the amount of times reaches the value of n2
    {
        int sum = ((strA[i]-'0')+carry);
        int rem=(sum%10);
        ResStr+=(rem+'0');
        carry = sum/10;
    }
 
    // Add remaining carry over value
    if (carry)
        ResStr+=(carry+'0');
    // reverse the resulting string back, because we reversed it at the beginning 
    reverse(ResStr.begin(), ResStr.end());
    
    cout << "The result is " << ResStr << endl;
    return 0;
}

它能够结转,因为它可以计算更小的数字,但它不能计算 9,999,999,999 +1。

最佳答案

问题在于您混淆了两个循环中的条件。

该算法要求最长的数字位于 strA 中,较短的数字位于 strB 中,因为:

int n1 = strA.length(), n2 = strB.length();

正确的循环是:

for (int i = 0; i < n2; i++)  // first loop, loop short
for (int i = n2; i < n1; i++) // second loop, do the rest in strA

Demo

我还建议您降低程序对用户输入数字的顺序的敏感度。输入数字后,如果 strBstrA 长,您可以交换顺序:

if(strB.length() > strA.length()) std::swap(strA, strB);

Demo

关于c++ - 我的代码无法计算非常大的金额,我不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74007252/

相关文章:

来自文本文件的 C++ 命令行输入

c++ - 将 8 个 bool 值保存到 1 个字节中

c++ - 故障排除自动矢量化原因 '1200'

c++ - 模板实例化不 "do inheritance"

c++ - CMake 包含第 3 方项目

c++ - 向类添加 const 的正确方法

c++ - shared_ptr 中 multidim 数组的类型是什么?

c++ - attachconsole的问题

c++ - C++中的依赖倒置(根据S.O.L.I.D原则)

C++如何从这个字符串中获取值