因此,我正在创建一个IntegerNumber类,该类需要能够输出大约26位数字的整数加法,例如:-12345678954688709764347890存储在B中,它是IntegerNumber类型。 A,B,C和D均为IntegerNumber类型。我没有问题,可以使用operator =函数将彼此的值相互分配,例如A = B或B =C。在主代码的后面,要求之一是能够输出数字之和,例如D = A + B,甚至进行A
如果这些数字在数字的长整数或整数整数范围内,我将不会遇到任何麻烦。当这些值是字符串时,我很难弄清楚如何添加-12345678954688709764347890 + 5678954688709764347890。将它们转换为可以添加甚至比较的类型的最佳方法是什么(A
这是我到目前为止的内容:
#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. 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: 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
最佳答案
在您的类(class)中有一个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