Advertisement
tomasaccini

Untitled

Jul 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. #include <vector>
  4.  
  5. unsigned long int base_6_to_10(const char* cadena) {
  6.     int len = strlen(cadena) - 1;
  7.     size_t mult = 1;
  8.     unsigned long int valor = 0;
  9.     for (; len >= 0; --len) {
  10.         int actual = cadena[len] - '0';
  11.         valor += actual * mult;
  12.         mult *= 6;
  13.     }
  14.     return valor;
  15. }
  16.  
  17. std::vector<char> base_10_to_any(unsigned long int valor, int base) {
  18.     std::vector<char> result;
  19.     while (valor >= 1) {
  20.         int resto = valor % base;
  21.         valor = valor / base;
  22.         if (resto >= 0 && resto <= 9) {
  23.             result.push_back(resto + '0');
  24.         } else {
  25.             result.push_back(resto - 10 + 'A');
  26.         }
  27.     }
  28.     std::vector<char> real_result;
  29.     for (int i = result.size() - 1; i >= 0; --i) {
  30.         real_result.push_back(result[i]);
  31.     }
  32.     return real_result;
  33. }
  34.  
  35. int main() {
  36.     char num[100];
  37.     std::cout << "Ingresar un numero en base 6: ";
  38.     std::cin >> num;
  39.     unsigned long int decimal = base_6_to_10(&num[0]);
  40.     std::cout << decimal << std::endl;
  41.  
  42.     int base;
  43.     std::cout << "Ingresar un numero en base 6: ";
  44.     std::cin >> base;
  45.     std::vector<char> en_base = base_10_to_any(decimal, base);
  46.  
  47.     std::vector<char>::iterator it = en_base.begin();
  48.     while (it != en_base.end()) {
  49.         std::cout << *it;
  50.         ++it;
  51.     }
  52.     std::cout << '\n';
  53.  
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement