SHARE
TWEET

Untitled

a guest May 16th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5.  
  6. #define COMANDO 2
  7. #define NOME 1024
  8. #define EMAIL 512
  9. #define TELEFONE 64
  10.  
  11. char comando[COMANDO];
  12. char nome[NOME];
  13. char email[EMAIL];
  14. char telefone[TELEFONE];
  15. char dominio[EMAIL];
  16. /*ESTRUTURAS */
  17.  
  18.  
  19. typedef struct node_t {
  20.     char *pnome;
  21.     char *pemail;
  22.     char *ptelefone;
  23.     char *pdominio;
  24.     struct node_t *next;
  25.     struct node_t *prev;
  26. } Node;
  27.  
  28. typedef struct limites {
  29.   Node * head;
  30.   Node * tail;
  31. }Limites;
  32.  
  33. /* hashTable */
  34. typedef struct el
  35. {
  36.   char *name;
  37.   Node *enderecolista;
  38.   struct el * next;
  39. }node_hash;
  40.  
  41. node_hash *hashTable[10000]={NULL};
  42. int hashTableSize=1083;
  43.  
  44. int stringToKey(char name[]) /*funcao de hash */
  45. {
  46.   unsigned int i=0;
  47.   int c,s =0;
  48.   for(i=0;i<strlen(name);i++)
  49.   {
  50.     c=name[i];
  51.     s = s+c;
  52.   }
  53.   return s;
  54. }
  55.  
  56. void addNode(char nome[],Limites * limites)
  57. {
  58.   int ascii,key;
  59.   node_hash *n, *nl;
  60.   n=(node_hash *)malloc(sizeof(node_hash));
  61.   n->name = malloc(strlen(nome)+1);
  62.   strcpy(n->name,nome);
  63.   n->enderecolista=limites->head;
  64.   n->next = NULL;
  65.   ascii = stringToKey(n->name);
  66.   key = ascii % hashTableSize;
  67.   if(hashTable[key] == NULL)
  68.   {
  69.     hashTable[key]=n;
  70.   }
  71.   else
  72.   {
  73.     for(nl=hashTable[key]; nl->next != NULL; nl=nl->next);
  74.     nl -> next =n;
  75.   }
  76. }
  77.  
  78. Node* searchNode(char nName[])
  79. {
  80.   int ascii = stringToKey(nName);
  81.   int key = ascii % hashTableSize;
  82.   node_hash *n;
  83.   for (n=hashTable[key];n!=NULL;n=n->next)
  84.   {
  85.     if(strcmp(n->name, nName)==0)
  86.     {
  87.       return n->enderecolista;
  88.     }
  89.   }
  90.   return NULL; /*Retorna null se n existir*/
  91. }
  92. void free_hash(node_hash *n)
  93. {
  94.   free(n->name);
  95.   free(n->enderecolista);
  96.   free(n);
  97. }
  98. void deleteNode(char nName[])
  99. {
  100.   int ascii=stringToKey(nName);
  101.   int key = ascii % hashTableSize;
  102.   node_hash *n, *nl;
  103.   if(strcmp(hashTable[key]->name,nName)==0)
  104.   {
  105.     n = hashTable[key];
  106.     if(hashTable[key]->next!=NULL){
  107.     hashTable[key]=hashTable[key]->next;
  108.   }
  109.     free_hash(n);
  110.     return;
  111.   }
  112.   for(n=hashTable[key];n->next != NULL;n=n->next)
  113.   {
  114.     nl= n->next;
  115.     if(strcmp(nl->name,nName)==0)
  116.     {
  117.       n->next = nl->next;
  118.       free_hash(nl);
  119.       break;
  120.     }
  121.   }
  122. }
  123. /* */
  124.  
  125. void free_node(Node* node)
  126. {
  127.   free(node->pnome);
  128.   free(node->pemail);
  129.   free(node->ptelefone);
  130.   free(node->pdominio);
  131.   free(node);
  132. }
  133.  
  134. Limites * add(char nome[], char email[], char telefone[], char dominio[], Limites *limites) {
  135.     Node *new_node;
  136.     new_node =(Node *) malloc(sizeof(Node));
  137.     new_node->pnome=malloc(strlen(nome)+1); /* strlen da me o tamanho do char tb? */
  138.     new_node->pemail=malloc(strlen(email)+1);
  139.     new_node->ptelefone=malloc(strlen(telefone)+1);
  140.     new_node->pdominio=malloc(strlen(dominio)+1);
  141.  
  142.     strcpy(new_node->pnome,nome);
  143.  
  144.     strcpy(new_node->pemail,email);
  145.     strcpy(new_node->ptelefone,telefone);
  146.     strcpy(new_node->pdominio,dominio);
  147.     new_node->next= limites->head;
  148.     new_node->prev= NULL;
  149.  
  150.     if(limites->head != NULL) limites->head->prev= new_node;
  151.  
  152.     limites->head = new_node;
  153.  
  154.     if(limites->tail==NULL) limites->tail = new_node;
  155.     printf("head:%s\ntail:%s\n",limites->head->pnome,limites->tail->pnome);
  156.     printf("Phead:%p\nPtail:%p\n",(void*)limites->head,(void*)limites->tail );
  157. return limites;
  158. }
  159.  
  160.  
  161.  
  162. Limites* removen(Limites* limites,Node* del)
  163. {
  164.   if (limites->head == NULL || del == NULL)
  165.     return limites;
  166.  
  167.   if(limites->tail==del && del->prev != NULL){
  168.     limites->tail=del->prev;
  169.     limites->tail->next=NULL;
  170.     free_node(del);
  171.     printf("head:%s\ntail:%s\n",limites->head->pnome,limites->tail->pnome);
  172.     printf("Phead:%p\nPtail:%p\n",(void*)limites->head,(void*)limites->tail );
  173.   }
  174.   else if(limites->tail==del && del->prev == NULL){
  175.     limites->head=NULL;
  176.     limites->tail=NULL;
  177.     free_node(del);
  178.     printf("beep\n");
  179.     printf("Phead:%p\nPtail:%p\n",(void*)limites->head,(void*)limites->tail );
  180.   }
  181.   else if(limites->head==del){
  182.     limites->head=del->next;
  183.     limites->head->prev=NULL;
  184.     free_node(del);
  185.     printf("head:%s\ntail:%s\n",limites->head->pnome,limites->tail->pnome);
  186.     printf("Phead:%p\nPtail:%p\n",(void*)limites->head,(void*)limites->tail );
  187.   }
  188.   else{
  189.     del->next->prev=del->prev;
  190.     free_node(del);
  191.     printf("head:%s\ntail:%s\n",limites->head->pnome,limites->tail->pnome);
  192.     printf("Phead:%p\nPtail:%p\n",(void*)limites->head,(void*)limites->tail );
  193.   }
  194. /*
  195.   if (limites->head == del && del->next != NULL){
  196.     limites->head = del->next;
  197.     printf("1head:%s\ntail:%s\n",limites->head->pnome,limites->tail->pnome);
  198.   }
  199.   if (limites->head == del && del->next == NULL){
  200.     limites->head = NULL;
  201.     printf("2head:%s\ntail:%s\n",limites->head->pnome,limites->tail->pnome);
  202.   }
  203.   if (del->next != NULL){
  204.     del->next->prev = del->prev;
  205.     printf("3head:%s\ntail:%s\n",limites->head->pnome,limites->tail->pnome);
  206.   }
  207.   if (del->prev != NULL){
  208.     del->prev->next = del->next;
  209.     printf("4head:%s\ntail:%s\n",limites->head->pnome,limites->tail->pnome);
  210.   }
  211.   if(del->next == NULL){
  212.     limites->tail->next = NULL;
  213.     limites->tail=limites->tail->prev;
  214.     printf("5head:%s\ntail:%s\n",limites->head->pnome,limites->tail->pnome);
  215.   }*/
  216.  
  217.  
  218.   /*free_node(del);*/
  219.   /*printf("6head:%s\ntail:%s\n",limites->head->pnome,limites->tail->pnome);*/
  220.   return limites;
  221. }
  222.  
  223.  
  224.  
  225.  
  226. /*
  227. Node * removen(char nome[], Node *head) {
  228.     Node *current_node = head;
  229.     Node *prev_node;
  230.     while ( current_node != NULL) {
  231.         if (strcmp(current_node->pnome,nome) == 0) {
  232.             if (current_node == head) {
  233.                  head = current_node->next;
  234.                  free_node(current_node);
  235.             } else {
  236.                 prev_node->next = current_node->next;
  237.             }
  238.         }
  239.         prev_node = current_node;
  240.         current_node = current_node->next;
  241.     }
  242. return(head);
  243. } */
  244.  
  245. Node* muda_mail(Node* mudar,char email[],char dominio[])
  246. {
  247.   strcpy(mudar->pemail,email);
  248.   strcpy(mudar->pdominio,dominio);
  249.   return mudar;
  250. }
  251.  
  252. void print(Limites *limite) {
  253.     Node *current_node=limite->tail;
  254.  
  255.     while ( current_node != NULL) {
  256.         printf("%s ", current_node->pnome);
  257.         printf("%s ", current_node->pemail);
  258.         printf("%s\n", current_node->ptelefone);
  259.         current_node = current_node->prev;
  260.     }
  261. }
  262.  
  263. void print_single(Node* node){
  264.   printf("%s ",node->pnome);
  265.   printf("%s ",node->pemail);
  266.   printf("%s\n",node->ptelefone);
  267. }
  268.  
  269. void conta_dominios(Limites * limite, char dominio[]){
  270.   Node *current_node=limite->tail;
  271.   long int contador=0;
  272.   while(current_node !=NULL){
  273.     if (strcmp(current_node->pdominio,dominio)==0)
  274.       contador++;
  275.     current_node= current_node->prev;
  276.   }
  277.   printf("%s:%ld\n",dominio,contador);
  278. }
  279.  
  280.  
  281.  /*                   INPUT      */
  282. void ler()
  283. {
  284.   int i=0;
  285.   int c;
  286.   int len;
  287.   int l=0;
  288.   int k=0;
  289.   int controlo;
  290.   char input_aux[3][NOME];
  291.   char input[COMANDO+NOME+EMAIL+TELEFONE];
  292.   memset(input_aux[0],0,strlen(input_aux[0]));
  293.   memset(input_aux[1],0,strlen(input_aux[1]));
  294.   memset(input_aux[2],0,strlen(input_aux[2]));
  295.  
  296.   memset(input,0,strlen(input));
  297.   c =getchar();
  298.   while(c!='\n')
  299.   {
  300.     input[i] = c;
  301.     c = getchar();
  302.     i++;
  303.   }
  304.   input[i]='\0';
  305.   len = strlen(input);
  306.   comando[0]=input[0];
  307.   for (i=2;i<len;i++)
  308.   {
  309.     if (input[i]==' ')
  310.     {
  311.       k++;
  312.       l=0;
  313.     }
  314.     else
  315.     {
  316.       input_aux[k][l]=input[i];
  317.       l++;
  318.     }
  319.   }
  320.   strcpy(nome,input_aux[0]);
  321.   strcpy(email,input_aux[1]);
  322.   strcpy(telefone,input_aux[2]);
  323.   if(comando[0]!='e')
  324.   {
  325.   len = strlen(email);
  326.   controlo=0;
  327.   k=0;
  328.   for(i=0;i<len;i++)
  329.     {
  330.       if(controlo)
  331.       {
  332.         dominio[k]=email[i];
  333.         k++;
  334.       }
  335.     if(email[i]=='@') controlo=1;
  336.   }
  337.   }
  338.   else
  339.   {
  340.     len = strlen(nome);
  341.     controlo=0;
  342.     k=0;
  343.     for(i=0;i<len;i++)
  344.       {
  345.         if(controlo)
  346.         {
  347.           dominio[k]=nome[i];
  348.           k++;
  349.         }
  350.       if(nome[i]=='@') controlo=1;
  351.   }
  352. }
  353. }
  354.  
  355. void limpa_input()
  356. {
  357.   memset(comando,0,strlen(comando));
  358.   memset(nome,0,strlen(nome));
  359.   memset(email,0,strlen(email));
  360.   memset(telefone,0,strlen(telefone));
  361.   memset(dominio,0,strlen(dominio));
  362.  
  363. }
  364. /*                MAIN                     */
  365.  
  366.  
  367. int main(){
  368.   Limites *limites = (Limites*)malloc(sizeof(Limites));
  369.   int control=1;
  370.   limites->head = NULL;
  371.   limites->tail= NULL;
  372.   printf("Phead:%p\nPtail:%p\n",(void*)limites->head,(void*)limites->tail );
  373. while(control){
  374.   limpa_input();
  375.   ler();
  376.   switch(comando[0]){
  377.     case 'a':
  378.     if(searchNode(nome)==NULL){
  379.       limites=add(nome,email,telefone,dominio,limites);
  380.       addNode(nome,limites);
  381.     }
  382.     else{
  383.       printf("Nome existente.\n");
  384.     }
  385.     break;
  386.  
  387.     case 'l':
  388.    print(limites);
  389.     break;
  390.  
  391.     case 'p':
  392.     if(searchNode(nome)!=NULL)
  393.     {
  394.     print_single(searchNode(nome));
  395.     }
  396.     else{
  397.       printf("Nome inexistente.\n");
  398.     }
  399.     break;
  400.  
  401.     case 'r':
  402.     if(searchNode(nome)!=NULL)
  403.     {
  404.     removen(limites,searchNode(nome));
  405.     deleteNode(nome);
  406.     }
  407.     else{
  408.       printf("Nome inexistente.\n");
  409.     }
  410.     break;
  411.  
  412.     case 'e':
  413.     if(searchNode(nome)!=NULL)
  414.     {
  415.     muda_mail(searchNode(nome),email,dominio);
  416.     }
  417.     else{
  418.       printf("Nome inexistente.\n");
  419.     }
  420.     break;
  421.  
  422.     case 'c':
  423.     conta_dominios(limites,nome);
  424.     break;
  425.  
  426.     case 'x':
  427.     control=0;
  428.     break;
  429.   }
  430.  
  431. }
  432.   return 0;
  433. }
  434.  
  435. /*ERROS:
  436. quando dou 5 removes da erro na hashtable double free corruption...(problema hash table confirmado)
  437. quando meto a 0 e adiciono 2 aquilo mete infinito a 2 (head e tail e no meio)(problema da lista???)
  438. a ppppppppp falha???????????????
  439.  
  440.  
  441.  */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top