我是靠谱客的博主 优美板凳,这篇文章主要介绍c++二,八,十,十六进制的互相转换(包括小数,负数,且保留小数点后六位)关于本文章内容说明进制间的转换思想功能目的代码实现,现在分享给大家,希望可以做个参考。

关于本文章内容说明

文章基本只是将作者本人编写的代码进行复制粘贴,而这份代码是作者大一上学期期末作业,若有不足之处,还请见谅。原因主要还是快到期末了,不想写代码,想着“借鉴”网上大佬们的代码,但是没找到合适的,于是自己就写一份吧。

进制间的转换思想

因为十进制的方便,所以利用十进制作为中间桥梁进行进制之间的转换。对于浮点数进行整数部分与小数部分的分离来计算,并在最后相加。

功能目的

1.各进制数字符串转换为十进制正数值;
2.分离十进制数值的整数和小数部分;
3.对分离出的整数、小数部分分别做转换,将其转换为目标进制;
4将转换好的整数、小数部分以及符号位拼接为字符串输出。

代码实现

菜单项的实现

构建一级菜单,利用循环构建,并且调用二级菜单

复制代码
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
void menu() { int n = -1; while (n != 0) { cout << "n**********************"; cout << "n(1)2进制数转换"; cout << "n(2)8进制数转换"; cout << "n(3)10进制数转换"; cout << "n(4)16进制数转换"; cout << "n(0)退出"; cout << "n**********************"; cout << "n请输入功能号:"; cin >> n; while (n < 0 || n>4) { cout << "n输入错误,请重新输入功能号:"; cin >> n; } if (n == 1) two_menu_1(); else if (n == 2) two_menu_2(); else if (n == 3) two_menu_3(); else if (n == 4) two_menu_4(); else return; } }

构建二级菜单,并且能够返回上一级菜单,共有四个分别对应(二进制,八进制,十进制,十六进制)

复制代码
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
void two_menu_1() { int n = -1; string x; while (n != 0) { cout << "n*******************************"; cout << "n(1)2进制数 ---> 8进制数"; cout << "n(2)2进制数 ---> 10进制数"; cout << "n(3)2进制数 ---> 16进制数"; cout << "n(0)返回上级目录"; cout << "n*******************************"; cout << "n请输入功能号:"; cin >> n; while (n < 0 || n>3) { cout << "n输入错误,请重新输入功能号:"; cin >> n; } if (n == 1) { cout << "n请输入2进制数:"; cin >> x; ndec(2, x); decn(x, 8); cout << "n8进制数为:" << x; } else if (n == 2) { cout << "n请输入2进制数:"; cin >> x; ndec(2, x); cout << "n10进制数为:" << x; } else if (n == 3) { cout << "n请输入2进制数:"; cin >> x; ndec(2, x); decn(x, 16); cout << "n16进制数为:" << x; } else menu(); } }//第一个子菜单 void two_menu_2() { int n = -1; string x; while (n != 0) { cout << "n*******************************"; cout << "n(1)8进制数 ---> 2进制数"; cout << "n(2)8进制数 ---> 10进制数"; cout << "n(3)8进制数 ---> 16进制数"; cout << "n(0)返回上级目录"; cout << "n*******************************"; cout << "n请输入功能号:"; cin >> n; while (n < 0 || n>3) { cout << ""; cin >> n; } if (n == 1) { cout << "请输入8进制数:"; cin >> x; ndec(8, x); decn(x, 2); cout << "n2进制数为:" << x; } else if (n == 2) { cout << "请输入8进制数:"; cin >> x; ndec(8, x); cout << "n10进制数为:" << x; } else if (n == 3) { cout << "请输入8进制数:"; cin >> x; ndec(8, x); decn(x, 16); cout << "n16进制数为:" << x << endl; } else menu(); } } void two_menu_3() { int n = -1; string x; while (n != 0) { cout << "n*******************************"; cout << "n(1)10进制数 ---> 2进制数"; cout << "n(2)10进制数 ---> 8进制数"; cout << "n(3)10进制数 ---> 16进制数"; cout << "n(0)返回上级目录"; cout << "n*******************************"; cout << "n请输入功能号:"; cin >> n; while (n < 0 || n>3) { cout << "n输入错误,请重新输入功能号:"; cin >> n; } if (n == 1) { cout << "n请输入10进制数:"; cin >> x; decn(x, 2); cout << "n2进制数为:" << x; } else if (n == 2) { cout << "n请输入10进制数:"; cin >> x; decn(x, 8); cout << "n8进制数为:" << x; } else if (n == 3) { cout << "n请输入10进制数:"; cin >> x; decn(x, 16); cout << "n16进制数为:" << x; } else menu(); } } void two_menu_4() { int n = -1; string x; while (n != 0) { cout << "n*******************************"; cout << "n(1)16进制数 ---> 2进制数"; cout << "n(2)16进制数 ---> 8进制数"; cout << "n(3)16进制数 ---> 10进制数"; cout << "n(0)返回上级目录"; cout << "n*******************************"; cout << "n请输入功能号:"; cin >> n; while (n < 0 || n>3) { cout << "n输入错误,请重新输入功能号:"; cin >> n; } if (n == 1) { cout << "n请输入16进制数:"; cin >> x; ndec(16, x); decn(x, 2); cout << "n2进制数为:" << x; } else if (n == 2) { cout << "n请输入16进制数:"; cin >> x; ndec(16, x); decn(x, 8); cout << "n8进制数为:" << x; } else if (n == 3) { cout << "n请输入16进制数:"; cin >> x; ndec(16, x); cout << "n10进制数为:" << x; } else menu(); } }

