Advertisement
Guest User

Untitled

a guest
May 19th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define L 10
  4. #define N 4
  5. #define Vo 1
  6. typedef struct
  7. {
  8. double vitesse;
  9. int *direction;//tableau de 4 booleens , [top bot left right]
  10. } Particule;
  11.  
  12. Particule **plan;
  13.  
  14. //Fonction pour afficher le plan
  15. void afficherPlan()
  16. {
  17. for (int row = 0; row < L; row++)
  18. {
  19. for (int column = 0; column < L; column++)
  20. {
  21. if (plan[row][column].vitesse != -1)
  22. printf("+ ");
  23. }
  24. printf("\n");
  25. }
  26. }
  27.  
  28. //Fonction pour vérifier si le plan contient toujours des particules de vitesse non nulle
  29. int etatFinal()
  30. {
  31. for (int row = 0; row < L; row++)
  32. {
  33. for (int column = 0; column < L; column++)
  34. {
  35. if (plan[row][column].vitesse != -1)
  36. return 0;
  37. }
  38. }
  39. return 1;
  40. }
  41. //Main function
  42. int main()
  43. {
  44. //Construction du plan (matrice carrée)
  45. plan = calloc(L, sizeof(Particule *) + 1);
  46. for (int i = 0; i < L; i++)
  47. {
  48. plan[i] = calloc(L, sizeof(Particule));
  49. }
  50.  
  51. for (int i = 0; i < L; i++)
  52. {
  53. for (int j = 0; j < L; j++)
  54. {
  55. plan[i][j].direction = calloc(4, sizeof(int));
  56. plan[i][j].vitesse = -1;
  57. }
  58. }
  59. //Initialisation de la particule du milieu (agregat)
  60. plan[(int)(L / 2)][(int)(L / 2)].direction;
  61. plan[(int)(L / 2)][(int)(L / 2)].vitesse = 0;
  62.  
  63. //Initialisation des autres particules
  64. int compteur = N;
  65. for (int i = 0; i < L; i++)
  66. {
  67. for (int j = 0; j < L; j++)
  68. {
  69. if (plan[i][j].vitesse != -1)
  70. {
  71. plan[i][j].vitesse = Vo;
  72. plan[i][j].direction = calloc(4,sizeof(int));
  73. for(int k = 0;k<4;k++)
  74. plan[i][j].direction[k] = rand() % 2;
  75. compteur--;
  76. }
  77. }
  78. }
  79.  
  80. //Début de la boucle de simulation
  81. while (!etatFinal())
  82. {
  83. for (int i = 0; i < L; i++)
  84. {
  85. for (int j = 0; j < L; j++)
  86. {
  87. if (plan[i][j].vitesse != -1)
  88. {
  89. //On déplace la particule
  90. int newRow = i;
  91. newRow -= plan[i][j].direction[0];
  92. newRow += plan[i][j].direction[1];
  93. int newCol = j;
  94. newCol -= plan[i][j].direction[2];
  95. newRow += plan[i][j].direction[3];
  96. plan[newCol][newRow] = plan[i][j];
  97. plan[i][j].vitesse = -1;
  98. }
  99. }
  100. }
  101. afficherPlan();
  102. system("pause");
  103. }
  104. system("pause");
  105. return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement