Advertisement
Guest User

Untitled

a guest
May 25th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 26.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "estruturas.h"
  5. #include "funcoes.h"
  6.  
  7. void insere_lista_user(lista_users lista, char  *username,char *password, char *data_nascimento, char *morada, char *telefone)
  8. {
  9.     lista_users no;
  10.     lista_users ant, inutil;
  11.     no = (lista_users) malloc (sizeof (USERS));
  12.     if (no != NULL)
  13.     {
  14.         strcpy(no->username,username);
  15.         strcpy(no->password,password);
  16.         strcpy(no->data_nascimento,data_nascimento);
  17.         strcpy(no->morada,morada);
  18.         strcpy(no->telefone,telefone);
  19.  
  20.         procura_lista_user(lista, username, &ant, &inutil);
  21.         no->info_pdi = inicia_lista_pdis_users();
  22.         no->next = ant ->next;
  23.         ant->next = no;
  24.     }
  25.  
  26. }
  27.  
  28. void procura_lista_user (lista_users lista, char *chave, lista_users *ant, lista_users *atual)
  29. {
  30.  
  31.     *ant = lista;
  32.     *atual = lista -> next;
  33.     while ((*atual) != NULL &&( strcmp((*atual)->username, chave) < 0))
  34.     {
  35.         *ant = *atual;
  36.         *atual = (*atual) -> next;
  37.     }
  38.     while ((*atual) != NULL &&( strcmp((*atual)->username, chave) == 0))
  39.     {
  40.         *atual = NULL;
  41.     }
  42.     if ((*atual) != NULL && (strcmp((*atual)->username, chave) != 0))
  43.     {
  44.         *atual = NULL;
  45.     }
  46. }
  47.  
  48. lista_users inicia_lista_users(void){
  49.     lista_users aux;
  50.     aux = (lista_users) malloc(sizeof(USERS));
  51.     if (aux != NULL) {
  52.         aux->username[0] = '\0';
  53.         aux->password[0] = '\0';
  54.         aux->data_nascimento[0] = '\0';
  55.         aux->morada[0] = '\0';
  56.         aux->telefone[0] = '\0';
  57.         aux->next = NULL;
  58.         aux->info_pdi = inicia_lista_pdis_users();
  59.     }
  60.     return aux;
  61. }
  62.  
  63.  
  64. int check_local(lista_locais lista, char  *it){
  65.     lista_locais ptr = lista->next;
  66.  
  67.     while(ptr!= NULL)
  68.     {
  69.         if (strcmp(ptr->nome, it) == 0)
  70.             return 1;
  71.  
  72.         ptr=ptr->next;
  73.     }
  74.     return 0;
  75. }
  76.  
  77. int check_pdi(lista_pdi lista, char  *it){
  78.     lista_pdi ptr = lista->next;
  79.  
  80.     while(ptr!= NULL)
  81.     {
  82.         if (strcmp(ptr->nome, it) == 0)
  83.             return 1;
  84.  
  85.         ptr=ptr->next;
  86.     }
  87.     return 0;
  88. }
  89.  
  90.  
  91. void insere_lista_pdis(lista_pdi lista, char  *it, char *npdi, char* dpdi, char* hpdi,int inscritos) {
  92.     lista_pdi no,ant,inutil;
  93.  
  94.     no = (lista_pdi) malloc(sizeof(PDI));
  95.  
  96.     if (no != NULL) {
  97.         strcpy(no->descricao,dpdi);
  98.         strcpy(no->horario,hpdi);
  99.         strcpy(no->nome,npdi);
  100.         no->n_inscritos_pdi = inscritos;
  101.  
  102.         procura_lista_pdis(lista,npdi,&ant,&inutil);
  103.  
  104.         no->next = ant->next;
  105.         ant->next = no;
  106.  
  107.     }
  108. }
  109.  
  110.  
  111.  
  112.  
  113. void insere_lista_locais(lista_locais lista, char  *it, char *npdi, char* dpdi, char* hpdi,int local_inscritos,int pdi_inscritos)
  114. {
  115.     lista_locais no;
  116.     lista_locais ant,inutil;
  117.     no = (lista_locais) malloc (sizeof(LOCAIS));
  118.     if (no != NULL)
  119.     {
  120.         if(check_local(lista,it)==0) {
  121.             strcpy(no->nome,it);
  122.             no->n_inscritos_local = local_inscritos;
  123.  
  124.             procura_lista_locais(lista, it, &ant, &inutil);
  125.             no->next = ant->next;
  126.             ant->next = no;
  127.  
  128.             no->lista_pdi_local = inicia_lista_pdis();
  129.             insere_lista_pdis(no->lista_pdi_local,it,npdi,dpdi,hpdi,pdi_inscritos);
  130.  
  131.         }else{
  132.  
  133.             procura_lista_locais(lista, it, &ant, &inutil);
  134.  
  135.             insere_lista_pdis(ant->lista_pdi_local,it,npdi,dpdi,hpdi,pdi_inscritos);
  136.  
  137.  
  138.  
  139.             /*insere na lista desse local, o pdi"*/
  140.         }
  141.     }
  142. }
  143.  
  144.  
  145. void  procura_lista_pdis(lista_pdi lista, char *chave, lista_pdi *ant, lista_pdi *atual)
  146. {
  147.  
  148.     *ant = lista;
  149.     *atual = lista -> next;
  150.     while ((*atual) != NULL &&( strcmp((*atual)->nome, chave) < 0))
  151.     {
  152.         *ant = *atual;
  153.         *atual = (*atual) -> next;
  154.     }
  155.     while ((*atual) != NULL &&( strcmp((*atual)->nome, chave) == 0))
  156.     {
  157.         *ant = *atual;
  158.         *atual = (*atual) -> next;
  159.     }
  160.     if ((*atual) != NULL && (strcmp((*atual)->nome, chave) != 0))
  161.     {
  162.         *atual = NULL;
  163.     }
  164. }
  165.  
  166. void  procura_pdis_inscritos(lista_pdi lista, int chave, lista_pdi *ant, lista_pdi *atual)
  167. {
  168.  
  169.     *ant = lista;
  170.     *atual = lista -> next;
  171.     while ((*atual) != NULL &&( (*atual)->n_inscritos_pdi< chave ))
  172.     {
  173.         *ant = *atual;
  174.         *atual = (*atual) -> next;
  175.     }
  176.     while ((*atual) != NULL &&((*atual)->n_inscritos_pdi == chave))
  177.     {
  178.         *ant = *atual;
  179.         *atual = (*atual) -> next;
  180.     }
  181. }
  182.  
  183. void procura_local_inscritos(lista_locais lista, int chave, lista_locais *ant, lista_locais *atual)
  184. {
  185.  
  186.     *ant = lista;
  187.     *atual = lista -> next;
  188.     while ((*atual) != NULL &&( (*atual)->n_inscritos_local>= chave ))
  189.     {
  190.         *ant = *atual;
  191.         *atual = (*atual) -> next;
  192.     }
  193.     while ((*atual) != NULL &&((*atual)->n_inscritos_local != chave))
  194.     {
  195.         *atual = NULL;
  196.     }
  197. }
  198.  
  199.  
  200.  
  201. void procura_lista_locais(lista_locais lista, char *chave, lista_locais *ant, lista_locais *atual)
  202. {
  203.  
  204.     *ant = lista;
  205.     *atual = lista -> next;
  206.     while ((*atual) != NULL &&( strcmp((*atual)->nome, chave) < 0))
  207.     {
  208.         *ant = *atual;
  209.         *atual = (*atual) -> next;
  210.     }
  211.     while ((*atual) != NULL &&( strcmp((*atual)->nome, chave) == 0))
  212.     {
  213.         *ant = *atual;
  214.         *atual = (*atual) -> next;
  215.     }
  216.     if ((*atual) != NULL && (strcmp((*atual)->nome, chave) != 0))
  217.     {
  218.         *atual = NULL;
  219.     }
  220. }
  221.  
  222.  
  223. lista_pdi inicia_lista_pdis(){
  224.     lista_pdi aux;
  225.  
  226.     aux = (lista_pdi) malloc(sizeof(PDI));
  227.     if (aux != NULL) {
  228.         aux->nome[0] = '\0';
  229.         aux->descricao[0] = '\0';
  230.         aux->horario[0] = '\0';
  231.         aux->next = NULL;
  232.     }
  233.     return aux;
  234. }
  235.  
  236.  
  237. lista_users_pdi inicia_lista_pdis_users(){
  238.     lista_users_pdi aux;
  239.     aux = (lista_users_pdi)malloc(sizeof(PDI_USERS));
  240.  
  241.     if (aux != NULL) {
  242.  
  243.         aux->pdi = NULL;
  244.         aux->next = NULL;
  245.     }
  246.     return aux;
  247. }
  248.  
  249.  
  250.  
  251. lista_locais inicia_lista_locais(void){
  252.     lista_locais aux;
  253.     aux = (lista_locais) malloc(sizeof(LOCAIS));
  254.     if (aux != NULL) {
  255.         aux->nome[0] = '\0';
  256.         aux->next = NULL;
  257.     }
  258.  
  259.     return aux;
  260. }
  261.  
  262.  
  263.  
  264.  
  265. void imprimir_lista_locais_pdis(lista_locais auxLocais){
  266.     lista_locais l = auxLocais->next; /* para saltar o header e nao apresentar o primeiro elemento criado pelo cria_lista() */
  267.  
  268.     while(l->next){
  269.         printf("%s - %d inscritos\n-----\n", l->nome,l->n_inscritos_local);
  270.         lista_pdi u = l->lista_pdi_local->next;
  271.         while(u) {
  272.             printf("%s - %d inscritos - %s - %s\n", u->nome,u->n_inscritos_pdi,u->descricao,u->horario);
  273.             u = u->next;
  274.         }
  275.         printf("\n------\n");
  276.  
  277.         l=l->next;
  278.     }
  279. }
  280.  
  281. void imprimir_lista_pdis(lista_locais auxLocais){
  282.     lista_locais l = auxLocais->next; /* para saltar o header e nao apresentar o primeiro elemento criado pelo cria_lista() */
  283.  
  284.     while(l){
  285.         lista_pdi u = l->lista_pdi_local->next;
  286.         while(u) {
  287.             printf("%s - %d inscritos - %s - %s\n", u->nome,u->n_inscritos_pdi,u->descricao,u->horario);
  288.             u = u->next;
  289.         }
  290.         free(u);
  291.         l=l->next;
  292.     }
  293. }
  294.  
  295. void imprimir_lista_locais(lista_locais auxLocais){
  296.     lista_locais l = auxLocais->next; /* para saltar o header e nao apresentar o primeiro elemento criado pelo cria_lista() */
  297.  
  298.     while(l){
  299.         printf("%s , %d inscritos\n-----\n", l->nome,l->n_inscritos_local);
  300.  
  301.         l=l->next;
  302.     }
  303. }
  304.  
  305.  
  306. void imprimir_lista_users(lista_users auxpdis){
  307.     lista_users l = auxpdis->next; /* para saltar o header e nao apresentar o primeiro elemento criado pelo cria_lista() */
  308.     while(l){
  309.         printf("Nome: %s\n",l->username);
  310.         printf("Password: %s\n",l->password);
  311.         printf("Data Nascimento: %s\n",l->data_nascimento);
  312.         printf("Morada: %s\n",l->morada);
  313.         printf("Telefone: %s\n",l->telefone);
  314.         printf("PDI HOT: %s\n",l->pdi_hot->nome);
  315.         printf("Local 1: %s\n",l->array_locais[0]->nome);
  316.         printf("Local 2: %s\n",l->array_locais[1]->nome);
  317.         printf("Local 3: %s\n",l->array_locais[2]->nome);
  318.         printf("--------\n");
  319.         l=l->next;
  320.     }
  321. }
  322.  
  323. void alterar_dados_password(lista_users users,char *user_logado,char *password,char *data,char *morada,char *tele){
  324.     lista_users ptr = users->next;
  325.  
  326.     while(ptr != NULL)
  327.     {
  328.  
  329.         if (strcmp(ptr->username, user_logado) == 0){
  330.             if(password != NULL)
  331.                 strcpy(ptr->password,password);
  332.             if(data != NULL)
  333.                 strcpy(ptr->data_nascimento,data);
  334.             if(morada != NULL)
  335.                 strcpy(ptr->morada,morada);
  336.             if(tele != NULL)
  337.                 strcpy(ptr->telefone,tele);
  338.         }
  339.         ptr=ptr->next;
  340.     }
  341.  
  342. }
  343.  
  344.  
  345. void load_users(lista_users auxUser,lista_locais locais){
  346.     FILE *fp;
  347.     char buffer[MAX];
  348.     char auxUsername[MAX];
  349.     char auxPassword[MAX];
  350.     char auxData_nascimento[MAX];
  351.     char auxMorada[MAX];
  352.     char auxTelefone[MAX];
  353.     char local[MAX];
  354.     char pdi_hot_name[MAX];
  355.     char pdi_name[MAX];
  356.     int pdihot = 0, pdis = 0 , loc = 0, novo = 0;
  357.  
  358.     if ((fp=fopen("users.txt", "r")) == NULL){
  359.         printf ("Erro ao abrir o ficheiro de utilizadores.\n");
  360.         exit(1);
  361.     }
  362.         while (!feof(fp)){
  363.             fgets(buffer, MAX, fp);
  364.             if(strcmp(buffer, "--LOCAIS\n") == 0){
  365.                 novo = 0;
  366.                 loc = 1;
  367.                 pdis = 0;
  368.                 pdihot = 0;
  369.                 continue;
  370.             }
  371.             if(strcmp(buffer, "--PDI\n") == 0){
  372.                 novo = 0;
  373.                 loc = 0;
  374.                 pdis = 1;
  375.                 pdihot = 0;
  376.                 continue;
  377.             }
  378.             if(strcmp(buffer, "--PDIHOT\n") == 0){
  379.                 novo = 0;
  380.                 loc = 0;
  381.                 pdis = 0;
  382.                 pdihot = 1;
  383.                 continue;
  384.             }
  385.             if(strcmp(buffer, "---\n") == 0){
  386.                 novo=1;
  387.                 loc=0;
  388.                 pdihot=0;
  389.                 pdis=0;
  390.                 continue;
  391.             }
  392.             if(novo){
  393.                 strcpy(auxUsername, strtok(buffer, "-"));
  394.                 strcpy(auxPassword, strtok(NULL, "-"));
  395.                 strcpy(auxData_nascimento, strtok(NULL, "-"));
  396.                 strcpy(auxMorada, strtok(NULL, "-"));
  397.                 strcpy(auxTelefone, strtok(NULL, "-"));
  398.                 insere_lista_user(auxUser,auxUsername,auxPassword,auxData_nascimento,auxMorada,auxTelefone);
  399.             }
  400.             if(pdihot){
  401.                 strcpy(pdi_hot_name, strtok(buffer, "-"));
  402.                 load_pref_pdihot(auxUser,locais,auxUsername,pdi_hot_name);
  403.             }
  404.  
  405.             if(loc){
  406.                 strcpy(local, strtok(buffer, "-"));
  407.                 load_pref_local(auxUser,locais,auxUsername,local);
  408.             }/*
  409.             if(pdis){
  410.                 strcpy(pdi_name, strtok(buffer, "-"));
  411.                 load_pref_pdi(auxUser,locais,auxUsername,pdi_name);
  412.             }*/
  413.  
  414.         }
  415.     fclose(fp);
  416. }
  417.  
  418.  
  419.  
  420. void load_pref_pdi(lista_users lista_u,lista_locais locais,char *username, char *pdi_name){
  421.  
  422.     lista_users inutil_user,ant_user;
  423.     lista_users_pdi aux_pdi,novo_no;
  424.     lista_locais aux2;
  425.  
  426.     procura_lista_user(lista_u,username,&ant_user,&inutil_user);
  427.     aux_pdi = ant_user->info_pdi;
  428.     aux2 = locais->next;
  429.  
  430.     while(aux2) {
  431.  
  432.         lista_pdi u = aux2->lista_pdi_local->next;
  433.         while (u) {
  434.  
  435.             if (strcmp(u->nome, pdi_name) == 0) {
  436.  
  437.                 novo_no = (lista_users_pdi) malloc(sizeof(PDI_USERS));
  438.                 novo_no->pdi = u;
  439.                 novo_no->next= NULL;
  440.  
  441.                 while (aux_pdi->next != NULL){
  442.                     aux_pdi = aux_pdi->next;
  443.                 }
  444.  
  445.                 aux_pdi->next =novo_no;
  446.                 novo_no->next = NULL;
  447.             }
  448.  
  449.             u = u->next;
  450.  
  451.         }
  452.         free(u);
  453.         aux2 = aux2->next;
  454.     }
  455.     free(aux2);
  456. }
  457.  
  458.  
  459. void load_pref_pdihot(lista_users lista_u,lista_locais locais,char *username , char * pdi_hot_name){
  460.     lista_users inutil_user,ant_user,aux_user_hot;
  461.     lista_locais aux2;
  462.     procura_lista_user(lista_u,username,&ant_user,&inutil_user);
  463.  
  464.     aux2 = locais->next;
  465.     aux_user_hot = ant_user;
  466.  
  467.  
  468.     while(aux2!=NULL) {
  469.  
  470.         lista_pdi u = aux2->lista_pdi_local->next;
  471.         while (u) {
  472.  
  473.             if (strcmp(u->nome, pdi_hot_name) == 0) {
  474.  
  475.                 PDI *novo_no_hot = u;
  476.                 aux_user_hot->pdi_hot = novo_no_hot;
  477.  
  478.             }
  479.  
  480.             u = u->next;
  481.         }
  482.         free(u);
  483.         aux2 = aux2->next;
  484.     }
  485.  
  486. }
  487.  
  488.  
  489. void load_pref_local(lista_users lista_u,lista_locais locais,char *username,char *nomelocal){
  490.  
  491.     lista_users inutil_user,ant_user,user_aux;
  492.     lista_locais ant,inutil;
  493.     int counter = 0;
  494.     procura_lista_user(lista_u,username,&ant_user,&inutil_user);
  495.     user_aux = ant_user;
  496.  
  497.     while (counter!= 3) {
  498.  
  499.         if (check_local(locais, nomelocal) == 1) {
  500.  
  501.             procura_lista_locais(locais, nomelocal, &ant, &inutil);
  502.  
  503.             user_aux->array_locais[counter] = ant;
  504.             /*lista_u->array_locais[counter]->next = NULL;*/
  505.  
  506.             counter++;
  507.         } else {
  508.             printf("Local não existe, escolha outro\n");
  509.         }
  510.     }
  511.  
  512. }
  513.  
  514.  
  515.  
  516.  
  517. void load_locais(lista_locais auxLocal){
  518.     FILE *fp;
  519.     int a,b;
  520.     char buffer[MAX];
  521.     char auxname[MAX];
  522.     char aux_pdi_name[MAX];
  523.     char aux_pdi_type[MAX];
  524.     char n_inscritos_local[MAX];
  525.     char n_inscritos_pdi[MAX];
  526.     char aux_schedule[MAX];
  527.  
  528.     if ((fp=fopen("locaisepdis.txt", "r")) == NULL){
  529.         printf ("Erro ao abrir o ficheiro de locais e pdi's.\n");
  530.         exit(1);
  531.     }else{
  532.         while (!feof(fp)){
  533.             fgets(buffer, MAX, fp);
  534.             strcpy(auxname, strtok(buffer, "-"));
  535.             strcpy(n_inscritos_local, strtok(NULL, ";"));
  536.             a= atoi(n_inscritos_local);
  537.             strcpy(aux_pdi_name, strtok(NULL, ";"));
  538.             strcpy(aux_pdi_type, strtok(NULL, ";"));
  539.             strcpy(aux_schedule, strtok(NULL, ";"));
  540.             strcpy(n_inscritos_pdi, strtok(NULL, ";"));
  541.             b = atoi(n_inscritos_pdi);
  542.             insere_lista_locais(auxLocal,auxname,aux_pdi_name,aux_pdi_type,aux_schedule,a,b);
  543.         }
  544.         fclose(fp);
  545.     }
  546. }
  547.  
  548. /*BUBBLE SORT PARA PDIS E LOCAIS (POR ORDEM ALFABETICA E POR N INSCRITOS)*/
  549. void ordena_pdis(lista_locais auxLocais){
  550.     lista_locais l = auxLocais->next;
  551.     while(l->next){
  552.         lista_pdi d = l->lista_pdi_local->next;
  553.         int n_nos=0;
  554.         n_nos = nNosPdi(d);
  555.         l=l->next;
  556.     }
  557. }
  558.  
  559. void ordena_pdis_Char(lista_locais auxLocais){
  560.     lista_locais l = auxLocais->next;
  561.     while(l){
  562.         lista_pdi u = l->lista_pdi_local->next;
  563.         int n_nos=0;
  564.         n_nos = nNosPdi(u);
  565.         bubbleSortCharPdi(&u,n_nos);
  566.         l=l->next;
  567.     }
  568. }
  569.  
  570.  
  571. lista_locais trocaNos(lista_locais ptr1, lista_locais ptr2)
  572. {
  573.     lista_locais tmp = ptr2->next;
  574.     ptr2->next = ptr1;
  575.     ptr1->next = tmp;
  576.     return ptr2;
  577. }
  578.  
  579. lista_pdi trocaNosPdi(lista_pdi ptr1, lista_pdi ptr2)
  580. {
  581.     lista_pdi tmp = ptr2->next;
  582.     ptr2->next = ptr1;
  583.     ptr1->next = tmp;
  584.     return ptr2;
  585. }
  586.  
  587. int nNos(lista_locais head)
  588. {
  589.     int count = 0;
  590.     lista_locais atual = head;
  591.     while (atual != NULL)
  592.     {
  593.         count++;
  594.         atual = atual->next;
  595.     }
  596.     return count;
  597. }
  598.  
  599. int nNosPdi(lista_pdi head)
  600. {
  601.     int count = 0;
  602.     lista_pdi atual = head;
  603.     while (atual != NULL)
  604.     {
  605.         count++;
  606.         atual = atual->next;
  607.     }
  608.     return count;
  609. }
  610.  
  611.  
  612. void bubbleSort(lista_locais * head, int count)
  613. {
  614.     lista_locais* h;
  615.     int i, j, troca;
  616.  
  617.     for (i = 0; i <= count; i++) {
  618.  
  619.         h = head;
  620.         troca = 0;
  621.  
  622.         for (j = 0; j < count - i - 1; j++) {
  623.  
  624.             lista_locais p1 = *h;
  625.             lista_locais p2 = p1->next;
  626.  
  627.             if (p1->n_inscritos_local > p2->n_inscritos_local) {
  628.  
  629.                 *h = trocaNos(p1, p2);
  630.                 troca = 1;
  631.             }
  632.  
  633.             h = &(*h)->next;
  634.         }
  635.  
  636.         if (troca == 0)
  637.             break;
  638.     }
  639. }
  640.  
  641. void bubbleSortPdi(lista_pdi * head, int count)
  642. {
  643.     lista_pdi* h;
  644.     int i, j, troca;
  645.  
  646.     for (i = 0; i <= count; i++) {
  647.  
  648.         h = head;
  649.         troca = 0;
  650.  
  651.         for (j = 0; j < count - i - 1; j++) {
  652.  
  653.             lista_pdi p1 = *h;
  654.             lista_pdi p2 = p1->next;
  655.  
  656.             if (p1->n_inscritos_pdi > p2->n_inscritos_pdi) {
  657.  
  658.                 *h = trocaNosPdi(p1, p2);
  659.                 troca = 1;
  660.             }
  661.  
  662.             h = &(*h)->next;
  663.         }
  664.  
  665.         if (troca == 0)
  666.             break;
  667.     }
  668. }
  669.  
  670. void bubbleSortChar(lista_locais * head, int count)
  671. {
  672.     lista_locais* h;
  673.     int i, j, troca;
  674.  
  675.     for (i = 0; i <= count; i++) {
  676.  
  677.         h = head;
  678.         troca = 0;
  679.  
  680.         for (j = 0; j < count - i - 1; j++) {
  681.  
  682.             lista_locais p1 = *h;
  683.             lista_locais p2 = p1->next;
  684.  
  685.             if (strcmp(p1->nome,p2->nome)>0) {
  686.  
  687.                 *h = trocaNos(p1, p2);
  688.                 troca = 1;
  689.             }
  690.  
  691.             h = &(*h)->next;
  692.         }
  693.  
  694.         if (troca == 0)
  695.             break;
  696.     }
  697. }
  698.  
  699. void bubbleSortCharPdi(lista_pdi * head, int count)
  700. {
  701.     lista_pdi* h;
  702.     int i, j, troca;
  703.  
  704.     for (i = 0; i <= count; i++) {
  705.  
  706.         h = head;
  707.         troca = 0;
  708.  
  709.         for (j = 0; j < count - i - 1; j++) {
  710.  
  711.             lista_pdi p1 = *h;
  712.             lista_pdi p2 = p1->next;
  713.  
  714.             if (strcmp(p1->nome,p2->nome)>0) {
  715.  
  716.                 *h = trocaNosPdi(p1, p2);
  717.                 troca = 1;
  718.             }
  719.  
  720.             h = &(*h)->next;
  721.         }
  722.  
  723.         if (troca == 0)
  724.             break;
  725.     }
  726. }
  727.  
  728. /*FIM DAS FUNCOES PARA O SORT*/
  729.  
  730.  
  731.  
  732.  
  733. void login_registo(lista_users auxUser,lista_locais locais){
  734.  
  735.     FILE *fp;
  736.     char uName[MAX];
  737.     char pWord[MAX];
  738.     char dNascimento[MAX];
  739.     char morada[MAX];
  740.     char telefone[MAX];
  741.     int i;
  742.     lista_users l;
  743.     l = auxUser->next; /* para saltar o header */
  744.  
  745.     printf("VIAGEM DO DEI \n----------------------\n");
  746.     printf("1. Login \n");
  747.     printf("2. Registar\n");
  748.     scanf("%d",&i);
  749.     getchar();
  750.     clear();
  751.  
  752.     switch(i){
  753.         case 1:
  754.             printf("Username: ");
  755.             scanf("%s",uName);
  756.             printf("Password: ");
  757.             scanf("%s",pWord);
  758.             while(l){
  759.                 if(strcmp(l->username, uName) == 0) {
  760.                     if(strcmp(l->password, pWord) == 0) {
  761.                         printf ("Dados Corretos. Login feito.\n");
  762.                         clear();
  763.                         menu(locais,auxUser,uName);
  764.                     }
  765.                 }
  766.                 l=l->next;
  767.             }
  768.             clear();
  769.             printf("Dados incorretos. Tente novamente.\n");
  770.             login_registo(auxUser,locais);
  771.             break;
  772.         case 2:
  773.             if ( ( fp=fopen("users.txt", "a+")) == NULL){
  774.                 printf ("ERRO AO ABRIR O FICHEIRO\n");
  775.                 exit(1);
  776.             }
  777.             printf("Indique os dados para completar o registo:\n");
  778.             printf("Username: ");
  779.             scanf("%s",uName);
  780.             printf("Password: ");
  781.             scanf("%s",pWord);
  782.             printf("Data nascimento: ");
  783.             scanf("%s",dNascimento);
  784.             printf("Morada: ");
  785.             scanf(" %[^\n]s", morada);
  786.             printf("Telefone: ");
  787.             scanf("%s",telefone);
  788.             insere_lista_user(auxUser,uName,pWord,dNascimento,morada,telefone);
  789.             clear();
  790.             menu(locais,auxUser,uName);
  791.             break;
  792.         default:
  793.             login_registo(auxUser,locais);
  794.     }
  795.     free(auxUser);
  796.     free(locais);
  797.     fclose(fp);
  798. }
  799.  
  800.  
  801. void escreve_users_ficheiro(lista_users auxUser){
  802.     lista_users l = auxUser->next;
  803.     FILE *fp;
  804.     if ( ( fp=fopen("users.txt", "w")) == NULL){
  805.         printf ("ERRO AO ABRIR O FICHEIRO\n");
  806.         exit(1);
  807.     }
  808.     while(l!=NULL){
  809.         if(l->next == NULL)
  810.             fprintf(fp,"%s-%s-%s-%s-%s-",l->username,l->password,l->data_nascimento,l->morada,l->telefone);
  811.  
  812.         else
  813.             fprintf(fp,"%s-%s-%s-%s-%s-\n",l->username,l->password,l->data_nascimento,l->morada,l->telefone);
  814.         l=l->next;
  815.     }
  816.     fclose(fp);
  817. }
  818.  
  819. void alterar_dados_utilizador(lista_locais locais,lista_users users,char * user_logado){
  820.     int opcao2;
  821.     char var[MAX];
  822.     clear();
  823.     printf("Alterar dados utilizador.\n -------------\n");
  824.     printf("Nome de utilizador: %s\n -------------\n",user_logado);
  825.     printf("Escolha a sua opcao \n");
  826.     printf("1 - Alterar password.\n");
  827.     printf("2 - Alterar data nascimento.\n");
  828.     printf("3 - Alterar morada.\n");
  829.     printf("4 - Alterar telefone.\n");
  830.     printf("5 - Retroceder\n");
  831.  
  832.     scanf("%d",&opcao2);
  833.     getchar();/*Para receber o enter e avançar*/
  834.     clear();
  835.     switch(opcao2){
  836.         case 1:
  837.             clear();
  838.             printf("Nova password: ");
  839.             scanf("%s",var);
  840.             alterar_dados_password(users,user_logado,var,NULL,NULL,NULL);
  841.             printf("Pressione enter para voltar\n");
  842.             getchar();
  843.             break;
  844.         case 2:
  845.             clear();
  846.             printf("Nova data de nascimento: ");
  847.             scanf("%s",var);
  848.             alterar_dados_password(users,user_logado,NULL,var,NULL,NULL);
  849.             printf("Pressione enter para voltar\n");
  850.             getchar();
  851.             break;
  852.         case 3:
  853.             clear();
  854.             printf("Nova morada:");
  855.             scanf("%s",var);
  856.             alterar_dados_password(users,user_logado,NULL,NULL,var,NULL);
  857.             printf("Pressione enter para voltar\n");
  858.             getchar();
  859.             break;
  860.  
  861.         case 4:
  862.             clear();
  863.             printf("Novo telefone:");
  864.             scanf("%s",var);
  865.             alterar_dados_password(users,user_logado,NULL,NULL,NULL,var);
  866.             printf("Pressione enter para voltar\n");
  867.             getchar();
  868.             break;
  869.  
  870.         case 5:
  871.             clear();
  872.             menu(locais,users,user_logado);
  873.     }
  874.     alterar_dados_utilizador(locais,users,user_logado);
  875. }
  876.  
  877. void insere_preferencias_user(lista_users users,lista_locais locais, char *user_logado){
  878.  
  879.  
  880.     int opcao3;
  881.  
  882.     printf("Adicionar preferencias de utilizador\n -------------\n");
  883.     printf("Escolha a sua opcao \n");
  884.     printf("1 - Inserir 3 locais\n");
  885.     printf("2 - Inserir PDI's\n");
  886.     printf("3 - Inserir PDI HOT\n");
  887.     printf("4 - Retroceder\n");
  888.  
  889.     scanf("%d",&opcao3);
  890.     getchar();/*Para receber o enter e avançar*/
  891.     clear();
  892.  
  893.     switch(opcao3){
  894.         case 1:
  895.             clear();
  896.             imprimir_lista_locais(locais);
  897.             int i=0;
  898.             char local[MAX];
  899.             while(i!=3){
  900.                 printf("Nome do Local : ");
  901.                 scanf(" %[^\n]s", local);
  902.                 load_pref_local(users,locais,user_logado,local);
  903.                 i++;
  904.             }
  905.  
  906.             break;
  907.         case 2:
  908.             clear();
  909.             imprimir_lista_locais_pdis(locais);
  910.             int j=0;
  911.             printf("Quantos PDI's pretende inserir?\n");
  912.             scanf("%d",&j);
  913.             char pdi_aux[MAX];
  914.             while(j!=0){
  915.                 printf("Nome do PDI : ");
  916.                 scanf(" %[^\n]s", pdi_aux);
  917.                 load_pref_pdi(users,locais,user_logado,pdi_aux);
  918.                 j--;
  919.             }
  920.             break;
  921.         case 3:
  922.             clear();
  923.             imprimir_lista_locais_pdis(locais);
  924.             char pdihot[MAX];
  925.             printf("Nome do PDI HOT : ");
  926.             scanf(" %[^\n]s", pdihot);
  927.             load_pref_pdi(users,locais,user_logado,pdihot);
  928.  
  929.             break;
  930.         case 4:
  931.             clear();
  932.             menu(locais,users,user_logado);
  933.             break;
  934.  
  935.         default:
  936.  
  937.             insere_preferencias_user(users,locais,user_logado);
  938.     }
  939.  
  940. }
  941.  
  942. /*FUNCOES PARA SIMPLIFICAR MENU*/
  943.  
  944. void clear(){
  945.     system("@cls||clear"); /*Clear para o terminal do windows*/
  946. }
  947.  
  948. void voltar_menu(){
  949.     printf("Prima Enter para voltar ao menu.");
  950.     getchar();
  951.     clear();
  952. }
  953.  
  954. /*
  955. MENU
  956. */
  957.  
  958.  
  959.  
  960. void menu(lista_locais locais,lista_users users,char * user_logado)
  961. {
  962.  
  963.     int opcao,opcao3;
  964.     lista_users ant,inutil;
  965.     printf("MENU PRINCIPAL \n----------------------\n");
  966.  
  967.     printf("Escolha a sua opcao \n");
  968.     printf("1 - Alterar dados utilizador.\n");
  969.     printf("2 - Listagem por ordem alfabetica.\n");
  970.     printf("3 - Adicionar preferencias de utilizador.\n");
  971.     printf("4 - Remover preferencias de utilizador.\n");
  972.     printf("5 - Listagem por popularidade.\n");
  973.     printf("6 - Gerar Viagem.\n");
  974.     printf("7 - Terminar o programa.\n");
  975.  
  976.     scanf("%d",&opcao);
  977.     getchar();/*Para receber o enter e avançar*/
  978.     clear();
  979.  
  980.     switch(opcao){
  981.         case 1:
  982.             alterar_dados_utilizador(locais,users,user_logado);
  983.         case 2:
  984.             clear();
  985.             printf("Listagem por ordem alfabetica\n -------------\n");
  986.             int list_size2 = nNos(locais);
  987.             bubbleSortChar(&locais,list_size2);
  988.             ordena_pdis_Char(locais);
  989.             imprimir_lista_locais_pdis(locais);
  990.             voltar_menu();
  991.             break;
  992.         case 3:
  993.             clear();
  994.             insere_preferencias_user(users,locais,user_logado);
  995.             voltar_menu();
  996.             break;
  997.         case 4:
  998.             clear();
  999.             printf("Remover preferencias de utilizador\n -------------\n");
  1000.             voltar_menu();
  1001.             break;
  1002.         case 5:
  1003.             clear();
  1004.             printf("Listagem por popularidade\n -------------\n");
  1005.             int list_size = nNos(locais);
  1006.             bubbleSort(&locais,list_size);
  1007.             ordena_pdis(locais);
  1008.             imprimir_lista_locais_pdis(locais);
  1009.             voltar_menu();
  1010.             break;
  1011.         case 6:
  1012.             clear();
  1013.             printf("Gerar Viagem\n -------------\n");
  1014.             voltar_menu();
  1015.             break;
  1016.         case 7:
  1017.             escreve_users_ficheiro(users);
  1018.             clear();
  1019.             printf("Programa terminado\n");
  1020.             exit(0);
  1021.             break;
  1022.     }
  1023.     menu(locais,users,user_logado);
  1024. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement