Menu et controle de la saisie d'un entier

Contenu du snippet

La fonction "short int menu(char*, short int, bool);"

La fonction menu()
Prototype:
short int menu(char *, short int, bool)
entrées:
1er paramatre: pointeur vers 1er element d'un vecteur de char comme suit:
char elemmenu[] = "Titre du menu\0Sortie/retour du menu(0)\01er prop(1)\02eme prop(2)\0etc...\0";
2eme parametre: short int qui donne le choix par défaut.
3eme paramatre: bool, si à vrai affiche les messages d'erreurs en cas d'erreur.

sortie: short int, "-1" en cas d'erreur, "0" en pour la sortie/retour, Si >1 donne le numéro de la proposition.

Utilisation:
Fleche du haut/bas pour changer de propsition (ou raccourci avec le pavé numérique pour les choix de 0 à maximum la 9eme propostion, si il y a)
valider avec la touche [ENTER]
Sortie rapide avec la touche [ESC] (choisi sortie/retour, donc retourne "0")

La fonction "int SaisirEntier(char[]);"
retourne un entier saisie au clavier (controle la saisie)

Source / Exemple :


/*******************************************************************************\

  • Nom: Thomas * Lieu: Belgique *
  • Pseudo: boygen * Projet: Bibliotheque de fonctions *
  • E-mail: boygen81@hotmail.com* Compilateur: Microsoft Visual C++ (6.0) *
  • Langage:C & C++ * O.S.: Windows 2000 Professionnel *
  • Fichier:fctperso.cpp * Rapport: (voir commentaires) *
\*******************************************************************************/ /******************************************************************************* les definitions des fonctions= int SaisirEntier(char[]); short int menu(char*, short int, bool);
                                                                                                                                                              • /
// les includes pour toutes les fonctions... #include <conio.h> #include <limits.h> #include <stdio.h> #include <stdlib.h> #include <windows.h> /*******************************************************************************/ // define pour "int SaisirEntier(char[]);" #define KEY_0 (int) '0' // definition des codes touche #define KEY_9 (int) '9' #define KEY_ENTER 13 #define KEY_BKDEL 8 #define KEY_MOINS (int) '-' #define KEY_PLUS (int) '+' /*******************************************************************************/ // define pour "short int menu(char*, short int, bool);" #define NBCOLSRC 80 #define NBLGNSRC 25 #define MENUSELECTED (FOREGROUND_BLUE|FOREGROUND_INTENSITY) #define MENUNOSELECTED (FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE) #define MENUFORM (FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_INTENSITY) #define MENUBK (FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE) #define MENUTXT 0 #define NORMTXT (FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_INTENSITY) #define NORMBK 0 #define ERRORCOLOR (FOREGROUND_RED) #define textattr(color) SetConsoleTextAttribute(STDOUT,color) #define textcolor(color) SetConsoleTextAttribute(STDOUT,back_color|(front_color=(color))) #define textbackground(color) SetConsoleTextAttribute(STDOUT,(back_color=((color)<<4))|front_color) #define HAUT 72 #define BAS 80 #define ESC 27 #define ENTER 13 #define NUMZERO 48 #define NORM (FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE) #define STDOUT GetStdHandle(STD_OUTPUT_HANDLE) WORD front_color=NORM; WORD back_color=0; /*******************************************************************************/ // les fonctions... /*******************************************************************************/ // La fonction "int SaisirEntier(char[]);" int SaisirEntier(char msg[]){ bool sgplus = false, sgmoins = false, zero = false; int entier = 0, keymax; char touche; do{ system("cls"); printf("%s", msg); //affiche la demande if(sgplus) printf("+"); // gestion de l'affichage des signes else if(sgmoins) printf("-"); if(zero) printf("0"); // gestion de l'affichage du zero else if(entier) printf("%d",entier); // sinon de l'entier touche=getch(); // saisie d'un chiffre ou d'une touche commande if(touche == 0) getch(); // vide le buffer (ex: protection contre les touches etendues) if(touche == KEY_BKDEL) { //gère la touche d'effacement if(!entier) {zero?zero = false :sgmoins = sgplus = false;} else entier /= 10; } if(!zero){ // gère les touches de signe if(!entier && !sgmoins && !sgplus){ if(touche == KEY_MOINS) sgmoins = true; else if(touche == KEY_PLUS) sgplus = true; } if(entier <= (INT_MAX / 10)){ // gere la taille mx de l'entier if(touche == KEY_0 && !entier) zero = true; else{ keymax = KEY_9; if(entier == (INT_MAX / 10)) keymax = KEY_0 + (INT_MAX % 10); if(touche >= KEY_0 && touche <= keymax) entier = (entier * 10) + (touche - KEY_0); // ajoute un chiffre au nombre } } } }while(touche != KEY_ENTER || (!entier && !zero)); // condition de sortie if(sgmoins) entier = -entier; // adapte la valeur en fonction du signe return entier; // retourne la valeur } /*******************************************************************************/ // La fonction "short int menu(char*, short int, bool);" /* La fonction menu() Prototype: short int menu(char *, short int, bool) entrées: 1er paramatre: pointeur vers 1er element d'un vecteur de char comme suit: char elemmenu[] = "Titre du menu\0Sortie/retour du menu(0)\01er prop(1)\02eme prop(2)\0etc...\0"; 2eme parametre: short int qui donne le choix par défaut. 3eme paramatre: bool, si à vrai affiche les messages d'erreurs en cas d'erreur. sortie: short int, "-1" en cas d'erreur, "0" en pour la sortie/retour, Si >1 donne le numéro de la proposition. Utilisation: Fleche du haut/bas pour changer de propsition (ou raccourci avec le pavé numérique pour les choix de 0 à maximum la 9eme propostion, si il y a) valider avec la touche [ENTER] Sortie rapide avec la touche [ESC] (choisi sortie/retour, donc retourne "0") A faire: Géré les caractère spéciaux tel que \t, Convertir les saut de ligne de \0 à \n, ...
  • /
void gotoxy(int x,int y){ COORD pos={x-1,y-1}; SetConsoleCursorPosition(STDOUT,pos); } void procchar(unsigned short int xfois, char cara){ for(unsigned short int i = 1; i <= xfois; i++) printf("%c", cara); } void writelgn(char *ligne, unsigned short int noprop){ unsigned short int c = -1; for(unsigned short int curprop = 0; curprop < noprop; curprop++) while(*(ligne + (++c))); for(unsigned short int i = c+1; *(ligne + i); i++) printf("%c", *(ligne + i)); } short int menu(char *propositions, short int choix, bool prterr){ unsigned short int nblignes=0, max, lgtitre, curlg=-1, decalage, pavnum, debutlgn; short int poiderror = -1, errorcode = 0; char touche; bool error = false; while(*(propositions + (++curlg)));// donne la longeure du titre debutlgn = max = lgtitre = curlg; while(*(propositions + debutlgn) || *(propositions + debutlgn + 1)){ // donne la ligne la plus grande et compte le nombre de ligne curlg = 0; while(*(propositions + (debutlgn + (++curlg) + 1))); if(curlg > max) max = curlg; debutlgn+= (curlg + 1); nblignes++; } textcolor(ERRORCOLOR); textbackground(NORMBK); if(nblignes == 1){ if(prterr) printf("*** Erreur menu : il faut au moins un titre et un element de fin ***\n"); errorcode += poiderror; } poiderror *= 2; if((nblignes* 2 + 6) > NBLGNSRC){ if(prterr) printf("*** Erreur menu : trop de proposition pour l'ecran ***\n"); errorcode += poiderror; } poiderror *= 2; if((max + 5) > NBCOLSRC){ if(prterr) printf("*** Erreur menu : proposition(s) trop longue(s) ***\n"); errorcode += poiderror; } poiderror *= 2; if(choix < 0 || choix >= nblignes){ if(prterr) printf("*** Erreur menu : choix par defaut hors des possibilitees ***\n"); errorcode += poiderror; } if(errorcode) return errorcode; else{ decalage = (NBCOLSRC - (max + 4))/2; system("cls");// efface l'ecran // dessinne le tableau textbackground(NORMBK); procchar(decalage, ' '); textcolor(MENUTXT);textbackground(MENUFORM); printf("ÉÍ"); procchar(max, 'Í'); printf("Í»\n"); textbackground(NORMBK); procchar(decalage, ' '); textcolor(MENUTXT);textbackground(MENUFORM); printf("º"); procchar((max-lgtitre)/2, ' '); printf("Ú"); procchar(lgtitre, 'Ä'); printf("¿"); procchar((max-lgtitre)/2 + (max-lgtitre)%2, ' '); printf("º"); textbackground(MENUBK);printf(" \n");textbackground(MENUFORM); textbackground(NORMBK); procchar(decalage, ' '); textcolor(MENUTXT);textbackground(MENUFORM); printf("º"); procchar((max-lgtitre)/2, ' '); textcolor(MENUTXT);textbackground(MENUFORM); printf("³"); writelgn(propositions, 0); // affichage du titre printf("³"); procchar((max-lgtitre)/2 + (max-lgtitre)%2, ' '); printf("º"); textbackground(MENUBK);printf(" \n");textbackground(MENUFORM); textbackground(NORMBK); procchar(decalage, ' '); textcolor(MENUTXT);textbackground(MENUFORM); printf("º"); procchar((max-lgtitre)/2, ' '); textcolor(MENUTXT);textbackground(MENUFORM); printf("À"); procchar(lgtitre, 'Ä'); printf("Ù"); procchar((max-lgtitre)/2 + (max-lgtitre)%2, ' '); printf("º"); textbackground(MENUBK);printf(" \n");textbackground(MENUFORM); for(int i = 1; i <= (nblignes * 2)+1; i++){ // pas i<= nblignes car il y a le titre textbackground(NORMBK); procchar(decalage, ' '); textcolor(MENUTXT);textbackground(MENUFORM); printf("º "); procchar(max, ' '); textcolor(MENUTXT);textbackground(MENUFORM); printf(" º"); textbackground(MENUBK);printf(" \n");textbackground(MENUFORM); } textbackground(NORMBK); procchar(decalage, ' '); textcolor(MENUTXT);textbackground(MENUFORM); printf("ÈÍ"); procchar(max, 'Í'); printf("ÍO"); textbackground(MENUBK);printf(" \n");textbackground(MENUFORM); textbackground(NORMBK); procchar(decalage+1, ' '); textbackground(MENUBK);procchar(max+4, ' '); textbackground(MENUFORM); do{ for(int i = 2; i <= nblignes; i++){ if (choix == (i-1)) textcolor(MENUSELECTED); else textcolor(MENUNOSELECTED); gotoxy(decalage+3,4+2*(i-1)); writelgn(propositions, i); // affichage des propostitions } if (choix == 0) textcolor(MENUSELECTED); else textcolor(MENUNOSELECTED);; gotoxy(decalage+3,4+2*nblignes); writelgn(propositions, 1); // affichage de la proposition de sortie touche=getch(); if(choix == 0) choix = nblignes; if(touche == BAS) choix = ++choix; if(touche == HAUT) choix = --choix; if(touche == ESC) choix = 0; if(nblignes > 10) pavnum = 10; else pavnum = nblignes; if (touche >= NUMZERO && touche < (NUMZERO + pavnum)) choix = touche - NUMZERO; choix %= nblignes; }while (touche!=ENTER && touche!=ESC); textcolor(NORMTXT);textbackground(NORMBK);gotoxy(1,1);system("cls"); // retour à un ecran vierge return choix; } } void main(void){ int choix=2, a; do{ switch(choix = menu("DEMO\0Quitter la demo\0Saisir un entier\0choix par defaut\0choix 3\0", choix, false)){ case 1: a = SaisirEntier("Entrer un entier:"); printf("\nVoici l'entier: %d\n", a); break; case 2: printf("Choix par defaut\n"); break; case 3: printf("Choix 3\n"); } system("pause"); }while(choix); }

Conclusion :


A faire (pour le menu):
Géré les caractère spéciaux tel que \t,
Convertir les saut de ligne de \0 à \n,
...

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.

Du même auteur (Boygen)