-
Notifications
You must be signed in to change notification settings - Fork 0
/
Num.cpp
168 lines (163 loc) · 3.82 KB
/
Num.cpp
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "Num.h"
#include<iostream>
#include<string>
#include<vector>
inline char Num::getBit(char pos) const
{
return _num[pos];
}
istream& operator>>(istream& in, Num& rhs)
{
string temp;
in>>temp;
int j;
if(temp[0]=='-')rhs._minus = true;
else rhs._minus=false;
for( j = 0; (temp[j] <'1' || temp[j] > '9') && j < temp.size(); j++ );
rhs._num.resize(temp.size() - j);
for( int i = temp.size()-1; i >= j; i-- )
rhs._num[temp.size() - 1 - i]=temp[i] - '0';
if(rhs.length()==0){rhs._minus = false;rhs._num.push_back(0);}
return in;
}
ostream& operator<<(ostream& out, Num& rhs)
{
if(rhs._minus)out<<'-';
for( int i = rhs.length() - 1; i >= 0 ; i-- )
out << int(rhs._num[i]);
return out;
}
inline int Num::length(void) const
{
return _num.size();
}
Num Num::operator+(const Num& rhs) const
{
vector<char> ans = length() > rhs.length() ? _num : rhs._num;
for( unsigned int i=0; i < min(length(),rhs.length()); i++ )
if(_minus^rhs._minus)ans[i]=_num[i]-rhs._num[i];
else ans[i]=_num[i]+rhs._num[i];
for(int i=0;i<ans.size();i++)cout<<int(ans[i]);
cout<<endl;
Num ans_num(ans);
if(_minus)ans_num.inverse();
return ans_num;
}
Num Num::operator-(const Num& rhs) const
{
vector<char> ans = length() > rhs.length() ? _num : rhs._num;
for( unsigned int i=0; i < min(length(),rhs.length()); i++ )
if(_minus^rhs._minus)ans[i]=_num[i]+rhs._num[i];
else ans[i]=_num[i]-rhs._num[i];
Num ans_num(ans);
if(_minus)ans_num.inverse();
return ans_num;
}
Num Num::operator*(const Num& rhs) const
{
vector<int> ans(length()+rhs.length(),0);
for( unsigned int i = 0; i < length(); i++ )
for( unsigned int j = 0; j < rhs.length(); j++ )
ans[i + j] += _num[i] * rhs._num[j];
unsigned int i;
for( i = 0; i < ans.size() - 1; i++ )
{
if(ans[i]>9)
{
ans[i+1]+=ans[i]/10;
ans[i]%=10;
}
}
for( ; ans[i] / 10 != 0; i++ )
{
ans.push_back(ans[i]/10);
ans[i]%=10;
}
Num ans_num(ans,_minus!=rhs._minus);
return ans_num;
}
inline Num& Num::operator++()
{
if(_minus)_num[0]--;
else _num[0]++;
carry();
decomp();
setMinus();
return *this;
}
inline Num Num::operator++(int)
{
Num tmp=*this;
if(_minus)_num[0]--;
else _num[0]++;
carry();
decomp();
setMinus();
return *this;
}
inline Num& Num::operator--()
{
if(_minus)_num[0]++;
else _num[0]--;
carry();
decomp();
setMinus();
return *this;
}
inline Num Num::operator--(int)
{
Num tmp=*this;
if(_minus)_num[0]++;
else _num[0]--;
carry();
decomp();
setMinus();
return *this;
}
void Num::carry(void)
{
unsigned int i;
for( i = 0; i < length() - 1; i++ )
{
if(_num[i]>9)
{
_num[i+1]+=_num[i]/10;
_num[i]%=10;
}
}
for( ; _num[i] / 10 != 0; i++ )
{
_num.push_back(_num[i]/10);
_num[i]%=10;
}
for( ; _num[i] == 0 && i != 0 ; i-- )
_num.pop_back();
}
void Num::decomp(void)
{
unsigned int i;
for( i = 0; i < length()-1; i++)
if(_num[i] < 0)
{
_num[i]+=10;
_num[i+1]-=1;
}
if(_num[i]<0)inverse();
for( ; _num[i] / 10 != 0; i++ )
{
_num.push_back(_num[i] / 10);
_num[i] %= 10;
}
for( ; _num[i] == 0 && i != 0; i-- )
_num.pop_back();
}
inline void Num::setMinus(void)
{
if(_num[_num.size()-1]<0)
{
inverse();
for( int i = 0; i < _num.size(); i++)
_num[i]*=-1;
}
if(length() == 0)_minus=false;
}