Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef char* string;
- typedef struct node {string car ; struct node * cdr ; } node, *List ;
- //typedef struct noeud {int i ; struct noeud * cdr ; } noeud, *Liste ;
- #define nil NULL
- #define car(node) ((node)->car)
- #define cdr(node) ((node)->cdr)
- /*#define i(noeud) ((noeud)->i)
- #define suiv(noeud) ((noeud)->cdr)*/
- void usage(char *) ;
- void libere ( List L);
- //Liste cons(int i, Liste L) ;
- List cons(string car, List L) ;
- void libere ( List L) ;
- void usage(char * message) ;
- List remplace(string element, string par, List L);
- //Liste remplace (int element, int par, Liste L);
- /*int main ()
- {
- List l = NULL;
- List ltemp = NULL;
- l = cons ("test", l);
- l = cons ("string", l);
- l = cons ("car", l);
- ltemp = l;
- while (ltemp != NULL)
- {
- printf("%s\n", car(ltemp));
- ltemp = cdr (ltemp);
- }
- remplace ("test", "euh", l);
- ltemp = l;
- while (ltemp != NULL)
- {
- printf("%s\n", car(ltemp));
- ltemp = cdr (ltemp);
- }
- return 1;
- }*/
- int main(int argc, char* argv[])
- {
- FILE *in, *out;
- char temp[BUFSIZ + 1];
- in = fopen( argv[3], "r" );
- out= fopen( argv[4], "w" );
- if (in == NULL || out == NULL)
- {
- printf ("erreur d'ouverture\n");
- return 1;
- }
- List L1 = NULL;
- List L2 = NULL;
- List L3 = NULL;
- while (fgets(temp, BUFSIZ, in) != NULL)
- {
- L1 = cons(temp, L1) ;
- printf("%s", temp) ;
- }
- L2 = remplace(argv[1], argv[2], L1);
- L3 = L1;
- while ( L3 != NULL ) {
- fputs(car(L3), out);
- fputc ('\n', out);
- L3 = cdr ( L3 );
- }
- fclose(in);
- fclose(out);
- return 0;
- }
- List cons(string car, List L)
- {
- List new = malloc(sizeof(node)) ;
- if (! new) usage("manque de RAM") ;
- new -> car = malloc (sizeof (char) * (strlen(car) + 1) ) ;
- strcpy (new -> car, car);
- new -> cdr = L;
- return (new);
- }
- List remplace(string element, string par, List L)
- {
- if (strcmp(car( L), element) == 0)
- {
- printf("hihihi\n");
- car( L) = par;
- return (cdr( L)==NULL)?L:remplace(element, par, cdr( L)) ;
- }
- if ( L) {
- return (cdr( L)==NULL)?L:remplace(element, par, cdr( L)) ;
- printf("hahaha\n");
- }
- return NULL ;
- }
- void libere ( List L) {
- List temp;
- while ( L != NULL)
- {
- temp = cdr ( L );
- free ( L );
- L = temp;
- }
- }
- void usage(char * message) { fprintf(stderr, "%s\n", message) ; exit(1) ; }
- /*Liste cons(int i, Liste L)
- {
- Liste new = malloc(sizeof(noeud)) ;
- if (! new) usage("manque de RAM") ;
- new -> i = i ;
- new -> cdr = L;
- return (new);
- }
- Liste remplace (int element, int par, Liste L)
- {
- if ( i(L) == element)
- {
- printf("hihihi\n");
- i( L) = par;
- return (suiv( L)==NULL)?L:remplace(element, par, suiv( L)) ;
- }
- if ( L) {
- return (suiv( L)==NULL)?L:remplace(element, par, suiv( L)) ;
- printf("hahaha\n");
- }
- return NULL ;
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement