Switch large case in C

3

I am developing a system for college and would like to know the best option, in the sense of code optimization, even improvement.

I have switch case for Menu, where I have 88 cases.

I have a screen called "Help", in this screen, the user can choose between 88 screens.

Example below:

To access the "Help" screen, it should type "help"

It will access this screen telaAjuda (); with several options inside it. These options are between 1 and 88. Example below:

printf("\n\t Op1 ...................................1\n");
printf("\n\t Op2....................................2\n");
printf("\n\t Op3....................................3\n");
printf("\n\t Op4....................................4\n");
printf("\n\t Op5...................................5\n\n");
...
printf("\n\t Op88...................................88\n\n");

And according to what is chosen, the user is taken to another screen of a certain number (as if it were a book pagination scheme).

If he chooses and enters the number 3, he will be taken to the Op3 screen.

The screen that uses switch case is subMenu_Ajuda () , which I call it below the help (I did two distinct functions for this, because if I want to call the help submenu elsewhere in the code, I will not need necessarily call the screen helps together), only to receive the value entered by the user and from it, choose the option in switch case .

Example below the submenu_help:

printf("\nnomePrograma : loginUsuario -> "); //usarlogin
scanf("%i", &opAjuda);

    switch (opAjuda) { //88 Cases

    case 1:
    system("clear");
    telaAjuda1 ();
    __fpurge(stdin);
    break;

    case 2:
    system("clear");
    telaAjuda2 ();
    __fpurge(stdin);
    break; 

    ...

    case 88:
    system("clear");
    telaAjuda88 ();
    __fpurge(stdin);
    break; 
}

Can I leave switch case of this same size or should I create a vector for all cases and call it when needed?

    
asked by anonymous 11.11.2015 / 14:25

1 answer

2

I will not go into other points, I will only do what is requested.

An array with a pointer to functions is created. All functions need to exist and be accessible by code to get their addresses.

To access the function, we first get the corresponding address according to the option you entered. The syntax for calling a function by its address is to find the pointer followed by the passing of the parameters. If you do not have parameters, just put () .

Remembering that all functions must be compatible. Have the same signature (same return and same parameters).

printf("\nnomePrograma : loginUsuario -> "); //usarlogin
scanf("%i", &opAjuda);
//seria bom validar se está entre 1 e 88
void (*opcoes[])() = {&telaAjuda1, &telaAjuda2, ..., &telaAjuda88}; //assinatura
system("clear");
(*opcoes[opAjuda - 1])();
__fpurge(stdin);

See running on ideone .

Obviously there in the ellipsis you need to put the name of all functions using the & operator by taking its address from each function, which obviously needs to exist.

    
11.11.2015 / 14:51