Advertisement
thinhckhcmus

Chuyển Đổi Các Cơ Số

Oct 9th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #define MAX 1000
  4. using namespace std;
  5. void Chuyen10Sang2(int a[32], int x)
  6. {
  7. int i = 31;
  8. while (x != 0)
  9. {
  10. a[i] = x % 2;
  11. x = x / 2;
  12. i--;
  13. }
  14. }
  15. void chuyen10sang16(char b[32], int x)
  16. {
  17. int i = 31;
  18. while (x != 0)
  19. {
  20. int du = x % 16;
  21. if (du <= 9)
  22. {
  23. b[i] = du + 48;
  24. }
  25. if (du == 10)
  26. {
  27. b[i] = 'A';
  28. }
  29. if (du == 11)
  30. {
  31. b[i] = 'B';
  32. }
  33. if (du == 12)
  34. {
  35. b[i] = 'C';
  36. }
  37. if (du == 13)
  38. {
  39. b[i] = 'D';
  40. }
  41. if (du == 14)
  42. {
  43. b[i] = 'E';
  44. }
  45. if (du == 15)
  46. {
  47. b[i] = 'F';
  48. }
  49. x = x / 16;
  50. i--;
  51. }
  52. }
  53. void xuatmang10sang2(int a[32])
  54. {
  55. int i = 31;
  56. for (int i = 0; i < 32; i++)
  57. {
  58. cout << " " << a[i];
  59. }
  60. }
  61. void xuatmang10sang16(char b[32])
  62. {
  63. int i = 31;
  64. for (int i = 0; i < 32; i++)
  65. {
  66. cout << " " << b[i];
  67. }
  68. cout << endl;
  69. }
  70. void Chuyen2Sang10(int c[32], int x)
  71. {
  72. int kq = 0, du, somu = 0;
  73. while (x > 0)
  74. {
  75. du = x % 10;
  76. kq = kq + du * pow(2, somu);
  77. somu++;
  78. x = x / 10;
  79. }
  80. cout << " " << kq;
  81. cout << endl;
  82. }
  83. int Chuyen16Sang10(char d[32])
  84. {
  85. int len = strlen(d);
  86. int base = 1;
  87. int dec_val = 0;
  88. for(int i=len -1;i>=0;i--)
  89. {
  90. if(d[i]>='0'&&d[i]<='9')
  91. {
  92. dec_val = dec_val + (d[i] - 48)*base;
  93. base = base * 16;
  94. }
  95. else if(d[i] >= 'A'&&d[i] <= 'F')
  96. {
  97. dec_val = dec_val + (d[i] - 55)*base;
  98. base = base * 16;
  99. }
  100. }
  101. return dec_val;
  102. }
  103. void xuatChuyen16Sang10(char d[32])
  104. {
  105. cout << Chuyen16Sang10(d) << endl;
  106. }
  107.  
  108. int Chuyen2Sang16(char num[MAX])
  109. {
  110. char hexa[MAX];
  111. int temp;
  112. long int i = 0, j = 0;
  113. while(num[i])
  114. {
  115. num[i] = num[i] - 48;
  116. i++;
  117. }
  118. --i;
  119. while (i - 2 >= 0)
  120. {
  121. temp = num[i - 3] * 8 + num[i - 2] * 4 + num[i - 1] * 2 + num[i];
  122. if(temp>9)
  123. {
  124. hexa[j++] = temp + 55;
  125. }
  126. else
  127. {
  128. hexa[j++] = temp + 48;
  129. }
  130. i = i - 4;
  131. }
  132. if(i==1)
  133. {
  134. hexa[j] = num[i - 1] * 2 + num[i] + 48;
  135. }
  136. else if(i==0)
  137. {
  138. hexa[j] = num[i] + 48;
  139. }
  140. else
  141. {
  142. --i;
  143. }
  144. cout << "he co so 16 la: \n";
  145. while(j>=0)
  146. {
  147. cout << hexa[j--];
  148. }
  149. cout << endl;
  150. return 0;
  151. }
  152.  
  153.  
  154. void main()
  155. {
  156. int a[32] = { 0 };// khoi tao so luong phan tu la 1
  157. char b[32];
  158. int c[32];
  159. char d[32];
  160. int luachon;
  161. cout << "<<<<<<<<<<<<<<<<<<<MENU LUA CHON>>>>>>>>>>>>>>>>>" << endl;
  162. cout << "Nhap Cac So Tuong Ung De Thuc Hien" << endl;
  163. cout << "So 1: Chuyen Tu He 10 Sang 2" << endl;
  164. cout << "So 2: Chuyen Tu 10 Sang 16" << endl;
  165. cout << "So 3: Chuyen Tu 2 Sang 10" << endl;
  166. cout << "So 4: Chuyen Tu 16 Sang 10" << endl;
  167. cout << "So 5: Chuyen Tu 2 Sang 16" << endl;
  168. cout << "So 6: De Thoat" << endl;
  169. cin >> luachon;
  170. switch (luachon)
  171. {
  172. case 1:
  173. {
  174. int x;
  175. cout << "nhap so bat ki de kiem tra: ";
  176. cin >> x;
  177. Chuyen10Sang2(a, x);
  178. xuatmang10sang2(a);
  179. break;
  180. }
  181. case 2:
  182. {
  183. int x;
  184. cout << "nhap so bat ki de kiem tra: ";
  185. cin >> x;
  186. for (int i = 0; i < 32; i++)
  187. {
  188. b[i] = '0';
  189. }
  190. chuyen10sang16(b, x);
  191. xuatmang10sang16(b);
  192. break;
  193. }
  194. case 3:
  195. {
  196. int x;
  197. cout << "nhap so bat ki de kiem tra: ";
  198. cin >> x;
  199. Chuyen2Sang10(c, x);
  200. break;
  201. }
  202. case 4:
  203. {
  204. cout << "nhap vao so bat ki de kiem tra: ";
  205. cin >> d;
  206. Chuyen16Sang10(d);
  207. xuatChuyen16Sang10(d);
  208. break;
  209. }
  210. case 5:
  211. {
  212. char num[MAX];
  213. cout << "nhap vao so bat ki de kiem tra: ";
  214. cin >> num;
  215. Chuyen2Sang16(num);
  216. break;
  217. }
  218. case 6:
  219. {
  220. break;
  221. cout << "nhan phim bat ki de thoat";
  222. }
  223. }
  224. system("pause");
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement