begin process at 2012 02 09 06:38:38
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Maths & Algorithmes

 > [VC++]CONVERSION ROMAIN->DÉCIMAL ET INVERSE

[VC++]CONVERSION ROMAIN->DÉCIMAL ET INVERSE


 Information sur la source

Note :
6 / 10 - par 3 personnes
6,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :Maths & Algorithmes Niveau :Débutant Date de création :26/05/2003 Date de mise à jour :26/05/2003 19:16:57 Vu / téléchargé :3 833 / 66

Auteur : camel

Ecrire un message privé
Site perso
Commentaire sur cette source (4)
Ajouter un commentaire et/ou une note

 Description

Voila 2 fonctions : Int2Roman et Roman2Int. Je rapelle (car je me suis renseigné sur un site) que le vrai nombre maximal en chiffre romains est 4988). Il peut y avoir 4 milles, 1 cinq-cents, 4 cents, 1 cinquante, 3 dix, 1 cinq et 3 un. J'espère être assez compréhensible ! Enfin la meilleure des choses c'est d'essayer le prog ou de matter la source @+ :)
Ah au fait, j'attends vos commentaires :)

Source

  • #include <stdlib.h>
  • #include <stdio.h>
  • #include <string.h>
  • struct s_roman
  • {
  • char roman_char;
  • short roman_int;
  • };
  • char* Int2Roman (int);
  • int Roman2Int (char*);
  • char j, debug;
  • s_roman roman[7];
  • int main (int argc, char *argv[])
  • {
  • char roman_nb[64];
  • short nombre = 0;
  • strcpy (roman_nb, "");
  • if ( (argc > 1) && (strcmp(argv[1], "debug=1") == 0) ) debug = 1;
  • /* INITIALISATION S_ROMAN */
  • roman[0].roman_char = 'M';
  • roman[1].roman_char = 'D';
  • roman[2].roman_char = 'C';
  • roman[3].roman_char = 'L';
  • roman[4].roman_char = 'X';
  • roman[5].roman_char = 'V';
  • roman[6].roman_char = 'I';
  • for (j=0 ; j<7 ; j++) roman[j].roman_int = 0;
  • printf ("* Int2Roman\nEntrez un nombre entier <= 4988 : ");
  • while ( (nombre <= 0) || (nombre > 4988) )
  • {
  • scanf ("%d", &nombre); flushall();
  • }
  • printf ("Nombre converti : %s\n", Int2Roman(nombre) );
  • printf ("* Roman2Int\nEntrez un nombre romain <= MMMMDCCCCLXXXVIII : ");
  • gets (roman_nb);
  • printf ("Nombre romain converti : %d\n", Roman2Int(roman_nb) );
  • system ("pause");
  • return (0);
  • }
  • char* Int2Roman (int nombre)
  • {
  • char *roman_nb = new char[64];
  • char i = 0;
  • strcpy (roman_nb, "");
  • while (nombre - 1000 >= 0)
  • {
  • nombre = nombre - 1000;
  • strncat (roman_nb, "M", 1);
  • i++;
  • if (debug == 1) printf ("roman_nb=%s nombre=%d i=%d\n", roman_nb, nombre, i);
  • }
  • while (nombre - 500 >= 0)
  • {
  • nombre = nombre - 500;
  • strncat (roman_nb, "D", 1);
  • i++;
  • if (debug == 1) printf ("roman_nb=%s nombre=%d i=%d\n", roman_nb, nombre, i);
  • }
  • while (nombre - 100 >= 0)
  • {
  • nombre = nombre - 100;
  • strncat (roman_nb, "C", 1);
  • i++;
  • if (debug == 1) printf ("roman_nb=%s nombre=%d i=%d\n", roman_nb, nombre, i);
  • }
  • while (nombre - 50 >= 0)
  • {
  • nombre = nombre - 50;
  • strncat (roman_nb, "L", 1);
  • i++;
  • if (debug == 1) printf ("roman_nb=%s nombre=%d i=%d\n", roman_nb, nombre, i);
  • }
  • while (nombre - 10 >= 0)
  • {
  • nombre = nombre - 10;
  • strncat (roman_nb, "X", 1);
  • i++;
  • roman[4].roman_int++;
  • if (roman[4].roman_int == 4)
  • {
  • roman_nb[i-4] = 'X';
  • roman_nb[i-3] = 'L';
  • roman_nb[i-2] = '\0';
  • i = i - 2;
  • }
  • if (debug == 1) printf ("roman_nb=%s nombre=%d i=%d\n", roman_nb, nombre, i);
  • }
  • while (nombre - 5 >= 0)
  • {
  • nombre = nombre - 5;
  • strncat (roman_nb, "V", 1);
  • i++;
  • if (debug == 1) printf ("roman_nb=%s nombre=%d i=%d\n", roman_nb, nombre, i);
  • }
  • while (nombre - 1 >= 0)
  • {
  • nombre = nombre - 1;
  • strncat (roman_nb, "I", 1);
  • i++;
  • roman[6].roman_int++;
  • if (roman[6].roman_int == 4)
  • {
  • roman_nb[i-4] = 'I';
  • roman_nb[i-3] = 'V';
  • roman_nb[i-2] = '\0';
  • i = i - 2;
  • if (roman_nb[i-3] == 'V')
  • {
  • roman_nb[i-3] = 'I';
  • roman_nb[i-2] = 'X';
  • roman_nb[i-1] = '\0';
  • i = i - 1;
  • }
  • }
  • if (debug == 1) printf ("roman_nb=%s nombre=%d i=%d\n", roman_nb, nombre, i);
  • }
  • for (j=0 ; j<7 ; j++) roman[j].roman_int = 0;
  • return (roman_nb);
  • }
  • // MDCLXVI
  • int Roman2Int (char* roman_nb)
  • {
  • short nombre = 0;
  • char i = 0;
  • while (roman_nb[i] != '\0')
  • {
  • if ( (roman_nb[i] != 'M') && (roman_nb[i] != 'D') && (roman_nb[i] != 'C')
  • && (roman_nb[i] != 'L') && (roman_nb[i] != 'X') && (roman_nb[i] != 'V')
  • && (roman_nb[i] != 'I') ) return (0);
  • if (roman_nb[i] == 'M')
  • {
  • if (roman[0].roman_int == 4) return (0);
  • nombre = nombre + 1000;
  • roman[0].roman_int++;
  • }
  • if (roman_nb[i] == 'D')
  • {
  • if (roman[1].roman_int == 1) return (0);
  • nombre = nombre + 500;
  • roman[1].roman_int++;
  • }
  • if (roman_nb[i] == 'C')
  • {
  • if (roman[2].roman_int == 4) return (0);
  • nombre = nombre + 100;
  • roman[2].roman_int++;
  • }
  • if (roman_nb[i] == 'L')
  • {
  • if (roman_nb[i-1] == 'X')
  • {
  • nombre = nombre - 10;
  • nombre = nombre + 40;
  • roman[3].roman_int++;
  • }
  • else
  • {
  • if (roman[3].roman_int == 4) return (0);
  • nombre = nombre + 50;
  • roman[3].roman_int++;
  • }
  • }
  • if (roman_nb[i] == 'X')
  • {
  • if (roman_nb[i-1] == 'I')
  • {
  • nombre = nombre - 1;
  • nombre = nombre + 9;
  • roman[4].roman_int++;
  • }
  • else
  • {
  • if (roman[4].roman_int == 4) return (0);
  • nombre = nombre + 10;
  • roman[4].roman_int++;
  • }
  • }
  • if (roman_nb[i] == 'V')
  • {
  • if (roman_nb[i-1] == 'I')
  • {
  • nombre = nombre - 1;
  • nombre = nombre + 4;
  • roman[5].roman_int++;
  • }
  • else
  • {
  • if (roman[5].roman_int == 2) return (0);
  • nombre = nombre + 5;
  • roman[5].roman_int++;
  • }
  • }
  • if (roman_nb[i] == 'I')
  • {
  • if (roman[6].roman_int == 4) return (0);
  • nombre = nombre + 1;
  • roman[6].roman_int++;
  • }
  • i++;
  • }
  • for (j=0 ; j<7 ; j++) roman[j].roman_int = 0;
  • return (nombre);
  • }
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct s_roman
{
	char	roman_char;
	short	roman_int;
};

