这是C++程序,大约加两个数字
This is C++ program, about add two numbers
这是太平洋问题。类LargeInt
的实现将使用动态物理结构来存储整数的各个数字,并将提供一些可以对整数执行的基本 I/O 和算术运算。
特别是,该类应包括:
- 默认构造函数
- 使
operator +
重载的运算符函数 - 使
operator ==
重载的运算符函数 - 使
operator <<
过载的运算符函数 - 使
operator >>
重载的运算符函数
注意 1:由于 LargeInt
类不包含指针,因此不需要复制构造函数或析构函数。
#include "targetver.h"
using namespace std;
class LargeInt
{
private:
char datain[200];
int databit[200];
int len;
int overflow;
LargeInt(char *x, int inlen)
{
int i = 0;
int j = inlen - 1;
len = inlen;
overflow = 0;
for (i = 0; i < 200; i++)
{
databit[i] = 0;
datain[i] = ' ';
}
for (i = 0; i < len; i++)
{
datain[i] = x[j];
j--;
}
}
~LargeInt();
void GetDataBit()
{
int i = 0;
for (i; i < len; i++)
databit[i] = datain[i] - 48;
}
public:
LargeInt& operator+(LargeInt& data);
bool operator==(LargeInt& data);
LargeInt& operator>>(int x);
LargeInt& operator<<(int x);
};
bool LargeInt::operator==(LargeInt& data)
{
if (this->len != data.len)
return false;
else
{
for (int i = 0; i < 200; i++)
{
if (this->databit[i] == data.databit[i])
continue;
else
return false;
}
return true;
}
}
LargeInt& LargeInt::operator+(LargeInt& data)
{
LargeInt t("0", 0);
int addlen;
if (this->len > data.len)
addlen = this->len;
else
addlen = data.len;
for (int i = 0; i < addlen; i--)
{
t.databit[i] = (data.databit[i] + this->databit[i] + t.overflow) % 10;
if ((data.databit[i] + this->databit[i] + t.overflow) >= 10)
t.overflow = 1;
}
t.len = addlen;
for (int i = 0; i < addlen; i++)
{
t.datain[i] = t.databit[i] + 48;
}
return t;
}
当我构建它时,它有一个这样的错误
警告1 警告 C4172:返回局部变量或临时变量的地址。
LargeInt& LargeInt::operator+(LargeInt& data)
删除 &,您的警告应该会消失。现在,返回引用了一个变量 t,该变量是函数的本地变量,当调用函数捕获返回时,该变量已超出范围。
相关文章:
- C++通过数字比较两个数字
- 将向量之间的数字放在另一个向量之间<vector>>如果两个数字的差值为 1
- 在 txt 文件中显示前两个数字的程序
- 形成一个斐波那契三角形,使得每个数字是上面左对角线或右对角线上两个数字的总和
- 两个数字的对称配对功能
- 查找中间两个数字的正则表达式的匹配项
- 使用C++具有两个数字的最短路径算法.(C++)
- 将两个数字添加为链表
- C++ 分段错误:11 错误,同时编码将两个数字相加的链接列表
- 我应该如何使用remove_if删除两个数字范围内的元素
- 是否有任何内置函数可以检查给定的两个数字在给定整数数组中的顺序是否相同?
- 子数组中两个数字的相同出现(连续)
- 如何使用位操作在单个整数中编码和解码两个数字
- 在 c++ 程序中调试链表以添加两个数字: 有人可以解释为什么 l3=l3->next;导致错误?
- 钻头以两个数字替换位
- C 如何在小数系统中添加两个数字
- 为什么我的代码不给最后两个数字
- 如何在给出前两个数字的级数中找到大于 x 的第 n 个最小子数组总和?
- 输入两个数字后程序关闭
- 代码是否交换数组的两个数字