Advertisement
alsagone

TD_2

Sep 24th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. //g. Ecrire un programme qui affiche une ligne de longueur N en alternant les espaces et les étoiles
  6.  
  7. void etoiles_espaces(int n)
  8. {
  9.     int i ;
  10.     for(i = 1 ; i<= n ; i++)
  11.     {
  12.         if (i%2 != 0)
  13.         {
  14.             printf(" ");
  15.         }
  16.  
  17.         else {printf("*") ;}
  18.      }
  19.  
  20.     printf("\n");
  21.     return ;
  22. }
  23.  
  24.  
  25. /* Explications :
  26. Comme il faut alterner les espaces et les étoiles, on teste la partié de i avec un modulo
  27. Par exemple pour etoiles_espaces(5),
  28. i = 1 -> impair -> espace
  29. i = 2 -> pair -> étoile
  30. i = 3 -> impair -> espace
  31. i = 4 -> pair -> étoile
  32. i = 5 -> impair -> espace
  33.  
  34. Résultat : * *
  35. */
  36.  
  37. // h. En réutilisant ce que vous avez fait, écrivez un programme qui affiche un damier de taille MxN
  38.  
  39. // Là, on va avoir besoin de refaire une fonction comme etoiles_espaces mais qui affiche une étoile en premier puis un espace --> etoiles_espace_v2(2) affiche *(espace)
  40. void etoiles_espaces_v2(int n)
  41. {
  42.     int i ;
  43.  
  44.     for(i = 1 ; i<= n ; i++)
  45.     {
  46.         if (i%2 != 0)
  47.         {
  48.             printf("*");
  49.         }
  50.  
  51.         else {printf(" ") ;}
  52.      }
  53.  
  54.     printf("\n");
  55.     return ;
  56. }
  57.  
  58. void damier ()
  59. {
  60.     int i,n,m ;
  61.  
  62.     printf("Taille du damier N : ") ;  //Nombre de lignes
  63.     scanf("%d",&n) ;
  64.  
  65.     printf("Longueur du damier m : ") ; //Taille de la ligne
  66.     scanf("%d",&m) ;
  67.  
  68.     for(i = 1 ; i <= n ; i++)
  69.     {
  70.    
  71.         if(i%2 !=0)
  72.         {
  73.             etoiles_espaces_v2(m) ;
  74.         }
  75.  
  76.         else
  77.         {
  78.             etoiles_espaces(m) ;
  79.         }
  80.     }
  81.  
  82.     printf("\n");
  83.     return ;
  84. }
  85.  
  86.  
  87.  
  88. /* Faites en sorte que l'utilisateur puisse afficher un damier en choisissant non
  89. seulement sa taille mais aussi le caractère utilisé. */
  90.  
  91.  
  92. /* Là je ne vois pas faire autrement que de ré-écrire les deux fonctions étoiles_espace
  93. en permettant à l'utilisateur de choisir son caractère */
  94.  
  95. void caracteres_espaces(int n, char c)
  96. {
  97.     int i ;
  98.  
  99.     for(i = 1 ; i <= n ; i++)
  100.     {
  101.         if (i%2 != 0)
  102.         {
  103.             printf(" ") ;
  104.         }
  105.  
  106.         else
  107.         {
  108.             printf("%c",c ) ;
  109.         }
  110.      }
  111.  
  112.     printf("\n");
  113.     return ;
  114. }
  115.  
  116.  
  117. void caracteres_espaces_v2(int n, char c)
  118. {
  119.     int i ;
  120.  
  121.     for(i = 1 ; i <= n ; i++)
  122.     {
  123.         if (i%2 != 0)
  124.         {
  125.             printf("%c",c ) ;
  126.         }
  127.  
  128.         else
  129.         {
  130.             printf(" ") ;
  131.         }
  132.      }
  133.  
  134.     printf("\n");
  135.     return ;
  136. }
  137.  
  138. void damier_caractere()
  139. {
  140.     char c ;
  141.     int n,m,i,j ;
  142.  
  143.     printf("Caractère souhaité ? ") ;
  144.     scanf(" %c",&c) ;
  145.  
  146.     printf("Taille du damier N : ") ;  //Nombre de lignes
  147.     scanf("%d",&n) ;
  148.  
  149.     printf("Longueur du damier m : ") ; //Taille de la ligne
  150.     scanf("%d",&m) ;
  151.  
  152.     for(i = 1 ; i <= n ; i++)
  153.     {
  154.    
  155.         if (i%2 !=0)
  156.         {
  157.             caracteres_espaces_v2(m,c) ;
  158.         }
  159.  
  160.         else
  161.         {
  162.             caracteres_espaces(m,c) ;
  163.         }
  164.     }
  165.     printf("\n");
  166.     return ;
  167. }
  168.  
  169.  
  170.  
  171.  
  172. //Figure 1
  173.  
  174. void afficher_ligne(int taille)
  175. {
  176.     if (taille == 0)
  177.     {
  178.         printf("La taille doit être un nombre positif non nul.\n");
  179.         exit(-1) ;
  180.     }
  181.  
  182.     int i ;
  183.     for (i = 1 ; i <= taille ; i++)
  184.     {
  185.         printf("*");
  186.     }
  187.     printf("\n");
  188.     return ;
  189. }
  190.  
  191. void pyramide_montante(int taille)
  192. {
  193.     if (taille == 0)
  194.     {
  195.         printf("La taille doit être un nombre positif non nul.\n");
  196.         exit(-1) ;
  197.     }
  198.  
  199.     int i ;
  200.     for (i = 1 ; i <= taille ; i++)
  201.     {
  202.         afficher_ligne(i) ;
  203.     }
  204.  
  205.     return ;
  206. }
  207.  
  208. /* pyramide_montante(5)
  209.     *
  210.     **
  211.     ***
  212.     ****
  213.     *****
  214. */
  215.  
  216. void pyramide_descendante(int taille)
  217. {
  218.     if (taille == 0)
  219.     {
  220.         printf("La taille doit être un nombre positif non nul.\n");
  221.         exit(-1) ;
  222.     }
  223.  
  224.     int i ;
  225.     for (i = taille ; i >= 1 ; i--)
  226.     {
  227.         afficher_ligne(i) ;
  228.     }
  229.  
  230.     return ;
  231. }
  232.  
  233. void triangle(int taille_max)
  234. {
  235.     if (taille_max == 0)
  236.     {
  237.         printf("La taille doit être un nombre positif non nul.\n");
  238.         exit(-1) ;
  239.     }
  240.  
  241.     pyramide_montante(taille_max) ;
  242.     pyramide_descendante(taille_max-1) ;
  243.  
  244.     return ;
  245. }
  246.  
  247.  
  248. //Test des fonctions
  249. int main()
  250. {
  251.     printf("Test de damier\n") ;
  252.     damier() ;
  253.  
  254.     printf("Test de damier_caractere\n") ;
  255.     damier_caractere() ;
  256.  
  257.     printf("Test triangle\n");
  258.     triangle(5) ;
  259.     return 0 ;
  260.  
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement