添加整数字符串

时间:2013-01-29 05:45:27

标签: c++ string integer

我正在尝试为一个更大的项目编写一个算法,该项目将采用两个大整数的字符串(为了演示而只使用10位数字)并将它们加在一起以生成一个准确表示的最终字符串两个原始字符串的总和。我意识到从一开始就有更好的方法可以解决这个问题,但我应该专门使用大整数字符串而不是长整数。

我的想法是取两个原始字符串,将它们反转,使它们的位置,数十个位置等等都正确排列以便添加。然后一次一个位置,将字符串中的字符转换为单个整数并将它们加在一起,然后将该总和用作最终字符串的位置或其他位置,一旦完成,也将反转回正确的字符顺序

在我遇到麻烦的时候,我认为正在准备一个事件,在这个事件中,来自字符串中相应位置的两个整数加上一个大于9的总和,然后我会将剩余部分带到下一个位置。例如,如果我的位置中有7和5可以增加到12,那么我会保留2并且一旦它回转到十位位置操作就将其加到十位置。

我没有得到任何方式准确的结果,并且在花费大量时间绊倒自己试图纠正我的算法后,我不确定我需要做些什么来解决这个问题。

希望我的预期过程是明确的,有人可以指出我正确的方向或纠正我在程序中可能遇到的一些错误。

提前致谢。

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{
    string str1 = "1234567890", str2 = "2345678901"; //Two original strings of large integers
    string rev_str1, rev_str2;
    int int1 = 0, int2 = 0;
    string final; //Final product string, sum of two original strings
    int temp_int = 0, buffer_int, remainder = 0;
    string temp_str = "", buffer_str;
    char buffer[100] = {0};

    cout << "str1 = " << str1 << endl;
    cout << endl;
    cout << "str2 = " << str2 << endl;
    cout << endl;

    rev_str1 = string(str1.rbegin(), str1.rend());
    rev_str2 = string(str2.rbegin(), str2.rend());

    for (int i = 0; i < 10; i++)
    {
        buffer_str = rev_str1.at(i);
        int1 = atoi(buffer_str.c_str());
        buffer_str = rev_str2.at(i);
        int2 = atoi(buffer_str.c_str());
        buffer_int += (int1 + int2 + remainder);
        remainder = 0;

        while (buffer_int > 9)
        {
            buffer_int -= 10;
            remainder += 10;
        }

        temp_str = itoa(buffer_int, buffer, 10);
        final += temp_str;
    }

    final = string(final.rbegin(), final.rend());

    cout << "final = " << final << endl;
    cout << endl;
}

3 个答案:

答案 0 :(得分:6)

这就是我想出的。这只是两个加号;如果你有更多,你将不得不适应一些东西,特别是进位,然后大于19,以及分配结果字符串的方式:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    // Two original strings of large integers
    string str1 = "1234567890",
           str2 = "2345678901234";

    // Zero-padd str1 and str2 to the same length
    size_t n = max(str1.size(), str2.size());
    if (n > str1.size())
        str1 = string(n-str1.size(), '0') + str1;
    if (n > str2.size())
        str2 = string(n-str2.size(), '0') + str2;

    // Final product string, sum of two original strings.
    // The sum of two integers has at most one digit more, for more inputs make
    // below reverse_iterator a back_insert_iterator, then reverse the result
    // and skip the removal of the padding.
    string final(n+1, '0');

    // The carry
    char carry = 0;

    // Iterators
    string::const_reverse_iterator s1 = str1.rbegin(), e = str1.rend(),
                                   s2 = str2.rbegin();
    string::reverse_iterator f = final.rbegin();

    // Conversion
    for (; s1 != e; ++s1, ++s2, ++f)
    {
        // Bracketing to avoid overflow
        char tmp = (*s1-'0')+(*s2-'0') + carry;
        if (tmp > 9)
        {
            carry = 1;
            tmp -= 10;
        }
        else
        {
            carry = 0;
        }
        *f = tmp + '0';
    }
    final[0] = carry + '0';

    // Remove leading zeros from result
    n = final.find_first_not_of("0");
    if (n != string::npos)
    {
        final = final.substr(n);
    }

    cout << "str1 = " << str1 << endl
         << "str2 = " << str2 << endl
         << "final = " << final << endl;
}

答案 1 :(得分:4)

你的问题是你携带10s而不是1s。当你添加19 + 5时,你在单位位置获得4,并在10s位置添加额外的1。您不会在10s位置添加额外的10个。

您只需更改此行:remainder += 10;remainder += 1;

此外,如果您有两个以上的加数,则不需要while循环。实际上,当您一次只添加两个数字时,您可以拥有的最大加数为9 + 9,只有1个。

答案 2 :(得分:-2)

#include<iostream>

使用命名空间std; 主(){

int sum =0;
int a;
int reminder;
cout<<"Enter the Number :"<<endl;
cin>>a;
while(a>0){
    reminder=a%10;
    sum=r+sum;
    a=a/10;`enter code here`

}
cout<<"Additon is :"<<sum<<endl;

}

相关问题