Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Adding big numbers together with operator overload
    text
    copied!<p>I am new to C++ and attempting to create a "BigInt" class. I decided to base most of the implementation on reading the numbers into vectors.</p> <p>So far I have only written the copy constructor for an input string. </p> <pre><code>Largenum::Largenum(std::string input) { for (std::string::const_iterator it = input.begin(); it!=input.end(); ++it) { number.push_back(*it- '0'); } } </code></pre> <p>The problem I am having is with the addition function. I have created a function which seems to work after I tested it a few times, but as you can see its highly inefficient. I have 2 different vectors such as:</p> <pre><code>std::vector&lt;int&gt; x = {1,3,4,5,9,1}; std::vector&lt;int&gt; y = {2,4,5,6}; </code></pre> <p>The way I thought to solve this problem was to add 0s before the shorter, in this case y vector to make both vectors have the same size such as:</p> <pre><code>x = {1,3,4,5,9,1}; y = {0,0,2,4,5,6}; </code></pre> <p>Then to add them using elementary style addition. </p> <p>I don't want to add 0s infront of vector Y as it would be slow with a large number. My current solution is to reverse the vector, then push_back the appropriate amount of 0s, then reverse it back. This may be slower then simply inserting at the front it seems, I have not tested yet.</p> <p>The problem is that after I do all of the addition on the vectors and push_back the result. I am left with a backward vector and I need to use reverse yet again! There has got to be a much better way then my method but I am stuck on finding it. Ideally I would make A const as well. Here is the code of the function:</p> <pre><code>Largenum Largenum::operator+(Largenum &amp;A) { bool carry = 0; Largenum sum; std::vector&lt;int&gt;::size_type max = std::max(A.number.size(), this-&gt;number.size()); std::vector&lt;int&gt;::size_type diff = std::abs (A.number.size()-this-&gt;number.size()); if (A.number.size()&gt;this-&gt;number.size()) { std::reverse(this-&gt;number.begin(), this-&gt;number.end()); for (std::vector&lt;int&gt;::size_type i = 0; i&lt;(max-diff); ++i) this-&gt;number.push_back(0); std::reverse(this-&gt;number.begin(), this-&gt;number.end()); } else if (this-&gt;number.size() &gt; A.number.size()) { std::reverse(A.number.begin(), A.number.end()); for (std::vector&lt;int&gt;::size_type i = 0; i&lt;(max-diff); ++i) A.number.push_back(0); std::reverse(A.number.begin(), A.number.end()); } for (std::vector&lt;int&gt;::size_type i = max; i!=0; --i) { int num = (A.number[i-1] + this-&gt;number[i-1] + carry)%10; sum.number.push_back(num); (A.number[i-1] + this-&gt;number[i-1] + carry &gt;= 10) ? carry = 1 : carry = 0; } if (carry) sum.number.push_back(1); reverse(sum.number.begin(), sum.number.end()); return sum; } </code></pre> <p>If anyone has any input that would be great, this is my first program using classes in C++ and its fairly overwhelming. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload