Advertisement
SergioRP

Projeto Edilson

Nov 27th, 2016
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.47 KB | None | 0 0
  1. //
  2. //  Sistema de Controle de Pacientes
  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 patient {
  18.     int id;
  19.     char name[30];
  20.     char dob[11];
  21.     float gluc;
  22.     float chol;
  23.     float w;
  24.     float h;
  25.     char gender;
  26. };
  27.  
  28. int i, j, total_patients = 0, capacity = 1;
  29. struct patient a[1];
  30.  
  31. float strToFloat(char str[]) {
  32.  
  33.     int size = strlen(str);
  34.     char str2[size];
  35.  
  36.     for (i = 0; str[i]; i++) {
  37.         if (str[i] == '.') {
  38.             str2[i] = ',';
  39.         } else str2[i] = str[i];
  40.     }
  41.     str2[size] = '\0';
  42.  
  43.     //printf("\n%.2f\n", strtod("30,30", NULL));
  44.  
  45.     return strtod(str2, NULL);
  46. }
  47.  
  48. void readLine(char *line, struct patient *patient) {
  49.  
  50.     char *substr;
  51.     int info_id = 1;
  52.     substr = strtok(line, ";");
  53.     while (substr != NULL) {
  54.  
  55.         switch(info_id) {
  56.  
  57.             case 1: // id
  58.                 patient->id = atoi(substr);
  59.                 break;
  60.  
  61.             case 2: // name
  62.                 strcpy(patient->name, substr);
  63.                 printf("%s\n", (substr));
  64.                 break;
  65.  
  66.             case 3: // date of birth
  67.                 strcpy(patient->dob, substr);
  68.                 break;
  69.  
  70.             case 4: // glucose
  71.                 patient->gluc = strToFloat(substr);
  72.                 printf("%s\n", (substr));
  73.                 break;
  74.  
  75.             case 5: // cholesterol
  76.                 patient->chol = strToFloat(substr);
  77.                 break;
  78.  
  79.             case 6: // weight
  80.                 patient->w = strToFloat(substr);
  81.                 break;
  82.  
  83.             case 7: // height
  84.                 patient->h = strToFloat(substr);
  85.                 break;
  86.  
  87.             case 8: // gender
  88.                 patient->gender = (substr[0]);
  89.                 break;
  90.  
  91.             default:
  92.                 break;
  93.  
  94.         }
  95.  
  96.         info_id++;
  97.         substr = strtok(NULL, ";");
  98.     }
  99.     return 0;
  100. }
  101.  
  102. void growArray(struct patient **a, int total, int *capacity) {
  103.     if (total == *capacity) {
  104.         *capacity *= 100;
  105.         *a = (struct patient*)realloc(*a, *capacity*sizeof(struct patient));
  106.     }
  107. }
  108.  
  109. void printPatient(struct patient patient) {
  110.  
  111.     printf("Paciente %s:\n", patient.name);
  112.     printf("- ID: %i\n", patient.id);
  113.     printf("- Data de nascimento: %s\n", patient.dob);
  114.     printf("- Glicose: %.2f\n", patient.gluc);
  115.     printf("- Colesterol: %.2f\n", patient.chol);
  116.     printf("- Peso: %.2fkg\n", patient.w);
  117.     printf("- Altura: %.2fm\n", patient.h);
  118.     printf("- Sexo: %c\n", patient.gender);
  119.  
  120. }
  121.  
  122. struct patient addPatient(struct patient **a, int *total, int *capacity) {
  123.     struct patient newPatient = {
  124.         *total + 1,
  125.         "",
  126.         "",
  127.         0,
  128.         0,
  129.         0,
  130.         0,
  131.         '\0'
  132.     };
  133.     printf("ID do novo paciente: %i\n", *total + 1);
  134.  
  135.     //newPatient.id = *total + 1;
  136.  
  137.     while (strlen(newPatient.name) < 1) {
  138.         system("cls");
  139.         printf("Digite o nome do novo paciente: ");
  140.         gets(newPatient.name);
  141.         fflush(stdin);
  142.  
  143.         if (strlen(newPatient.name) > 0)
  144.             break;
  145.  
  146.         printf("Digite um nome!\n");
  147.     }
  148.  
  149.     while(strlen(newPatient.dob) != 10) {
  150.         printf("Digite a data de nascimento do novo paciente (dd/mm/aaaa): ");
  151.         gets(newPatient.dob);
  152.         fflush(stdin);
  153.  
  154.         if (strlen(newPatient.dob) == 10)
  155.             break;
  156.  
  157.         printf("Digite uma data válida!\n");
  158.     }
  159.  
  160.     printf("Digite o nivel de glicose do paciente: ");
  161.     scanf("%f", &newPatient.gluc);
  162.  
  163.     printf("Digite o nivel de colesterol do paciente: ");
  164.     scanf("%f", &newPatient.chol);
  165.  
  166.     printf("Digite o peso do paciente: ");
  167.     scanf("%f", &newPatient.w);
  168.  
  169.     printf("Digite a altura do paciente: ");
  170.     scanf("%f", &newPatient.h);
  171.  
  172.     get_gender:
  173.     printf("Digite o sexo do paciente (m/f): ");
  174.     char patient_gender = getche();
  175.     patient_gender = tolower(patient_gender);
  176.  
  177.     switch(patient_gender) {
  178.         case 'm':
  179.             break;
  180.         case 'f':
  181.             break;
  182.         default:
  183.             printf("Digite um sexo valido!\n");
  184.             goto get_gender;
  185.             break;
  186.     }
  187.  
  188.     newPatient.gender = patient_gender;
  189.  
  190.     system("cls");
  191.     printPatient(newPatient);
  192.  
  193.  
  194.     while (1) {
  195.         printf("\nDeseja adicionar esse paciente? <s/n>: ");
  196.  
  197.         switch(tolower(getche())) {
  198.             case 's':
  199.                 *total += 1;
  200.                 growArray(a, *total, capacity);
  201.                 return newPatient;
  202.                 break;
  203.             case 'n':
  204.                 newPatient.id = -1;
  205.                 return newPatient;
  206.                 break;
  207.             default:
  208.                 printf("\nDigite um valor válido!");
  209.                 break;
  210.         }
  211.     }
  212.  
  213. }
  214.  
  215. void printAllPatients(struct patient *a, int total) {
  216.  
  217.     for (i = 0; i < total; i++) {
  218.         printf("Nome: %s; DN: %s; Glic.: %.2f; Col.: %.2f; Peso: %.2fkg; Altura: %.2fm; Sexo: %c\n\n", a[i].name, a[i].dob, a[i].gluc, a[i].chol, a[i].w, a[i].h, a[i].gender);
  219.     }
  220.  
  221. }
  222.  
  223. char menu() {
  224.     printf("***********************************\n");
  225.     printf("**      Sistema de Controle      **\n");
  226.     printf("**          de Pacientes         **\n");
  227.     printf("**                               **\n");
  228.     printf("**           Feito por:          **\n");
  229.     printf("**       Sergio Toledo Piza      **\n");
  230.     printf("***********************************\n");
  231.     printf("**                               **\n");
  232.     printf("**   1 -  Adicionar Paciente     **\n");
  233.     printf("**   2 -  Gerar IMC              **\n");
  234.     printf("**   3 -  Mostrar Pacientes      **\n");
  235.     printf("**   4 -  Resultados IMC         **\n");
  236.     printf("**   5 -  Pacientes Diabeticos   **\n");
  237.     printf("**   6 -  Sair do Programa       **\n");
  238.     printf("**                               **\n");
  239.     printf("***********************************\n\n");
  240.     printf("Digite sua opção: ");
  241.     return getche();
  242. }
  243.  
  244.  
  245. void saveFile(struct patient *a, char *filename) {
  246.     FILE *f = fopen(filename, "w");
  247.     if (f == NULL) {
  248.         printf("Erro ao salvar arquivo!\n");
  249.         return;
  250.     }
  251.     printf("Aguarde! Salvando arquivo...\n");
  252.  
  253.     for (i = 0; a[i].id; i++) {
  254.         if (a[i].id != -1) {
  255.             if (a[i].id == 1)
  256.                 fprintf(f, "%i;%s;%s;%.2f;%.2f;%.2f;%.2f;%c;\n", a[i].id, a[i].name, a[i].dob, a[i].gluc, a[i].chol, a[i].w, a[i].h, a[i].gender);
  257.             else if (a[i].id == a[i - 1].id + 1)
  258.                 fprintf(f, "%i;%s;%s;%.2f;%.2f;%.2f;%.2f;%c;\n", a[i].id, a[i].name, a[i].dob, a[i].gluc, a[i].chol, a[i].w, a[i].h, a[i].gender);
  259.         }
  260.     }
  261.     printf("Arquivo salvo!\n");
  262.     fclose(f);
  263. }
  264.  
  265. char *strtolower(char str[]) {
  266.     char *x = str;
  267.     for (j = 0; j < x[j]; j++) {
  268.         x[j] = tolower(x[j]);
  269.     }
  270.     return x;
  271. }
  272.  
  273. float imcCalc(float w, float h) {
  274.     return w / (h * h);
  275. }
  276.  
  277. void imcCreate(struct patient *a, int total) {
  278.     FILE *imc_file;
  279.  
  280.     imc_file = fopen("imc.txt", "a+");
  281.     fclose(fopen("imc.txt", "w"));
  282.  
  283.     for (i = 0; i < total; i++)
  284.         fprintf(imc_file, "%s: %.2f\n", a[i].name, imcCalc(a[i].w, a[i].h));
  285.  
  286.     printf("Arquivo imc.txt criado!\n");
  287.  
  288.     fclose(imc_file);
  289. }
  290.  
  291. void imcResults(int total_patients) {
  292.  
  293.     FILE *imc_file;
  294.     imc_file = fopen("imc.txt", "r");
  295.  
  296.     if (imc_file == NULL) {
  297.         printf("Gere o arquivo do IMC no item 2 do menu!\n");
  298.         return;
  299.     }
  300.  
  301.     const size_t imc_line_size = 500;
  302.     char *imc_line = malloc(imc_line_size);
  303.  
  304.     char imc_patients[total_patients][imc_line_size];
  305.  
  306.     i = 0;
  307.     while (fgets(imc_line, imc_line_size, imc_file) != NULL)  {
  308.         strcpy(imc_patients[i], imc_line);
  309.         i++;
  310.     }
  311.  
  312.     for (j = 0; j < total_patients - 1; j++) {
  313.         for (i = 0; i < total_patients - 1; i++) {
  314.             if (0 < strcmp(imc_patients[i], imc_patients[i +1])) {
  315.                 char pchTemp[imc_line_size];
  316.                 strcpy(pchTemp, imc_patients[i]);
  317.                 strcpy(imc_patients[i], imc_patients[i +1]);
  318.                 strcpy(imc_patients[i +1], pchTemp);
  319.             }
  320.         }
  321.     }
  322.     for (i = 0; i < total_patients; i++) {
  323.         printf("%s", imc_patients[i]);
  324.     }
  325.  
  326.     free(imc_line);
  327.     fclose(imc_file);
  328. }
  329.  
  330. int getNumberFromString() {
  331.     char strn[30];
  332.     gets(strn);
  333.     fflush(stdin);
  334.     return atoi(strn);
  335. }
  336.  
  337. char getche_and_clear() {
  338.     char ch = getche();
  339.     system("cls");
  340.     return ch;
  341. }
  342.  
  343. int empty_patients(int total) {
  344.     if (total == 0) {
  345.         printf("Não existe nenhum paciente registrado!\n");
  346.         return 1;
  347.     }
  348.     return 0;
  349. }
  350.  
  351. int main()
  352. {
  353.     setlocale(LC_ALL, "portuguese");
  354.  
  355.     FILE *auth;
  356.  
  357.     auth = fopen("auth.csv", "a+");
  358.  
  359.     //fprintf(auth, "aaa:bbb\n");
  360.  
  361.     char auth_line[50];
  362.     fgets(auth_line, 50, auth);
  363.     int auth_size = strlen(auth_line);
  364.     char auth_pw[50], auth_user[50];
  365.  
  366.     //strcpy(auth_user, auth_line);
  367.  
  368.     int found_usr = 0;
  369.  
  370.     for (i = 0; i < auth_size; i++) {
  371.         if (auth_line[i] == ':') {
  372.             found_usr = 1;
  373.             auth_user[i] == '\0';
  374.             break;
  375.         }
  376.         auth_user[i] = auth_line[i];
  377.     }
  378.  
  379.     if (found_usr) {
  380.         j = strlen(auth_user) + 1;
  381.         //printf("%i", j);
  382.         for (i = 0; i < auth_size; i++) {
  383.             if (auth_line[j + i] == '\n' || auth_line[j + i] == '\0' || auth_line[j + i] == EOF) {
  384.                 auth_pw[i] = '\0';
  385.                 break;
  386.             }
  387.             else
  388.                 auth_pw[i] = auth_line[j + i];
  389.         }
  390.         //printf("%i", strlen(auth_pw));
  391.     }
  392.  
  393.     char login_usr[21], login_pw[21];
  394.  
  395.     if (!found_usr || strlen(auth_pw) == 0) {
  396.  
  397.         do {
  398.             printf("Digite um novo usuario (5 - 20 caracteres): ");
  399.             gets(login_usr);
  400.             fflush(stdin);
  401.         } while (strlen(login_usr) < 5 || strlen(login_usr) > 20);
  402.  
  403.  
  404.         do {
  405.             printf("Digite uma nova senha (5 - 20 caracteres): ");
  406.             gets(login_pw);
  407.             fflush(stdin);
  408.         } while (strlen(login_pw) < 5 || strlen(login_pw) > 20);
  409.  
  410.         fclose(fopen("auth.csv", "w"));
  411.         fprintf(auth, "%s:%s\n", login_usr, login_pw);
  412.  
  413.         system("cls");
  414.     }
  415.  
  416.     else {
  417.         do {
  418.             printf("Usuario: ");
  419.             gets(login_usr);
  420.             fflush(stdin);
  421.             printf("Senha: ");
  422.             gets(login_pw);
  423.             fflush(stdin);
  424.  
  425.             //printf("%i\n%i\n\n", strcmp(login_usr, auth_user), strcmp(login_pw, auth_pw));
  426.             //printf("%s:%s\n", auth_user, auth_pw);
  427.  
  428.             if (strcmp(login_pw, auth_pw) == 0 && strcmp(strtolower(login_usr), strtolower(auth_user)) == 0)
  429.                 break;
  430.             else
  431.                 printf("Login ou senha invalidos!\n");
  432.  
  433.             system("pause");
  434.             system("cls");
  435.         } while (1);
  436.     }
  437.  
  438.     free(auth_line);
  439.     fclose(auth);
  440.  
  441.     system("cls");
  442.  
  443.     FILE* fh;
  444.     char filename[30];
  445.     printf("*********************************\n");
  446.     printf("**     Sistema de Controle     **\n");
  447.     printf("**         de Pacientes        **\n");
  448.     printf("*********************************\n\n");
  449.     printf("Inicializando programa...\n");
  450.  
  451.     strcpy(filename, "patients.csv");
  452.  
  453.     //goto processPatientsArray;
  454.  
  455.     fh = fopen(filename, "a+");
  456.  
  457.     //http://stackoverflow.com/questions/3501338/c-read-file-line-by-line
  458.     //read line by line
  459.     const size_t line_size = 500;
  460.     char* line = malloc(line_size);
  461.  
  462.     total_patients = 0;
  463.     struct patient* a = (struct patient*)malloc(capacity*sizeof(struct patient));
  464.  
  465.     while (fgets(line, line_size, fh) != NULL)  {
  466.         growArray(&a, total_patients, &capacity);
  467.         readLine(line, &a[total_patients]);
  468.         total_patients++;
  469.     }
  470.     if (total_patients == 0)
  471.         a[0].id = -1;
  472.  
  473.     saveFile(a, filename);
  474.     system("cls");
  475.     free(line);
  476.     fclose(fh);
  477.     //http://stackoverflow.com/questions/3501338/c-read-file-line-by-line
  478.  
  479.     char opcao;
  480.     do {
  481.         opcao = menu();
  482.         int fileEdited = 0;
  483.         struct patient aux;
  484.         struct patient diabeticos[total_patients];
  485.         int n_diabeticos;
  486.  
  487.         system("cls");
  488.  
  489.         if (opcao == '6')
  490.             break;
  491.  
  492.         switch(opcao) {
  493.             case '1':
  494.                 aux = addPatient(&a, &total_patients, &capacity);
  495.                 printf("\n");
  496.                 if (aux.id != -1) {
  497.                     printf("Paciente %s adicionado!\n", aux.name);
  498.                     a[total_patients - 1] = aux;
  499.                     fileEdited = 1;
  500.                 } else {
  501.                     printf("Operação cancelada!\n");
  502.                 }
  503.                 break;
  504.  
  505.             case '2':
  506.                 if (empty_patients(total_patients))
  507.                     break;
  508.  
  509.                 imcCreate(a, total_patients);
  510.  
  511.                 break;
  512.  
  513.             case '3':
  514.                 if (empty_patients(total_patients))
  515.                     break;
  516.  
  517.                 printAllPatients(a, total_patients);
  518.                 break;
  519.  
  520.             case '4':
  521.                 if (empty_patients(total_patients))
  522.                     break;
  523.  
  524.                 imcResults(total_patients);
  525.  
  526.                 break;
  527.  
  528.             case '5':
  529.                 if (empty_patients(total_patients))
  530.                     break;
  531.  
  532.                 n_diabeticos = 0;
  533.  
  534.                 for (i = 0; i < total_patients; i++) {
  535.                     if (a[i].gluc > 120){
  536.                         diabeticos[n_diabeticos] = a[i];
  537.                         n_diabeticos++;
  538.                     }
  539.                 }
  540.  
  541.                 if (n_diabeticos == 0) {
  542.                     printf("Não existem pacientes diabeticos.\n");
  543.                     break;
  544.                 }
  545.  
  546.                 printf("Pacientes diabeticos:\n");
  547.                 for (i = 0; i < n_diabeticos; i++) {
  548.                     printf("Nome: %s; Glicemia: %.2f\n", diabeticos[i].name, diabeticos[i].gluc);
  549.                 }
  550.  
  551.                 break;
  552.  
  553.             default:
  554.                 printf("Opção inválida!\n", opcao);
  555.                 break;
  556.         }
  557.  
  558.         if (fileEdited == 1) {
  559.             saveFile(a, filename);
  560.         }
  561.  
  562.         system("pause");
  563.         system("cls");
  564.     } while(1);
  565.  
  566.     printf("Encerrando programa...\n");
  567.     saveFile(a, filename);
  568.     printf("Programa finalizado com sucesso!\n");
  569.  
  570.     system("PAUSE");
  571.     return 0;
  572. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement