Advertisement
Guest User

Final 2

a guest
Nov 23rd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <locale.h>
  5.  
  6. typedef enum { eletrico, computacao, civil, mecanico, quimico } tipo;
  7.  
  8. typedef struct {
  9.     char nome[50];
  10.     char cpf[25];
  11.     char endereco[50];
  12.     tipo tipo;
  13. }Engenheiro;
  14.  
  15. Engenheiro agenda[100];
  16. Engenheiro favoritos[100];
  17.  
  18. void rodar_programa();
  19. void listar_engenheiro(int contador);
  20. void listar_favoritos(int contadorfav);
  21. int adicionar_favoritos(int index, int contadorfav);
  22. void atualizar_engenheiro(int index);
  23. int adicionar_engenheiro(int contador, int contadorfav);
  24. int procurar_engenheiro(int contador);
  25. int remover_engenheiro(int index, int contador, int who);
  26. int inicializar(int contador);
  27. char* getTipoEngenheiro(tipo enumerador);
  28.  
  29.  
  30. int main() {
  31.  
  32.     setlocale(LC_ALL, "Portuguese");
  33.  
  34.     rodar_programa();
  35.  
  36.     return 0;
  37. }
  38.  
  39. void rodar_programa() {
  40.  
  41.     int op, fav;
  42.     int contador = 0;
  43.     int contadorfav = 0;
  44.     int whil = 1;
  45.  
  46.     contador = inicializar(contador);
  47.  
  48.     while (whil == 1) {
  49.         printf("================================\n");   // MENU PRINCIPAL
  50.         printf("0 - CADASTRAR\n");                      // MENU PRINCIPAL
  51.         printf("1 - LISTAR\n");                         // MENU PRINCIPAL
  52.         printf("2 - PESQUISAR\n");                      // MENU PRINCIPAL
  53.         printf("3 - ATUALIZAR\n");                      // MENU PRINCIPAL
  54.         printf("4 - REMOVER\n");                        // MENU PRINCIPAL
  55.         printf("5 - SAIR\n");                           // MENU PRINCIPAL
  56.         printf("6 - FAVORITOS\n");                      // MENU PRINCIPAL
  57.         printf("================================\n");   // MENU PRINCIPAL
  58.         printf("\nSelecione uma das opções: ");         // MENU PRINCIPAL
  59.         scanf("%d", &op);
  60.         getchar();
  61.  
  62.         switch (op) {
  63.         case 0:
  64.             contador = adicionar_engenheiro(contador, contadorfav);
  65.             break;
  66.         case 1:
  67.             listar_engenheiro(contador);
  68.             break;
  69.         case 2:
  70.             procurar_engenheiro(contador);
  71.             break;
  72.         case 3:
  73.             atualizar_engenheiro(procurar_engenheiro(contador));
  74.             break;
  75.         case 4:
  76.             contador = remover_engenheiro(procurar_engenheiro(contador), contador, 1);
  77.             break;
  78.         case 5:
  79.             whil = 0;
  80.         case 6:
  81.             printf("\n\n=========================================\n");
  82.             printf("1 - ADICIONAR ENGENHEIRO AOS FAVORITOS\n");
  83.             printf("2 - LISTAR FAVORITOS\n");
  84.             printf("3 - REMOVER FAVORITOS\n");
  85.             printf("\n===========================================\n\n");
  86.             scanf("%d", &fav);
  87.  
  88.             if (fav == 1)
  89.             {
  90.                 contadorfav = adicionar_favoritos(procurar_engenheiro(contador), contadorfav);
  91.                 break;
  92.             }
  93.             if (fav == 2)
  94.             {
  95.                 listar_favoritos(contadorfav);
  96.                 break;
  97.             }
  98.             if (fav == 3) {
  99.                 contadorfav = remover_engenheiro(procurar_engenheiro(contadorfav), contadorfav, 0);
  100.             }
  101.  
  102.         default:
  103.  
  104.             break;
  105.         }
  106.     }
  107.  
  108. }
  109.  
  110. int adicionar_engenheiro(int contador, int contadorfav) {
  111.  
  112.     char nome[50];
  113.     char cpf[50];
  114.     char endereco[50];
  115.     tipo tipo = eletrico;
  116.     int op;
  117.  
  118.     printf("Insira o nome (Digite a Primeira Letra Maiscula!): ");
  119.     scanf(" %[^\n]s", nome);
  120.  
  121.     printf("\nInsira o cpf: ");
  122.     scanf(" %[^\n]s", cpf);
  123.  
  124.     printf("\nInsira o Endereço: ");
  125.     scanf(" %[^\n]s", endereco);
  126.  
  127.     printf("\nInsira o tipo: (0 = eletrico, 1 = computação, 2 = civil, 3 = mecanico, 4 = quimico)");
  128.     scanf("%d", &tipo);
  129.  
  130.  
  131.     strcpy(agenda[contador].nome, nome);
  132.     strcpy(agenda[contador].cpf, cpf);
  133.     strcpy(agenda[contador].endereco, endereco);
  134.     agenda[contador].tipo = tipo;
  135.     contador++;
  136.  
  137.     printf("\nEngenheiro Adicionado com Sucesso\n");
  138.  
  139.     return contador;
  140. }
  141.  
  142. void listar_engenheiro(int contador) {
  143.  
  144.     char nome[50];
  145.     char cpf[50];
  146.     char endereco[50];
  147.     tipo tipo = eletrico;
  148.     int i, j;
  149.  
  150.     for (i = 1; i < contador; i++) {
  151.         for (j = 1; j < contador; j++) {
  152.             if (strcmp(agenda[j - 1].nome, agenda[j].nome) > 0) {
  153.                 strcpy(nome, agenda[j - 1].nome);
  154.                 strcpy(agenda[j - 1].nome, agenda[j].nome);
  155.                 strcpy(agenda[j].nome, nome);
  156.  
  157.                 strcpy(cpf, agenda[j - 1].cpf);
  158.                 strcpy(agenda[j - 1].cpf, agenda[j].cpf);
  159.                 strcpy(agenda[j].cpf, cpf);
  160.  
  161.                 strcpy(endereco, agenda[j - 1].endereco);
  162.                 strcpy(agenda[j - 1].endereco, agenda[j].endereco);
  163.                 strcpy(agenda[j].endereco, endereco);
  164.  
  165.                 tipo = agenda[j - 1].tipo;
  166.                 agenda[j - 1].tipo = agenda[j].tipo;
  167.                 agenda[j].tipo = tipo;
  168.             }
  169.         }
  170.     }
  171.  
  172.     for (i = 0; i < contador; i++) {
  173.         printf("\nNome do individuo: %s\n", agenda[i].nome);
  174.         printf("CPF: %s\n", agenda[i].cpf);
  175.         printf("Endereco: %s\n", agenda[i].endereco);
  176.         printf("Tipo de Engenheiro: %s\n\n", getTipoEngenheiro(agenda[i].tipo));
  177.     }
  178. }
  179.  
  180. int procurar_engenheiro(int contador) { // Função para Procurar Engenheiros
  181.  
  182.     char nome[50];
  183.     int i;
  184.     setlocale(LC_ALL, "Portuguese");
  185.     printf("Qual nome: ");
  186.     scanf("%s", nome);
  187.  
  188.     for (i = 0; i < contador; i++) {
  189.         if (strcmp(nome, agenda[i].nome) == 0)
  190.         {
  191.             printf("\nNome do individuo: %s\n", agenda[i].nome);
  192.             printf("CPF: %s\n", agenda[i].cpf);
  193.             printf("Endereco: %s\n", agenda[i].endereco);
  194.             printf("Tipo de Engenheiro: %s\n\n", getTipoEngenheiro(agenda[i].tipo));
  195.             return i;
  196.         }
  197.     }
  198.     if (strcmp(nome, agenda[i].nome) != 0)
  199.     {
  200.         printf("\nNenhum Engenheiro com este nome foi encontrado\n");
  201.         return -1;
  202.     }
  203. }
  204.  
  205. void atualizar_engenheiro(int index) { // Função da Atualização dos dados dos engenheiros cadastrados
  206.  
  207.     char nome[50];
  208.     char cpf[50];
  209.     char endereco[50];
  210.     tipo tipo = eletrico;
  211.  
  212.     printf("Insira o novo nome: (Digite a Primeira Letra Maiscula!)");
  213.     scanf("%s", nome);
  214.  
  215.  
  216.     printf("\nInsira o novo Endereço: ");
  217.     scanf("%s", endereco);
  218.  
  219.     printf("\nInsira o novo tipo (0 = eletrico, 1 = computação, 2 = civil, 3 = mecanico, 4 = quimico):");
  220.     scanf("%d", &tipo);
  221.  
  222.     strcpy(agenda[index].nome, nome);
  223.     strcpy(agenda[index].endereco, endereco);
  224.     agenda[index].tipo = tipo;
  225.  
  226.     printf("\nEngenheiro Atualizado com Sucesso\n");
  227. }
  228.  
  229. int remover_engenheiro(int index, int contador, int who) {   // Essa Função Remove o engenheiro procurado.
  230.  
  231.     if (index == -1) {
  232.         return contador;
  233.     }
  234.     if (who == 1) {
  235.         for (index; index < contador; index++) {
  236.             strcpy(agenda[index].nome, agenda[index + 1].nome);
  237.             strcpy(agenda[index].cpf, agenda[index + 1].cpf);
  238.             strcpy(agenda[index].endereco, agenda[index + 1].endereco);
  239.             agenda[index].tipo = agenda[index + 1].tipo;
  240.         }
  241.     }
  242.     else {
  243.         for (index; index < contador; index++) {
  244.             strcpy(favoritos[index].nome, favoritos[index + 1].nome);
  245.             strcpy(favoritos[index].cpf, favoritos[index + 1].cpf);
  246.             strcpy(favoritos[index].endereco, favoritos[index + 1].endereco);
  247.             favoritos[index].tipo = favoritos[index + 1].tipo;
  248.         }
  249.     }
  250.    
  251.  
  252.     printf("\nEngenheiro removido com sucesso!");
  253.  
  254.     contador--;
  255.     return contador;
  256. }
  257.  
  258. int adicionar_favoritos(int contador, int contadorfav) {
  259.  
  260.     int op;
  261.     char nome[50];
  262.     char cpf[50];
  263.     char endereco[50];
  264.  
  265.     tipo tipo = eletrico;
  266.  
  267.     printf("\nDeseja Adicionar aos favoritos?: (1) PARA SIM (0) PARA NÃO \n\n");
  268.     scanf("%d", &op);
  269.  
  270.     if (op == 1) {
  271.         strcpy(favoritos[contadorfav].nome, agenda[contador].nome);
  272.         strcpy(favoritos[contadorfav].cpf, agenda[contador].cpf);
  273.         strcpy(favoritos[contadorfav].endereco, agenda[contador].endereco);
  274.         favoritos[contadorfav].tipo = agenda[contador].tipo;
  275.  
  276.         printf("\nAdicionado com sucesso!!!\n\n");
  277.  
  278.         contadorfav++;
  279.         return contadorfav;
  280.     }
  281.  
  282. }
  283.  
  284. void listar_favoritos(int contadorfav) {
  285.  
  286.     char nome[50];
  287.     char cpf[50];
  288.     char endereco[50];
  289.     tipo tipo = eletrico;
  290.     int i, j;
  291.  
  292.     for (i = 1; i < contadorfav; i++) {
  293.         for (j = 1; j < contadorfav; j++) {
  294.             if (strcmp(favoritos[j - 1].nome, favoritos[j].nome) > 0) {
  295.                 strcpy(nome, favoritos[j - 1].nome);
  296.                 strcpy(favoritos[j - 1].nome, favoritos[j].nome);
  297.                 strcpy(favoritos[j].nome, nome);
  298.  
  299.                 strcpy(cpf, favoritos[j - 1].cpf);
  300.                 strcpy(favoritos[j - 1].cpf, favoritos[j].cpf);
  301.                 strcpy(favoritos[j].cpf, cpf);
  302.  
  303.                 strcpy(endereco, favoritos[j - 1].endereco);
  304.                 strcpy(favoritos[j - 1].endereco, favoritos[j].endereco);
  305.                 strcpy(favoritos[j].endereco, endereco);
  306.  
  307.                 tipo = favoritos[j - 1].tipo;
  308.                 favoritos[j - 1].tipo = favoritos[j].tipo;
  309.                 favoritos[j].tipo = tipo;
  310.             }
  311.         }
  312.     }
  313.  
  314.     for (i = 0; i < contadorfav; i++) {
  315.         printf("\nNome do individuo: %s\n", favoritos[i].nome);
  316.         printf("CPF: %s\n", favoritos[i].cpf);
  317.         printf("Endereco: %s\n", favoritos[i].endereco);
  318.         printf("Tipo de Engenheiro: %s\n\n", getTipoEngenheiro(favoritos[i].tipo));
  319.     }
  320. }
  321.  
  322. int inicializar(int contador) {
  323.     strcpy(agenda[0].nome, "Gustavo");              // Essa Função Pré estabelece os engenheiros já cadastrados no sistema.
  324.     strcpy(agenda[0].cpf, "123456789");
  325.     strcpy(agenda[0].endereco, "Rua");
  326.     agenda[0].tipo = computacao;
  327.     contador++;
  328.  
  329.     strcpy(agenda[1].nome, "Ruan");
  330.     strcpy(agenda[1].cpf, "123456789");
  331.     strcpy(agenda[1].endereco, "Rua");
  332.     agenda[1].tipo = computacao;
  333.     contador++;
  334.  
  335.     strcpy(agenda[2].nome, "Amanda");
  336.     strcpy(agenda[2].cpf, "123456789");
  337.     strcpy(agenda[2].endereco, "Rua");
  338.     agenda[2].tipo = computacao;
  339.     contador++;
  340.  
  341.     strcpy(agenda[3].nome, "Beto");
  342.     strcpy(agenda[3].cpf, "123456789");
  343.     strcpy(agenda[3].endereco, "Rua");
  344.     agenda[3].tipo = computacao;
  345.     contador++;
  346.  
  347.     strcpy(agenda[4].nome, "Pedro");
  348.     strcpy(agenda[4].cpf, "123456789");
  349.     strcpy(agenda[4].endereco, "Rua");
  350.     agenda[4].tipo = computacao;
  351.     contador++;
  352.  
  353.     strcpy(agenda[5].nome, "Tiago");
  354.     strcpy(agenda[5].cpf, "777777");
  355.     strcpy(agenda[5].endereco, "RuaAlpaca");
  356.     agenda[5].tipo = eletrico;
  357.     contador++;
  358.  
  359.     strcpy(agenda[6].nome, "Olivia");
  360.     strcpy(agenda[6].cpf, "123456789");
  361.     strcpy(agenda[6].endereco, "Rua");
  362.     agenda[6].tipo = computacao;
  363.     contador++;
  364.  
  365.     strcpy(agenda[7].nome, "Igor");
  366.     strcpy(agenda[7].cpf, "123456789");
  367.     strcpy(agenda[7].endereco, "Rua");
  368.     agenda[7].tipo = computacao;
  369.     contador++;
  370.  
  371.     strcpy(agenda[8].nome, "Xayah");
  372.     strcpy(agenda[8].cpf, "123456789");
  373.     strcpy(agenda[8].endereco, "Rua");
  374.     agenda[8].tipo = computacao;
  375.     contador++;
  376.  
  377.     strcpy(agenda[9].nome, "Junior");
  378.     strcpy(agenda[9].cpf, "123456789");
  379.     strcpy(agenda[9].endereco, "Rua");
  380.     agenda[9].tipo = computacao;
  381.     contador++;
  382.  
  383.     return contador;
  384. }
  385.  
  386. char* getTipoEngenheiro(tipo enumerador) { // Função para retornar String.
  387.     static char tipo[50];
  388.  
  389.     if (enumerador == eletrico) {
  390.         strcpy(tipo, "Elétrico");
  391.     }
  392.     else if (enumerador == computacao) {
  393.         strcpy(tipo, "Computação");
  394.     }
  395.     else if (enumerador == civil) {
  396.         strcpy(tipo, "Civil");
  397.     }
  398.     else if (enumerador == mecanico) {
  399.         strcpy(tipo, "Mecanico");
  400.     }
  401.     else if (enumerador == quimico) {
  402.         strcpy(tipo, "Quimico");
  403.     }
  404.     return tipo;
  405. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement