forked from czarny247/kurs_cpp_lato_2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
long_int.cpp
213 lines (201 loc) · 4.82 KB
/
long_int.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <cstdint>
#include <iostream>
#include <string>
#include <cassert>
struct LongInt
{
//Constructor
LongInt(int left, int right) : left(left), right(right)
{
}
LongInt() = default;
//Constructor
LongInt(std::string str)
{
int number = std::stoi(str);
left = number / 100;
right = number % 100;
}
//Copy constructor
LongInt(const LongInt& other)=default;
//Destructor
~LongInt()
{
std::cout << "Delete " << toString() << std::endl;
}
//Method
std::string toString()
{
// naprawa bug'a
if (left==0)
return std::to_string(right);
else if (right<10)
return std::to_string(left)+"0"+std::to_string(right);
else
return std::to_string(left) + std::to_string(right);
}
//Operators
LongInt operator+=(const LongInt& r)
{
left += r.left;
right += r.right;
left += right / 100;
right = right % 100;
return *this;
}
LongInt operator+(const LongInt& r)
{
left = r.left + left;
right = r.right + right;
left = left + (right / 100);
right = right % 100;
return *this;
}
LongInt operator-=(const LongInt& r)
{
if (r.left>left)
{
left -=r.left;
if (r.right>right)
{
right = r.right-right;
}
else
{
left++;
right= r.right+100-right;
}
left -= right / 100;
right = right % 100;
}
else if (r.left<left)
{
left -=r.left;
if (r.right>right)
{
left--;
right+=100;
right -= r.right;
}
else
right -= r.right;
left -= right / 100;
right = right % 100;
}
else
{
left = left - r.left;
if (r.right>right)
{
right -= r.right;
}
else
{
left++;
right= r.right+100-right;
}
left = left - (right / 100);
right = right % 100;
}
return *this;
}
LongInt operator-(const LongInt& r)
{
if (r.left>left)
{
left = left - r.left;
if (r.right>right)
{
right = r.right-right;
}
else
{
left++;
right= r.right+100-right;
}
left -=(right / 100);
right = right % 100;
}
else if (r.left<left)
{
left = left - r.left;
if (r.right>right)
{
left--;
right+=100;
right = left - r.right;
}
else
right = right - r.right;
left = left - (right / 100);
right = right % 100;
}
else
{
left = left - r.left;
if (r.right>right)
{
right -= r.right;
}
else
{
left++;
right= r.right+100-right;
}
left -= -(right / 100);
right = right % 100;
}
return *this;
}
//Copy operator
LongInt& operator=(const LongInt& other)
{
left = other.left;
right = other.right;
return *this;
}
//Insertion operator
friend std::ostream &operator<<(std::ostream &out,const LongInt &rhs)
{
out<<rhs.left<<rhs.right<<std::endl;
return out;
}
//Extraction opearator
friend std::istream &operator>>(std::istream &in, LongInt &rhs)
{
in>>rhs.left;
in>>rhs.right;
return in;
}
//Members
int left;
int right;
};
int main()
{
LongInt val1(1, 70);
LongInt val2("5980");
LongInt val3("0");
LongInt val4("10");
LongInt val5("3001");
LongInt val6("1101");
LongInt val7("1165");
LongInt val8("2313");
LongInt val99;
val1+=val2;
val3+val4;
val6-=val7;
val7-val8;
std::cout << "Val1: "<< val1.toString() << std::endl;
std::cout << "Val2: "<< val2.toString() << std::endl;
std::cout << "Val3: "<< val3.toString() << std::endl;
std::cout << "Val4: "<< val4.toString() << std::endl;
std::cout << "Val5: "<< val5.toString() << std::endl;
std::cout << "Val6: "<< val6.toString() << std::endl;
std::cout << "Val7: "<< val7.toString() << std::endl;
std::cout << "Val8: "<< val8.toString() << std::endl;
std::cout<<val1<<std::endl;
std::cin>>val99;
std::cout<<"You entered: "<<val99<<std::endl;
return 0;
}