Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "merci.h"
  4.  
  5.  
  6. graphs initialisation_graphe()
  7. {
  8. graphs graphique;
  9. int tmps;
  10. printf("\n Souhaitez vous que je graphs soit un graphs oriente ? \n");
  11. printf("tapez 1 pour oui \n");
  12. printf("Tapez 2 pour Non \n");
  13. scanf("%d",&tmps);
  14. graphique.type = tmps;
  15. do
  16. {
  17. printf("Combien de noeuds possede le graphique ? ( Max 25) : ");
  18. scanf("%d",&tmps);
  19. }while(tmps >25 || tmps < 0 );
  20.  
  21. graphique.degres = tmps;
  22. graphique.sommet = malloc(sizeof(char)*tmps);
  23.  
  24. for (int i = 0 ; i < tmps ; i++)
  25. {
  26. fflush(stdin);
  27. printf("Entrez la valeur du sommet n %d : ",i);
  28. scanf("%c",&graphique.sommet[i]);
  29. printf(" La valeur du sommets vaut %c \n",graphique.sommet[i]);
  30. }
  31. printf("combien de lien possede le graph ? :");
  32. scanf("%d",&tmps);
  33. graphique.nombreLiens = tmps;
  34. graphique.liens = malloc(sizeof(char)*tmps);
  35. for(int i = 0 ; i < tmps ;i ++)
  36. {
  37. graphique.liens[i] = malloc(sizeof(char)*3);
  38. }
  39. int j = 0 ;
  40. for(int i = 0 ; i< tmps ;i++)
  41. {
  42. printf("point de depart du lien : ");
  43. fflush(stdin);
  44.  
  45. scanf(" %c",&graphique.liens[i][j]);
  46. printf("point d'arrive du lien : ");
  47. fflush(stdin);
  48. scanf(" %c",&graphique.liens[i][j+1]);
  49. printf("poids du lien : ");
  50. fflush(stdin);
  51. scanf(" %c",&graphique.liens[i][j+2]);
  52.  
  53. }
  54.  
  55. return graphique;
  56. }
  57. int isInGraph(graphs graphique, char som)
  58. {
  59. for(int i = 0; i <graphique.degres; i++)
  60. {
  61. if(graphique.sommet[i] == som)
  62. {
  63. return 1;
  64. }
  65. }
  66. return 0;
  67. }
  68. char* voisin_niveau_1(graphs graphiques, char sommets)
  69. {
  70. char *voisin;
  71. voisin = malloc(sizeof(char)*graphiques.degres);
  72. int nbvoisin = 0;
  73. do
  74. {
  75. printf("\n de quel sommet voulez vous connaitre les voisin : ");
  76. fflush(stdin);
  77. scanf(" %c",&sommets);
  78. }while(isInGraph(graphiques,sommets) == 0);
  79.  
  80. printf(" les voisin du sommets %c sont : ",sommets);
  81. for(int i = 0; i < graphiques.nombreLiens;i++)
  82. {
  83. if(graphiques.liens[i][0] == sommets && graphiques.type == 1)
  84. {
  85. printf(" %c",graphiques.liens[i][1]);
  86. voisin[nbvoisin] = graphiques.liens[i][1];
  87. nbvoisin+=1;
  88. }
  89. if(graphiques.liens[i][0] == sommets && graphiques.type == 2)
  90. {
  91. printf(" %c",graphiques.liens[i][1]);
  92. voisin[nbvoisin] = graphiques.liens[i][1];
  93. nbvoisin+=1;
  94. }
  95. if(graphiques.liens[i][1] == sommets && graphiques.type == 2)
  96. {
  97. printf(" %c, ",graphiques.liens[i][0]);
  98. voisin[nbvoisin] = graphiques.liens[i][0];
  99. nbvoisin+=1;
  100. }
  101. }
  102. printf("\n");
  103. return voisin;
  104. }
  105.  
  106. int parcours_profondeur(graphs graphique,char sommet,int vis)
  107. {
  108.  
  109. char* visit = malloc(sizeof(char)*graphique.degres);
  110. char* voisinage = malloc(sizeof(char)*graphique.degres);
  111. printf("%c",sommet);
  112. voisinage = voisin_niveau_1(graphique,sommet);
  113. for (int i = 0; i < strlen(voisinage);i++)
  114. {
  115. if(strchr(visit,voisinage[i]) == NULL)
  116. {
  117. visit[vis] = voisinage[i];
  118. parcours_profondeur(graphique,voisinage[i],vis+1);
  119. }
  120. else
  121. {
  122. return 0;
  123. }
  124. }
  125.  
  126. }
  127.  
  128. int parcours_largeur(graphs graphique, char sommet,int vis){
  129.  
  130. char * visit = malloc(sizeof(char)*graphique.degres);
  131. char* voisinage = malloc(sizeof(char)*graphique.degres);
  132. printf("%c",sommet);
  133. //voisinage = ;
  134.  
  135. }
  136.  
  137. int main()
  138. {
  139.  
  140. while(1)
  141. {
  142. int choix = 0;
  143. graphs graphset;
  144. char sommets;
  145. printf("[1]Initialisation du graphe\n");
  146. printf("[2]Determiner les voisins directs d'un sommet\n");
  147. printf("[3]Parcours en profondeur d'un graph oriente \n");
  148. printf("[4]Parcours en profondeur d'un graph oriente (en cours) \n");
  149.  
  150. printf("[5]Quitter le programme\n");
  151. scanf("%d", &choix);
  152.  
  153. switch(choix)
  154. {
  155. case 1:
  156. graphset = initialisation_graphe();
  157. break;
  158. case 2:
  159.  
  160. do
  161. {
  162. printf("\n De quel sommet voulez-vous connaitre les voisin :\n ");
  163. fflush(stdin);
  164. scanf(" %c",&sommets);
  165. }while(isInGraph(graphset,sommets) == 0);
  166.  
  167. voisin_niveau_1(graphset,sommets);
  168. break;
  169. case 3 :
  170. parcours_profondeur(graphset,graphset.sommet[0],0);
  171. printf("\n");
  172. break;
  173. case 4 :
  174. parcours_largeur(graphset,graphset.sommet[0],0);
  175. break;
  176. case 5 :
  177. return 1;
  178. break;
  179.  
  180. }
  181. }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement