我正在为学校做一个项目,我需要创建一个 bigint 类,到目前为止它有 4 个要求。
1.) 编写一个方法来编写一个 bigint,每行最多打印 80 位数字。
2.) 编写一个方法来比较两个 bigint 是否相等。它应该返回一个布尔值。
3.) 编写一个方法来将 bigint 初始化为您提供的 int 值 [0, maxint]。
4.) 编写一个将 bigint 初始化为 char[] 的方法。
我认为我的 2 和 3 是正确的,但是我在比较两个 bigint 时遇到了麻烦,我希望有人能引导我朝着正确的方向前进,如何将打印限制为每行 80 位。
到目前为止,这是我的代码:
.h 文件
class bigint
{
public:
bigint(); //default constructor
bool operator==(const bigint& num1);
bigint( int n);
bigint(char new_digits[]);
private:
int digit[MAX];
int digitb[MAX];
};
这是实现文件:
#include "bigint.h"
#include<cassert>
#include<iostream>
//default constructor
bigint::bigint()
{
for (int i = 0; i < MAX; i++)
{
digit[i] = 0;
}
}
bigint::bigint( int n )
{
int i = 0;
while(n > 0)
{
digit[i] = n % 10;
n = n /10;
++i;
break;
}
for(i; i< MAX; ++i)
digit[i] = 0;
}
bool bigint::operator==(const bigint& num1)
{
for(int i = 0; i < MAX; i++)
{
if (num1.digit == num1.digit)
return true;
}
return false;
}
bigint::bigint(char new_digit[])
{
int i = 0;
//Reads the characters of numbers until it is ended by the null symbol
while(new_digit[i] != '\0')
++i;
--i;
//Converts the characters into int values and puts them in the digit array
while( i >= 0)
{
digit[i] = new_digit[i] - '0';
--i;
}
}
}
int main()
{
#include<iostream>
using namespace std;
using PROJECT_1::bigint;
bigint a(0);
assert(a == 0);
}
顺便说一句,我不想为我一小时前刚开始的作业找到答案。我整天都在为此工作,最后我屈服并寻求帮助。