begin process at 2012 05 27 14:15:28
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Maths & Algorithmes

 > CHANGEMENT DE BASES (2 À 36)

CHANGEMENT DE BASES (2 À 36)


 Description

Compilé avec Borland 5.5

Source

  • #include <iostream.h>
  • #include <conio.h>
  • #include <stdlib.h>
  • void Base(const char *nombre, const int base1, const int base2)
  • {
  • int ascii, i, j = 0, len = 0 ;
  • long d = 1, e = 0 ;
  • char *res, c ;
  • if((res = (char *) malloc(sizeof(char)))==NULL)
  • {
  • cerr << " Allocation memoire impossible !" ;
  • exit(1) ;
  • }
  • while(nombre[len+1]!='\0')
  • {
  • len++ ;
  • }
  • // Conversion de base1 en base 10
  • for(i=len;i>=0;i--)
  • {
  • ascii = nombre[i];
  • if((ascii<48)||(ascii>122))
  • {
  • cout << "Caractere inconnu." ;
  • getch() ;
  • exit(2) ;
  • }
  • else if(ascii>=97)
  • {
  • ascii = ascii - 87 ;
  • }
  • else if(ascii>=65)
  • {
  • ascii = ascii - 55 ;
  • }
  • else
  • {
  • ascii = ascii - 48 ;
  • }
  • e = e + (ascii * d) ;
  • d = d * base1 ;
  • }
  • // Conversion de base 10 en base2
  • while(e!=0)
  • {
  • d = e % base2 ;
  • e = (e - d)/base2 ;
  • if(d>9)
  • {
  • ascii = 55 + d ;
  • }
  • else
  • {
  • ascii = 48 + d ;
  • }
  • res[j] = char(ascii) ;
  • j++ ;
  • if((res = (char *) realloc(res, j*sizeof(char)))==NULL)
  • {
  • cerr << "Agrandissement du bloc impossible" ;
  • exit(3) ;
  • }
  • }
  • res[j] = '\0' ;
  • for(i=0;i<=(j-1)/2;i++)
  • {
  • c = res[i] ;
  • res[i] = res[j-i-1] ;
  • res[j-i-1] = c ;
  • }
  • cout << res ;
  • }
  • void main()
  • {
  • clrscr() ;
  • int b1, b2 , buffer=0, i=0 ;
  • char *m ;
  • if((m = (char *) malloc(sizeof(char)))==NULL)
  • {
  • cerr << " Allocation memoire impossible !" ;
  • exit(4) ;
  • }
  • cout << "Base de 2 a 36\n\nNombre a convertir ?\n" ;
  • while(buffer!=13)
  • {
  • if((m = (char *) realloc(m, (i+1)*sizeof(char)))==NULL)
  • {
  • cerr << "Agrandissement du bloc impossible" ;
  • exit(5) ;
  • }
  • buffer = getche() ;
  • m[i] = char(buffer) ;
  • i++ ;
  • }
  • m[i-1] = '\0' ;
  • while(getchar()!='\n') ;
  • cout << "Base de depart ?\n" ;
  • cin >> b1 ;
  • cout << "Base d\'arrivee ?\n" ;
  • cin >> b2 ;
  • cout << "\n\t" << m << " (" << b1 << ") --> " ;
  • Base(m, b1, b2) ;
  • cout << " (" << b2 << ")\n" ;
  • getch();
  • }
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>

void Base(const char *nombre, const int base1, const int base2)
{
	int ascii, i, j = 0, len = 0 ;
	long d = 1, e = 0 ;
	char *res, c ;
	if((res = (char *) malloc(sizeof(char)))==NULL)
	{
		cerr << " Allocation memoire impossible !" ;
		exit(1) ;
	}


	while(nombre[len+1]!='\0')
	{
		len++ ;
	}

	// Conversion de base1 en base 10
	for(i=len;i>=0;i--)
	{
		ascii = nombre[i];

		if((ascii<48)||(ascii>122))
		{
			cout << "Caractere inconnu." ;
			getch() ;
			exit(2) ;
		}
		else if(ascii>=97)
		{
			ascii = ascii - 87 ;
		}
		else if(ascii>=65)
		{
			ascii = ascii - 55 ;
		}
		else
		{
			ascii = ascii - 48 ;
		}

		e = e + (ascii * d) ;
		d = d * base1 ;
	}


	// Conversion de base 10 en base2
	while(e!=0)
	{
		d = e % base2 ;
		e = (e - d)/base2 ;
		if(d>9)
		{
			ascii = 55 + d ;
		}
		else
		{
			ascii = 48 + d ;
		}
		res[j] = char(ascii) ;
		j++ ;
		if((res = (char *) realloc(res, j*sizeof(char)))==NULL)
		{
			cerr << "Agrandissement du bloc impossible" ;
			exit(3) ;
		}
	}
	res[j] = '\0' ;

	for(i=0;i<=(j-1)/2;i++)
	{
		c = res[i] ;
		res[i] = res[j-i-1] ;
		res[j-i-1] = c ;
	}

	cout << res ;
}


void main()
{
	clrscr() ;
	int b1, b2 , buffer=0, i=0 ;
	char *m ;
	if((m = (char *) malloc(sizeof(char)))==NULL)
	{
		cerr << " Allocation memoire impossible !" ;
		exit(4) ;
	}

	cout << "Base de 2 a 36\n\nNombre a convertir ?\n" ;
	while(buffer!=13)
	{
		if((m = (char *) realloc(m, (i+1)*sizeof(char)))==NULL)
		{
			cerr << "Agrandissement du bloc impossible" ;
			exit(5) ;
		}
		buffer = getche() ;
		m[i] = char(buffer) ;
		i++ ;
	}
	m[i-1] = '\0' ;
	while(getchar()!='\n') ;

	cout << "Base de depart ?\n" ;
	cin >> b1 ;
	cout << "Base d\'arrivee ?\n" ;
	cin >> b2 ;


	cout << "\n\t" << m << " (" << b1 << ")  -->  " ;
	Base(m, b1, b2) ;
	cout << " (" << b2 << ")\n" ;


	getch();
}
 

 Conclusion

Le déroulement du changement de bases est assez simple et se décompose en  deux parties.

Si on entre un nombre de la forme :
a[n]a[n-1]...a[2]a[1]a[0] en base b1 (où 0&lt;=a[i]&lt;=b1)

le nombre en base 10 s'écrit sous la forme :
a[n]*b1^(n)+a[n-1]*b1^(n-1)+...+a[1]*b1^(1)+a[0] *b1^(0)

Puis, on convertit le nombre en base2
en calculant les plus petits termes d'abord, i.e. on obtient :
c[0]c[1]...c[p] (où (où 0&lt;=c[i]&lt;=b2)

que l'on réordonne en :
c[p]...c[1]c[0]


 Sources du même auteur

TRANSFORMER UN INT/DOUBLE... EN CHAINE DE CARACTÈRES DE TYPE...
Source avec Zip ICONE DANS LA BARRE DES TACHES AVEC MENU (VC++)
Source avec Zip SERVEUR/CLIENT SOUS WINDOWS EN MODE CONSOLE (VC++,DEVCPP,BOR...
Source avec Zip CLASSE DE CALCUL MATRICIEL (VC++ ET DEVCPP)
CLASSE DE CALCUL MATRICIEL (VC++ ET DEVCPP)

 Sources de la même categorie

Source avec Zip UN EXAMPLE D'APPLICATION EN CUDA DE L'ALGORITHME DE SCAN POU... par oguzaras
Source avec Zip Source avec une capture CHIFFREMENT DE VIGENERE par lajouad
Source avec Zip Source avec une capture ANALYSE SYNTAXIQUE par lajouad
Source avec Zip Source avec une capture STRUCTURE D'UNE MATRICE PAR LES LISTE LINÉAIRE (NON CONTUGUS... par benzarabel
Source avec Zip Source avec une capture DESSINER UNE ARBRE BINAIRE( MODE CONSOLE): par benzarabel

Commentaires et avis

Aucun commentaire pour le moment.

 Ajouter un commentaire




Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Mai 2012
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

Consulter la suite du CalendriCode

A découvrir



 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,484 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales