Creating an IntegerNumber class for a homework ass

2019-07-29 10:22发布

问题:

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

回答1:

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