char*		Int2Roman (int);
int			Roman2Int (char*);

char		j, debug;
s_roman		roman[7];

int		main (int argc, char *argv[])
{
	char	roman_nb[64];
	short	nombre = 0;

	strcpy (roman_nb, "");

	if ( (argc > 1) && (strcmp(argv[1], "debug=1") == 0) ) debug = 1;

	/* INITIALISATION S_ROMAN */
	roman[0].roman_char = 'M';
	roman[1].roman_char = 'D';
	roman[2].roman_char = 'C';
	roman[3].roman_char = 'L';
	roman[4].roman_char = 'X';
	roman[5].roman_char = 'V';
	roman[6].roman_char = 'I';
	for (j=0 ; j<7 ; j++) roman[j].roman_int = 0;


	printf ("* Int2Roman\nEntrez un nombre entier <= 4988 : ");
	while ( (nombre <= 0) || (nombre > 4988) )
	{
		scanf ("%d", &nombre); flushall();
	}
	printf ("Nombre converti : %s\n", Int2Roman(nombre) );

	printf ("* Roman2Int\nEntrez un nombre romain <= MMMMDCCCCLXXXVIII : ");
	gets (roman_nb);
	printf ("Nombre romain converti : %d\n", Roman2Int(roman_nb) );

	system ("pause");

	return (0);
}

