-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_num.cpp
More file actions
60 lines (53 loc) · 948 Bytes
/
my_num.cpp
File metadata and controls
60 lines (53 loc) · 948 Bytes
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
#include "my_num.h"
#include <cmath>
#include <iostream>
#include <string>
MyNum::MyNum() {s=0;}
MyNum::MyNum(s_t num) {
s=0;
setNum(num);
}
const store_t &MyNum::operator[](int i) const{
return value[i];
}
s_t MyNum::getNum() const {
s_t acc=0;
for (unsigned short i=0;i<s;i++)
acc+=(s_t)ceil(pow(65536,i))*value[i];
return acc;
}
void MyNum::setNum(s_t num) {
s = checkSize(num);
unsigned short i=0;
while (num>0) {
value[i] = (num%65536);
num/=65536;
i++;
}
}
bool MyNum::read(std::istream& in_str) {
in_str>>s;
if (in_str.eof()) {
s=0;
return false;
}
for (unsigned short i=0;i<s;i++) {
in_str>>(value[i]);
}
return true;
}
void MyNum::write(std::ostream& out_str) {
out_str<<s<<' ';
for (unsigned short i=0;i<s;i++) {
out_str<<value[i]<<' ';
}
}
char checkSize(s_t num) {
s_t val = 65536;
int i=1;
while(val<=num) {
val*=65536;
i++;
}
return i;
}