Advertisement
Guest User

Untitled

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