Advertisement
SergioRP

Untitled

Nov 1st, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 19.22 KB | None | 0 0
  1. //
  2. //  Sistema de Controle de Clientes
  3. //
  4. //  Criado por Sergio Toledo Piza
  5. //  Copyright © 2016. Nenhum direito reservado.
  6. //
  7. //  Agradecimentos especiais ao Google e ao Stackoverflow
  8. //
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <conio.h>
  13. #include <ctype.h>
  14. #include <locale.h>
  15. #include <string.h>
  16.  
  17. struct client {
  18.     int id;
  19.     char name[30];
  20.     char address[50];
  21.     char city[30];
  22.     char country[20];
  23.     char cep[10];
  24.     char dob[15];
  25.     char phone[16];
  26.     float total;
  27. };
  28.  
  29. int i, j, total_clients = 0, capacity = 1;
  30. struct client a[1];
  31.  
  32. float strToFloat(char str[]) {
  33.  
  34.     int size = strlen(str);
  35.     char str2[size];
  36.  
  37.     for (i = 0; str[i]; i++) {
  38.         if (str[i] == '.') {
  39.             str2[i] = ',';
  40.         } else str2[i] = str[i];
  41.     }
  42.     str2[size] = '\0';
  43.  
  44.     printf("\n%.2f\n", strtod("30,30", NULL));
  45.  
  46.     return strtod(str2, NULL);
  47. }
  48.  
  49. void readLine(char *line, struct client *client) {
  50.  
  51.     int substr_end = 0, info_id = 1;
  52.     int line_size = strlen(line);
  53.  
  54.     char substr[line_size];
  55.  
  56.     for (i = 0; i < line_size; i++) {
  57.         char ch = line[i];
  58.  
  59.         if (ch != ';' && ch != '\n' && ch != EOF) {
  60.             substr[substr_end] = ch;
  61.             substr_end++;
  62.         } else {
  63.             substr[substr_end] = '\0';
  64.  
  65.             //printf("%s %i\n", substr, info_id);
  66.  
  67.             switch(info_id) {
  68.  
  69.             case 1: // id
  70.                 client->id = atoi(substr);
  71.                 break;
  72.  
  73.             case 2: // name
  74.                 strcpy(client->name, substr);
  75.                 break;
  76.  
  77.             case 3:
  78.                 strcpy(client->address, substr);
  79.                 break;
  80.  
  81.             case 4: // city
  82.                 strcpy(client->city, substr);
  83.                 break;
  84.  
  85.             case 5: // country
  86.                 strcpy(client->country, substr);
  87.                 break;
  88.  
  89.             case 6: // cep
  90.                 strcpy(client->cep, substr);
  91.                 break;
  92.  
  93.             case 7: //date of birth
  94.                 strcpy(client->dob, substr);
  95.                 break;
  96.  
  97.             case 8: // phone number
  98.                 strcpy(client->phone, substr);
  99.                 break;
  100.  
  101.             case 9: // sales total
  102.                 client->total = strToFloat(substr);
  103.                 break;
  104.  
  105.             default:
  106.                 i = line_size;
  107.                 break;
  108.  
  109.             }
  110.  
  111.             substr_end = 0;
  112.             substr[0] = '\0';
  113.             info_id++;
  114.         }
  115.  
  116.     }
  117. }
  118.  
  119. void printClient(struct client client) {
  120.  
  121.     printf("Cliente %s:\n", client.name);
  122.     printf("- ID: %i\n", client.id);
  123.     printf("- Endereço: %s\n", client.address);
  124.     printf("- Cidade: %s\n", client.city);
  125.     printf("- País: %s\n", client.country);
  126.     printf("- CEP: %s\n", client.cep);
  127.     printf("- Data de Nascimento: %s\n", client.dob);
  128.     printf("- Telefone: %s\n", client.phone);
  129.     printf("- Total Vendas: R$%.2f\n", client.total);
  130.  
  131. }
  132.  
  133. void growArray(struct client **a, int total, int *capacity) {
  134.     if (total == *capacity) {
  135.         *capacity *= 2;
  136.         *a = (struct client*)realloc(*a, *capacity*sizeof(struct client));
  137.     }
  138. }
  139.  
  140. struct client addClient(struct client **a, int *total, int *capacity) {
  141.     struct client newClient = {
  142.         *total + 1,
  143.         "",
  144.         "",
  145.         "",
  146.         "",
  147.         "",
  148.         "",
  149.         "",
  150.         0
  151.     };
  152.     printf("ID do novo cliente: %i\n", *total + 1);
  153.  
  154.     //newClient.id = *total + 1;
  155.  
  156.     while (strlen(newClient.name) < 1) {
  157.         printf("Digite o nome do novo cliente: ");
  158.         gets(newClient.name);
  159.         fflush(stdin);
  160.  
  161.         if (strlen(newClient.name) > 0)
  162.             break;
  163.  
  164.         printf("Digite um nome!\n");
  165.     }
  166.  
  167.     while (strlen(newClient.address) < 1) {
  168.         printf("Digite o endereço do novo cliente: ");
  169.         gets(newClient.address);
  170.         fflush(stdin);
  171.  
  172.         if (strlen(newClient.address) > 0)
  173.             break;
  174.  
  175.         printf("Digite um endereço!\n");
  176.     }
  177.  
  178.  
  179.  
  180.     while (strlen(newClient.city) < 1) {
  181.         printf("Digite a cidade do novo cliente: ");
  182.         gets(newClient.city);
  183.         fflush(stdin);
  184.  
  185.         if (strlen(newClient.city) > 0)
  186.             break;
  187.  
  188.         printf("Digite uma cidade!\n");
  189.     }
  190.  
  191.     while (strlen(newClient.country) < 1) {
  192.         printf("Digite o país do novo cliente: ");
  193.         gets(newClient.country);
  194.         fflush(stdin);
  195.  
  196.         if (strlen(newClient.country) > 0)
  197.             break;
  198.  
  199.         printf("Digite um país!\n");
  200.     }
  201.  
  202.     while (strlen(newClient.cep) < 9) {
  203.         printf("Digite o cep do novo cliente (12345-678): ");
  204.         gets(newClient.cep);
  205.         fflush(stdin);
  206.  
  207.         if (strlen(newClient.cep) >= 9)
  208.             break;
  209.  
  210.         printf("Digite um cep válido!\n");
  211.     }
  212.  
  213.     while(strlen(newClient.dob) != 10) {
  214.         printf("Digite a data de nascimento do novo cliente (dd/mm/aaaa): ");
  215.         gets(newClient.dob);
  216.         fflush(stdin);
  217.  
  218.         if (strlen(newClient.dob) == 10)
  219.             break;
  220.  
  221.         printf("Digite uma data válida!\n");
  222.     }
  223.  
  224.     while (strlen(newClient.phone) < 10) {
  225.         printf("Digite o telefone do novo cliente ( (00) 99999-9999 ): ");
  226.         gets(newClient.phone);
  227.         fflush(stdin);
  228.  
  229.         if (strlen(newClient.phone) >= 10)
  230.             break;
  231.  
  232.         printf("Digite um telefone válido!\n");
  233.     }
  234.  
  235.     char aux[10];
  236.     do {
  237.         printf("Digite o total de vendas do novo cliente: ");
  238.  
  239.         gets(aux);
  240.         fflush(stdin);
  241.  
  242.         if (strlen(aux) > 0)
  243.             break;
  244.  
  245.         printf("Digite um valor!\n");
  246.  
  247.     } while (strlen(aux) < 1);
  248.  
  249.     newClient.total = strToFloat(aux);
  250.  
  251.     system("cls");
  252.     printClient(newClient);
  253.  
  254.  
  255.     while (1) {
  256.         printf("\nDeseja adicionar esse cliente? <s/n>: ");
  257.  
  258.         switch(tolower(getche())) {
  259.             case 's':
  260.                 *total += 1;
  261.                 growArray(a, *total, capacity);
  262.                 return newClient;
  263.                 break;
  264.             case 'n':
  265.                 newClient.id = -1;
  266.                 return newClient;
  267.                 break;
  268.             default:
  269.                 printf("\nDigite um valor válido!");
  270.                 break;
  271.         }
  272.     }
  273.  
  274. }
  275.  
  276. void printAllClients(struct client *a, int total, char sort_by) {
  277.  
  278.     struct client b[total];
  279.     for (i = 0; i < total; i++)
  280.         b[i] = a[i]; // To not modify the main array
  281.  
  282.     if (sort_by == 'i') {
  283.         //https://www.vivaolinux.com.br/script/Ordenacao-QuickSort
  284.         struct client aux;
  285.         int left = 0, right = total - 1, x;
  286.  
  287.         quick:
  288.         x = left;
  289.         for(i = left + 1; i <= right; i++){
  290.             j = i;
  291.             if(b[j].id < b[x].id){
  292.                 aux = b[j];
  293.                 while(j > x){
  294.                     b[j] = b[j-1];
  295.                     j--;
  296.                 }
  297.                 b[j] = aux;
  298.                 x++;
  299.             }
  300.         }
  301.         if(x-1 >= left){
  302.             right = x - 1;
  303.             goto quick;
  304.         }
  305.         if(x+1 <= right){
  306.             left = x + 1;
  307.             goto quick;
  308.         }
  309.  
  310.         for (i = 0; i < total; i++) {
  311.             printf("ID: %i; Nome: %s; Endereço: %s; Cidade: %s; País: %s; Cep: %s; Data de nascimento: %s; Telefone: %s; Total vendas: R$%.2f\n\n", b[i].id, b[i].name, b[i].address, b[i].city, b[i].country, b[i].cep, b[i].dob, b[i].phone, b[i].total);
  312.         }
  313.     }
  314.  
  315.     if (sort_by == 'n') {
  316.         for (j = 0; j < total - 1; j++) {
  317.             for (i = 0; i < total - 1; i++) {
  318.                 if (0 < strcmp(b[i].name, b[i +1].name)) {
  319.                     struct client pchTemp = b[i];
  320.                     b[i] = b[i +1];
  321.                     b[i +1] = pchTemp;
  322.                 }
  323.             }
  324.         }
  325.         for (i = 0; i < total; i++) {
  326.             printf("Nome: %s; ID: %i; Endereço: %s; Cidade: %s; País: %s; Cep: %s; Data de nascimento: %s; Telefone: %s; Total vendas: R$%.2f\n\n", b[i].name, b[i].id, b[i].address, b[i].city, b[i].country, b[i].cep, b[i].dob, b[i].phone, b[i].total);
  327.         }
  328.     }
  329.  
  330.     if (sort_by == 't') {
  331.         // By Sergio Toledo Piza
  332.         for (i = 0; i < total; i++) {
  333.             int lower_index = i;
  334.             float lower_number = b[i].total;
  335.  
  336.             for (j = i + 1; j < total; j++) {
  337.                 if (b[j].total < lower_number) {
  338.                     lower_index = j;
  339.                     lower_number = b[j].total;
  340.                 }
  341.             }
  342.  
  343.             struct client x = b[i];
  344.             b[i] = b[lower_index];
  345.             b[lower_index] = x;
  346.         }
  347.         for (i = 0; i < total; i++) {
  348.             printf("Total vendas: R$%.2f; ID: %i; Endereço: %s; Nome: %s; Cidade: %s; País: %s; Cep: %s; Data de nascimento: %s; Telefone: %s;\n\n", b[i].total, b[i].id, b[i].name, b[i].address, b[i].city, b[i].country, b[i].cep, b[i].dob, b[i].phone);
  349.         }
  350.     }
  351. }
  352.  
  353. char menu() {
  354.     printf("*********************************\n");
  355.     printf("**     Sistema de Controle     **\n");
  356.     printf("**         de Clientes         **\n");
  357.     printf("**                             **\n");
  358.     printf("**          Feito por:         **\n");
  359.     printf("**      Sergio Toledo Piza     **\n");
  360.     printf("*********************************\n");
  361.     printf("**                             **\n");
  362.     printf("**   1 -   Adicionar cliente   **\n");
  363.     printf("**   2 -   Modificar cliente   **\n");
  364.     printf("**   3 -   Excluir cliente     **\n");
  365.     printf("**   4 -   Pesquisar cliente   **\n");
  366.     printf("**   5 -   Exibir clientes     **\n");
  367.     printf("**   Esc - Sair do programa    **\n");
  368.     printf("**                             **\n");
  369.     printf("*********************************\n\n");
  370.     printf("Digite sua opção: ");
  371.     return getche();
  372. }
  373.  
  374. int saveFile(struct client *a, char *filename) {
  375.     FILE *f = fopen(filename, "w");
  376.     if (f == NULL) {
  377.         printf("Erro ao salvar arquivo!\n");
  378.         return;
  379.     }
  380.     printf("Aguarde! Salvando arquivo...\n");
  381.     int hasDeletedClients = 0;
  382.  
  383.     for (i = 0; a[i].id; i++) {
  384.         if (a[i].id != i + 1 && a[i].id != -1) {
  385.             a[i].id = i + 1;
  386.             hasDeletedClients = 1;
  387.         }
  388.  
  389.         if (a[i].id != -1)
  390.             fprintf(f, "%i;%s;%s;%s;%s;%s;%s;%s;%.2f\n", a[i].id, a[i].name, a[i].address, a[i].city, a[i].country, a[i].cep, a[i].dob, a[i].phone, a[i].total);
  391.         else
  392.             hasDeletedClients = 1;
  393.  
  394.     }
  395.     printf("Arquivo salvo!\n");
  396.     fclose(f);
  397.     return hasDeletedClients;
  398. }
  399.  
  400. char *strtolower(char str[]) {
  401.     char *x = str;
  402.     for (j = 0; j < x[j]; j++) {
  403.         x[j] = tolower(x[j]);
  404.     }
  405.     return x;
  406. }
  407.  
  408. struct client searchClient(struct client *a, int id, char name[], char searchType) {
  409.     struct client notFound;
  410.     notFound.id = -1;
  411.     if (id == 0 && searchType == 'i') {
  412.         printf("Operação de busca cancelada!\n");
  413.         return notFound;
  414.     }
  415.  
  416.     for (i = 0; a[i].id; i++) {
  417.         if (searchType == 'i') {
  418.             if (a[i].id == id) {
  419.                 return a[i];
  420.             }
  421.         } else {
  422.             char name_search[30]; // To not directly modify the client's name
  423.             strcpy(name_search, a[i].name);
  424.             if (strcmp(strtolower(name_search), strtolower(name)) == 0) {
  425.                 return a[i];
  426.             }
  427.         }
  428.     }
  429.  
  430.     return notFound;
  431. }
  432.  
  433. int editClient(struct client *a, int client_id) {
  434.     if (a->id != client_id) {
  435.         printf("Erro ao processar ID do cliente!\n");
  436.         return 1;
  437.     }
  438.  
  439.     int didPressEsc = 0;
  440.  
  441.     editClientForm:
  442.     printf("Modificando ");
  443.     printClient(*a);
  444.     printf("\n");
  445.     printf("********************************************************\n");
  446.     printf("* 1: Endereço  4: CEP                  7: Total Vendas *\n");
  447.     printf("* 2: Cidade    5: Data de Nascimento   Esc: Voltar     *\n");
  448.     printf("* 3: País      6: Número de Telefone                   *\n");
  449.     printf("********************************************************\n");
  450.     printf("\nDigite o número do campo que deseja modificar: ");
  451.     char aux = getche();
  452.     char aux2[50];
  453.  
  454.     system("cls");
  455.     printf("Modificando Cliente %s:\n", a->name);
  456.     printf("Campo: ");
  457.     switch (aux) {
  458.         case '1':
  459.             printf("endereço.\n");
  460.             printf("Valor atual: %s\n", a->address);
  461.             printf("Novo valor: ");
  462.             gets(aux2);
  463.             fflush(stdin);
  464.  
  465.             if (strlen(aux2) == 0)
  466.                 strcpy(aux2, a->address);
  467.  
  468.             strcpy(a->address, aux2);
  469.             printf("Endereço atualizado!\n");
  470.             break;
  471.  
  472.         case '2':
  473.             printf("cidade.\n");
  474.             printf("Valor atual: %s\n", a->city);
  475.             printf("Novo valor: ");
  476.             gets(aux2);
  477.             fflush(stdin);
  478.  
  479.             if (strlen(aux2) == 0)
  480.                 strcpy(aux2, a->city);
  481.  
  482.             strcpy(a->city, aux2);
  483.             printf("Cidade atualizada!\n");
  484.             break;
  485.  
  486.         case '3':
  487.             printf("país.\n");
  488.             printf("Valor atual: %s\n", a->country);
  489.             printf("Novo valor: ");
  490.             gets(aux2);
  491.             fflush(stdin);
  492.  
  493.             if (strlen(aux2) == 0)
  494.                 strcpy(aux2, a->country);
  495.  
  496.             strcpy(a->country, aux2);
  497.             printf("País atualizado!\n");
  498.             break;
  499.  
  500.         case '4':
  501.             printf("CEP.\n");
  502.             printf("Valor atual: %s\n", a->cep);
  503.             printf("Novo valor (12345-678): ");
  504.             gets(aux2);
  505.             fflush(stdin);
  506.  
  507.             if (strlen(aux2) == 0)
  508.                 strcpy(aux2, a->cep);
  509.  
  510.             strcpy(a->cep, aux2);
  511.             printf("CEP atualizado!\n");
  512.             break;
  513.  
  514.         case '5':
  515.             printf("data de nascimento.\n");
  516.             printf("Valor atual: %s\n", a->dob);
  517.             printf("Novo valor (dd/mm/aaaa): ");
  518.             gets(aux2);
  519.             fflush(stdin);
  520.  
  521.             if (strlen(aux2) == 0)
  522.                 strcpy(aux2, a->dob);
  523.  
  524.             strcpy(a->dob, aux2);
  525.             printf("Data de nascimento atualizada!\n");
  526.             break;
  527.  
  528.         case '6':
  529.             printf("telefone.\n");
  530.             printf("Valor atual: %s\n", a->phone);
  531.             printf("Novo valor (99) 99999-9999: ");
  532.             gets(aux2);
  533.             fflush(stdin);
  534.  
  535.             if (strlen(aux2) == 0)
  536.                 strcpy(aux2, a->phone);
  537.  
  538.             strcpy(a->phone, aux2);
  539.             printf("Telefone atualizado!\n");
  540.             break;
  541.  
  542.         case '7':
  543.             printf("total vendas.\n");
  544.             printf("Valor atual: R$%.2f\n", a->total);
  545.             printf("Novo valor: ");
  546.             gets(aux2);
  547.             fflush(stdin);
  548.  
  549.             if (strlen(aux2) != 0)
  550.                 a->total = strToFloat(aux2);
  551.  
  552.             printf("Total vendas atualizado!\n");
  553.             break;
  554.  
  555.         case 27:
  556.             system("cls");
  557.             printf("Operação cancelada!\n");
  558.             didPressEsc = 1;
  559.             break;
  560.  
  561.         default:
  562.             printf("inválido.\n");
  563.             system("PAUSE");
  564.             system("cls");
  565.             goto editClientForm;
  566.             break;
  567.     }
  568.     return didPressEsc;
  569. }
  570.  
  571. int getNumberFromString() {
  572.     char strn[30];
  573.     gets(strn);
  574.     fflush(stdin);
  575.     return atoi(strn);
  576. }
  577.  
  578. int deleteClient(struct client *a, int client_id) {
  579.     int error = 0;
  580.     if (a->id != client_id) {
  581.         printf("Erro ao processar ID do cliente!\n");
  582.         return 1;
  583.     }
  584.     printClient(*a);
  585.     delete_prompt:
  586.     printf("\nDeseja mesmo apagar este cliente? <s/n>: ");
  587.     switch (tolower(getche())) {
  588.  
  589.         case 's':
  590.             printf("\nCliente %s apagado!\n", a->name);
  591.             a->id = -1;
  592.             return 0;
  593.             break;
  594.  
  595.         case 'n':
  596.             printf("\nOperação cancelada!\n");
  597.             return 1;
  598.             break;
  599.  
  600.         default:
  601.             printf("\nDigite uma opção válida!");
  602.             goto delete_prompt;
  603.             break;
  604.     }
  605.  
  606.     printf("\nErro ao deletar cliente.\n");
  607.     return 1;
  608. }
  609.  
  610. char getche_and_clear() {
  611.     char ch = getche();
  612.     system("cls");
  613.     return ch;
  614. }
  615.  
  616. int empty_clients(int total) {
  617.     if (total == 0) {
  618.         printf("Não existe nenhum cliente registrado!\n");
  619.         return 1;
  620.     }
  621.     return 0;
  622. }
  623.  
  624. int main()
  625. {
  626.     setlocale(LC_ALL, "portuguese");
  627.  
  628.  
  629.     FILE* fh;
  630.     char filename[30];
  631.     printf("*********************************\n");
  632.     printf("**     Sistema de Controle     **\n");
  633.     printf("**         de Clientes         **\n");
  634.     printf("*********************************\n\n");
  635.     printf("Inicializando programa...\n");
  636.  
  637.     file_open:
  638.     strcpy(filename, "clientes.csv");
  639.     printf("Deseja abrir o arquivo \"%s\"? <s/n>: ", filename);
  640.  
  641.     switch (tolower(getche())) {
  642.         case 's':
  643.             system("cls");
  644.             break;
  645.  
  646.         case 'n':
  647.             printf("\nDigite o nome do arquivo que deseja abrir: ");
  648.             gets(filename);
  649.             fflush(stdin);
  650.             break;
  651.  
  652.         default:
  653.             printf("\nDigite uma opção válida!\n");
  654.             goto file_open;
  655.             break;
  656.     }
  657.  
  658.     goto processClientsArray;
  659.  
  660.     reProcessClientsArray:
  661.         printf("Corrigindo problemas...\n");
  662.         printf("Pressione qualquer tecla para recarregar o arquivo.\n");
  663.         system("PAUSE");
  664.         total_clients = 0;
  665.         system("cls");
  666.  
  667.     processClientsArray:
  668.     fh = fopen(filename, "r");
  669.     //check if file exists
  670.     if (fh == NULL){
  671.         printf("Arquivo \"%s\" não encontrado!\n", filename);
  672.         printf("Pressione qualquer tecla para continuar.\n");
  673.         getch();
  674.         goto file_open;
  675.     }
  676.  
  677.     //http://stackoverflow.com/questions/3501338/c-read-file-line-by-line
  678.     //read line by line
  679.     const size_t line_size = 500;
  680.     char* line = malloc(line_size);
  681.  
  682.     total_clients = 0;
  683.     struct client* a = (struct client*)malloc(capacity*sizeof(struct client));
  684.  
  685.     while (fgets(line, line_size, fh) != NULL)  {
  686.         growArray(&a, total_clients, &capacity);
  687.         readLine(line, &a[total_clients]);
  688.         total_clients++;
  689.     }
  690.     if (total_clients == 0)
  691.         a[0].id = -1;
  692.  
  693.     saveFile(a, filename);
  694.     system("cls");
  695.     free(line);
  696.     fclose(fh);
  697.     //http://stackoverflow.com/questions/3501338/c-read-file-line-by-line
  698.  
  699.     char opcao;
  700.     do {
  701.         opcao = menu();
  702.         int fileEdited = 0;
  703.         struct client aux;
  704.  
  705.         system("cls");
  706.  
  707.         if (opcao == 27)
  708.             break;
  709.  
  710.         switch(opcao) {
  711.             case '1':
  712.                 aux = addClient(&a, &total_clients, &capacity);
  713.                 printf("\n");
  714.                 if (aux.id != -1) {
  715.                     printf("Cliente %s adicionado!\n", aux.name);
  716.                     a[total_clients - 1] = aux;
  717.                     fileEdited = 1;
  718.                 } else {
  719.                     printf("Operação cancelada!\n");
  720.                 }
  721.                 break;
  722.  
  723.             case '2':
  724.                 if (empty_clients(total_clients))
  725.                     break;
  726.  
  727.                 printf("Digite o código (id) do cliente que deseja modificar: ");
  728.                 aux = searchClient(a, getNumberFromString(), "", 'i');
  729.                 int didPressEsc;
  730.                 clientEdit:
  731.                 didPressEsc = editClient(&a[aux.id - 1], aux.id);
  732.  
  733.                 if (didPressEsc == 1)
  734.                     break;
  735.  
  736.                 if (saveFile(a, filename) == 1) {
  737.                     fileEdited = 1;
  738.                     printf("O programa encontrou uma inconveniência com o ID dos clientes e irá resolver o problema!\n");
  739.                     break;
  740.                 }
  741.  
  742.                 getEdit:
  743.                 printf("Deseja alterar mais um campo do cliente %s? <s/n>: ", aux.name);
  744.                 switch(tolower(getche())) {
  745.                     case 's':
  746.                         system("cls");
  747.                         goto clientEdit;
  748.                         break;
  749.  
  750.                     case 'n':
  751.                         break;
  752.  
  753.                     default:
  754.                         printf("\nDigite um valor válido!\n");
  755.                         goto getEdit;
  756.                         break;
  757.                 }
  758.                 //fileEdited = 1;
  759.                 break;
  760.  
  761.             case '3':
  762.                 if (empty_clients(total_clients))
  763.                     break;
  764.  
  765.                 printf("Digite o ID do cliente que deseja apagar: ");
  766.                 aux = searchClient(a, getNumberFromString(), "", 'i');
  767.                 if (deleteClient(&a[aux.id - 1], aux.id) == 0) { // if didn't have errors
  768.                     fileEdited = 1;
  769.                 }
  770.                 break;
  771.  
  772.             case '4':
  773.                 if (empty_clients(total_clients))
  774.                     break;
  775.  
  776.                 pesquisaCliente:
  777.  
  778.                     printf("1 - Pesquisa por código (id)\n");
  779.                     printf("2 - Pesquisa por nome\n");
  780.                     printf("Esc - Voltar\n\n");
  781.  
  782.                     printf("Digite sua opção: ");
  783.                     char aux3[30];
  784.                     int should_break = 0;
  785.  
  786.                     switch(getche_and_clear()) {
  787.                         case 27:
  788.                             printf("Operação cancelada!\n");
  789.                             should_break = 1;
  790.                             break;
  791.  
  792.                         case '1':
  793.                             printf("Digite o código do cliente: ");
  794.                             aux = searchClient(a, getNumberFromString(), "", 'i');
  795.                             break;
  796.  
  797.                         case '2':
  798.                             printf("Digite o nome do cliente: ");
  799.                             scanf("%[^\n]s", aux3); //gets(aux3); // Was causing minor bugs
  800.                             aux3[strlen(aux3)] = '\0';
  801.                             fflush(stdin);
  802.                             aux = searchClient(a, 0, aux3, 'n');
  803.                             break;
  804.  
  805.                         default:
  806.                             printf("Opção inválida!\n");
  807.                             system("PAUSE");
  808.                             system("cls");
  809.                             goto pesquisaCliente;
  810.                             break;
  811.  
  812.                     }
  813.  
  814.                 if (should_break)
  815.                     break;
  816.  
  817.                 if (aux.id != -1)
  818.                     printClient(aux);
  819.                 else
  820.                     printf("Cliente não encontrado.\n");
  821.  
  822.                 break;
  823.  
  824.             case '5':
  825.                 if (empty_clients(total_clients))
  826.                     break;
  827.  
  828.                 getOrder:
  829.  
  830.                 printf("Ordenar clientes por:\n");
  831.                 printf("1 - ID (código)\n");
  832.                 printf("2 - Nome\n");
  833.                 printf("3 - Total vendas\n");
  834.                 printf("Esc - Voltar\n\n");
  835.  
  836.                 printf("Digite sua opção: ");
  837.                 switch(getche_and_clear()) {
  838.                     case '1':
  839.                         printf("Ordenação por id:\n");
  840.                         printAllClients(a, total_clients, 'i');
  841.                         break;
  842.  
  843.                     case '2':
  844.                         printf("Ordenação por nome:\n");
  845.                         printAllClients(a, total_clients, 'n');
  846.                         break;
  847.  
  848.                     case '3':
  849.                         printf("Ordenação por total de vendas:\n");
  850.                         printAllClients(a, total_clients, 't');
  851.                         break;
  852.  
  853.                     case 27:
  854.                         printf("Operação cancelada!\n");
  855.                         break;
  856.  
  857.                     default:
  858.                         printf("Digite uma opção válida!\n");
  859.                         system("PAUSE");
  860.                         system("cls");
  861.                         goto getOrder;
  862.                         break;
  863.                 }
  864.                 break;
  865.  
  866.             default:
  867.                 printf("Opção inválida!\n", opcao);
  868.                 break;
  869.         }
  870.  
  871.         if (fileEdited == 1) {
  872.             if (saveFile(a, filename) == 1) {
  873.                 goto reProcessClientsArray;
  874.             }
  875.         }
  876.  
  877.         system("pause");
  878.         system("cls");
  879.     } while(1);
  880.  
  881.     printf("Encerrando programa...\n");
  882.     saveFile(a, filename);
  883.     printf("Programa finalizado com sucesso!\n");
  884.  
  885.     system("PAUSE");
  886.     return 0;
  887. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement