Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // By Leandro Soares
- #include <stdio.h>
- #include "Validations.h"
- #define TRUE 1
- #define MAX 99999
- #define MIN 0
- #define BIN 2
- #define OCT 8
- #define DEC 10
- int _Elev(int number, int power); // Funcao similar a pow(...);
- int ToBinOct(int numero, unsigned base); // Funcao que converte decimal para octal ou binario
- int ValidateBOD(int numero, int base); // Validacao dos numeros e bases.
- int ToDec(int numero, int base); // Funcao que converte binario e ou octal para decimal.
- int main()
- {
- int numero, base1, base2, resultado=0;
- printf("\nNumero, base1, base2: ");
- fflush(stdin);
- scanf("%d, %d, %d", &numero, &base1, &base2); // Leitura numero, base1 e base2
- // Verificacao do numero lido e das bases.
- if(!_ValSupInf(numero, MIN, MAX) || ValidateBOD(numero, base1) == 0 || ValidateBOD(NULL, base2) == -1 )
- {
- printf("\n\nERRO\n\n");
- return 0;
- }
- if( base1 == base2 ) // Se a base1 for igual a base2, entao o resultado é igual ao numero.
- resultado = numero;
- else
- {
- switch(base1)
- {
- case BIN:
- switch(base2)
- {
- case OCT: resultado = ToDec(numero, BIN); // Bin to Dec.
- resultado = ToBinOct(resultado, OCT); break; // Dec to Oct.
- case DEC: resultado = ToDec(numero, BIN); break; // Bin to Dec.
- }
- break;
- case OCT:
- switch(base2)
- {
- case BIN: resultado = ToDec(numero, OCT); // Oct to Dec.
- resultado = ToBinOct(resultado, BIN); break; // Dec to Bin.
- case DEC: resultado = ToDec(numero, OCT); break; // Oct to Dec.
- }
- break;
- case DEC:
- switch(base2)
- {
- case BIN: resultado = ToBinOct(numero, BIN);break; // Dec ti Bin.
- case OCT: resultado = ToBinOct(numero, OCT);break; // Dec to Oct.
- }
- break;
- }
- }
- printf("\n\nResultado: %d\n\n\n", resultado);
- return 0;
- }
- int _Elev(int number, int power) // funcao similar a funcao pow(...);
- {
- int quad=1, c;
- for(c=1;c <= power;c++)
- quad *= number;
- return quad;
- }
- int ToDec(int numero, int base) // Funcao que converte binario e ou octal para decimal.
- {
- int resto, fac, spin, resultado, soma, temp;
- resto=spin=fac=resultado=soma=0;
- temp=numero;
- for(spin=0;temp != 0;spin++)
- {
- resto = temp % 10; // Resto do numero( ultimo digito ).
- temp /= 10;
- fac = _Elev(base, spin); // Elevar esse numero a sua potencia.
- if( base == 2 )
- {
- if( resto == 1 ) // Se a base for igual a 2 e o resto igual 1.
- soma += fac;
- }
- else // Se a base for 8...
- {
- resultado = resto * fac;
- soma += resultado;
- }
- }
- return soma;
- }
- int ToBinOct(int numero, unsigned base) // Funcao que converte decimal para octal ou binario
- {
- int temp, resto, factor;
- resto = 0;
- temp = numero;
- for(factor = 1; temp != 0; factor *= 10)
- {
- resto = (temp % base) * factor + resto;
- temp /= base;
- }
- return resto;
- }
- int ValidateBOD(int numero, int base) // Funcao que valida um numero e uma base(decimal, octal ou binaria).
- {
- int resto = 0, temp;
- temp = numero;
- if( base != 2 && base != 8 && base != 10 )
- return -1;
- if( base == 10 )
- return 1;
- while(temp != 0)
- {
- resto = temp % 10;
- temp /= 10;
- if( resto < 0 || resto > base )
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement