Esoteric strategies
To Reply from @Ruy Neto deals with the main. But the question is not limited to practical resolutions, it resembles another exercise. Since it is an exercise, let’s exercise?
goto
int main() {
int op;
goto INICIO;
SAIDA:
saudacoes_tchau();
return 0;
INICIO:
saudacoes_oi();
VOLTA:
op = le_operacao();
trata_operacao(op);
if (op) {
goto VOLTA;
} else {
goto SAIDA;
}
}
The goto
will force an unconditional jump to some place pointed in the code1. In case, to VOLTA
, the command immediately after saudacoes_oi()
1: in the case of C, and in some other languages that support goto
, the "place pointed in the code" is not totally arbitrary; it needs to be within the same function. If I am not mistaken, in Pascal, the goto
could go to any arbitrary point in the system, leaving the flow of code more macaronic than "normal"; fortunately C does not allow it, has the restriction of being in the same function
Direct function recursion, tail condition
void funcao_menu() {
int op;
op = le_operacao();
trata_operacao(op);
if (op) {
funcao_menu();
}
}
int main() {
saudacoes_oi();
funcao_menu();
saudacoes_tchau();
return 0;
}
Recursively calling the function to handle the available operations in the menu. Note that here I always run the whole function to, then choose whether to call, on the tail, from the function.
Indirect function recursion, function pointer
void funcao_menu() {
void (*proxima_chamada)();
int op;
proxima_chamada = &saudacoes_tchau;
op = le_operacao();
trata_operacao(op);
if (op) {
proxima_chamada = &funcao_menu;
}
(*proxima_chamada)();
}
int main() {
saudacoes_oi();
funcao_menu();
return 0;
}
Now, the choice is made between calling the tchau
or the function itself.
Direct function recursion, condition in argument
void funcao_menu(int old_op) {
int op;
if (old_op) {
op = le_operacao();
trata_operacao(op);
funcao_menu(op);
}
}
int main() {
saudacoes_oi();
funcao_menu(1);
saudacoes_tchau();
return 0;
}
Here, who defines whether there will be recursion or not is the function parameter. If a true argument is passed, it is executed; otherwise it closes the loop.
Indirect recursion to function, mutually recursive functions
void funcao_menu();
void trata_operacao_chama_funcao_menu(int op) {
if (op) {
trata_operacao(op);
funcao_menu();
}
}
void funcao_menu() {
trata_operacao_chama_funcao_menu(le_operacao());
}
int main() {
saudacoes_oi();
funcao_menu();
saudacoes_tchau();
return 0;
}
trata_operacao_chama_funcao_menu
flame funcao_menu
what call trata_operacao_chama_funcao_menu
.
Recursion using system calls
For this, I will remove the salutation from oi
, because it’s hard to define when it should be done.
int main(int argc, char **argv) {
int op = le_operacao();
if (op) {
trata_operacao(op);
return system(argv[0]);
}
return 0;
}
Command line?
– Jefferson Quesado
Yes, I need those command lines to always appear after you close your registration
– Elvis Serafim
Possible duplicate of What is the usefulness and importance of "do... while"?
– Edson Reis
@Edsonreis I disagree to be duplicate. In this question, the user wants to know which is the best to make a menu. In the other, nothing is said about menu.
– Victor Stafusa