非法性判断

注意小数点的个数,不符合进制要求的数字或字符。
n为进制,x为输入的进制数,sign为正负,point为小数点个数。(可以用do while进行化简)

复制代码
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
int legal(int n, string& x,int sign) { unsigned int point = 0, illegal = 0,i; if (x[0] == '-') sign++; for (i = 0 + sign; i < x.length(); i++) { if (x[i] == '.') { point++; continue; } if (x[i] < '0' || x[i] >= n + '0') illegal++; if(n==16&&(x[i]>='A'||x[i]<='F')) continue; } while (point >= 2 || illegal > 0) { cout << "输入错误,请重新输入:"; cin >> x; sign = 0; if (x[0] == '-') sign++; point = 0, illegal = 0; for (i = 0 + sign; i < x.size(); i++) { if (x[i] == '.') { point++; continue; } if (x[i] < '0' || x[i]>n + '0') illegal++; if (n == 16 && (x[i] >= 'A' || x[i] <= 'F')) continue; } } return point; }

其他进制转换为十进制

复制代码
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
void ndec(int n, string& x) { unsigned int whole_n = 0, i, j, k, point = 0, sign = 0, num = 1, whole = 0, small_n = 0, wn = 1, sn = 0, control_small = 0;//whole为整数部分,whole_n为整数位数(若为负数不会加一) double small = 0, new_num; if (x[0] == '-') sign++; point=legal(n, x, sign); if (point == 1) { for (i = 0 + sign; i < x.size(); i++, whole_n++) if (x[i] == '.') break; for (i = i + 1; i < x.size(); i++) small_n++; for (j = 0 + sign; j < whole_n + sign; j++) { num = 1; for (k = 0; k < whole_n + sign - j - 1; k++) num = num * n; if (x[j] >= 'A' && x[j] <= 'F') whole = whole + (int(x[j]) - 55) * num; else whole = whole + (int(x[j]) - 48) * num; } for (control_small = 0, j = j + 1; j < x.size(); j++) { if (control_small >= 6) break; num = 1; for (k = 0; k < j - whole_n - sign; k++) num = num * n; if (x[j] >= 'A' && x[j] <= 'F') small = small + (int(x[j]) - double(55)) / num; else small = small + (int(x[j]) - double(48)) / num; control_small++; } } else { for (i = 0 + sign; i < x.size(); i++) whole_n++; for (i = 0 + sign; i < x.size() + sign; i++) { num = 1; for (k = 0; k < x.size() - i - 1; k++) num = num * n; if (x[i] >= 'A' && x[i] <= 'F') whole = whole + (int(x[i]) - 55) * num; else whole = whole + (int(x[i]) - 48) * num; } } new_num = whole + small; double t = new_num;//以下是将数字转化为字符串 x = ""; if (sign) x += '-'; while (int(t) / 10 != 0) { wn++; t = t / 10; } t = new_num; if (point) { while (t * 10 != (int(t)) * double(10)) { sn++; t = t * 10; } } t = new_num; for (i = 0 + sign; i < wn + sign; i++) { num = 1; for (k = 0; k < wn + sign - i - 1; k++) num = num * 10; x += int(t / num) + '0'; t = int(t) % num; } if (point) { x += '.'; t = new_num; for (i = 0; i < sn; i++) { x += int((t - int(t)) * 10) + '0'; t = t * 10; } } }

十进制转换为其他进制

复制代码
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
void decn(string& x, int n) { unsigned int whole_n = 0, small_n = 0, i, j, point = 0, sign = 0, whole = 0, control_small; double small = 0, new_num, t; if (x[0] == '-') sign++; point = legal(10, x, sign); for (i = 0 + sign; i < x.size(); i++) { if (x[i] == '.') break; whole *= 10; whole = whole + int(x[i] - '0'); } for (j = x.size() - 1; j > i; j--) { small = small + x[j] - '0'; small = small / 10; } new_num = whole + small; x = ""; if (sign) x = "-"; fun_1(whole, x, n); t = small; if (point) { x += '.'; control_small = 0; while (control_small < 5 && int(t * n) != t * n) { if (t * n >= 10) x = x + char(int(t * n) + 55); else x = x + char(int(t * n) + 48); t = t * n - int(t * n); control_small++; } if (t * n >= 10) x = x + char(int(t * n) + 55); else x = x + char(int(t * n) + 48); } } void fun_1(int i, string& x, int n) { if (i >= n) { fun_1(i / n, x, n); if (i >= 10) x = x + char(i % n + 55); else x = x + char(i % n + 48); } else { if (i >= 10) x = x + char(i + 55); else x = x + char(i + 48); } }

全部代码

复制代码
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#include<string> #include <iostream> #include<iomanip> using namespace std; void menu(); void two_menu_1(); void two_menu_2(); void two_menu_3(); void two_menu_4(); void ndec(int, string&); void decn(string&, int); void fun_1(int, string&, int); int legal(int, string&,int); int main() { menu(); return 0; } void menu() { int n = -1; while (n != 0) { cout << "n**********************"; cout << "n(1)2进制数转换"; cout << "n(2)8进制数转换"; cout << "n(3)10进制数转换"; cout << "n(4)16进制数转换"; cout << "n(0)退出"; cout << "n**********************"; cout << "n请输入功能号:"; cin >> n; while (n < 0 || n>4) { cout << "n输入错误,请重新输入功能号:"; cin >> n; } if (n == 1) two_menu_1(); else if (n == 2) two_menu_2(); else if (n == 3) two_menu_3(); else if (n == 4) two_menu_4(); else return; } }//主菜单的实现 void two_menu_1() { int n = -1; string x; while (n != 0) { cout << "n*******************************"; cout << "n(1)2进制数 ---> 8进制数"; cout << "n(2)2进制数 ---> 10进制数"; cout << "n(3)2进制数 ---> 16进制数"; cout << "n(0)返回上级目录"; cout << "n*******************************"; cout << "n请输入功能号:"; cin >> n; while (n < 0 || n>3) { cout << "n输入错误,请重新输入功能号:"; cin >> n; } if (n == 1) { cout << "n请输入2进制数:"; cin >> x; ndec(2, x); decn(x, 8); cout << "n8进制数为:" << x; } else if (n == 2) { cout << "n请输入2进制数:"; cin >> x; ndec(2, x); cout << "n10进制数为:" << x; } else if (n == 3) { cout << "n请输入2进制数:"; cin >> x; ndec(2, x); decn(x, 16); cout << "n16进制数为:" << x; } else menu(); } }//第一个子菜单 void two_menu_2() { int n = -1; string x; while (n != 0) { cout << "n*******************************"; cout << "n(1)8进制数 ---> 2进制数"; cout << "n(2)8进制数 ---> 10进制数"; cout << "n(3)8进制数 ---> 16进制数"; cout << "n(0)返回上级目录"; cout << "n*******************************"; cout << "n请输入功能号:"; cin >> n; while (n < 0 || n>3) { cout << ""; cin >> n; } if (n == 1) { cout << "请输入8进制数:"; cin >> x; ndec(8, x); decn(x, 2); cout << "n2进制数为:" << x; } else if (n == 2) { cout << "请输入8进制数:"; cin >> x; ndec(8, x); cout << "n10进制数为:" << x; } else if (n == 3) { cout << "请输入8进制数:"; cin >> x; ndec(8, x); decn(x, 16); cout << "n16进制数为:" << x << endl; } else menu(); } } void two_menu_3() { int n = -1; string x; while (n != 0) { cout << "n*******************************"; cout << "n(1)10进制数 ---> 2进制数"; cout << "n(2)10进制数 ---> 8进制数"; cout << "n(3)10进制数 ---> 16进制数"; cout << "n(0)返回上级目录"; cout << "n*******************************"; cout << "n请输入功能号:"; cin >> n; while (n < 0 || n>3) { cout << "n输入错误,请重新输入功能号:"; cin >> n; } if (n == 1) { cout << "n请输入10进制数:"; cin >> x; decn(x, 2); cout << "n2进制数为:" << x; } else if (n == 2) { cout << "n请输入10进制数:"; cin >> x; decn(x, 8); cout << "n8进制数为:" << x; } else if (n == 3) { cout << "n请输入10进制数:"; cin >> x; decn(x, 16); cout << "n16进制数为:" << x; } else menu(); } } void two_menu_4() { int n = -1; string x; while (n != 0) { cout << "n*******************************"; cout << "n(1)16进制数 ---> 2进制数"; cout << "n(2)16进制数 ---> 8进制数"; cout << "n(3)16进制数 ---> 10进制数"; cout << "n(0)返回上级目录"; cout << "n*******************************"; cout << "n请输入功能号:"; cin >> n; while (n < 0 || n>3) { cout << "n输入错误,请重新输入功能号:"; cin >> n; } if (n == 1) { cout << "n请输入16进制数:"; cin >> x; ndec(16, x); decn(x, 2); cout << "n2进制数为:" << x; } else if (n == 2) { cout << "n请输入16进制数:"; cin >> x; ndec(16, x); decn(x, 8); cout << "n8进制数为:" << x; } else if (n == 3) { cout << "n请输入16进制数:"; cin >> x; ndec(16, x); cout << "n10进制数为:" << x; } else menu(); } } void ndec(int n, string& x) { unsigned int whole_n = 0, i, j, k, point = 0, sign = 0, num = 1, whole = 0, small_n = 0, wn = 1, sn = 0, control_small = 0;//whole为整数部分,whole_n为整数位数(若为负数不会加一) double small = 0, new_num; if (x[0] == '-') sign++; point=legal(n, x, sign); if (point == 1) { for (i = 0 + sign; i < x.size(); i++, whole_n++) if (x[i] == '.') break; for (i = i + 1; i < x.size(); i++) small_n++; for (j = 0 + sign; j < whole_n + sign; j++) { num = 1; for (k = 0; k < whole_n + sign - j - 1; k++) num = num * n; if (x[j] >= 'A' && x[j] <= 'F') whole = whole + (int(x[j]) - 55) * num; else whole = whole + (int(x[j]) - 48) * num; } for (control_small = 0, j = j + 1; j < x.size(); j++) { if (control_small >= 6) break; num = 1; for (k = 0; k < j - whole_n - sign; k++) num = num * n; if (x[j] >= 'A' && x[j] <= 'F') small = small + (int(x[j]) - double(55)) / num; else small = small + (int(x[j]) - double(48)) / num; control_small++; } } else { for (i = 0 + sign; i < x.size(); i++) whole_n++; for (i = 0 + sign; i < x.size() + sign; i++) { num = 1; for (k = 0; k < x.size() - i - 1; k++) num = num * n; if (x[i] >= 'A' && x[i] <= 'F') whole = whole + (int(x[i]) - 55) * num; else whole = whole + (int(x[i]) - 48) * num; } } new_num = whole + small; double t = new_num;//以下是将数字转化为字符串 x = ""; if (sign) x += '-'; while (int(t) / 10 != 0) { wn++; t = t / 10; } t = new_num; if (point) { while (t * 10 != (int(t)) * double(10)) { sn++; t = t * 10; } } t = new_num; for (i = 0 + sign; i < wn + sign; i++) { num = 1; for (k = 0; k < wn + sign - i - 1; k++) num = num * 10; x += int(t / num) + '0'; t = int(t) % num; } if (point) { x += '.'; t = new_num; for (i = 0; i < sn; i++) { x += int((t - int(t)) * 10) + '0'; t = t * 10; } } } void decn(string& x, int n) { unsigned int whole_n = 0, small_n = 0, i, j, point = 0, sign = 0, whole = 0, control_small; double small = 0, new_num, t; if (x[0] == '-') sign++; point = legal(10, x, sign); for (i = 0 + sign; i < x.size(); i++) { if (x[i] == '.') break; whole *= 10; whole = whole + int(x[i] - '0'); } for (j = x.size() - 1; j > i; j--) { small = small + x[j] - '0'; small = small / 10; } new_num = whole + small; x = ""; if (sign) x = "-"; fun_1(whole, x, n); t = small; if (point) { x += '.'; control_small = 0; while (control_small < 5 && int(t * n) != t * n) { if (t * n >= 10) x = x + char(int(t * n) + 55); else x = x + char(int(t * n) + 48); t = t * n - int(t * n); control_small++; } if (t * n >= 10) x = x + char(int(t * n) + 55); else x = x + char(int(t * n) + 48); } } void fun_1(int i, string& x, int n) { if (i >= n) { fun_1(i / n, x, n); if (i >= 10) x = x + char(i % n + 55); else x = x + char(i % n + 48); } else { if (i >= 10) x = x + char(i + 55); else x = x + char(i + 48); } } int legal(int n, string& x,int sign) { unsigned int point = 0, illegal = 0,i; if (x[0] == '-') sign++; for (i = 0 + sign; i < x.length(); i++) { if (x[i] == '.') { point++; continue; } if (x[i] < '0' || x[i] >= n + '0') illegal++; if(n==16&&(x[i]>='A'||x[i]<='F')) continue; } while (point >= 2 || illegal > 0) { cout << "输入错误,请重新输入:"; cin >> x; sign = 0; if (x[0] == '-') sign++; point = 0, illegal = 0; for (i = 0 + sign; i < x.size(); i++) { if (x[i] == '.') { point++; continue; } if (x[i] < '0' || x[i]>n + '0') illegal++; if (n == 16 && (x[i] >= 'A' || x[i] <= 'F')) continue; } } return point; }

最后

以上就是优美板凳最近收集整理的关于c++二,八,十,十六进制的互相转换(包括小数,负数,且保留小数点后六位)关于本文章内容说明进制间的转换思想功能目的代码实现的全部内容,更多相关c++二,八,十,十六进制内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(71)

评论列表共有 0 条评论

立即
投稿
返回
顶部