Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.33 KB | None | 0 0
  1. //============================================================================
  2. // Name        : CambiaBases.cpp
  3. // Author      : Miguel Angel Pau Diaz
  4. // Version     :
  5. // Copyright   : Fundamentos de Programación 2010
  6. // Description : Cambio de bases de números
  7. //============================================================================
  8.  
  9. #include <iostream>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <ctype.h>
  13. #include <math.h>
  14. using namespace std;
  15.  
  16.  
  17. //*** Declaración de funciones ***
  18.  
  19. bool CheckInput (string &data, int bas);    //Comprueba entrada de datos
  20. string deBaseaBase (string &num, int bas1, int bas2);   //Convierte de una base a otra
  21. float Pot (int bas, int pot);       //x^y
  22. char NDigit (string &num, int dig);
  23.  
  24. //********************************
  25.  
  26. int main(int argc, char* argv[])
  27. {
  28.     int base1, base2, digito;
  29.     char cm, odigit;
  30.     string input, output;
  31.  
  32.     do {
  33.         system("cls");
  34.         cout << "\n TRATAMIENTO NUMERICO ENTRE BASES" << endl;
  35.         cout << "\n Seleccione opcion:" << endl;
  36.         cout << "\n C - Conversion Base - Base" << endl;
  37.         cout << "   M - Mostrar i-esimo digito en otra base" << endl;
  38.         cin >> cm;
  39.     }while (cm!='C' && cm!='c' && cm!='M' && cm!='m');
  40.  
  41.     switch (cm){
  42.         case 'C': case 'c':
  43.             do{
  44.                 do {
  45.                     system ("cls");
  46.                     cout << "\n CONVERSION BASE - BASE " << endl;
  47.                     cout << "\n Introduce un numero en cualquier base entre 1 y 36: ";
  48.                     cin >> input;
  49.                     cout << "\n Indica su base (1-16): ";
  50.                     cin >> base1;
  51.                 }while (CheckInput(input, base1)==false);
  52.                 cout << "\n Indica la base a la que quieres convertirlo (1-36): ";
  53.                 cin >> base2;
  54.             }while (base2<0 && base2>36);
  55.  
  56.             output = deBaseaBase(input, base1, base2);
  57.             cout << "\n  "<<input << " (base " << base1 << ") = " << output << " (base " << base2 << ")" << endl << endl;
  58.             break;
  59.         case 'M': case 'm':
  60.             do{
  61.                 do{
  62.                     do {
  63.                         system ("cls");
  64.                         cout << "\n DETERMINACION DEL I-ESIMO DIGITO EN OTRA BASE " << endl;
  65.                         cout << "\n Introduce un numero en cualquier base entre 1 y 36: ";
  66.                         cin >> input;
  67.                         cout << "\n Indica su base (1-16): ";
  68.                         cin >> base1;
  69.                     }while (CheckInput(input, base1)==false);
  70.                     cout << "\n Indica la base a la que quieres convertirlo (1-36): ";
  71.                     cin >> base2;
  72.                 }while (base2<0 && base2>36);
  73.                 cout << "\n Indica un dígito a mostrar en la nueva base (0-40): ";
  74.                 cin >> digito;
  75.             }while (digito<0 || digito>40);
  76.  
  77.             output = deBaseaBase(input, base1, base2);
  78.             odigit = NDigit(output, digito);
  79.             //cout << "\n  "<<input << " (base " << base1 << ") = " << output << " (base " << base2 << ")" << endl;
  80.             cout << "\n  Digito " << digito << ": " << odigit << endl << endl;
  81.             break;
  82.         }
  83.  
  84.     system("pause");
  85.  
  86.     return 0;
  87. }
  88.  
  89. //*********************************************************
  90. //  CheckInput: Verifica datos de entrada
  91. //      data - cadena alfa con datos de entrada
  92. //      bas  - base del número data
  93. //
  94. //  retorno:    true - datos correctos
  95. //              false - error en entrada de datos
  96. //              data - reformateada a mayúsculas
  97. //*********************************************************
  98. bool CheckInput (string &data, int bas){
  99.     bool reti=true;
  100.     int acheck=54;      // ASCII en función de la base [A=10 => ASCII(54+11)]
  101.  
  102.     if (bas<1 || bas>36)
  103.         reti=false;
  104.     else {
  105.         for (int i=data.size()-1; i>=0; i--){
  106.             data[i] = toupper(data[i]); //  A mayúsculas
  107.         }
  108.         for (int i=data.size()-1; i>=0; i--){
  109.             if (data[i]>47 && data[i]<58){  // Comprueba si es número (ASCII 48 a 57)
  110.                 if (bas==1){
  111.                     if ((data[i]-48)==bas)
  112.                         reti=true;          //trues no necesarios, se dejan por claridad (*)
  113.                     else
  114.                         reti=false;
  115.                 }else{
  116.                     if ((data[i]-48)<bas)   // Los dígitos menores que la base, i.e. bin 01
  117.                         reti=true;          //(*)
  118.                     else
  119.                         reti=false;
  120.                 }
  121.             }else if ((data[i]>64 && data[i]<=acheck+bas)) // Digitos < base
  122.                 reti=true;              //(*)
  123.             else
  124.                 reti=false;;
  125.  
  126.             if (reti==false) break;     //Para salir del bucle  si error en datos
  127.         }
  128.     }
  129.     return reti;
  130. }
  131.  
  132. //*********************************************************
  133. //  deBaseaBase: Convierte un número de una base a otra
  134. //      num - cadena alfa con numero codificado en base bas1
  135. //      bas1 - base del número origen
  136. //      bas2 - base del número destino
  137. //  retorno: cadena alfa con número convertido a base bas2
  138. //*********************************************************
  139. string deBaseaBase (string &num, int bas1, int bas2){
  140.     int potencia=0;
  141.     int acheck=54;      // ASCII en función de la base [A = ASCII(54+11)]
  142.     long int acum=0;
  143.     string sBase="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  144.     string sPila="", sResult="";
  145.  
  146.     //Primero convertimos a decimal
  147.     if (bas1==1)
  148.         acum=num.size();    //cadena de "1s" de longitud N
  149.     else{
  150.         for (int i=num.size()-1; i>=0; i--){
  151.             if (num[i]>47 && num[i]<58){
  152.                 acum+=(num[i]-48)*Pot(bas1,potencia);
  153.                 potencia++;
  154.             }else if (num[i]>64 && num[i]<=acheck+bas1){
  155.                 acum+=(num[i]-55)*Pot(bas1,potencia);
  156.                 potencia++;
  157.             }else{
  158.                 sResult="Err";
  159.                 return sResult;
  160.             }
  161.         }
  162.     }
  163.     // Luego convertimos a segunda base
  164.     if (bas2==1){
  165.         while (acum>0){
  166.             sResult+= "1";
  167.             acum--;
  168.         }
  169.     }else{
  170.         while (acum>0){
  171.             sPila+=sBase[acum%bas2];    // Se apilan en orden inverso
  172.             acum/=bas2;
  173.         }
  174.         for (int i=sPila.size()-1; i>=0; i--){
  175.             sResult+=sPila[i];          //Invierte orden de los dígitos
  176.         }
  177.     }
  178.     return sResult;
  179. }
  180.  
  181. //*********************************************************
  182. //  NDigit: Determina un dígito de un numero  X^Y
  183. //      bas - base
  184. //      pot - potencia
  185. //
  186. //  retorno: X^Y
  187. //*********************************************************
  188. char NDigit (string &num, int dig){
  189.     char cResult='0';
  190.     int size;
  191.  
  192.     size=num.size()-1;
  193.     if (dig>size){
  194.         return cResult;
  195.     }
  196.     else{
  197.         cResult=num[size-dig];
  198.     }
  199.     return cResult;
  200. }
  201.  
  202. //*********************************************************
  203. //  Pot: X^Y
  204. //      bas - base
  205. //      pot - potencia
  206. //
  207. //  retorno: X^Y
  208. //*********************************************************
  209. float Pot (int bas, int pot){
  210.     float bep;
  211.  
  212.     if (pot==0)
  213.         bep=1;
  214.     else{
  215.         bep=1;
  216.         for (int i=1; i<=pot; i++){
  217.             bep*=bas;
  218.         }
  219.     }
  220.     return bep;
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement