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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
#include<stdio.h> /* For printf, fprintf */
#include<stdlib.h> /* For NULL, stderr */
#include<string.h> /* For strcpy */
/* Structures */
typedef struct menu{
char label[255];
struct menu *next; /* the next item */
struct menu *sub; /* the sub menu */
} Menu;
/* Functions */
Menu* menuCreate(char* label, Menu *next, Menu *sub){
fprintf(stderr, "menuCreate: start\n");
Menu *menu;
menu = malloc(2 * sizeof(Menu));
strcpy(menu->label, label);
menu->next = next;
menu->sub = sub;
fprintf(stderr, "\tmenuCreate: label='%s'\n", menu->label);
fprintf(stderr, "menuCreate: end\n");
return(menu);
}
Menu* menuNext(Menu *menu, char* label){
fprintf(stderr, "menuNext: start\n");
fprintf(stderr, "\tmenuNext: looking for next of label '%s'\n", label);
/* if it is found */
if(strcmp(menu->label, label) == 0){
fprintf(stderr, "\tmenuNext: label '%s' found\n", menu->label);
if(menu->next != NULL){
fprintf(stderr, "\tmenuNext: next found (label='%s')\n", menu->next->label);
fprintf(stderr, "menuNext: end\n");
return(menu->next);
}
else{
fprintf(stderr, "\tmenuNext: no next found (label='%s')\n", menu->label);
fprintf(stderr, "menuNext: end\n");
return(menu);
}
}
else{
/* if it has a sub */
if(menu->sub != NULL){
fprintf(stderr, "\tmenuNext: label '%s' have a sub\n", menu->label);
menuNext(menu->sub, label);
}
else{
fprintf(stderr, "\tmenuNext: label '%s' havn't a sub\n", menu->label);
}
/* if it has a next */
if(menu->next != NULL){
fprintf(stderr, "\tmenuNext: label '%s' have a next\n", menu->label);
menuNext(menu->next, label);
}
else{
fprintf(stderr, "\tmenuNext: label '%s' havn't a next\n", menu->label);
}
}
}
/* Main function */
int main(){
Menu *menu1, *menu_current;
menu1=menuCreate(
"Play\0",
menuCreate(
"Options\0",
menuCreate(
"Exit\0",
NULL,
NULL
),
menuCreate(
"Video\0",
menuCreate(
"Audio\0",
menuCreate(
"Control\0",
menuCreate(
"Game\0",
menuCreate(
"Back\0",
NULL,
NULL
),
NULL
),
NULL
),
NULL
),
NULL
)
),
NULL
);
menu_current=menu1;
/* Call menuNext 3 times */
printf("main: call menuNext on label '%s'\n", menu_current->label);
menu_current=menuNext(menu1, menu_current->label);
printf("main: label is now '%s'\n", menu_current->label);
printf("main: call menuNext on label '%s'\n", menu_current->label);
menu_current=menuNext(menu1, menu_current->label);
printf("main: label is now '%s'\n", menu_current->label);
printf("main: call menuNext on label '%s'\n", menu_current->label);
menu_current=menuNext(menu1, menu_current->label);
printf("main: label is now '%s'\n", menu_current->label);
return(0);
} |
Partager