为C ++中的作业分配创建一个IntegerNumber类,以将大整数合并为超出长范围的字符串 [英] Creating an IntegerNumber class for a homework assignment in C++ to sum large integers as strings exceeding long range

查看:443
本文介绍了为C ++中的作业分配创建一个IntegerNumber类,以将大整数合并为超出长范围的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我创建一个IntegerNumber类,需要能够输出一个大约26位数的整数的加法,例如:-12345678954688709764347890被存储到B是一个类型IntegerNumber。 A,B,C和D都是类型IntegerNumber。我没有使用operator =函数将每个的值彼此分配,如A = B或B = C的问题。稍后在主代码中,要求之一是能够输出数字的和,如D = A + B或者甚至比较A < B。

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.

如果这些数字在数字的long或int范围内,我不会有麻烦。我有麻烦弄清楚当这些值是字符串时如何添加-12345678954688709764347890 + 5678954688709764347890。什么是最好的方式将它们转换为一个类型,它可以添加或甚至比较(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);

}

由于某些原因,我有strcpy的警告: 警告4警告C4996:'strcpy':此函数或变量可能不安全。请考虑使用strcpy_s。要禁用弃用,请使用_CRT_SECURE_NO_WARNINGS。有关详细信息,请参阅联机帮助。 c:\users\danny\documents\visual studio 2010\projects\hw6\hw6\hw6.cpp 106 1 HW6

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

还有strcat有相同的错误,我试图更改为strcpy_s和strcat_s但我得到一个错误说: 6 IntelliSense:没有重载函数的实例strcpy_s匹配参数列表c: \users\danny\documents\visual studio 2010 \projects\hw6\hw6\hw6.cpp 89 3 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

推荐答案

在类中有一个类型为 std :: vector< char> 的字段,数字,然后你可以对 operator +()(就像你在学校中做的)向量的相应数字求和并返回结果。

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;      
          }
          //...
};

编辑:

顺便说一句,以下是开始的样子: http://www.ideone.com/Yb5Nn

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

这篇关于为C ++中的作业分配创建一个IntegerNumber类,以将大整数合并为超出长范围的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