开发者

C++ multiplication of 2 strings representing decimal value of a number

开发者 https://www.devze.com 2023-02-21 23:11 出处:网络
The idea is to overload an operator * so it can multiply two strings representing decimal value of a number. The operator is part of a bigger class but that is not important. The algorithm is the same

The idea is to overload an operator * so it can multiply two strings representing decimal value of a number. The operator is part of a bigger class but that is not important. The algorithm is the same as in elementary school :)

Here's my code:

Bignumber operator* (Bignumber x, Bignumber y ){
    int i, j, transfer=0, tmp, s1, s2, k;
    char add[1];
    string sol;
    string a, b;
    Bignumber v1, v2;
    a=x.GetValue();
    b=y.GetValue();

    a.insert(0,"0");
    b.insert(0,"0");

    for(i=a.length()-1; i>=0; i--){
        s1 = (int) a[i]-48;
        for(k=a.length()-i-1; k >0 ; k--){
            sol+="0";
        }
        for(j=b.length()-1; j >=0; j--){
            s2=(int) b[j]-48;
            tmp=s1*s2+transfer;
            if(tmp >= 10){
                 transfer=tmp/10;
                 tmp=tmp-(10*transfer);
             }
            itoa(tmp, add, 10);
            sol.insert(0, add);
        }
        v1=sol;
        v2=v1+v2;
        sol.erase(0);
        transfer=0;
    }
    return v2;
}

This works fine most of the time but for some random values it doesnt work properly. like for example for 128*28 it returns 4854 instead of 3584.

Any idea what might be the problem?

operator开发者_StackOverflow中文版s + and = are already overloaded for the class Bignumber and they work fine.


While my first answer solves your issue (by my testing, anyway), here's an alternative implementation; I don't have your Bignumber class so I wrote a small fake one to test with:

#include <string>
#include <ios>
#include <iostream>
#include <ostream>
#include <sstream>

class Bignumber
{
    static inline unsigned long long strtoull(std::string const& str)
    {
        unsigned long long val;
        return std::istringstream(str) >> val ? val : 0uLL;
    }

    unsigned long long val_;

public:
    Bignumber() : val_() { }
    explicit Bignumber(unsigned long long const val) : val_(val) { }
    explicit Bignumber(std::string const& str) : val_(strtoull(str)) { }

    Bignumber& operator +=(Bignumber const rhs)
    {
        val_ += rhs.val_;
        return *this;
    }

    std::string GetValue() const
    {
        std::ostringstream oss;
        oss << val_;
        return oss.str();
    }
};

Bignumber operator *(Bignumber const x, Bignumber const y)
{
    typedef std::string::const_reverse_iterator cr_iter_t;

    std::string const& a = '0' + x.GetValue();
    std::string const& b = '0' + y.GetValue();

    Bignumber ret;
    for (cr_iter_t a_iter = a.rbegin(), a_iter_end = a.rend(); a_iter != a_iter_end; ++a_iter)
    {
        unsigned transfer = 0u;
        std::string sol(a.end() - a_iter.base(), '0');
        for (cr_iter_t b_iter = b.rbegin(), b_iter_end = b.rend(); b_iter != b_iter_end; ++b_iter)
        {
            unsigned tmp = static_cast<unsigned>(*a_iter - '0') * static_cast<unsigned>(*b_iter - '0') + transfer;
            if (tmp >= 10u)
            {
                transfer = tmp / 10u;
                tmp -= transfer * 10u;
            }
            sol.insert(sol.begin(), static_cast<char>(tmp + '0'));
        }
        ret += Bignumber(sol);
    }
    return ret;
}

int main()
{
    Bignumber const z = Bignumber(123456789uLL) * Bignumber(987654321uLL);
    std::cout << std::boolalpha << (z.GetValue() == "121932631112635269") << std::endl;
}


itoa null-terminates the string it writes, so add is too small for the data being written to it, resulting in memory corruption. Change the definition of add to char add[2]; and it should work.

0

精彩评论

暂无评论...
验证码 换一张
取 消