char*		Int2Roman (int nombre)
{
	char	*roman_nb = new char[64];
	char	i = 0;

	strcpy (roman_nb, "");

	while (nombre - 1000 >= 0) 
	{
		nombre = nombre - 1000;
		strncat (roman_nb, "M", 1);
		i++;
		if (debug == 1) printf ("roman_nb=%s nombre=%d i=%d\n", roman_nb, nombre, i);
	}

	while (nombre - 500 >= 0) 
	{
		nombre = nombre - 500;
		strncat (roman_nb, "D", 1);
		i++;
		if (debug == 1) printf ("roman_nb=%s nombre=%d i=%d\n", roman_nb, nombre, i);
	}

	while (nombre - 100 >= 0) 
	{
		nombre = nombre - 100;
		strncat (roman_nb, "C", 1);
		i++;

		if (debug == 1) printf ("roman_nb=%s nombre=%d i=%d\n", roman_nb, nombre, i);
	}

	while (nombre - 50 >= 0) 
	{
		nombre = nombre - 50;
		strncat (roman_nb, "L", 1);
		i++;

		if (debug == 1) printf ("roman_nb=%s nombre=%d i=%d\n", roman_nb, nombre, i);
	}

	while (nombre - 10 >= 0) 
	{
		nombre = nombre - 10;
		strncat (roman_nb, "X", 1);
		i++;

		roman[4].roman_int++;

		if (roman[4].roman_int == 4)
		{
			roman_nb[i-4] = 'X';
			roman_nb[i-3] = 'L';
			roman_nb[i-2] = '\0';
			i = i - 2;
		}

		if (debug == 1) printf ("roman_nb=%s nombre=%d i=%d\n", roman_nb, nombre, i);
	}

	while (nombre - 5 >= 0) 
	{
		nombre = nombre - 5;
		strncat (roman_nb, "V", 1);
		i++;
		if (debug == 1) printf ("roman_nb=%s nombre=%d i=%d\n", roman_nb, nombre, i);
	}

	while (nombre - 1 >= 0) 
	{
		nombre = nombre - 1;
		strncat (roman_nb, "I", 1);
		i++;

		roman[6].roman_int++;

		if (roman[6].roman_int == 4)
		{
			roman_nb[i-4] = 'I';
			roman_nb[i-3] = 'V';
			roman_nb[i-2] = '\0';

			i = i - 2;

			if (roman_nb[i-3] == 'V')
			{
				roman_nb[i-3] = 'I';
				roman_nb[i-2] = 'X';
				roman_nb[i-1] = '\0';

				i = i - 1;
			}
		}

		if (debug == 1) printf ("roman_nb=%s nombre=%d i=%d\n", roman_nb, nombre, i);
	}
	
	for (j=0 ; j<7 ; j++) roman[j].roman_int = 0;
	return (roman_nb);
}

