C algorithm for registration, search and exclusion

Asked

Viewed 575 times

1

Good night people, all right? I’m developing this algorithm and I’m having a hard time deleting names I’ve registered, I don’t have advanced C skills, and I don’t know if the way I did the code is right or 100% optimized, but it’s working, accepted all kinds of tips and instructions for study of a particular topic, thank you.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

int choice,gen;
int i;
char nomes[25][25];
char name[50][50];
char voider;

int main(){



            void menu(void);


            for(int x = 0; x <= 25; x++)
            for(int j = 0; j < 1; j++)
            nomes[x][j]='-';

            menu();

return 0;

}


void menu(void){

    void cadastro(void);
    void pesquisa(void);


printf("\n/----------------------------------------------------------------------/");
printf("\n/----------------------------------------------------------------------/");
printf("\n/-----------------------G A T H E R E R V2.0 --------------------------/");
printf("\n/----------------------------------------------------------------------/");
printf("\n/----------------------------------------------------------------------/");
printf("\n\nPress enter to continue...");
gets(&voider);


    system("cls");

printf("\n/----------------------------------------------------------------------/");
printf("\n/-----------------------M A I N   M E N U -----------------------------/");
printf("\n/----------------------------------------------------------------------/");
printf("\n/----------------------------------------------------------------------/");
printf("\n/---1 - REGISTER PEOPLE ===============================================/");
printf("\n/---2 - SEARCH PEOPLE =================================================/");
printf("\n/---3 - SHOW REGISTER =================================================/");
printf("\n/---4 - EXCLUDE PEOPLE ================================================/");

printf("\n/----------------------------------------------------------------------/");
printf("\n/---Select a option----------------------------------------------------/\n");
scanf("%d",&choice);
fflush(stdin);

        switch (choice){

  case 1:

    system("cls");
    cadastro();
    break;

  case 2:

    system("cls");
    pesquisa();
    break;

  case 3:

    system("cls");
    imprimi();
    break;

  case 4:

    system("cls");
    excluir();
    break;

}
}
void cadastro(void){

int g;
g = 0;


    while(g == 0){
        for(int x = 0; x <= 25; x++)
            for(int j = 0; j < 25; j++)
                if(strcmp(&nomes[x][j], "-") == 0){
        printf("\n/----------------------------------------------------------------------/");
        printf("\n/---------------------------REGISTER PEOPLE----------------------------/");
        printf("\n/----------------------------------------------------------------------/");
        printf("\n/----------------------------------------------------------------------/");
        printf("\n/-----Enter a name or press 1 to leave to menu ------------------------/\n");
                    scanf("%s", &nomes[x]);
                    system("cls");
                    if(strcmp(&nomes[x], "1") == 0){
                        if(strcmp(&nomes[x], "1") == 0){
                            nomes[x][j] = '-';
                        }
                        menu();
                    }
                }

    }

}
void pesquisa(void){

  void menu(void);





  printf("\n/----------------------------------------------------------------------/");
  printf("\n/---------------------------SEARCH PEOPLE -----------------------------/");
  printf("\n/----------------------------------------------------------------------/");
  printf("\n/----------------------------------------------------------------------/");
  printf("\n/--------- Enter the name to search in database: ----------------------/\n");
  fflush(stdin);
  gets(name);


  for(i = 0; i<= 25 ;i++){



  if(strcmp(&nomes[i],name[0]) == 0){




system("cls");

    printf("\n/----------------------------------------------------------------------/");
    printf("\n/---------------------------SEARCH PEOPLE -----------------------------/");
    printf("\n/----------------------------------------------------------------------/");
    printf("\n/----------------------------------------------------------------------/");
    printf("\n/---------The following name has been found: %s                         ",nomes[i]);

}
  }

  printf("\n/----------------------------------------------------------------------/");
  printf("\n/----------------------------------------------------------------------/");
  printf("\n/---1 -SEARCH AGAIN ===================================================/");
  printf("\n/---2 -GOTO MENU ======================================================/\n");



scanf("%d",&gen);

if(gen == 1){
    system("cls");
    pesquisa();
}
if(gen == 2){
    system("cls");
    menu();

}





}
void imprimi(void){

        printf("\n/----------------------------------------------------------------------/");
        printf("\n/---------------------------REGISTER LIST------------------------------/");
        printf("\n/----------------------------------------------------------------------/");
        printf("\n/----------------------------------------------------------------------/");
for(i = 0;i <=25;i++){

    printf("\nNome encontrado: %s",nomes[i]);


}

printf("\n1 - GOTO MENU\n");
scanf("%d",&choice);


if(choice == 1);

menu();

}
void excluir(void){

        printf("\n/----------------------------------------------------------------------/");
        printf("\n/---------------------------EXCLUDE PEOPLE-----------------------------/");
        printf("\n/----------------------------------------------------------------------/");
        printf("\n/----------------------------------------------------------------------/");
        printf("\n/----Enter a name to exclude:                                           \n");
        fflush(stdin);
        gets(name);

// Aqui eu insiro a comparação que irá trocar o "name" por um "-", simbolizando vazio quando eu entrar no menu "register list".



    system("cls");

    printf("\n/----------------------------------------------------------------------/");
    printf("\n/---------------------------EXCLUDE PEOPLE ----------------------------/");
    printf("\n/----------------------------------------------------------------------/");
    printf("\n/----------------------------------------------------------------------/");
    printf("\n/---------The following name has been excluded:                        ");




  printf("\n/----------------------------------------------------------------------/");
  printf("\n/----------------------------------------------------------------------/");
  printf("\n/---1 -EXCLUDE AGAIN ==================================================/");
  printf("\n/---2 -GOTO MENU ======================================================/\n");

    scanf("%d",&gen);

    if(gen == 1){
    system("cls");
    excluir();
    }
    if(gen == 2){
    system("cls");
    menu();
    }

    }



