ArfIsAToe

profa tetmanyek pt1

Jun 7th, 2021 (edited)
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define INDICE_INVALIDE 0
  4. typedef double ELEMENT;
  5. typedef struct {
  6.     int nombre_elements;
  7.     ELEMENT elements[100];
  8. }LISTE_TABLEAU;
  9.  
  10. int liste_pleine(LISTE_TABLEAU *liste)
  11. {
  12.     /// zedt'ha ena khater profa ma ketbethhech
  13.     return liste->nombre_elements ==100;
  14. }
  15. void liste_initialiser(LISTE_TABLEAU *liste)
  16. {
  17.     liste->nombre_elements=0;
  18. }
  19. ELEMENT liste_element(LISTE_TABLEAU *liste, int indice)
  20. {
  21.     if (indice >=0 && indice < liste->nombre_elements)
  22.         return liste->elements[indice];
  23.     else
  24.     {
  25.         printf("Erreur d'indice\n");
  26.         return 0;
  27.     }
  28. }
  29. int liste_ajouter(LISTE_TABLEAU* liste, ELEMENT e)
  30. {
  31.     int indice;
  32.     if(!liste_pleine(liste))
  33.     {
  34.         indice= liste->nombre_elements;
  35.         liste->elements[indice]=e;
  36.         liste->nombre_elements++;
  37.     }
  38.     else
  39.         indice= INDICE_INVALIDE;
  40.     return indice;
  41. }
  42. int liste_inserer(LISTE_TABLEAU *liste, int indice, ELEMENT e)
  43. {
  44.     int i, resultat;
  45.     if (!liste_pleine(liste))
  46.         if (indice >= 0 && indice <= liste->nombre_elements)
  47.         {
  48.             for (i= liste->nombre_elements;i>indice;i--)
  49.                 liste->elements[i]=liste->elements[i-1];
  50.             liste->nombre_elements++;
  51.             liste->elements[indice]=e;
  52.             resultat= indice;
  53.         }
  54.         else
  55.             resultat= INDICE_INVALIDE;
  56.     else
  57.         resultat= INDICE_INVALIDE;
  58.  
  59.     return resultat;
  60. }
  61. int liste_supprimer(LISTE_TABLEAU *liste, int indice)
  62. {
  63.     int i,resultat;
  64.     if (indice >=0 && indice < liste->nombre_elements)
  65.     {
  66.         for (i = indice +1; i < liste->nombre_elements; i++)
  67.             liste->elements[i-1]= liste->elements[i];
  68.         liste->nombre_elements--;
  69.         resultat=indice;
  70.     }
  71.     else
  72.         resultat= INDICE_INVALIDE;
  73.  
  74.     return resultat;
  75. }
  76. int liste_nombre_elements(LISTE_TABLEAU *liste)
  77. {
  78.     /// zedt'ha ena khater profa ma ketbethhech
  79.     return liste->nombre_elements;
  80. }
  81. void afficher_liste(LISTE_TABLEAU *liste)
  82. {
  83.     int i,nombre_elements;
  84.     nombre_elements= liste_nombre_elements(liste);
  85.     printf("{");
  86.     for (i =0; i<nombre_elements; i++)
  87.     {
  88.         if (i>0)
  89.             printf(", ");
  90.         printf("%lg",liste_element(liste,i));
  91.     }
  92.     printf("}");
  93. }
  94.  
  95. int main()
  96. {
  97.     LISTE_TABLEAU *liste;
  98.     int n,x;
  99.     ELEMENT e;
  100.     // all the code she wrote page 1-16 (Listes : Implémentation par les tableaux)
  101.     printf("What Size List ya got:\n");
  102.     liste_initialiser(liste);
  103.     scanf("%d",&n);
  104.     printf("Fill In The List:\n");
  105.     for (int a=0;a<n;a++)
  106.     {
  107.         scanf("%lf",&e);
  108.         x=liste_ajouter(liste,e);
  109.     }
  110.     int answer;
  111.     do
  112.     {
  113.         ///    system("cls");
  114.         printf("\n\nPick One\n\t1: Add an element to the end of the list.\n\t2: Add an element to a specific position.\n\t3: Delete an element by position.\n\t4: Print List.\n\t0:Stop.\n");
  115.         scanf("%d",&answer);
  116.  
  117.        switch(answer)
  118.        {
  119.        case 1:
  120.             system("cls");
  121.             printf("type element: ");
  122.             scanf("%lf",&e);
  123.             x=liste_ajouter(liste,e);
  124.             break;
  125.        case 2:
  126.             system("cls");
  127.             printf("type element and position:\n");
  128.             scanf("%lf%d",&e,&x);
  129.             x=liste_inserer(liste,x,e);
  130.             break;
  131.        case 3:
  132.             system("cls");
  133.             printf("type position of the element you want to delete: ");
  134.             scanf("%d",&x);
  135.             x=liste_supprimer(liste,x);
  136.             break;
  137.        case 4:
  138.             system("cls");
  139.             printf("_______________________________________\n");
  140.             afficher_liste(liste);
  141.             printf("\n_______________________________________\n");
  142.             break;
  143.        case 0:
  144.             system("cls");
  145.             break;
  146.        default:
  147.             system("cls");
  148.             system("color 4");
  149.             ///printf(“\033[0;31m”);
  150.             printf("ERROR: You Have Picked An Non Existent Option...\nClick On Any Button To Retry...|");
  151.             getch();
  152.             system("color 7");
  153.             system("cls");
  154.             ///printf(“\033[0m”);
  155.        }
  156.  
  157.     }while(answer!=0);
  158.     system("color 5");
  159.     printf("Thanks..");
  160. }
  161.  
Advertisement
Add Comment
Please, Sign In to add comment