1

So I am creating an IntegerNumber class that needs to be able to output an addition of integers that are about 26 digits long, for example : -12345678954688709764347890 is stored into B which is a the type IntegerNumber. A,B,C, and D are all type IntegerNumber. I don't have a problem assigning the values of each to each other like A = B or B = C using an operator= function. Later in the main code, one of the requirements is to be able to output the sum of numbers like D = A + B or even do a comparison of A < B.

I wouldn't have trouble doing this if these numbers were within the long or int range of numbers. I am having trouble figuring out how to do the addition of -12345678954688709764347890 + 5678954688709764347890 when these values are strings. What would be the best way to convert these into a type where it could be added or even compared ( A < B)?

Here is what I have so far:

#include <iostream>
#include <cstring>
using namespace std;

class IntegerNumber
{

    friend ostream& operator<<(ostream &, const IntegerNumber&);
    friend IntegerNumber operator+(const IntegerNumber&, const IntegerNumber&);
    friend bool operator<(const IntegerNumber&, const IntegerNumber&);
    friend bool operator==(const IntegerNumber&, const IntegerNumber&);
    friend bool operator!=(const IntegerNumber&, const IntegerNumber&);

private:

    char *intnum;

public:

    IntegerNumber();  //default constructor

    IntegerNumber(const char *); //constructor with C-string argument

    IntegerNumber(const IntegerNumber &); //copy constructor

    ~IntegerNumber(); //destructor

    IntegerNumber& operator=(const IntegerNumber &rhsObject); //assignment operator

    int Length(); //returns length of string




};

void main() {

    IntegerNumber A; // IntegerNumber object is created and A contains the integer 0

    IntegerNumber B("-12345678954688709764347890"); // IntegerNumber object B is created and B contains the negative number shown within the quotes " "

    IntegerNumber C = "5678954688709764347890"; // IntegerNumber object C
                                                //is created and C contains the positive number shown within the quotes " "
    IntegerNumber D(B); // IntegerNumber object D is created and D contains
                        // the number that B contains
    A = B; // assigns the value of A to that of B
    cout << A << endl; // output to screen the integer in A
    B = C; // assigns the value of B to that of C
    cout << A << endl; // output to screen the integer in A
                       // value of A must be same as before.
    cout << D << endl; // output to screen the integer in D
                       // value of D must be same as before.
    cout << B << endl; // output to screen the integer in B
                       // value of B must be same as that of C
    D = A + B;
    cout << D << endl; // output the sum of the numbers A and B
    if ( A < B ) {
            C = A + B;
            cout << C << endl; // output the sum of A and B
    }

    else {
        A = B + C;
        cout << A << endl; // output the sum of B and C
    }

    if (A == B || C != D)
        cout << A << " " << D << endl; // output values of A and D
}

IntegerNumber::IntegerNumber() {

    intnum = new char[2];

    intnum = "0";

}

IntegerNumber::IntegerNumber(const char *str) {

    intnum = new char[strlen(str) +1];

    strcpy(intnum, str);

}

IntegerNumber::IntegerNumber(const IntegerNumber &ob) {

    intnum = new char[strlen(ob.intnum) +1];

    strcpy(intnum, ob.intnum);

}

IntegerNumber::~IntegerNumber() {

    delete [] intnum;

}

IntegerNumber& IntegerNumber::operator=(const IntegerNumber &ob) {

    if (this != &ob) {

        delete [] intnum;

        intnum = new char[strlen(ob.intnum) +1];

        strcpy(intnum, ob.intnum);

    }

    return *this;
}

int IntegerNumber::Length() {

    return strlen(intnum);

}

ostream& operator<<(ostream &out, const IntegerNumber &ob) {

    out << ob.intnum;

    return out;

}

IntegerNumber operator+(const IntegerNumber &lhs, const IntegerNumber &rhs) {

    int strLength = strlen(lhs.intnum) + strlen(rhs.intnum) +1;

    char *tmpStr = new char[strLength];

    strcpy(tmpStr, lhs.intnum);

    strcat(tmpStr, rhs.intnum);

    IntegerNumber retStr(tmpStr);

    delete [] tmpStr;

    return retStr;

}

bool operator==(const IntegerNumber& lhs, const IntegerNumber& rhs) {

    return (strcmp(lhs.intnum, rhs.intnum) == 0);

}

