Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Sistema de Controle de Pacientes
- //
- // Criado por Sergio Toledo Piza
- // Copyright © 2016. Nenhum direito reservado.
- //
- // Agradecimentos especiais ao Google e ao Stackoverflow
- //
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <ctype.h>
- #include <locale.h>
- #include <string.h>
- struct patient {
- int id;
- char name[30];
- char dob[11];
- float gluc;
- float chol;
- float w;
- float h;
- char gender;
- };
- int i, j, total_patients = 0, capacity = 1;
- struct patient a[1];
- float strToFloat(char str[]) {
- int size = strlen(str);
- char str2[size];
- for (i = 0; str[i]; i++) {
- if (str[i] == '.') {
- str2[i] = ',';
- } else str2[i] = str[i];
- }
- str2[size] = '\0';
- //printf("\n%.2f\n", strtod("30,30", NULL));
- return strtod(str2, NULL);
- }
- void readLine(char *line, struct patient *patient) {
- char *substr;
- int info_id = 1;
- substr = strtok(line, ";");
- while (substr != NULL) {
- switch(info_id) {
- case 1: // id
- patient->id = atoi(substr);
- break;
- case 2: // name
- strcpy(patient->name, substr);
- printf("%s\n", (substr));
- break;
- case 3: // date of birth
- strcpy(patient->dob, substr);
- break;
- case 4: // glucose
- patient->gluc = strToFloat(substr);
- printf("%s\n", (substr));
- break;
- case 5: // cholesterol
- patient->chol = strToFloat(substr);
- break;
- case 6: // weight
- patient->w = strToFloat(substr);
- break;
- case 7: // height
- patient->h = strToFloat(substr);
- break;
- case 8: // gender
- patient->gender = (substr[0]);
- break;
- default:
- break;
- }
- info_id++;
- substr = strtok(NULL, ";");
- }
- return 0;
- }
- void growArray(struct patient **a, int total, int *capacity) {
- if (total == *capacity) {
- *capacity *= 100;
- *a = (struct patient*)realloc(*a, *capacity*sizeof(struct patient));
- }
- }
- void printPatient(struct patient patient) {
- printf("Paciente %s:\n", patient.name);
- printf("- ID: %i\n", patient.id);
- printf("- Data de nascimento: %s\n", patient.dob);
- printf("- Glicose: %.2f\n", patient.gluc);
- printf("- Colesterol: %.2f\n", patient.chol);
- printf("- Peso: %.2fkg\n", patient.w);
- printf("- Altura: %.2fm\n", patient.h);
- printf("- Sexo: %c\n", patient.gender);
- }
- struct patient addPatient(struct patient **a, int *total, int *capacity) {
- struct patient newPatient = {
- *total + 1,
- "",
- "",
- 0,
- 0,
- 0,
- 0,
- '\0'
- };
- printf("ID do novo paciente: %i\n", *total + 1);
- //newPatient.id = *total + 1;
- while (strlen(newPatient.name) < 1) {
- system("cls");
- printf("Digite o nome do novo paciente: ");
- gets(newPatient.name);
- fflush(stdin);
- if (strlen(newPatient.name) > 0)
- break;
- printf("Digite um nome!\n");
- }
- while(strlen(newPatient.dob) != 10) {
- printf("Digite a data de nascimento do novo paciente (dd/mm/aaaa): ");
- gets(newPatient.dob);
- fflush(stdin);
- if (strlen(newPatient.dob) == 10)
- break;
- printf("Digite uma data válida!\n");
- }
- printf("Digite o nivel de glicose do paciente: ");
- scanf("%f", &newPatient.gluc);
- printf("Digite o nivel de colesterol do paciente: ");
- scanf("%f", &newPatient.chol);
- printf("Digite o peso do paciente: ");
- scanf("%f", &newPatient.w);
- printf("Digite a altura do paciente: ");
- scanf("%f", &newPatient.h);
- get_gender:
- printf("Digite o sexo do paciente (m/f): ");
- char patient_gender = getche();
- patient_gender = tolower(patient_gender);
- switch(patient_gender) {
- case 'm':
- break;
- case 'f':
- break;
- default:
- printf("Digite um sexo valido!\n");
- goto get_gender;
- break;
- }
- newPatient.gender = patient_gender;
- system("cls");
- printPatient(newPatient);
- while (1) {
- printf("\nDeseja adicionar esse paciente? <s/n>: ");
- switch(tolower(getche())) {
- case 's':
- *total += 1;
- growArray(a, *total, capacity);
- return newPatient;
- break;
- case 'n':
- newPatient.id = -1;
- return newPatient;
- break;
- default:
- printf("\nDigite um valor válido!");
- break;
- }
- }
- }
- void printAllPatients(struct patient *a, int total) {
- for (i = 0; i < total; i++) {
- 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);
- }
- }
- char menu() {
- printf("***********************************\n");
- printf("** Sistema de Controle **\n");
- printf("** de Pacientes **\n");
- printf("** **\n");
- printf("** Feito por: **\n");
- printf("** Sergio Toledo Piza **\n");
- printf("***********************************\n");
- printf("** **\n");
- printf("** 1 - Adicionar Paciente **\n");
- printf("** 2 - Gerar IMC **\n");
- printf("** 3 - Mostrar Pacientes **\n");
- printf("** 4 - Resultados IMC **\n");
- printf("** 5 - Pacientes Diabeticos **\n");
- printf("** 6 - Sair do Programa **\n");
- printf("** **\n");
- printf("***********************************\n\n");
- printf("Digite sua opção: ");
- return getche();
- }
- void saveFile(struct patient *a, char *filename) {
- FILE *f = fopen(filename, "w");
- if (f == NULL) {
- printf("Erro ao salvar arquivo!\n");
- return;
- }
- printf("Aguarde! Salvando arquivo...\n");
- for (i = 0; a[i].id; i++) {
- if (a[i].id != -1) {
- if (a[i].id == 1)
- 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);
- else if (a[i].id == a[i - 1].id + 1)
- 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);
- }
- }
- printf("Arquivo salvo!\n");
- fclose(f);
- }
- char *strtolower(char str[]) {
- char *x = str;
- for (j = 0; j < x[j]; j++) {
- x[j] = tolower(x[j]);
- }
- return x;
- }
- float imcCalc(float w, float h) {
- return w / (h * h);
- }
- void imcCreate(struct patient *a, int total) {
- FILE *imc_file;
- imc_file = fopen("imc.txt", "a+");
- fclose(fopen("imc.txt", "w"));
- for (i = 0; i < total; i++)
- fprintf(imc_file, "%s: %.2f\n", a[i].name, imcCalc(a[i].w, a[i].h));
- printf("Arquivo imc.txt criado!\n");
- fclose(imc_file);
- }
- void imcResults(int total_patients) {
- FILE *imc_file;
- imc_file = fopen("imc.txt", "r");
- if (imc_file == NULL) {
- printf("Gere o arquivo do IMC no item 2 do menu!\n");
- return;
- }
- const size_t imc_line_size = 500;
- char *imc_line = malloc(imc_line_size);
- char imc_patients[total_patients][imc_line_size];
- i = 0;
- while (fgets(imc_line, imc_line_size, imc_file) != NULL) {
- strcpy(imc_patients[i], imc_line);
- i++;
- }
- for (j = 0; j < total_patients - 1; j++) {
- for (i = 0; i < total_patients - 1; i++) {
- if (0 < strcmp(imc_patients[i], imc_patients[i +1])) {
- char pchTemp[imc_line_size];
- strcpy(pchTemp, imc_patients[i]);
- strcpy(imc_patients[i], imc_patients[i +1]);
- strcpy(imc_patients[i +1], pchTemp);
- }
- }
- }
- for (i = 0; i < total_patients; i++) {
- printf("%s", imc_patients[i]);
- }
- free(imc_line);
- fclose(imc_file);
- }
- int getNumberFromString() {
- char strn[30];
- gets(strn);
- fflush(stdin);
- return atoi(strn);
- }
- char getche_and_clear() {
- char ch = getche();
- system("cls");
- return ch;
- }
- int empty_patients(int total) {
- if (total == 0) {
- printf("Não existe nenhum paciente registrado!\n");
- return 1;
- }
- return 0;
- }
- int main()
- {
- setlocale(LC_ALL, "portuguese");
- FILE *auth;
- auth = fopen("auth.csv", "a+");
- //fprintf(auth, "aaa:bbb\n");
- char auth_line[50];
- fgets(auth_line, 50, auth);
- int auth_size = strlen(auth_line);
- char auth_pw[50], auth_user[50];
- //strcpy(auth_user, auth_line);
- int found_usr = 0;
- for (i = 0; i < auth_size; i++) {
- if (auth_line[i] == ':') {
- found_usr = 1;
- auth_user[i] == '\0';
- break;
- }
- auth_user[i] = auth_line[i];
- }
- if (found_usr) {
- j = strlen(auth_user) + 1;
- //printf("%i", j);
- for (i = 0; i < auth_size; i++) {
- if (auth_line[j + i] == '\n' || auth_line[j + i] == '\0' || auth_line[j + i] == EOF) {
- auth_pw[i] = '\0';
- break;
- }
- else
- auth_pw[i] = auth_line[j + i];
- }
- //printf("%i", strlen(auth_pw));
- }
- char login_usr[21], login_pw[21];
- if (!found_usr || strlen(auth_pw) == 0) {
- do {
- printf("Digite um novo usuario (5 - 20 caracteres): ");
- gets(login_usr);
- fflush(stdin);
- } while (strlen(login_usr) < 5 || strlen(login_usr) > 20);
- do {
- printf("Digite uma nova senha (5 - 20 caracteres): ");
- gets(login_pw);
- fflush(stdin);
- } while (strlen(login_pw) < 5 || strlen(login_pw) > 20);
- fclose(fopen("auth.csv", "w"));
- fprintf(auth, "%s:%s\n", login_usr, login_pw);
- system("cls");
- }
- else {
- do {
- printf("Usuario: ");
- gets(login_usr);
- fflush(stdin);
- printf("Senha: ");
- gets(login_pw);
- fflush(stdin);
- //printf("%i\n%i\n\n", strcmp(login_usr, auth_user), strcmp(login_pw, auth_pw));
- //printf("%s:%s\n", auth_user, auth_pw);
- if (strcmp(login_pw, auth_pw) == 0 && strcmp(strtolower(login_usr), strtolower(auth_user)) == 0)
- break;
- else
- printf("Login ou senha invalidos!\n");
- system("pause");
- system("cls");
- } while (1);
- }
- free(auth_line);
- fclose(auth);
- system("cls");
- FILE* fh;
- char filename[30];
- printf("*********************************\n");
- printf("** Sistema de Controle **\n");
- printf("** de Pacientes **\n");
- printf("*********************************\n\n");
- printf("Inicializando programa...\n");
- strcpy(filename, "patients.csv");
- //goto processPatientsArray;
- fh = fopen(filename, "a+");
- //http://stackoverflow.com/questions/3501338/c-read-file-line-by-line
- //read line by line
- const size_t line_size = 500;
- char* line = malloc(line_size);
- total_patients = 0;
- struct patient* a = (struct patient*)malloc(capacity*sizeof(struct patient));
- while (fgets(line, line_size, fh) != NULL) {
- growArray(&a, total_patients, &capacity);
- readLine(line, &a[total_patients]);
- total_patients++;
- }
- if (total_patients == 0)
- a[0].id = -1;
- saveFile(a, filename);
- system("cls");
- free(line);
- fclose(fh);
- //http://stackoverflow.com/questions/3501338/c-read-file-line-by-line
- char opcao;
- do {
- opcao = menu();
- int fileEdited = 0;
- struct patient aux;
- struct patient diabeticos[total_patients];
- int n_diabeticos;
- system("cls");
- if (opcao == '6')
- break;
- switch(opcao) {
- case '1':
- aux = addPatient(&a, &total_patients, &capacity);
- printf("\n");
- if (aux.id != -1) {
- printf("Paciente %s adicionado!\n", aux.name);
- a[total_patients - 1] = aux;
- fileEdited = 1;
- } else {
- printf("Operação cancelada!\n");
- }
- break;
- case '2':
- if (empty_patients(total_patients))
- break;
- imcCreate(a, total_patients);
- break;
- case '3':
- if (empty_patients(total_patients))
- break;
- printAllPatients(a, total_patients);
- break;
- case '4':
- if (empty_patients(total_patients))
- break;
- imcResults(total_patients);
- break;
- case '5':
- if (empty_patients(total_patients))
- break;
- n_diabeticos = 0;
- for (i = 0; i < total_patients; i++) {
- if (a[i].gluc > 120){
- diabeticos[n_diabeticos] = a[i];
- n_diabeticos++;
- }
- }
- if (n_diabeticos == 0) {
- printf("Não existem pacientes diabeticos.\n");
- break;
- }
- printf("Pacientes diabeticos:\n");
- for (i = 0; i < n_diabeticos; i++) {
- printf("Nome: %s; Glicemia: %.2f\n", diabeticos[i].name, diabeticos[i].gluc);
- }
- break;
- default:
- printf("Opção inválida!\n", opcao);
- break;
- }
- if (fileEdited == 1) {
- saveFile(a, filename);
- }
- system("pause");
- system("cls");
- } while(1);
- printf("Encerrando programa...\n");
- saveFile(a, filename);
- printf("Programa finalizado com sucesso!\n");
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement