Итак, я создаю класс IntegerNumber, который должен иметь возможность выводить сложение целых чисел длиной около 26 цифр, например: -12345678954688709764347890 хранится в B, который является типом IntegerNumber. A, B, C и D имеют тип IntegerNumber. У меня нет проблем с присвоением значений друг другу, таких как A = B или B = C, с помощью функции operator =. Позже в основном коде одно из требований - иметь возможность выводить сумму чисел, например, 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