bool operator!=(const IntegerNumber& lhs, const IntegerNumber& rhs) {

    return (strcmp(lhs.intnum, rhs.intnum) != 0);

}

bool operator<(const IntegerNumber& lhs, const IntegerNumber& rhs) {

    return (strcmp(lhs.intnum, rhs.intnum) < 0);

}

For some reason, I'm having warnings for strcpy: Warning 4 warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. c:\users\danny\documents\visual studio 2010\projects\hw6\hw6\hw6.cpp 106 1 HW6

And also strcat with the same error, I tried changing to strcpy_s and strcat_s but I get an error saying: 6 IntelliSense: no instance of overloaded function "strcpy_s" matches the argument list c:\users\danny\documents\visual studio 2010\projects\hw6\hw6\hw6.cpp 89 3 HW6

Sam
  • 1,509
  • 3
  • 19
  • 28
j2581
  • 21
  • 1
  • 5
  • Is your homework assignment related to solving the big integer problem, or is it just the approach that you took to tackle the main issue? If it is the latter then I'd suggest you try http://gmplib.org/ – Khaled Nassar Apr 24 '11 at 18:14
  • Acutally there is not type for such a long numbers. You might no be allowed to use libraries that handle such large numbers, so you need to think on how to add these digits as multiple parts, with carry over and following some more steps. –  Apr 24 '11 at 18:15
  • possible duplicate of [How to implement big int in C++](http://stackoverflow.com/questions/269268/how-to-implement-big-int-in-c). Also check this [BigInt library](https://mattmccutchen.net/bigint/) – Prasoon Saurav Apr 24 '11 at 18:18
  • The full problem states to design an IntegerNumber class so that the following code works: IntegerNumber A; IntegerNumber B("-21839123912937981938217389213"); IntegerNumber C = "473824792472893472473284729473"; IntegerNumber D(B); A = B; B = D; D = A + B; if (A < B) { C = A + B; } else A = B + C; – j2581 Apr 24 '11 at 18:50
  • You'll find some good answers from the link that Prasoon posted. – Khaled Nassar Apr 24 '11 at 19:12

1 Answers1

2

Have a field of type std::vector<char> in your class, and store all the digits of the big number, in it and then you can sum the corresponding digits of the vectors in operator+() (just like you did in school) and return the result.

class IntegerNumber
{
   //make sure that m_digits contains only digit: digit means, 0 to 9.
   //when you add 9 plus 4, it becomes 14, but you don't put in into m_digits,
   //rather you just put 3 (unit digit of 13), the 1 goes in the second round of sum!
   std::vector<char> m_digits;
   public:
          IntegerNumber();
          IntegerNumber(const std::string &number)
          {
               //parse the string 'number' and populate the m_digits;
          }
          IntegerNumber operator+(const IntegerNumber & number);
          {
               IntegerNumber result;
               //sum all the corresponding digits of number.m_digits and this->m_digits
               //and store in result.m_digits;
               return result;      
          }
          //...
};

EDIT:

By the way, here is how the start should look like : http://www.ideone.com/Yb5Nn

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 1
    make is vector if you are gonna store single digits – sehe Apr 24 '11 at 18:20
  • @Nawaz: Summing up corresponding digits is not the problem, summing up big **integers** is. – Khaled Nassar Apr 24 '11 at 18:22
  • 1
    @Khaled: Exactly. Summing up corresponding digits is the *solution* of of the problem of the summing up of the big integers which are represented by the `m_digits` internally. – Nawaz Apr 24 '11 at 18:24
  • Summing up corresponding digits: 123 + 190 = 1, 2, 3 + 1, 9, 0 = 2, 11, 3 = 2113. Real value = 123 + 190 = 313. It's either I'm misunderstanding your point or you should re-think about it. – Khaled Nassar Apr 24 '11 at 18:29
  • 1
    @Khaled: That is how you did addition when in schools? – Nawaz Apr 24 '11 at 18:31
  • How I as a __human-being__ would do it is totally different from how a __machine__ would. I was merely pointing out to the point of view of the computer. – Khaled Nassar Apr 24 '11 at 18:32
  • 1
    @Khaled: You can make the machine do in the same way you do in schools. When you sum "2 + 9", you write in just "1" in paper, and "1" goes in the second round of summing up. – Nawaz Apr 24 '11 at 18:35
  • Indeed I can, but again I was trying to get you to mention it because you didn't mention it in the original post :) – Khaled Nassar Apr 24 '11 at 18:36
  • @Khaled: Yeah. I didn't mention many things in the original post. The question is how far should I go explaining this. Should I write the real code, or atleast the pseudocode? Or just *some* hint is enough without going into much detail? – Nawaz Apr 24 '11 at 18:38
  • A hint in the incorrect direction is not considered a hint anymore, your original post explicitly stated that all you had to do is sum up corresponding digits, he could have done that without even asking the question. You are missing the point that if the OP knew how to manipulate big integers via "simple school mathematics" he wouldn't have asked here, and it's well known that the big integer problem is a bit deep; else there wouldn't be as many libraries for big integers around.. – Khaled Nassar Apr 24 '11 at 18:43
  • @Khaled: Incorrect? How exactly? My hint can be less informative, but incorrect? Also, even my original post said that store all the digits in the vector. Do you seriously think "11" is digit? How exactly you're going to store it in "m_digits" especially when its a container of 'digit' only? – Nawaz Apr 24 '11 at 18:45
  • @Khaled : You said *it's well known that the big integer problem is a bit deep; else there wouldn't be as many libraries for big integers around*.... the librares you're talking about do lot more than "summing up the big integers", my friend. Summing up the big integers are not big problem at all; its easy problem, that is why the first year students are asked to solve this in their assignment. – Nawaz Apr 24 '11 at 18:50
  • I'd like you to point out to where I regarded "11" as a single digit, my reply simply simulated your idea and I'd say it was pretty accurate seeing as you're holding a vector of **char** type not **digit** type (yeah it doesn't exist, but that's not the point). And yes your hint would have directed the OP in the wrong path. If you would like to keep arguing with no argument just to prove that you were right, then please continue. I don't see any point of going on with this mini-convo. – Khaled Nassar Apr 24 '11 at 18:51
  • @Khaled: If you came up with this example *"123 + 190 = 1, 2, 3 + 1, 9, 0 = 2, 11, 3 = 2113."* just to show that my hint is incorrect, then I can conclude that you think "11" is a digit. Because how else can anyone think that I could have suggested this, especially when I said that the vector should contain the digit of the big integers. – Nawaz Apr 24 '11 at 18:54
  • @Nawaz: You still don't see that your vector holds **char** s not **digit** s? And that the example I introduced would be valid from the **machine** 's perspective? – Khaled Nassar Apr 24 '11 at 18:58
  • @Khaled: Ohhh... .so just because it holds `char`, that means "11" is a digit? Did I not say the vector is a container of digit? Even it's name `m_digits` suggests that its a container of digit...Or. only if I make it `std::vector`, then you would say that my hint is correct, else its incorrect, really? – Nawaz Apr 24 '11 at 18:59
  • Here is the link to the assignment problem, just to clarify the problem a little more http://www.avkf.org/SPR_STEVENS/cs570ws/lectures/Assignment6.pdf – j2581 Apr 24 '11 at 19:04
  • Are we REALLY having this conversation? Do I have to REALLY explain it even further as to why "11" is a digit from the perspective of the machine? Isn't it already obvious what I'm trying to do? Do you REALLY have to prove that you were right from the beginning? If that's the situation then gg, you win. – Khaled Nassar Apr 24 '11 at 19:06
  • @Khaled: Ohhh.. not again. No perspective. No philosophy. Thank you. :-) – Nawaz Apr 24 '11 at 19:11
  • avkf.org/SPR_STEVENS/cs570ws/lectures/Assignment6.pdf that's the full assignment for the problem, by the way, i don't learn about vectors until next lecture, so i think i have to do this using arrays and char – j2581 Apr 24 '11 at 19:55
  • @Danny Yun: No problem, Danny. You can use arrays also, instead of vector. Here is how your start should be : http://www.ideone.com/Yb5Nn – Nawaz Apr 24 '11 at 19:56
  • @Nawaz: Here is the link once again for the full assignment avkf.org/SPR_STEVENS/cs570ws/lectures/Assignment6.pdf , the professor told us to do this using char arrays, but I also have to take into account negative numbers. I can't seem to figure out how to implement the negative value sign into the array and properly do an addition with the two large integers. He told us to take the absolute value of the negative number and compare it (if A>=B', do A-B' to get a positive value, else do B'-A to get a negative value). I can't seem to get a grasp on how to put that into code. – j2581 Apr 25 '11 at 16:54