-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path355.cpp
86 lines (80 loc) · 1.75 KB
/
355.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
#include <cctype>
#include <iostream>
#include <string>
long c2v(const char c)
{
if (isdigit(c)) {
return long(c - '0');
}
switch (c) {
case 'A':
return 10;
case 'B':
return 11;
case 'C':
return 12;
case 'D':
return 13;
case 'E':
return 14;
case 'F':
return 15;
default:
return 16;
}
}
std::string v2c(const long v)
{
if (v < 10) {
return std::to_string(v);
}
switch (v) {
case 10:
return "A";
case 11:
return "B";
case 12:
return "C";
case 13:
return "D";
case 14:
return "E";
case 15:
return "F";
}
}
int main(void)
{
bool valid;
long old_base, new_base, value, d;
std::string old_value, new_value;
while (std::cin >> old_base >> new_base >> old_value) {
valid = true;
value = 0;
for (int i = 0; valid && i < old_value.size(); ++i) {
d = c2v(old_value[i]);
valid = d < old_base;
value = value * old_base + d;
}
if (valid) {
new_value = "";
if (value) {
while (value) {
d = value % new_base;
new_value = v2c(d) + new_value;
value /= new_base;
}
}
else {
new_value = "0";
}
std::cout << old_value << " base " << old_base << " = " << new_value
<< " base " << new_base << std::endl;
}
else {
std::cout << old_value << " is an illegal base " << old_base
<< " number" << std::endl;
}
}
return 0;
}