Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <conio.h>
  4. #include <windows.h>
  5. #include <vector>
  6. #include <string>
  7. #include <math.h>
  8.  
  9. using namespace std;
  10.  
  11. void func1(int sys1, int sys2, string num);
  12. void func2(int sys1, int sys2, string num);
  13.  
  14. int main()
  15. {
  16. SetConsoleCP(1251);
  17. SetConsoleOutputCP(1251);
  18. cout << "Их 13-ричной системы в 5-ричную: ";
  19. string num;
  20. getline(cin, num);
  21. cout << "С массивом: ";
  22. func1(13, 5, num);
  23. cout << '\n' << "Без массива: ";
  24. func2(13, 5, num);
  25.  
  26. _getch();
  27. return 0;
  28. }
  29.  
  30. void func1(int sys1, int sys2, string num) {
  31. int i, j;
  32. int vectsize = num.size();
  33. int dec = 0, count = 0;
  34. for (i = 0, j = vectsize - 1; i < vectsize - 1, j >= 0; i++, j--) {
  35. if ((num[j] - '0') >= 0 && (num[j] - '0') < 9) {
  36. dec += (num[j] - '0') * pow(sys1, i);
  37. count++;
  38. }
  39. else if (int(num[j]) >= 65 && int(num[j]) <= 90 && (num[j] - '7') < sys1) {
  40. dec += (num[j] - '7') * pow(sys1, i);
  41. count++;
  42. }
  43. else {
  44. cout << "Введите корректное число!";
  45. //return 0;
  46. }
  47. }
  48. char newnum[100] = "";
  49. i = 0;
  50. while (dec >= 1) {
  51. newnum[i] = dec % sys2 + '0';
  52. dec /= sys2;
  53. i++;
  54. }
  55. for (i - 1; i >= 0; i--) {
  56. cout << newnum[i];
  57. }
  58.  
  59. }
  60.  
  61. void func2(int sys1, int sys2, string num) {
  62. long long int des1 = 0, tr1 = 1;
  63. //cout << '\n' << "БЕЗ МАССИВОВ" << '\n' << "Введите число в тринадцатеричной системе: ";
  64. char chislo = ' ';
  65. int l = num.size();
  66. l = l - 1;
  67. //перевод в десятичную
  68. while (chislo != '\n') {
  69. chislo = getchar();
  70. if (chislo == '\n')
  71. break;
  72. else if (chislo <= '9')
  73. des1 += (chislo - '0') * pow(13, l);
  74. else
  75. des1 += (chislo - 'A' + 10) * pow(13, l);
  76. l--;
  77. }
  78. //перевод в троичную
  79. cout << "Пятиричное число: ";
  80. if (des1 == 0)
  81. cout << 0;
  82. else {
  83. while (tr1 <= des1) {
  84. tr1 *= 5;
  85. }
  86. while (tr1 > 1) {
  87. tr1 /= 5;
  88. if (des1 / tr1 < 10)
  89. cout << (des1 / tr1);
  90. else
  91. cout << (des1 / tr1) + 'A' - 10;
  92. des1 %= tr1;
  93. }
  94. }
  95. _getch();
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement