Advertisement
Kolyach

4.8

Dec 2nd, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. int converter10(char string[100000], char digits[100], int basis) {
  8.     int amount = strlen(string), summ = 0, digit_equivalent, i = 0;
  9.     for (amount; amount > 0; amount--) {
  10.         for (int j = 0; j < 35; j++) {
  11.             if (string[amount - 1] == digits[j]) {
  12.                 digit_equivalent = j;
  13.                 summ += digit_equivalent * pow(basis, i);
  14.                 break;
  15.             }
  16.         }
  17.         i++;
  18.         digit_equivalent = 0;
  19.     }
  20.     return summ;
  21. }
  22. void converter_x(long int number, char otvet[100000], char digits[100000], int basis) {
  23.     int i = 0, ostatok = 0, int_part = 0;
  24.     while (number / basis > 0) {
  25.         ostatok = number % basis;
  26.         int_part = number / basis;
  27.         otvet[i] = digits[ostatok];
  28.         i++;
  29.         number = int_part;
  30.     }
  31.     if (number / basis == 0) {
  32.         otvet[i] = digits[number];
  33.     }
  34. }
  35. int main() {
  36.     setlocale(LC_ALL, "Russian");
  37.     char default_number[100000], new_number[100], digit_mass[100] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'G', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
  38.     long int default_basis, new_basis, min_default_basis = 0;
  39.     cout << "Введите исходное число: ";
  40.     cin >> default_number;
  41.     memset(&new_number, 0, sizeof(new_number));
  42.     for (int i = 0; i < strlen(default_number); i++) {
  43.         for (int j = 0; j < strlen(digit_mass); j++) {
  44.             if ((default_number[i] == digit_mass[j]) && j >= min_default_basis) {
  45.                 min_default_basis = j;
  46.                 break;
  47.             }
  48.         }
  49.     }
  50.     cout << "Введите систему счисления исходного числа: ";
  51.     cin >> default_basis;
  52.     if (default_basis < min_default_basis + 1) {
  53.         cout << "Введенное вами основание слишком маленькое. Введите другое: ";
  54.     }
  55.     else {
  56.         cout << "Введите новое основание для этого числа: ";
  57.         cin >> new_basis;
  58.         if (new_basis == default_basis) {
  59.             cout << "Переведенное число: " << default_number << endl;
  60.         }
  61.         else if (new_basis == 10) {
  62.             cout << "Переведенное число: " << converter10(default_number, digit_mass, default_basis) << endl;
  63.         }
  64.         else {
  65.             long int new_chislo = converter10(default_number, digit_mass, default_basis);
  66.             converter_x(new_chislo, new_number, digit_mass, new_basis);
  67.             int x = strlen(new_number) - 1;
  68.             cout << "Переведенное число: ";
  69.             for (x; x >= 0; x--) {
  70.                 cout << new_number[x];
  71.             }
  72.             cout << endl;
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement