This repository was archived by the owner on Nov 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseConverter.cpp
More file actions
294 lines (228 loc) · 6.78 KB
/
Copy pathBaseConverter.cpp
File metadata and controls
294 lines (228 loc) · 6.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include "bits/stdc++.h"
using namespace std;
map<char, int> mp_from;
map<int, char> mp_to;
// functions
// to convert number to Decimal
string numToDecimal(string num, int base);
// to convert Decimal to any base
string numToBase(string num, int to_base);
int main() {
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
cout<<"Enter base of number: \n";
int from_base; cin>>from_base;
// store the characters and their values in separate maps
// using maps, it'll very easy to get corresponding value
for(int i=0; i<min(10, from_base); i++) {
char ch = i + '0';
mp_from[ch] = i;
}
if(from_base > 10) {
cout<<"Enter char and their value: \n";
for(int i=10; i<from_base; i++) {
char character; cin>>character;
int value; cin>>value;
if(value >= from_base) {
cout<<"Invalid Value\n Enter a valid value: ";
cin>>value;
}
mp_from[character] = value;
}
}
cout<<"Enter number: \n";
string from_num; cin>>from_num;
// just to pass the validation of number
mp_from['.'] = -100;
// validate entered number
for(auto i:from_num) {
if(mp_from.find(i) == mp_from.end()) {
cout<<"Invalid Number, Try with some other number\n";
return 0;
}
}
cout<<"Enter base in which we have to convert: \n";
int to_base; cin>>to_base;
// store the characters and their values in separate maps
// using maps, it'll very easy to get corresponding value
for(int i=0; i<min(to_base, 10); i++) {
char ch = i + '0';
mp_to[i] = ch;
}
if(to_base > 10) {
cout<<"Enter char and their value: \n";
for(int i=10; i<to_base; i++) {
char character; cin>>character;
int value; cin>>value;
if(value >= to_base) {
cout<<"\n Invalid Value \n Enter a valid value";
cin>>value;
}
mp_to[value] = character;
}
}
if(from_base == to_base) {
cout<<"Converting to same base\n";
cout<<from_num<<endl;
} else {
string decimalNumber = "", to_num="";
if(from_base != 10) {
decimalNumber = numToDecimal(from_num, from_base);
cout<<"\nNumber in base 10: "<<decimalNumber;
}
if(to_base != 10) {
to_num = numToBase(decimalNumber, to_base);
cout<<"\nNumber in base "<<to_base<<": "<<to_num;
}
}
return 0;
}
string numToDecimal(string num, int base) {
string decimalNumber = "";
vector<int> fractoinalPart, integerPart;
// storing integer part and fractional part separately
int i=0;
while(num[i]!='.' && i<num.length()) {
integerPart.push_back(mp_from[num[i]]);
i++;
}
i++;
while(i<num.length()) {
fractoinalPart.push_back(mp_from[num[i]]);
i++;
}
reverse(integerPart.begin(), integerPart.end());
cout<<"\n\nConverting number in base 10:\n";
if(fractoinalPart.size()>0) {
double decNum=0;
int power = 1;
int cnt=0;
for(auto j:integerPart) {
// printing steps
cout<<"("<<j<<"*"<<base<<"^"<<cnt<<") + ";
cnt++;
// actual calculation
decNum += j*power;
power *= base;
}
power = base;
cnt = 1;
for(auto j:fractoinalPart) {
// printing steps
cout<<"("<<j<<"*"<<base<<"^-"<<cnt<<")";
if(cnt<fractoinalPart.size()) {
// print +
cout<<" + ";
}
cnt++;
// actual calculation
decNum += (float)j/(float)power;
power *= base;
}
decimalNumber = to_string(decNum);
} else {
long long int decNum=0;
int power = 1;
int cnt=1;
for(auto j:integerPart) {
// printing calculation
cout<<"("<<j<<"*"<<base<<"^"<<cnt<<")";
if(cnt<integerPart.size()) {
// print "+"
cout<<" + ";
} else {
// dont't print +
}
cnt++;
// actual calculation
decNum += j*power;
power *= base;
}
decimalNumber = to_string(decNum);
}
return decimalNumber;
}
string numToBase(string num, int to_base) {
int i=0;
string fractionalPart="", integerPart="";
while(num[i]!='.' && i<num.length()) {
integerPart+=num[i++];
}
i++;
while(i<num.length()) {
fractionalPart+=num[i++];
}
// converting string to long
long long int intPart = stol(integerPart, nullptr, 10);
double fracPart;
// if there is some fractional part, then convert the string to double
if(fractionalPart.length()>0)
fracPart = stol(fractionalPart, nullptr, 10);
// now, the double will look like, '3298219' but we want '.3298219', so divide it by power of 10
fracPart/=pow(10,fractionalPart.length());
// string to store answer
string finalNum="";
cout<<"\n\nConverting from base 10 to base "<<to_base<<": \n";
cout<<"\nInteger part of base 10 Number: \n";
while(intPart > 0) {
int temp = intPart % to_base;
// printing steps
cout<<intPart<<"/"<<to_base<<", ";
cout<<"Remainder: "<<temp<<endl;
finalNum+= mp_to[temp];
intPart/=to_base;
}
// reversing the order of remainders obtained
reverse(finalNum.begin(), finalNum.end());
// for fractional part
if(fractionalPart.length()>0) {
// adding '.' in final string
finalNum+= ".";
cout<<"\nFractional Part of base 10 number: \n";
int cnt = 0;
while(cnt++<=fractionalPart.length() && fracPart>0.00) {
fracPart*=to_base;
int temp = (int)fracPart;
cout<<fracPart<<" * "<<to_base<<" ==> ";
cout<<temp<<endl;
finalNum += mp_to[temp];
if(temp>0) {
fracPart = abs(temp-fracPart);
}
}
}
return finalNum;
}
/*
SAMPLE-INPUT
---------------
6
345.34
16
A 10
B 11
C 12
D 13
E 14
F 15
SAMPLE-OUTPUT
----------------
Converting number in base 10:
(5*6^0) + (4*6^1) + (3*6^2) + (3*6^-1) + (4*6^-2)
Number in base 10: 137.611111
Converting from base 10 to base 16:
Integer part of base 10 Number:
137/16, Remainder: 9
8/16, Remainder: 8
Fractional Part of base 10 number:
9.77778 * 16 ==> 9
12.4444 * 16 ==> 12
7.11066 * 16 ==> 7
1.7705 * 16 ==> 1
12.3279 * 16 ==> 12
5.24698 * 16 ==> 5
3.95162 * 16 ==> 3
Number in base 16: 89.9C71C53
*/