C 编程分配涉及大整数值

C++ Programming Assignment dealing with large integer values

本文关键字:整数 编程 分配      更新时间:2023-10-16

,所以我为C 类要做的这项任务本质上是为了证明我们可以处理创建类和运营商的超载。现在,我们必须超载函数 , *, =, *=,==,!=,<,< =,>,>,> =, ^的二进制函数 ^(hyperint是该分配的类),我们有构建构造函数:()和输入输出流以及前缀/后缀。现在,我已经为所有这些操作员重载构建了标头文件:

class Hyperint
{
public:
Hyperint(void);
~Hyperint(void);

const Hyperint & operator+= (const Hyperint & right);
const Hyperint & operator*= (const Hyperint & right);
const Hyperint operator^ (long e) const;
const Hyperint &operator++ (int);
const Hyperint &operator-- (int);

friend const bool operator== (const Hyperint &left, const Hyperint &right);
friend const bool operator!= (const Hyperint &left, const Hyperint &right);
friend const bool operator< (const Hyperint &left, const Hyperint &right);
friend const bool operator<= (const Hyperint &left, const Hyperint &right);
friend const bool operator> (const Hyperint &left, const Hyperint &right);
friend const bool operator>= (const Hyperint &left, const Hyperint &right);
friend std::istream & operator >> (std::istream & is, Hyperint & hypint);
friend std::ostream & operator << (std::ostream & os, const Hyperint & hypint);

};
const Hyperint operator+ (const Hyperint & left, const Hyperint &right);
const Hyperint operator* (const Hyperint & left, const Hyperint &right);

现在我有几个设计问题。因为构造函数呼吁输入长时间,所以键入长时间才能处理,因为长期只能处理以下整数:-2,147,483,647至2,147,483,647具体说,构造函数必须接受长变量作为参数)。现在,我遇到的问题是如何计算一个如此大的值,我正在考虑将长期转换为字符串并以这种方式处理数字,但是我不确定这是否适合这种设计。或者我什至想以某种方式将数字存储到矢量中。我的问题本质上是你们认为最适合这种项目的设计格式?喜欢如何将两个HyperInt编号添加在一起?非常感谢,任何帮助都非常感谢!哦,最后一个快速的问题,我的老师还说我们需要进行"转换来布尔",我不确定他的意思吗?如果您有任何想法,那就太好了!再次感谢!

您的老师所说的Hyperint只是任意数量的十进制数字或二进制数字。您可以在阵列或矢量中存储尽可能多的数字,然后对它们进行小学算术。

要说明,请考虑以下乘法操作:

        23958233
            5830 ×
    ------------
        00000000 ( =      23,958,233 ×     0)
       71874699  ( =      23,958,233 ×    30)
     191665864   ( =      23,958,233 ×   800)
    119791165    ( =      23,958,233 × 5,000)
    ------------
    139676498390 ( = 139,676,498,390        )

因此,您有两个操作数:23958233和5830。假设一个节点图形为单一链接列表(出于说明性目的),每个操作数的小数位数将存储在您的矢量中:

2 --> 3 --> 9 --> 5 --> 8 --> 2 --> 3 --> 3

5 --> 9 --> 8 --> 8

换句话说,矢量中的每个元素都会存储一个十进制数字,每个向量将存储一个完整的数字。您将以类似的方式存储中间结果(每个中间结果的一个向量),然后将它们全部添加在一起。

请注意,这只是说明了单个乘法操作。对于将数字提高到功率,您必须重复此乘法,但是很多次。

bignumber库是为此目的,它们通常将整数存储为一个字节或整数数组的数组。他们可以代表任意的大型" bigintegers",有时还可以代表小数。我最喜欢的实现大数字算术的库之一是libtommath:http://libtom.org/?page=features&amp; newsitems = 5&amp; whatfile = ltm

如果您有时间,也可以查看该家伙的其他液体,即使是C,它的源代码也很棒,干净和生产质量,我通常不喜欢C libs。

尽管它是一个C库,您可以从中作弊,也可以在允许的情况下扭曲它。libtommath中的" bn_"东西是指" bignumber"。