-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.h
112 lines (108 loc) · 2.13 KB
/
class.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#ifndef H_class
#define H_class
#include <stdio.h>
#include <cmath>
#include "func.h"
#define Limit_dicimals 10000
class BS //À¯¸®¼ö : Rational Number -> (b/a)
{
protected:
ull a, b; // ºÐ¸ð , ºÐÀÚ
bool sign; //0 -> (+), 1 -> (-)
public:
BS() : a(1), b(0), sign(0) {}
BS(ull a, ull b, bool sign = 0) : a(a), b(b), sign(sign) { this->simplify(); }
BS(ll p, ll q)
{
if (p * q < 0) sign = 1;
else sign = 0;
a = abs(p);
b = abs(q);
}
~BS() {}
operator double() const
{
double k = ((double)b / a) * (sign ? (-1) : (1));
//k = (double)((long long)(k * (Limit_dicimals))) / Limit_dicimals;
k = round(k * Limit_dicimals) / Limit_dicimals;
//printf("--%lf\n", k);
return k;
}
operator ll() const
{
return (b / a) * (sign ? (-1) : (1));
}
void simplify()
{
if (this->b == 0)
{
this->a = 1;
return;
}
long long g = gcd(this->a, this->b);
this->a /= g; this->b /= g;
return;
}
BS operator= (const BS& tmp)
{
this->a = tmp.a;
this->b = tmp.b;
this->sign = tmp.sign;
return *this;
}
const BS operator+ (BS& tmp) const;
const BS operator+= (BS& tmp)
{
return *this = (*this) + (tmp);
}
const BS operator* (BS& tmp) const;
const BS operator*= (BS& tmp)
{
return *this = (*this) * (tmp);
}
bool operator== (BS& tmp) const
{
return (tmp.a == a) and (tmp.b == b) and (tmp.sign == sign);
}
void Test_pt() const //print
{
if (a == 1) printf("%lld : %d\n", b, sign);
else printf("%lld/%lld : %d\n", b, a, sign);
}
void pt() const
{
if (sign == true) printf("-");
if (a == 1) printf("%lld", b);
else printf("%lld/%lld", b, a);
}
void fpt(FILE* p) const
{
if (sign == true) fprintf(p,"-");
if (a == 1) fprintf(p, "%lld", b);
else fprintf(p, "%lld/%lld", b, a);
}
ull geta() const { return this->a; }
ull getb() const { return this->b; }
bool getsign() const { return this->sign; }
};
BS RtoBS(double R);
BS RtoBS(ll R);
/*class BS_exp //BS + BS exponent
{
private:
BS val, exp;
public:
BS_exp() : val(1, 1, 0), exp(1, 1, 0) {}
BS_exp(BS& a, BS& b) : val(a), exp(b) {}
~BS_exp() {}
void simplify()
{
val.simplify();
exp.simplify();
}
void expPlus(BS b)
{
exp *= b;
}
};*/
#endif