Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef char* string;
  6. typedef struct node {string car ; struct node * cdr ; } node, *List ;
  7.  
  8. //typedef struct noeud {int i ; struct noeud * cdr ; } noeud, *Liste ;
  9.  
  10.  
  11.  
  12. #define nil NULL
  13. #define car(node) ((node)->car)
  14. #define cdr(node) ((node)->cdr)
  15.  
  16.  
  17. /*#define i(noeud) ((noeud)->i)
  18. #define suiv(noeud) ((noeud)->cdr)*/
  19.  
  20. void usage(char *) ;
  21. void libere ( List L);
  22.  
  23. //Liste cons(int i, Liste L) ;
  24. List cons(string car, List L) ;
  25. void libere ( List L) ;
  26. void usage(char * message) ;
  27. List remplace(string element, string par, List L);
  28. //Liste remplace (int element, int par, Liste L);
  29.  
  30. /*int main ()
  31. {
  32. List l = NULL;
  33. List ltemp = NULL;
  34.  
  35. l = cons ("test", l);
  36. l = cons ("string", l);
  37. l = cons ("car", l);
  38.  
  39. ltemp = l;
  40. while (ltemp != NULL)
  41. {
  42. printf("%s\n", car(ltemp));
  43. ltemp = cdr (ltemp);
  44.  
  45. }
  46.  
  47. remplace ("test", "euh", l);
  48.  
  49. ltemp = l;
  50. while (ltemp != NULL)
  51. {
  52. printf("%s\n", car(ltemp));
  53. ltemp = cdr (ltemp);
  54.  
  55. }
  56.  
  57.  
  58.  
  59. return 1;
  60. }*/
  61.  
  62.  
  63.  
  64. int main(int argc, char* argv[])
  65. {
  66.  
  67. FILE *in, *out;
  68. char temp[BUFSIZ + 1];
  69. in = fopen( argv[3], "r" );
  70. out= fopen( argv[4], "w" );
  71.  
  72. if (in == NULL || out == NULL)
  73. {
  74. printf ("erreur d'ouverture\n");
  75. return 1;
  76. }
  77.  
  78. List L1 = NULL;
  79. List L2 = NULL;
  80. List L3 = NULL;
  81. while (fgets(temp, BUFSIZ, in) != NULL)
  82. {
  83. L1 = cons(temp, L1) ;
  84. printf("%s", temp) ;
  85. }
  86.  
  87. L2 = remplace(argv[1], argv[2], L1);
  88. L3 = L1;
  89. while ( L3 != NULL ) {
  90. fputs(car(L3), out);
  91. fputc ('\n', out);
  92. L3 = cdr ( L3 );
  93. }
  94. fclose(in);
  95. fclose(out);
  96. return 0;
  97. }
  98.  
  99.  
  100. List cons(string car, List L)
  101. {
  102. List new = malloc(sizeof(node)) ;
  103. if (! new) usage("manque de RAM") ;
  104. new -> car = malloc (sizeof (char) * (strlen(car) + 1) ) ;
  105. strcpy (new -> car, car);
  106. new -> cdr = L;
  107. return (new);
  108. }
  109.  
  110. List remplace(string element, string par, List L)
  111. {
  112. if (strcmp(car( L), element) == 0)
  113. {
  114. printf("hihihi\n");
  115. car( L) = par;
  116. return (cdr( L)==NULL)?L:remplace(element, par, cdr( L)) ;
  117. }
  118. if ( L) {
  119. return (cdr( L)==NULL)?L:remplace(element, par, cdr( L)) ;
  120. printf("hahaha\n");
  121. }
  122. return NULL ;
  123. }
  124.  
  125. void libere ( List L) {
  126. List temp;
  127. while ( L != NULL)
  128. {
  129. temp = cdr ( L );
  130. free ( L );
  131. L = temp;
  132. }
  133. }
  134. void usage(char * message) { fprintf(stderr, "%s\n", message) ; exit(1) ; }
  135.  
  136. /*Liste cons(int i, Liste L)
  137. {
  138. Liste new = malloc(sizeof(noeud)) ;
  139. if (! new) usage("manque de RAM") ;
  140. new -> i = i ;
  141. new -> cdr = L;
  142. return (new);
  143. }
  144.  
  145.  
  146. Liste remplace (int element, int par, Liste L)
  147. {
  148. if ( i(L) == element)
  149. {
  150. printf("hihihi\n");
  151. i( L) = par;
  152. return (suiv( L)==NULL)?L:remplace(element, par, suiv( L)) ;
  153. }
  154. if ( L) {
  155. return (suiv( L)==NULL)?L:remplace(element, par, suiv( L)) ;
  156. printf("hahaha\n");
  157. }
  158. return NULL ;
  159. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement