-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsum.cpp
More file actions
205 lines (169 loc) · 3.78 KB
/
sum.cpp
File metadata and controls
205 lines (169 loc) · 3.78 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
#include "sum.hpp"
string upperCase(string s)
{
for(char& c : s)
{
c = toupper(c);
}
return s;
}
int convertToNumb(char c)
{
return (int) c - 55;
}
char convertToChar(int n)
{
return (char) (n+55);
}
int convertToDecimal(string A, int base)
{
int expoNent = 0;
char c;
int res = 0;
for(int i = A.length() - 1; i >= 0; i--)
{
c = A[i];
//cout << convertToNumb(c) << " ";
int temp = c - '0';
if(0 <= temp && temp <= 9)
{
res += temp * pow(base,expoNent);
}
else
{
res += convertToNumb(c) * pow(base,expoNent);
}
expoNent++;
}
return res;
}
stack<char> sumOfBase_36(string A, string B, int base)
{
int x = convertToDecimal(A,base);
int y = convertToDecimal(B,base);
int remainings = 0;
int total = x + y;
stack<char> res;
while(total != 0)
{
remainings = total % base;
if( 0 <= remainings && remainings <= 9)
{
res.push(remainings +'0');
}
else
{
res.push(convertToChar(remainings));
}
total /= base;
}
return res;
}
bool checkValid_36(string A, string B, int base)
{
for(char& c : A)
{
if( c - '0' > 9)
{
if(convertToNumb(c) >= base)
{
return false;
}
}
}
for(char& c : B)
{
if( c - '0' > 9)
{
if(convertToNumb(c) >= base)
{
return false;
}
}
}
return true;
}
bool checkValid(long x, long y, int base)
{
int digit = 0;
while(x != 0 && y != 0)
{
digit = x % 10;
if(digit >= base)
{
return true;
}
digit = y % 10;
if(digit >= base)
{
return true;
}
x/= 10;
y/= 10;
}
return false;
}
stack<long> sumofBase(long x, long y, int base)
{
stack<long> sumRes;
int c1 = 0;
int c2 = 0;
int res = 0; //result of the division with the base
long sum = 0; //sum of two digits
bool returnYet = false; //check whether the calculation has returned excess value
//Loop through every digit of the two numbers to do calculation
while(x != 0 || y != 0)
{
c1 = x % 10;
c2 = y % 10;
//Check if the remaining of the divisons has been taken to calculate
if(returnYet)
{
sum = c1 + c2 + res;
returnYet = false;
}
else
{
sum = c1 + c2;
}
//Compare the sum of the digits with the base
if(sum < base)
{
sumRes.push(sum);
}
else
{
sumRes.push(sum % base);
res = sum / base;
returnYet = true;
}
x /= 10;
y /= 10;
}
//if the remaining of the division has not been calculated pushed to the result
if(returnYet)
{
if(x == 0 && y == 0 && res != 0)
{
sumRes.push(res);
}
}
return sumRes;
}
void printStack(const stack<char>& s){
for (auto it = s; !it.empty(); it.pop()) {
cout << it.top(); // Print the top element
}
cout << endl;
}
void printStack(const stack<long>& s) {
for (auto it = s; !it.empty(); it.pop()) {
cout << it.top(); // Print the top element
}
cout << endl;
}
// int main(){
// stack<char> s;
// s = sumOfBase_36("AB", "CB", 16);
// printStack(s);
// }