Advertisement
FoxTuGa

Conversions ODC-DEC-BIN

May 8th, 2011
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.16 KB | None | 0 0
  1. // By Leandro Soares
  2.  
  3. #include <stdio.h>
  4. #include "Validations.h"
  5. #define TRUE 1
  6. #define MAX 99999
  7. #define MIN 0
  8. #define BIN 2
  9. #define OCT 8
  10. #define DEC 10
  11.  
  12. int _Elev(int number, int power);       // Funcao similar a pow(...);
  13. int ToBinOct(int numero, unsigned base);        // Funcao que converte decimal para octal ou binario
  14. int ValidateBOD(int numero, int base);      // Validacao dos numeros e bases.
  15. int ToDec(int numero, int base);        // Funcao que converte binario e ou octal para decimal.
  16.  
  17. int main()
  18. {
  19.     int numero, base1, base2, resultado=0;
  20.     printf("\nNumero, base1, base2: ");
  21.     fflush(stdin);
  22.     scanf("%d, %d, %d", &numero, &base1, &base2);       // Leitura numero, base1 e base2
  23.    
  24.     // Verificacao do numero lido e das bases.
  25.     if(!_ValSupInf(numero, MIN, MAX) || ValidateBOD(numero, base1) == 0 || ValidateBOD(NULL, base2) == -1 )
  26.     {
  27.         printf("\n\nERRO\n\n");
  28.         return 0;
  29.     }
  30.     if( base1 == base2 )        // Se a base1 for igual a base2, entao o resultado é igual ao numero.
  31.         resultado = numero;
  32.     else
  33.     {
  34.         switch(base1)
  35.         {
  36.         case BIN:
  37.             switch(base2)
  38.             {
  39.             case OCT:   resultado = ToDec(numero, BIN);     // Bin to Dec.
  40.                         resultado = ToBinOct(resultado, OCT); break;    // Dec to Oct.
  41.             case DEC:   resultado = ToDec(numero, BIN); break;      // Bin to Dec.
  42.             }
  43.             break;
  44.         case OCT:
  45.             switch(base2)
  46.             {
  47.             case BIN:   resultado = ToDec(numero, OCT);     // Oct to Dec.
  48.                         resultado = ToBinOct(resultado, BIN); break;    // Dec to Bin.
  49.             case DEC:   resultado = ToDec(numero, OCT); break;      // Oct to Dec.
  50.             }
  51.             break;
  52.         case DEC:
  53.             switch(base2)
  54.             {
  55.             case BIN:   resultado = ToBinOct(numero, BIN);break;    // Dec ti Bin.
  56.             case OCT:   resultado = ToBinOct(numero, OCT);break;    // Dec to Oct.
  57.             }
  58.             break;
  59.         }
  60.     }
  61.     printf("\n\nResultado: %d\n\n\n", resultado);
  62.     return 0;
  63. }
  64.  
  65. int _Elev(int number, int power)        // funcao similar a funcao pow(...);
  66. {
  67.     int quad=1, c;
  68.     for(c=1;c <= power;c++)
  69.         quad *= number;
  70.     return quad;
  71. }
  72.  
  73. int ToDec(int numero, int base)     // Funcao que converte binario e ou octal para decimal.
  74. {
  75.     int resto, fac, spin, resultado, soma, temp;
  76.     resto=spin=fac=resultado=soma=0;
  77.  
  78.     temp=numero;
  79.     for(spin=0;temp != 0;spin++)       
  80.     {
  81.         resto = temp % 10;      // Resto do numero( ultimo digito ).
  82.         temp /= 10;    
  83.         fac = _Elev(base, spin);        // Elevar esse numero a sua potencia.
  84.         if( base == 2 )
  85.         {
  86.             if( resto == 1 )        // Se a base for igual a 2 e o resto igual 1.
  87.                 soma += fac;
  88.         }
  89.         else        // Se a base for 8...
  90.         {
  91.             resultado = resto * fac;
  92.             soma += resultado;
  93.         }
  94.     }
  95.     return soma;
  96. }
  97.  
  98. int ToBinOct(int numero, unsigned base)     // Funcao que converte decimal para octal ou binario
  99. {
  100.     int temp, resto, factor;
  101.     resto = 0;
  102.  
  103.     temp = numero;        
  104.     for(factor = 1; temp != 0; factor *= 10)   
  105.     {
  106.         resto = (temp % base) * factor + resto;
  107.         temp /= base;
  108.     }
  109.     return resto;
  110. }
  111.  
  112. int ValidateBOD(int numero, int base)       // Funcao que valida um numero e uma base(decimal, octal ou binaria).
  113. {
  114.     int resto = 0, temp;
  115.     temp = numero;
  116.     if( base != 2 && base != 8 && base != 10 )
  117.         return -1;
  118.     if( base == 10 )
  119.         return 1;
  120.  
  121.     while(temp != 0)
  122.     {
  123.         resto = temp % 10;
  124.         temp /= 10;
  125.         if( resto < 0 || resto > base )
  126.             return 0;
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement