-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigint.h
91 lines (83 loc) · 2.76 KB
/
bigint.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include <algorithm>
using namespace std;
/**
* typedefs
*/
typedef int int32;
typedef short int16;
typedef char int8;
typedef unsigned int uint32;
typedef unsigned short uint16;
typedef unsigned char uint8;
typedef unsigned char uchar;
/**
* @class BigInt
*/
class BigInt {
public:
// > static class members:
static uint16 baseOutput; //base using for output (10 by default)
private:
// > members:
uint16* x;
uint16 len;
bool neg;
public:
// > ctors:
BigInt();
BigInt(const BigInt& b); //copy ctor
BigInt(const char* str, uint16 base = 0);
BigInt(uint16 n);
BigInt(uint32 n);
BigInt(int16 n);
BigInt(int32 n);
// > destructor:
~BigInt();
// > getters, setters:
bool isNegative();
void setNegative(bool _neg);
// > methods:
void copy(const BigInt& b); //copy
void no_zeros(); //ñîêðàòèòü íóëè
// ñðàâíåíèå:
private:
static int cmp(const uint16* a, uint16 a_len, const uint16* b, uint16 b_len);
public:
static int cmp_abs(const BigInt& a, const BigInt& b);
static int cmp_sign(const BigInt& a, const BigInt& b);
bool is_zero() const;
// àðèôì. îïåðàöèè + - * / %
static BigInt add(const BigInt& a, const BigInt& b, bool sub_xor = false);
static BigInt sub(const BigInt& a_, const BigInt& b_, bool sum_xor = false);
void sub(const BigInt& b);
static BigInt mul(const BigInt& a, uint16 b);
static BigInt mul(const BigInt& a, const BigInt& b);
static BigInt div(const BigInt& a, uint16 b, uint16* rest_out);
static uint16 mod(const BigInt& a, uint16 b);
static BigInt div(const BigInt& a, const BigInt& b, BigInt** rest_out, bool mod = false);
static BigInt mod(const BigInt& a, const BigInt& b);
// from/to string
void fromString(const char* str, uint16 base = 0);
char* toString(char** strToDelete = NULL, uint16 base = 0, bool with_prefix = true, bool with_sign = true, bool big = true) const;
// > operators:
BigInt& operator=(const BigInt& b); //îïåðàöèÿ ïðèñâàèâàíèÿ - êîïèðîâàíèåì
friend BigInt operator+(const BigInt& a, const BigInt& b);
friend BigInt operator-(const BigInt& a, const BigInt& b);
friend BigInt operator*(const BigInt& a, const BigInt& b);
friend BigInt operator/(const BigInt& a, const BigInt& b);
friend BigInt operator%(const BigInt& a, const BigInt& b);
friend bool operator==(const BigInt& a, const BigInt& b);
friend bool operator!=(const BigInt& a, const BigInt& b);
friend bool operator>(const BigInt& a, const BigInt& b);
friend bool operator<(const BigInt& a, const BigInt& b);
friend bool operator>=(const BigInt& a, const BigInt& b);
friend bool operator<=(const BigInt& a, const BigInt& b);
friend istream& operator>>(istream& _in, BigInt& a);
friend ostream& operator<<(ostream& _out, const BigInt& a);
};