Swih

Untitled

Feb 25th, 2021
781
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  main.c
  3. //  TP2-ALGO4
  4. //
  5. //  Created by Dayan Decamp on 19/02/2021.
  6. //  Copyright © 2021 Dayan Decamp. All rights reserved.
  7. //
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. typedef char CharTwenty[20];
  14.  
  15. typedef struct _word {
  16.     CharTwenty FrenchWord;
  17.     CharTwenty EnglishWord;
  18.     struct _word *mr, *ml;
  19.    
  20. } word, *dictionary;
  21.  
  22. dictionary createWord (CharTwenty FrenchWord,CharTwenty EnglishWord) {
  23.     dictionary dictio = NULL;
  24.     dictio = (dictionary) malloc(sizeof(struct _word));
  25.    
  26.     strcpy(dictio->EnglishWord, EnglishWord);
  27.     strcpy(dictio->FrenchWord, FrenchWord);
  28.    
  29.     dictio->ml=NULL;
  30.     dictio->mr=NULL;
  31.    
  32.     return dictio;
  33. }
  34.  
  35. dictionary addDico(dictionary dico,CharTwenty FrenchWord,CharTwenty EnglishWord) {
  36.     if (dico == NULL) {
  37.         return createWord(FrenchWord, EnglishWord);
  38.     } else {
  39.         if (FrenchWord[0] <= dico->FrenchWord[0]) {
  40.             dico->ml = addDico(dico->ml, FrenchWord, EnglishWord);
  41.         } else {
  42.             dico->mr = addDico(dico->mr, FrenchWord, EnglishWord);
  43.         }
  44.         return dico;
  45.     }
  46. }
  47.  
  48. void displayDico(dictionary dico) {
  49.     if (dico != NULL) {
  50.         displayDico(dico->ml);
  51.         printf("MOT FR : [%20s]-[%20s] : ENG MOT\n",dico->FrenchWord,dico->EnglishWord);
  52.         displayDico(dico->mr);
  53.     }
  54. }
  55.  
  56.  
  57. int searchWord(dictionary dico,CharTwenty searchWENG) {
  58.     if (dico == NULL) {
  59.         printf("Le mot %s est inconnu !",searchWENG);
  60.         return 1;
  61.     } else {
  62.         if (searchWENG[0]== dico->FrenchWord[0]) {
  63.             printf("La traduction de %s est %s\n",searchWENG,dico->EnglishWord);
  64.             return 0;
  65.         } else if (searchWENG[0] < dico->FrenchWord[0]) {
  66.             return searchWord(dico->ml, searchWENG);
  67.         } else {
  68.             return searchWord(dico->mr, searchWENG);
  69.         }
  70.     }
  71. }
  72.  
  73.  
  74. void countWordFR (dictionary dico, int * count) {
  75.     if (dico == NULL) {
  76.         count=0;
  77.     } else {
  78.         int countL =0;
  79.         countWordFR(dico->ml, &countL);
  80.         int countR = 0;
  81.         countWordFR(dico->mr, &countR);
  82.         *count = countL + countR + 1;
  83.     }
  84. }
  85.  
  86. void searchMostLongWord(dictionary dico,CharTwenty * W) {
  87.     if (dico != NULL) {
  88.         if (strlen(dico->FrenchWord) > strlen(*W)) {
  89.             strcpy(*W, dico->FrenchWord);
  90.             searchMostLongWord(dico->mr, W);
  91.             searchMostLongWord(dico->ml, W);
  92.         } else {
  93.             searchMostLongWord(dico->mr, W);
  94.             searchMostLongWord(dico->ml, W);
  95.         }
  96.     }
  97.    
  98. }
  99.  
  100. void deleteWordFR(dictionary dico,dictionary *newDico,CharTwenty deleteWord) {
  101.     CharTwenty newFR;
  102.     CharTwenty newENG;
  103.     if (dico != NULL) {
  104.         deleteWordFR(dico->ml,newDico,deleteWord);
  105.        
  106.         strcpy(newFR, dico->EnglishWord);
  107.         strcpy(newENG, dico->FrenchWord);
  108.         if (strcmp(deleteWord, dico->FrenchWord)!=0) {
  109.             *newDico = addDico(*newDico, newFR, newENG);
  110.         }
  111.        
  112.         deleteWordFR(dico->mr,newDico,deleteWord);
  113.     }
  114. }
  115.  
  116. void copyDico(dictionary dico,dictionary *newDico) {
  117.     CharTwenty newFR;
  118.     CharTwenty newENG;
  119.     if (dico != NULL) {
  120.         copyDico(dico->ml,newDico);
  121.        
  122.         strcpy(newFR, dico->EnglishWord);
  123.         strcpy(newENG, dico->FrenchWord);
  124.         *newDico = addDico(*newDico, newFR, newENG);
  125.        
  126.         copyDico(dico->mr,newDico);
  127.     }
  128. }
  129.  
  130.  
  131.  
  132. int main(int argc, const char * argv[]) {
  133.     // insert code here...
  134.     dictionary dico = NULL;
  135.     CharTwenty WFR;
  136.     CharTwenty WENG;
  137.     puts("------------------------- DISPLAY DICO -------------------------");
  138.     strcpy(WFR, "Bonjour");
  139.     strcpy(WENG, "Hello");
  140.     dico = addDico(dico, WFR, WENG);
  141.    
  142.     strcpy(WFR, "Bouteille");
  143.     strcpy(WENG, "Bottle");
  144.     dico = addDico(dico, WFR, WENG);
  145.    
  146.     strcpy(WFR, "Carte");
  147.     strcpy(WENG, "Card");
  148.     dico = addDico(dico, WFR, WENG);
  149.    
  150.     strcpy(WFR, "Voiture");
  151.     strcpy(WENG, "Car");
  152.     dico = addDico(dico, WFR, WENG);
  153.    
  154.     strcpy(WFR, "Ecran");
  155.     strcpy(WENG, "Screen");
  156.     dico = addDico(dico, WFR, WENG);
  157.    
  158.     displayDico(dico);
  159.     puts("----------------------------------------------------------------\n");
  160.     displayDico(dico);
  161.    
  162.    
  163.     puts("----------------------------------------------------------------\n");
  164.  
  165.    
  166.     puts("-------------------------- COUNT DICO --------------------------");
  167.     int countWord;
  168.     countWordFR(dico, &countWord);
  169.     printf("Il y a %d mots dans le dictionnaire\n",countWord);
  170.     puts("----------------------------------------------------------------\n");
  171.  
  172.     puts("---------------------- MOST LONG WORD DICO ---------------------");
  173.     CharTwenty longWord;
  174.     strcpy(longWord, "");
  175.     searchMostLongWord(dico, &longWord);
  176.     printf("Il y a %s mots dans le dictionnaire\n",longWord);
  177.     puts("----------------------------------------------------------------\n");
  178.  
  179.     puts("------------------------ DELETE WORD ---------------------------");
  180.    
  181.     displayDico(dico);
  182.     dictionary newDicoDelete = NULL;
  183.     deleteWordFR(dico,&newDicoDelete,"Voiture");
  184.     puts("-------------------------- WITH DELETE -------------------------");
  185.  
  186.     displayDico(newDicoDelete);
  187.     puts("----------------------------------------------------------------\n");
  188.    
  189.    
  190.     puts("------------------------  COPY DICO -----------------------------");
  191.     dictionary newDico = NULL;
  192.     copyDico(dico,&newDico);
  193.     displayDico(newDico);
  194.     puts("-----------------------------------------------------------------\n");
  195.    
  196.     return 0;
  197. }
  198.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×