所以我正在创建一个 IntegerNumber 类,它需要能够输出大约 26 位长的整数的加法,例如:-12345678954688709764347890 存储到 B 中,它是 IntegerNumber 类型。 A、B、C 和 D 都是 IntegerNumber 类型。我没有问题使用 operator= 函数将每个值分配给彼此,例如 A = B 或 B = C。稍后在主代码中,其中一个要求是能够输出数字的总和,例如 D = A + B,甚至可以对 A < B 进行比较。
如果这些数字在 long 或 int 数字范围内,我这样做就不会有问题。当这些值是字符串时,我无法弄清楚如何添加 -12345678954688709764347890 + 5678954688709764347890。将它们转换为可以添加甚至比较的类型的最佳方法是什么 (A < B)?
这是我目前所拥有的:
#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 的警告:Warning 4 warning C4996: 'strcpy': This function or variable may be unsafe。考虑改用 strcpy_s。要禁用弃用,请使用 _CRT_SECURE_NO_WARNINGS。详情请参见在线帮助。 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\匹配文档\visual studio 2010\projects\hw6\hw6\hw6.cpp 89 3 HW6
最佳答案
有一个类型为 std::vector<char>
的字段在你的类中,并将大数的所有数字存储在其中,然后你可以对 operator+()
中 vector 的相应数字求和(就像你在学校做的那样)并返回结果。
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
关于c++ - 在 C++ 中为家庭作业创建 IntegerNumber 类,以将大整数求和为超出长范围的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5772298/