// MDCLXVI

int			Roman2Int (char* roman_nb)
{
	short	nombre = 0;
	char	i = 0;
	
	while (roman_nb[i] != '\0') 
	{
		if ( (roman_nb[i] != 'M') && (roman_nb[i] != 'D') && (roman_nb[i] != 'C') 
			&& (roman_nb[i] != 'L') && (roman_nb[i] != 'X') && (roman_nb[i] != 'V') 
			&& (roman_nb[i] != 'I') ) return (0);

		if (roman_nb[i] == 'M')
		{
			if (roman[0].roman_int == 4) return (0);

			nombre = nombre + 1000;
			roman[0].roman_int++;
		}

		if (roman_nb[i] == 'D')
		{
			if (roman[1].roman_int == 1) return (0);

			nombre = nombre + 500;
			roman[1].roman_int++;
		}
		
		if (roman_nb[i] == 'C')
		{
			if (roman[2].roman_int == 4) return (0);

			nombre = nombre + 100;
			roman[2].roman_int++;
		}

		if (roman_nb[i] == 'L')
		{
			if (roman_nb[i-1] == 'X')
			{
				nombre = nombre - 10;
				nombre = nombre + 40;
				roman[3].roman_int++;
			}
			else
			{
				if (roman[3].roman_int == 4) return (0);
			
				nombre = nombre + 50;
				roman[3].roman_int++;
			}
		}

		if (roman_nb[i] == 'X')
		{
			if (roman_nb[i-1] == 'I')
			{
				nombre = nombre - 1;
				nombre = nombre + 9;
				roman[4].roman_int++;
			}
			else
			{
				if (roman[4].roman_int == 4) return (0);
			
				nombre = nombre + 10;
				roman[4].roman_int++;
			}
		}

		if (roman_nb[i] == 'V')
		{
			if (roman_nb[i-1] == 'I')
			{
				nombre = nombre - 1;
				nombre = nombre + 4;
				roman[5].roman_int++;
			}
			else
			{
				if (roman[5].roman_int == 2) return (0);
			
				nombre = nombre + 5;
				roman[5].roman_int++;
			}
		}

		if (roman_nb[i] == 'I')
		{
			if (roman[6].roman_int == 4) return (0);
			
			nombre = nombre + 1;
			roman[6].roman_int++;
		}

		i++;
	}
	
	for (j=0 ; j<7 ; j++) roman[j].roman_int = 0;
	return (nombre);
}

 Conclusion

En espérant que ca marche bien car ya tellement de nombres à tester !

 Fichier Zip

Les Membres Club peuvent télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip


 Sources du même auteur

Source avec Zip [VC++] CUBE 3D QUI TOURNE AVEC OPENGL/GLUT
Source avec Zip [VC++6] CRYPTAGE VIGENERE UTILISANT UNE CLASS

 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

Commentaire de camel le 01/06/2003 16:13:22

vos commentaires svp !

Commentaire de Mindiell le 09/07/2003 08:31:45

C'est quoi cette histoire de nombre romain maximal ???

Commentaire de magic_Nono le 09/06/2005 14:48:20

pas mal

a propos, C plutot du 'unsigned int' que du 'int'

jamais vu de négatif ou de nul ds en nb romain

sinon,
pourrais tu stp citer ta source de 'nombre romain maximal'
stp

thx
B

Commentaire de camel le 11/03/2007 11:01:48

Désolé, à l'époque j'étais encore jeune et impétueux, je devais pas avoir compris quelque chose. Là j'ai fais des recherches et je suis tombé sur ça :

http://pedroiy.free.fr/alphabets/romain.htm

Donc ce que j'avais compris comme "plus grand nombre romain existant" était en fait le "plus long nombre romain que l'on puisse écrire".

Autant pour moi ;)

 Ajouter un commentaire




Nos sponsors


Sondage...

CalendriCode

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

Photothèque

 
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 : 1,264 sec (3)

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