Inside the "delete" function, I’m having trouble trying to compare the "name" I get with the names that were stored in the "names" array, I can make the comparison "work" however, I can only replace only the first character, being for example "Lucas" to "-ucas".

1 answer

1

Hi all right ? I hope so =).

From what I saw of your code it would be good if you were to refactor it, and change some gaps, besides taking into account the OS that is being executed. Some explosion:

rather than putting: system('cls'), it would be more recommended to do at the beginning of the code something like this:

#ifndef __UNIX__
  #define limpar() system("clear");
  #define pressEnter() getchar();
#else
  #define limpar() system('cls');
  #define pressEnter() getch();
#endif

This code snippet will be interpreted and run at compile time, so Voce has the guarantee that it will work with the OS correctly.


Another important point 'and that Voce made a hole beeem entrho to return to the menu and other parts of the code, I recommend you do something more style Neese:

while(choice!=5){ switch(choice)....}

Because when a function is called, the program 'stops', performs that function in the special then goes back to where it was, and continues until the end of the execution, I made a small example using default(When the selected value is not 'and none of the cases)

ps: I will put at the end and comment


Another thing, global variables, try not to use them, I know it is tempting but do not create them within the function and pass as parameter and return. Declare a struct(out of the main as if it were global), then make an array, and then mess with it.


And finally strcmp(), it compares two string, so just pass fearlessly and do not compare a char to string, one of the characteristics of the string in C 'and that they always end in ' 0', so if you look for this element Voce can be sure that it arrived at the end, but if not ? Then the program will probably go through your memory or until you reach the end of it or even interpret something that it considers ' 0'.

example:

   char str[10]="coisas";
   char str2[11]="treco";
   int result = strcmp(str,str2);

   o strcmp vai retornar certos parametros para cada situacao

Example code:

#define TAMANHO 50 // Isso facilita em caso queira mudar o tamanho do vetor
#define TAMANHO_NOME 100

  struct pessoa
  {
       char nome[TAMANHO_NOME];
  };

   void adiciona(struct pessoa* lista_p,int tamanho)
   {
            // te recomendo fazer de forma dinamica 

   }

  void deleta(struct pessoa* lista_p,int tamanho)
  {
            usando o strcmp 
            e caso alocou dinamicamente use free();

   }

   int main()
   {
       int a=0;

  struct pessoa* lista_p[TAMANHO]; //Crio um array de ponteiro do tipo              struch pessoa
   //Lembrando que ele vai de lista_p[0] ate lista_p[49] 

  while(a!=3)
  {
    printf("Aperte 1 para adicionar");
    printf("Aperte 2 para deletar");
    printf("Aperte 3 para sair");
    scanf("%d", &a);

    switch(a){


        case 1:
            adiciona(*lista_p,TAMANHO); // Em c a menos que coloque uma condicao de parada no ultimo elemento, tem que se passar o numero de elementos
            break;

        case 2:

            deleta(*lista_p, TAMANHO);
            break;

        default:
            printf("Aperte uma das opcoes\n");
            break;
      }

  }
  return 0;

}

AND SEARCH FOR POINTERS, ARE VERY IMPORTANT IN C!!!!!

Cara I think 'and this discloses the lack of accents, but my PC is in English and I’m worried about changing...= P

reference:

https://www.ime.usp.br/~pf/algorithms/lessons/stru.html

http://blog.lucaspolo.com.br/2010/07/lendo-string-com-espacos-na-funcao.html

https://www.ime.usp.br/~pf/algoritmos/aulas/Pont.html

https://docs.microsoft.com/pt-br/cpp/preprocessor/preprocessor-directives?view=vs-2019

https://www.ime.usp.br/~pf/algorithms/classes/aloca.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.