-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlong_sum.cpp
More file actions
213 lines (184 loc) · 5.75 KB
/
long_sum.cpp
File metadata and controls
213 lines (184 loc) · 5.75 KB
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 <string>
#include <vector>
#include <iostream>
#include <algorithm>
char digitToChar(int digit)
{
return '0' + static_cast<char>(digit);
}
int charToDigit(char ch)
{
return static_cast<int>(ch - '0');
}
#if 0
using namespace std;
char& digitAsChar(string& number, size_t index)
{
return *(number.rbegin() + index);
}
int digit(const string& number, size_t index)
{
return charToDigit(*(number.rbegin() + index));
}
string numbersSum(const string& first, const string& second)
{
string sum(max(first.size(), second.size()) + 1, digitToChar(0));
for (size_t iSum = 0; iSum < sum.size(); ++iSum) {
int digitSum = digit(sum, iSum);
if (iSum < first.size()) {
digitSum += digit(first, iSum);
}
if (iSum < second.size()) {
digitSum += digit(second, iSum);
}
if (digitSum >= 10) {
digitAsChar(sum, iSum + 1) = digitToChar(1);
digitSum -= 10;
}
digitAsChar(sum, iSum) = digitToChar(digitSum);
}
sum.erase(sum.begin(), sum.begin() + std::min(
sum.length(), sum.find_first_not_of(digitToChar(0))));
if (sum.empty()) {
sum.assign(1, digitToChar(0));
}
return sum;
}
#else
typedef std::vector<signed short> Long;
Long stringToLong(const std::string& string)
{
Long result(string.length());
std::transform(string.rbegin(), string.rend(), result.begin(), &charToDigit);
return result;
}
void cutLeadingZeros(Long* number)
{
size_t newSize = number->size();
while (newSize > 1 && (*number)[newSize - 1] == 0) {
--newSize;
}
number->resize(newSize);
}
std::string longToString(const Long& number)
{
// cutLeadingZeros(&number);
std::string result(number.size(), '\0');
std::transform(number.begin(), number.end(), result.rbegin(), &digitToChar);
return result;
}
const int BASE = 10;
Long sum(const Long& first, const Long& second)
{
Long result(std::max(first.size(), second.size()) + 1, 0);
for (size_t iSum = 0; iSum + 1 < result.size(); ++iSum) {
int digitsSum = result[iSum];
if (iSum < first.size()) {
digitsSum += first[iSum];
}
if (iSum < second.size()) {
digitsSum += second[iSum];
}
if (digitsSum >= BASE) {
result[iSum + 1] = 1;
result[iSum] = digitsSum - BASE;
} else {
result[iSum] = digitsSum;
}
}
cutLeadingZeros(&result);
return result;
}
Long multiply(const Long& first, const Long& second)
{
Long result(first.size() + second.size(), 0);
for (size_t iFirst = 0; iFirst < first.size(); ++iFirst) {
for (size_t iSecond = 0; iSecond < second.size(); ++iSecond) {
const size_t iResult = iFirst + iSecond;
result[iResult] += first[iFirst] * second[iSecond];
result[iResult + 1] += result[iResult] / BASE;
result[iResult] %= BASE;
}
}
for (size_t iResult = 0; iResult + 1 < result.size(); ++iResult) {
result[iResult + 1] += result[iResult] / BASE;
result[iResult] %= BASE;
}
cutLeadingZeros(&result);
return result;
}
template<class BinaryOperation>
std::string applyBinaryOperation(const std::string& first, const std::string& second,
BinaryOperation binaryOperation)
{
return longToString(binaryOperation(stringToLong(first), stringToLong(second)));
}
std::string numbersSum(const std::string& first, const std::string& second)
{
return applyBinaryOperation(first, second, &sum);
}
std::string numbersMultiply(const std::string& first, const std::string& second)
{
return applyBinaryOperation(first, second, &multiply);
}
#endif
template<class BinaryOperation>
void testNumbersOperation(const std::string& first, const std::string& second,
const std::string& correctResult, BinaryOperation binaryOperation)
{
const std::string result = binaryOperation(first, second);
if (result != correctResult) {
std::cerr << "Test failed: sum of " << first << " and " << second
<< " equals " << result << ", correct is " << correctResult << std::endl;
throw std::logic_error("Test failed");
}
}
void testNumbersSum(const std::string& first, const std::string& second,
const std::string& correctSum)
{
testNumbersOperation(first, second, correctSum, &numbersSum);
}
void testNumbersMultiply(const std::string& first, const std::string& second,
const std::string& correctProduct)
{
testNumbersOperation(first, second, correctProduct, &numbersMultiply);
}
void runTests()
{
testNumbersSum("00", "0", "0");
testNumbersSum("1", "0", "1");
testNumbersSum("10", "0", "10");
testNumbersSum("5", "04", "9");
testNumbersSum("05", "5", "10");
testNumbersSum("9", "1", "10");
testNumbersSum("99", "001", "100");
testNumbersSum("1543", "57", "1600");
testNumbersSum("104", "87", "191");
testNumbersSum("000023", "1001", "1024");
testNumbersSum("1", "999", "1000");
testNumbersSum("9999", "1", "10000");
std::cerr << "Summation tests passed successfully." << std::endl;
testNumbersMultiply("1", "0", "0");
testNumbersMultiply("1543", "0", "0");
testNumbersMultiply("0", "43", "0");
testNumbersMultiply("1", "1", "1");
testNumbersMultiply("2", "2", "4");
testNumbersMultiply("1543", "100", "154300");
testNumbersMultiply("77", "13", "1001");
testNumbersMultiply("25", "25", "625");
testNumbersMultiply("100", "010", "1000");
std::cerr << "Multiplication tests passed successfully." << std::endl;
}
namespace {
int main_()
{
runTests();
return 0;
std::string first;
std::string second;
std::cin >> first >> second;
const std::string sum = numbersSum(first, second);
std::cout << sum << std::endl;
return 0;
}
}