Advertisement
Guest User

Final 2

a guest
Nov 23rd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.98 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, int who);
  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, 1);
  71.             break;
  72.         case 3:
  73.             atualizar_engenheiro(procurar_engenheiro(contador, 1));
  74.             break;
  75.         case 4:
  76.             contador = remover_engenheiro(procurar_engenheiro(contador, 1), 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, 1), 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, 0), 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, int who) { // 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.     if (who == 1) {
  189.         for (i = 0; i < contador; i++) {
  190.             if (strcmp(nome, agenda[i].nome) == 0)
  191.             {
  192.                 printf("\nNome do individuo: %s\n", agenda[i].nome);
  193.                 printf("CPF: %s\n", agenda[i].cpf);
  194.                 printf("Endereco: %s\n", agenda[i].endereco);
  195.                 printf("Tipo de Engenheiro: %s\n\n", getTipoEngenheiro(agenda[i].tipo));
  196.                 return i;
  197.             }
  198.         }
  199.         if (strcmp(nome, agenda[i].nome) != 0)
  200.         {
  201.             printf("\nNenhum Engenheiro com este nome foi encontrado\n");
  202.             return -1;
  203.         }
  204.     }
  205.     else {
  206.         for (i = 0; i < contador; i++) {
  207.             if (strcmp(nome, favoritos[i].nome) == 0)
  208.             {
  209.                 printf("\nNome do individuo: %s\n", favoritos[i].nome);
  210.                 printf("CPF: %s\n", favoritos[i].cpf);
  211.                 printf("Endereco: %s\n", favoritos[i].endereco);
  212.                 printf("Tipo de Engenheiro: %s\n\n", getTipoEngenheiro(favoritos[i].tipo));
  213.                 return i;
  214.             }
  215.         }
  216.         if (strcmp(nome, favoritos[i].nome) != 0)
  217.         {
  218.             printf("\nNenhum Engenheiro com este nome foi encontrado\n");
  219.             return -1;
  220.         }
  221.     }
  222.    
  223. }
  224.  
  225. void atualizar_engenheiro(int index) { // Função da Atualização dos dados dos engenheiros cadastrados
  226.  
  227.     char nome[50];
  228.     char cpf[50];
  229.     char endereco[50];
  230.     tipo tipo = eletrico;
  231.  
  232.     printf("Insira o novo nome: (Digite a Primeira Letra Maiscula!)");
  233.     scanf("%s", nome);
  234.  
  235.  
  236.     printf("\nInsira o novo Endereço: ");
  237.     scanf("%s", endereco);
  238.  
  239.     printf("\nInsira o novo tipo (0 = eletrico, 1 = computação, 2 = civil, 3 = mecanico, 4 = quimico):");
  240.     scanf("%d", &tipo);
  241.  
  242.     strcpy(agenda[index].nome, nome);
  243.     strcpy(agenda[index].endereco, endereco);
  244.     agenda[index].tipo = tipo;
  245.  
  246.     printf("\nEngenheiro Atualizado com Sucesso\n");
  247. }
  248.  
  249. int remover_engenheiro(int index, int contador, int who) {   // Essa Função Remove o engenheiro procurado.
  250.  
  251.     if (index == -1) {
  252.         return contador;
  253.     }
  254.     if (who == 1) {
  255.         for (index; index < contador; index++) {
  256.             strcpy(agenda[index].nome, agenda[index + 1].nome);
  257.             strcpy(agenda[index].cpf, agenda[index + 1].cpf);
  258.             strcpy(agenda[index].endereco, agenda[index + 1].endereco);
  259.             agenda[index].tipo = agenda[index + 1].tipo;
  260.         }
  261.     }
  262.     else {
  263.         for (index; index < contador; index++) {
  264.             strcpy(favoritos[index].nome, favoritos[index + 1].nome);
  265.             strcpy(favoritos[index].cpf, favoritos[index + 1].cpf);
  266.             strcpy(favoritos[index].endereco, favoritos[index + 1].endereco);
  267.             favoritos[index].tipo = favoritos[index + 1].tipo;
  268.         }
  269.     }
  270.    
  271.  
  272.     printf("\nEngenheiro removido com sucesso!");
  273.  
  274.     contador--;
  275.     return contador;
  276. }
  277.  
  278. int adicionar_favoritos(int contador, int contadorfav) {
  279.  
  280.     int op;
  281.     char nome[50];
  282.     char cpf[50];
  283.     char endereco[50];
  284.  
  285.     tipo tipo = eletrico;
  286.  
  287.     printf("\nDeseja Adicionar aos favoritos?: (1) PARA SIM (0) PARA NÃO \n\n");
  288.     scanf("%d", &op);
  289.  
  290.     if (op == 1) {
  291.         strcpy(favoritos[contadorfav].nome, agenda[contador].nome);
  292.         strcpy(favoritos[contadorfav].cpf, agenda[contador].cpf);
  293.         strcpy(favoritos[contadorfav].endereco, agenda[contador].endereco);
  294.         favoritos[contadorfav].tipo = agenda[contador].tipo;
  295.  
  296.         printf("\nAdicionado com sucesso!!!\n\n");
  297.  
  298.         contadorfav++;
  299.         return contadorfav;
  300.     }
  301.  
  302. }
  303.  
  304. void listar_favoritos(int contadorfav) {
  305.  
  306.     char nome[50];
  307.     char cpf[50];
  308.     char endereco[50];
  309.     tipo tipo = eletrico;
  310.     int i, j;
  311.  
  312.     for (i = 1; i < contadorfav; i++) {
  313.         for (j = 1; j < contadorfav; j++) {
  314.             if (strcmp(favoritos[j - 1].nome, favoritos[j].nome) > 0) {
  315.                 strcpy(nome, favoritos[j - 1].nome);
  316.                 strcpy(favoritos[j - 1].nome, favoritos[j].nome);
  317.                 strcpy(favoritos[j].nome, nome);
  318.  
  319.                 strcpy(cpf, favoritos[j - 1].cpf);
  320.                 strcpy(favoritos[j - 1].cpf, favoritos[j].cpf);
  321.                 strcpy(favoritos[j].cpf, cpf);
  322.  
  323.                 strcpy(endereco, favoritos[j - 1].endereco);
  324.                 strcpy(favoritos[j - 1].endereco, favoritos[j].endereco);
  325.                 strcpy(favoritos[j].endereco, endereco);
  326.  
  327.                 tipo = favoritos[j - 1].tipo;
  328.                 favoritos[j - 1].tipo = favoritos[j].tipo;
  329.                 favoritos[j].tipo = tipo;
  330.             }
  331.         }
  332.     }
  333.  
  334.     for (i = 0; i < contadorfav; i++) {
  335.         printf("\nNome do individuo: %s\n", favoritos[i].nome);
  336.         printf("CPF: %s\n", favoritos[i].cpf);
  337.         printf("Endereco: %s\n", favoritos[i].endereco);
  338.         printf("Tipo de Engenheiro: %s\n\n", getTipoEngenheiro(favoritos[i].tipo));
  339.     }
  340. }
  341.  
  342. int inicializar(int contador) {
  343.     strcpy(agenda[0].nome, "Gustavo");              // Essa Função Pré estabelece os engenheiros já cadastrados no sistema.
  344.     strcpy(agenda[0].cpf, "123456789");
  345.     strcpy(agenda[0].endereco, "Rua");
  346.     agenda[0].tipo = computacao;
  347.     contador++;
  348.  
  349.     strcpy(agenda[1].nome, "Ruan");
  350.     strcpy(agenda[1].cpf, "123456789");
  351.     strcpy(agenda[1].endereco, "Rua");
  352.     agenda[1].tipo = computacao;
  353.     contador++;
  354.  
  355.     strcpy(agenda[2].nome, "Amanda");
  356.     strcpy(agenda[2].cpf, "123456789");
  357.     strcpy(agenda[2].endereco, "Rua");
  358.     agenda[2].tipo = computacao;
  359.     contador++;
  360.  
  361.     strcpy(agenda[3].nome, "Beto");
  362.     strcpy(agenda[3].cpf, "123456789");
  363.     strcpy(agenda[3].endereco, "Rua");
  364.     agenda[3].tipo = computacao;
  365.     contador++;
  366.  
  367.     strcpy(agenda[4].nome, "Pedro");
  368.     strcpy(agenda[4].cpf, "123456789");
  369.     strcpy(agenda[4].endereco, "Rua");
  370.     agenda[4].tipo = computacao;
  371.     contador++;
  372.  
  373.     strcpy(agenda[5].nome, "Tiago");
  374.     strcpy(agenda[5].cpf, "777777");
  375.     strcpy(agenda[5].endereco, "RuaAlpaca");
  376.     agenda[5].tipo = eletrico;
  377.     contador++;
  378.  
  379.     strcpy(agenda[6].nome, "Olivia");
  380.     strcpy(agenda[6].cpf, "123456789");
  381.     strcpy(agenda[6].endereco, "Rua");
  382.     agenda[6].tipo = computacao;
  383.     contador++;
  384.  
  385.     strcpy(agenda[7].nome, "Igor");
  386.     strcpy(agenda[7].cpf, "123456789");
  387.     strcpy(agenda[7].endereco, "Rua");
  388.     agenda[7].tipo = computacao;
  389.     contador++;
  390.  
  391.     strcpy(agenda[8].nome, "Xayah");
  392.     strcpy(agenda[8].cpf, "123456789");
  393.     strcpy(agenda[8].endereco, "Rua");
  394.     agenda[8].tipo = computacao;
  395.     contador++;
  396.  
  397.     strcpy(agenda[9].nome, "Junior");
  398.     strcpy(agenda[9].cpf, "123456789");
  399.     strcpy(agenda[9].endereco, "Rua");
  400.     agenda[9].tipo = computacao;
  401.     contador++;
  402.  
  403.     return contador;
  404. }
  405.  
  406. char* getTipoEngenheiro(tipo enumerador) { // Função para retornar String.
  407.     static char tipo[50];
  408.  
  409.     if (enumerador == eletrico) {
  410.         strcpy(tipo, "Elétrico");
  411.     }
  412.     else if (enumerador == computacao) {
  413.         strcpy(tipo, "Computação");
  414.     }
  415.     else if (enumerador == civil) {
  416.         strcpy(tipo, "Civil");
  417.     }
  418.     else if (enumerador == mecanico) {
  419.         strcpy(tipo, "Mecanico");
  420.     }
  421.     else if (enumerador == quimico) {
  422.         strcpy(tipo, "Quimico");
  423.     }
  424.     return tipo;
  425. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement