- /********************
- * Switch de string *
- * par ordiman85 *
- ********************/
-
- #include <conio.h>
- #include <stdio.h>
- #include <string.h>
-
-
- // char* str est la string à "switcher"
- // IMPORTANT !! Mettre 0 comme dernier argument !
-
- #ifdef __cplusplus
-
- // Fonction pour C++
- unsigned int s_(char* str, ...)
- {
- unsigned int i = 0;
- while (*(++i+&str) != 0)
- if (strcmp(str, (char*)*(&str+i)) == 0)
- return i;
- return 0;
- }
-
- #else
-
- // Fonction pour le langage C
- unsigned int s_(char* str, char* cmp[])
- {
- unsigned int i = 0;
- while (cmp[i] != 0) {
- if (strcmp(str, cmp[i]) == 0)
- return i+1;
- i++;
- }
- return 0;
- }
-
- #endif
-
- // Fonction principale
- int main()
- {
- printf("Test du switch de strings :\r\n");
- // La string à tester est "switch", sa position est 4
- char* String = "switch";
-
- // On récupère un entier qui correspond à la position de la string identifiée
-
- // Méthode 1 (C++)
- #ifdef __cplusplus
-
- unsigned int id = s_(String, "Hello", "world", "!", "switch", "strings", 0);
-
- #else // Méthode 2 (C)
-
- char* comp[] = {"Hello", "world", "!", "switch", "strings", 0};
- unsigned int id = s_(String, comp);
-
- #endif
-
- // Le switch
- switch (id)
- {
- case 1: // "Hello"
- printf("%d. Hello", id);
- break;
- case 2: // "world"
- printf("%d. world", id);
- break;
- case 3: // "!"
- printf("%d. !", id);
- break;
- case 4: // "switch"
- printf("%d. switch", id);
- break;
- case 5: // "strings"
- printf("%d. strings", id);
- break;
- default: // Pas identifié, id = 0
- printf("Pas dans la liste (%d)", id);
- break;
- }
-
- // Pause et fin
- getch();
- return 0;
- }
/********************
* Switch de string *
* par ordiman85 *
********************/
#include <conio.h>
#include <stdio.h>
#include <string.h>
// char* str est la string à "switcher"
// IMPORTANT !! Mettre 0 comme dernier argument !
#ifdef __cplusplus
// Fonction pour C++
unsigned int s_(char* str, ...)
{
unsigned int i = 0;
while (*(++i+&str) != 0)
if (strcmp(str, (char*)*(&str+i)) == 0)
return i;
return 0;
}
#else
// Fonction pour le langage C
unsigned int s_(char* str, char* cmp[])
{
unsigned int i = 0;
while (cmp[i] != 0) {
if (strcmp(str, cmp[i]) == 0)
return i+1;
i++;
}
return 0;
}
#endif
// Fonction principale
int main()
{
printf("Test du switch de strings :\r\n");
// La string à tester est "switch", sa position est 4
char* String = "switch";
// On récupère un entier qui correspond à la position de la string identifiée
// Méthode 1 (C++)
#ifdef __cplusplus
unsigned int id = s_(String, "Hello", "world", "!", "switch", "strings", 0);
#else // Méthode 2 (C)
char* comp[] = {"Hello", "world", "!", "switch", "strings", 0};
unsigned int id = s_(String, comp);
#endif
// Le switch
switch (id)
{
case 1: // "Hello"
printf("%d. Hello", id);
break;
case 2: // "world"
printf("%d. world", id);
break;
case 3: // "!"
printf("%d. !", id);
break;
case 4: // "switch"
printf("%d. switch", id);
break;
case 5: // "strings"
printf("%d. strings", id);
break;
default: // Pas identifié, id = 0
printf("Pas dans la liste (%d)", id);
break;
}
// Pause et fin
getch();
return 0;
}