Bonjour à la communauté developpez.net

Alors voilà, je fais des études en classe préparatoire mathématiques supplémentaires, je débute depuis moins d'une semaine en programmation sur c++ et j'essaie de faire un exercice de cours que j'ai bien avancé mais sur lequel je bloque par manque d'expérience.

Je dois produire un algorithme et le coder en c++. Cet exercice a pour objectif de convertir un entier en base 10 vers la base 16 et inversement à travers un petit menu. A noter qu'il faut que je mémorise les digits acceptés en base 16 dans un vecteur de type caractère et que ce vecteur servira pour les deux sens de conversion.

Algorithme:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
programme conversion
	choix : caractere
	hexadecimal : chaîne
	entiernaturel, k : entier
debut
	// boucle sur le menu
	choix <- "Z"
	tantque choix <> "Q" et choix <> "q"
 
		// afficher le menu
		afficher "conversion base 10 en base 16.....1"
		afficher "conversion base 16 en base 10.....2"
		afficher "quitter l'application.............Q"
		saisir choix
 
		// conversion de base 10 en base 16
		si choix = "1" alors
			hexadecimal <- ""
			afficher "entrer un entier naturel = "
			saisir entiernaturel
			tantque entiernaturel <> 0
				hexadecimal <- str(entiernaturel mod 16) + hexadecimal
				entiernaturel <- entiernaturel div 16
			fintantque
			afficher "conversion en hexadecimal = " + hexadecimal
		sinon
 
			// conversion de base 16 en base 10
			si choix = "2" alors
				entiernaturel <- 0
				k <- 0
				afficher "entrer un nombre hexadecimal = "
				saisir hexadecimal
 
				tantque taille(hexadecimal) > 0
					entiernaturel <- entiernaturel + val(droite(hexadecimal, 1)) * 16^k
					hexadecimal <- gauche(hexadecimal, taille(hexadecimal)-1)
					k <- k + 1
				fintantque
				afficher "conversion en base 10 = " + entiernaturel
			finsi
		finsi
 
	fintantque
fin
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* programme conversion
*  but : convertir un entier naturel en hexadecimal et inversement
*  auteur : td
*  date : 03/01/2017
*/
 
#include <iostream>
#include <math.h>
#include <string>
#include <sstream>
 
using namespace std;
 
int main()
{
    char choix ;
    string hexadecimal ;
    int entiernaturel ;
 
    // boucle sur le menu
    choix = 'Z' ;
    while (choix != 'Q' && choix != 'q')
    {
 
        // afficher le menu
        cout << endl << "conversion base 10 en base 16 .......... 1" ;
        cout << endl << "conversion base 16 en base 10 .......... 2" ;
        cout << endl << "quitter l'application................... Q" ;
        cout << endl << "votre choix ........................... " ;
        cin >> choix ;
 
        // conversion base 10 en base 16
        if (choix == '1')
        {
            hexadecimal = "" ;
            cout << "entrer un entier naturel = " ;
            cin >> entiernaturel ;
            while (entiernaturel != 0)
            {
                // récupération du reste et conversion en caractère
                char c = (entiernaturel%16) + '0' ;
                // concaténation du caractère
                hexadecimal = c + hexadecimal ;
                entiernaturel = entiernaturel / 16 ;
            }
            cout << "conversion en hexadecimal = " + hexadecimal ;
        }
        else
        {
 
            // conversion base 16 en base 10
            if (choix == '2')
            {
                entiernaturel = 0 ;
                int k = 0 ;
                cout << "entrer un nombre hexadecimal = " ;
                cin >> hexadecimal ;
                while (hexadecimal.size() > 0)
                {
                    // extration du dernier caractère du nombre hexadecimal
                    string s = hexadecimal.substr(hexadecimal.size()-1, 1) ;
                    istringstream myStream(s);
                    int result ;
                    // conversion du caractère en entier naturel
                    myStream>>result ;
                    // ajout dans entiernaturel multiplié par la puissance de 16
                    entiernaturel += result * pow(16, k) ;
                    // on enlève le dernier caractère
                    hexadecimal = hexadecimal.substr(0, hexadecimal.size()-1) ;
                    k++ ;
                }
                cout << "conversion en base 10 = " << entiernaturel ;
            }
 
        }
 
     }
 
 
    return 0;
}
Merci d'avance!

debprog01