Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. typedef struct node {string car ; struct node * cdr ; } node, *list ;
  5. #define nil NULL
  6. #define car(node) ((node)->car)
  7. #define cdr(node) ((node)->cdr)
  8. #define BUFSIZ 100
  9. typedef char* string
  10. void usage(char *)
  11. void libere (list L);
  12.  
  13. int main(int argc, char* argv[])
  14. {
  15.  
  16. FILE *in, *out;
  17. in = fopen( argv[3], "r" );
  18. out= fopen( argv[4], "w" );
  19.  
  20. if (in == NULL || out == NULL)
  21. printf ("erreur d'ouverture")
  22.  
  23. list L1 = NULL;
  24. while (fgets(temp, BUFSIZ, in))
  25. { L1 = cons(temp, L1) ;
  26. printf("%s", temp) ; }
  27. L2 = L1;
  28. list L2 = remplace(argv[1], argv[2], L1);
  29. L3 = L2;
  30. while ( L3 != NULL ) {
  31. fputs(car(L3), out);
  32. fputc ('\n', out)
  33. L3 = cdr ( L3 );
  34. }
  35. fclose(in);
  36. fclose(out);
  37. return 0;
  38. }
  39.  
  40.  
  41. list cons(int car, list L)
  42. {
  43. list new = malloc(sizeof(node)) ;
  44. if (! new) usage("manque de RAM") ;
  45. new -> car = car ;
  46. new -> cdr = L
  47. }
  48.  
  49. list remplace(string element, string par, list L)
  50. {
  51. if (strcmp(car(L), element) == 0)
  52. {
  53. car(L) = par;
  54. return (cdr(L)==NULL)?L:remplace(element, par, cdr(L)) ;
  55. }
  56. if (L)
  57. return (cdr(L)==NULL)?L:remplace(element, par, cdr(L)) ;
  58. return NULL ;
  59. }
  60.  
  61. void libere (list L) {
  62. list temp;
  63. while (L != NULL)
  64. {
  65. temp = cdr ( L );
  66. free ( L );
  67. L = temp;
  68. }
  69. }
  70. void usage(char * message) { fprintf(stderr, "%s\n", message) ; exit(1) ; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement