-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase conversion.cpp
More file actions
133 lines (104 loc) · 1.83 KB
/
Copy pathbase conversion.cpp
File metadata and controls
133 lines (104 loc) · 1.83 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
/**
Sample input
-----------
3
101 bin
101 dec
8f hex
Sample output
------------
Case 1:
5 dec
5 hex
Case 2:
65 hex
1100101 bin
Case 3:
143 dec
10001111 bin
*/
#include <bits/stdc++.h>
using namespace std;
/*
// Convert a Decimal Base to Any Base
void convert10tob(int N, int b)//number, base
{
if (N == 0)
return;
int x = N % b;
N /= b;
if (x < 0)
N += 1;
convert10tob(N, b);
cout<< x < 0 ? x + (b * -1) : x;
return;
}
*/
char digit_hex(int x)
{
if(x >= 0 && x < 10) return (x + '0');
else if (x < 16) return (x - 10 + 'a');
else return '!';
}
string tobin(int x)
{
string tmp;
char c;
while(x > 0)
{
c = x % 2 + '0';
tmp = c + tmp;
x /= 2;
}
return tmp;
}
string tohex(int x)
{
string tmp;
while(x > 0)
{
tmp = digit_hex(x % 16) + tmp;
x /= 16;
}
return tmp;
}
void bin(char * c, string s)
{
int x = strtol(c, 0, 2);
cout << x << " dec" << endl;
cout << tohex(x) << " hex" << endl;
}
void dec(char * c, string s)
{
int x = strtol(c, 0, 10);
cout << tohex(x) << " hex" << endl;
cout << tobin(x) << " bin" << endl;
}
void hex(char * c, string s)
{
int x = strtol(c, 0, 16);
cout << x << " dec" << endl;
cout << tobin(x) << " bin" << endl;
}
int main()
{
int n, count = 1;
char c[50];
string s;
cin >> n;
while(n--)
{
cin >> c >> s;
cout << "Case " << count << ":" << endl;
if(s == "bin"){
bin(c, s);
}else if(s == "dec"){
dec(c, s);
}else{
hex(c, s);
}
count++;
cout << endl;
}
return 0;
}