在 C++ 中为家庭作业创建 IntegerNumber 类;将大整数求和为超出长范围的字符串
因此,我正在创建一个 IntegerNumber 类,它需要能够输出大约 26 位长的整数的加法,例如:-12345678954688709764347890 存储到 B 中,B 是 IntegerNumber 类型。 A、B、C、D 都是 IntegerNumber 类型。我没有问题使用运算符 = 函数将每个值相互分配,例如 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 警告:警告 4 警告 C4996:'strcpy':此函数或变量可能不安全。考虑使用 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\documents\visual 匹配工作室 2010\projects\hw6\hw6\hw6.cpp 89 3 HW6
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的类中有一个
std::vector
类型的字段,并将大数的所有数字存储在其中,然后您可以将中向量的相应数字相加operator+()
(就像你在学校所做的那样)并返回结果。编辑:
顺便说一句,开始应该是这样的: http://www.ideone.com/Yb5Nn< /a>
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 inoperator+()
(just like you did in school) and return the result.EDIT:
By the way, here is how the start should look like : http://www.ideone.com/Yb5Nn