Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //============================================================================
- // Name : CambiaBases.cpp
- // Author : Miguel Angel Pau Diaz
- // Version :
- // Copyright : Fundamentos de Programación 2010
- // Description : Cambio de bases de números
- //============================================================================
- #include <iostream>
- #include <stdlib.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <math.h>
- using namespace std;
- //*** Declaración de funciones ***
- bool CheckInput (string &data, int bas); //Comprueba entrada de datos
- string deBaseaBase (string &num, int bas1, int bas2); //Convierte de una base a otra
- float Pot (int bas, int pot); //x^y
- char NDigit (string &num, int dig);
- //********************************
- int main(int argc, char* argv[])
- {
- int base1, base2, digito;
- char cm, odigit;
- string input, output;
- do {
- system("cls");
- cout << "\n TRATAMIENTO NUMERICO ENTRE BASES" << endl;
- cout << "\n Seleccione opcion:" << endl;
- cout << "\n C - Conversion Base - Base" << endl;
- cout << " M - Mostrar i-esimo digito en otra base" << endl;
- cin >> cm;
- }while (cm!='C' && cm!='c' && cm!='M' && cm!='m');
- switch (cm){
- case 'C': case 'c':
- do{
- do {
- system ("cls");
- cout << "\n CONVERSION BASE - BASE " << endl;
- cout << "\n Introduce un numero en cualquier base entre 1 y 36: ";
- cin >> input;
- cout << "\n Indica su base (1-16): ";
- cin >> base1;
- }while (CheckInput(input, base1)==false);
- cout << "\n Indica la base a la que quieres convertirlo (1-36): ";
- cin >> base2;
- }while (base2<0 && base2>36);
- output = deBaseaBase(input, base1, base2);
- cout << "\n "<<input << " (base " << base1 << ") = " << output << " (base " << base2 << ")" << endl << endl;
- break;
- case 'M': case 'm':
- do{
- do{
- do {
- system ("cls");
- cout << "\n DETERMINACION DEL I-ESIMO DIGITO EN OTRA BASE " << endl;
- cout << "\n Introduce un numero en cualquier base entre 1 y 36: ";
- cin >> input;
- cout << "\n Indica su base (1-16): ";
- cin >> base1;
- }while (CheckInput(input, base1)==false);
- cout << "\n Indica la base a la que quieres convertirlo (1-36): ";
- cin >> base2;
- }while (base2<0 && base2>36);
- cout << "\n Indica un dígito a mostrar en la nueva base (0-40): ";
- cin >> digito;
- }while (digito<0 || digito>40);
- output = deBaseaBase(input, base1, base2);
- odigit = NDigit(output, digito);
- //cout << "\n "<<input << " (base " << base1 << ") = " << output << " (base " << base2 << ")" << endl;
- cout << "\n Digito " << digito << ": " << odigit << endl << endl;
- break;
- }
- system("pause");
- return 0;
- }
- //*********************************************************
- // CheckInput: Verifica datos de entrada
- // data - cadena alfa con datos de entrada
- // bas - base del número data
- //
- // retorno: true - datos correctos
- // false - error en entrada de datos
- // data - reformateada a mayúsculas
- //*********************************************************
- bool CheckInput (string &data, int bas){
- bool reti=true;
- int acheck=54; // ASCII en función de la base [A=10 => ASCII(54+11)]
- if (bas<1 || bas>36)
- reti=false;
- else {
- for (int i=data.size()-1; i>=0; i--){
- data[i] = toupper(data[i]); // A mayúsculas
- }
- for (int i=data.size()-1; i>=0; i--){
- if (data[i]>47 && data[i]<58){ // Comprueba si es número (ASCII 48 a 57)
- if (bas==1){
- if ((data[i]-48)==bas)
- reti=true; //trues no necesarios, se dejan por claridad (*)
- else
- reti=false;
- }else{
- if ((data[i]-48)<bas) // Los dígitos menores que la base, i.e. bin 01
- reti=true; //(*)
- else
- reti=false;
- }
- }else if ((data[i]>64 && data[i]<=acheck+bas)) // Digitos < base
- reti=true; //(*)
- else
- reti=false;;
- if (reti==false) break; //Para salir del bucle si error en datos
- }
- }
- return reti;
- }
- //*********************************************************
- // deBaseaBase: Convierte un número de una base a otra
- // num - cadena alfa con numero codificado en base bas1
- // bas1 - base del número origen
- // bas2 - base del número destino
- // retorno: cadena alfa con número convertido a base bas2
- //*********************************************************
- string deBaseaBase (string &num, int bas1, int bas2){
- int potencia=0;
- int acheck=54; // ASCII en función de la base [A = ASCII(54+11)]
- long int acum=0;
- string sBase="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- string sPila="", sResult="";
- //Primero convertimos a decimal
- if (bas1==1)
- acum=num.size(); //cadena de "1s" de longitud N
- else{
- for (int i=num.size()-1; i>=0; i--){
- if (num[i]>47 && num[i]<58){
- acum+=(num[i]-48)*Pot(bas1,potencia);
- potencia++;
- }else if (num[i]>64 && num[i]<=acheck+bas1){
- acum+=(num[i]-55)*Pot(bas1,potencia);
- potencia++;
- }else{
- sResult="Err";
- return sResult;
- }
- }
- }
- // Luego convertimos a segunda base
- if (bas2==1){
- while (acum>0){
- sResult+= "1";
- acum--;
- }
- }else{
- while (acum>0){
- sPila+=sBase[acum%bas2]; // Se apilan en orden inverso
- acum/=bas2;
- }
- for (int i=sPila.size()-1; i>=0; i--){
- sResult+=sPila[i]; //Invierte orden de los dígitos
- }
- }
- return sResult;
- }
- //*********************************************************
- // NDigit: Determina un dígito de un numero X^Y
- // bas - base
- // pot - potencia
- //
- // retorno: X^Y
- //*********************************************************
- char NDigit (string &num, int dig){
- char cResult='0';
- int size;
- size=num.size()-1;
- if (dig>size){
- return cResult;
- }
- else{
- cResult=num[size-dig];
- }
- return cResult;
- }
- //*********************************************************
- // Pot: X^Y
- // bas - base
- // pot - potencia
- //
- // retorno: X^Y
- //*********************************************************
- float Pot (int bas, int pot){
- float bep;
- if (pot==0)
- bep=1;
- else{
- bep=1;
- for (int i=1; i<=pot; i++){
- bep*=bas;
- }
- }
- return bep;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement