Browse Source

TP8

master
parent
commit
04bb2bec3a
12 changed files with 313 additions and 0 deletions
  1. 4
    0
      TP8/.gitignore
  2. 27
    0
      TP8/Makefile
  3. 35
    0
      TP8/main.c
  4. 78
    0
      TP8/part2.c
  5. 2
    0
      TP8/samples/.gitignore
  6. 2
    0
      TP8/samples/balanced.txt
  7. 2
    0
      TP8/samples/degenerated.txt
  8. 1
    0
      TP8/samples/empty.txt
  9. 1
    0
      TP8/samples/leaf.txt
  10. 2
    0
      TP8/samples/unspecified.txt
  11. 112
    0
      TP8/trees.c
  12. 47
    0
      TP8/trees.h

+ 4
- 0
TP8/.gitignore View File

@@ -0,0 +1,4 @@
1
+*.a
2
+*.o
3
+main
4
+part2

+ 27
- 0
TP8/Makefile View File

@@ -0,0 +1,27 @@
1
+all: main pdfs dots
2
+
3
+main: main.c libtrees.a
4
+	gcc $^ -o $@
5
+
6
+part2: part2.c libtrees.a
7
+	gcc $^ -o $@
8
+
9
+%.o: %.c
10
+	gcc -c $^ -o $@
11
+
12
+lib%.a: %.o
13
+	ar rcs $@ $^
14
+
15
+pdfs: $(patsubst %.txt,%.pdf,$(shell ls samples/*.txt))
16
+dots: $(patsubst %.txt,%.dot,$(shell ls samples/*.txt))
17
+
18
+samples/%.pdf: samples/%.dot
19
+	dot -Tpdf $< -o $@
20
+
21
+samples/%.dot: samples/%.txt part2
22
+	./part2 $<
23
+
24
+.PHONY: clean
25
+
26
+clean:
27
+	rm -r lib*.a *.o samples/*.dot samples/*.pdf

+ 35
- 0
TP8/main.c View File

@@ -0,0 +1,35 @@
1
+/*Source Code From Laure Gonnord*/
2
+
3
+#include <stdio.h>
4
+#include <stdlib.h>
5
+#include <stdbool.h>
6
+#include "trees.h"
7
+
8
+
9
+int main(){
10
+  Tree mt1,mt2,mt3;
11
+  mt1 = mkEmptyTree();
12
+  mt2 = mkEmptyTree();
13
+
14
+  if (isEmpty(mt1)) printf("mt1 empty\n"); 
15
+  mt3 = cons(42,mt1,mt2);
16
+  print_lvr(mt3);
17
+
18
+  printf("\n -- \n");
19
+  FILE* fp = fopen("samples/balanced.txt","r");
20
+  /* FILE* fp = fopen("samples/unspecified.txt","r"); */
21
+  /* FILE* fp = fopen("samples/degenerated.txt","r"); */
22
+
23
+  Tree abr1= mkEmptyTree();
24
+
25
+  load_tree(fp, &abr1);
26
+
27
+  //Only values
28
+  print_lvr(abr1);
29
+  printf("\n -- \n");
30
+  //father->son
31
+  print_rec_edges(abr1);
32
+  printf("\n -- \n");
33
+
34
+  return 0;
35
+}

+ 78
- 0
TP8/part2.c View File

@@ -0,0 +1,78 @@
1
+/*Source Code From Bernard Carré and Laure Gonnord */
2
+
3
+#include <stdio.h>
4
+#include <stdlib.h>
5
+#include <stdbool.h>
6
+#include <string.h>
7
+#include "trees.h"
8
+
9
+#define MAX_STRING 255
10
+
11
+// Main -- Do not modify !
12
+
13
+// iterate the process: load, print (Part 1) and then generate_dot (Part 2) on the n files_names
14
+void handle(int n, char *file_names[]);
15
+
16
+// main - usage: main <file_name_1> <file_name_2> <file_name_3>...
17
+int main (int argc, char *argv[]) {
18
+  if (argc < 2) {
19
+    fprintf(stderr, "usage: main <file_names (.txt files)>\n");
20
+  } else {
21
+    handle(argc-1, &argv[1]); // handle the files
22
+  }
23
+  return 0;
24
+}
25
+
26
+// utility: build a new file name dest from src whose extension is replaced by the parameter ext
27
+// for example : file.txt -> file.dot
28
+void build_name(char *src, char *dest, char *ext);
29
+
30
+// iterate the process: load, print (Part 1) and then generate_dot (Part 2) on the n files_names
31
+void handle(int n, char *file_names[]) {
32
+  Tree t;
33
+  FILE *fp;
34
+  int i;
35
+  for (i=0; i<n; i++) {
36
+    fp=fopen(file_names[i], "r");
37
+    if (fp==NULL) {
38
+      fprintf(stderr, "no such file, or unreachable: %s\n", file_names[i]);
39
+    } else {
40
+      // Part 1
41
+      t=NULL;
42
+      load_tree(fp, &t);
43
+      fclose(fp);
44
+      printf("---------------\n%s:\n", file_names[i]);
45
+      print_lvr(t); // on std output
46
+      printf("\n");
47
+      
48
+      //Part 2
49
+      
50
+      char dot_file_name[MAX_STRING], pdf_file_name[MAX_STRING], command[MAX_STRING];
51
+      
52
+      // new name for he dot file = file_name in which ".txt" is replaced by ".dot"
53
+      build_name(file_names[i], dot_file_name, ".dot");
54
+      fp=fopen(dot_file_name, "w");
55
+      if (fp == NULL) {
56
+	fprintf(stderr, "unreachable: %s\n", dot_file_name);
57
+      } else {
58
+	generate_dot(t,fp); // Call to the printing function 
59
+	printf("dot file %s has been generated\n",dot_file_name);
60
+	fclose(fp);
61
+	// new name for the pdf file = file_name in which ".txt" is replaced by ".pdf"
62
+	build_name(file_names[i], pdf_file_name, ".pdf");
63
+	// transform to .pdf with the dot system command
64
+	sprintf(command, "dot -Tpdf %s -o %s", dot_file_name, pdf_file_name);
65
+	system(command);
66
+	printf("pdf file %s has been generated\n", pdf_file_name);
67
+      }
68
+      
69
+    }
70
+  }
71
+}
72
+
73
+// utility: build a new file name dest from src whose extension is replaced by the parameter ext
74
+// for example : file.txt -> file.dot
75
+void build_name(char *src, char *dest, char *ext) {
76
+  strcpy(dest, src);
77
+  strcpy(&dest[strlen(src)-4], ext);
78
+}

+ 2
- 0
TP8/samples/.gitignore View File

@@ -0,0 +1,2 @@
1
+*.pdf
2
+*.dot

+ 2
- 0
TP8/samples/balanced.txt View File

@@ -0,0 +1,2 @@
1
+8 4 2 1 3 6 5 7 12 10 9 11 14 13 15
2
+

+ 2
- 0
TP8/samples/degenerated.txt View File

@@ -0,0 +1,2 @@
1
+1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
2
+

+ 1
- 0
TP8/samples/empty.txt View File

@@ -0,0 +1 @@
1
+

+ 1
- 0
TP8/samples/leaf.txt View File

@@ -0,0 +1 @@
1
+1

+ 2
- 0
TP8/samples/unspecified.txt View File

@@ -0,0 +1,2 @@
1
+9 4 2 11 3 6 5 15 12 10 8 1 14 13 7
2
+

+ 112
- 0
TP8/trees.c View File

@@ -0,0 +1,112 @@
1
+/*Source Code From Laure Gonnord and Bernard Carré*/
2
+
3
+#include "trees.h"
4
+
5
+// construction of a tree
6
+Tree cons(int val, Tree left, Tree right) {
7
+  PtNode new = (PtNode)malloc(sizeof(Node));
8
+  new->val=val;
9
+  new->left=left;
10
+  new->right=right;
11
+  return new;
12
+}
13
+
14
+// create an empty tree
15
+Tree mkEmptyTree(){
16
+  return NULL;
17
+}
18
+
19
+// t is empty?
20
+bool isEmpty (Tree t) {
21
+  return t==NULL;
22
+}
23
+
24
+// t is a leaf?
25
+bool isLeaf (Tree t) {
26
+  return (!isEmpty(t) && isEmpty(t->left) && isEmpty(t->right));
27
+}
28
+
29
+// add x in a bst wtr its value.
30
+void add(Tree *p_t, int x) {
31
+    if (*p_t == NULL) {
32
+        *p_t = cons(x, NULL, NULL);
33
+    } else if (x <= (*p_t)->val) {
34
+        add(&(*p_t)->left, x);
35
+    } else {
36
+        add(&(*p_t)->right, x);
37
+    }
38
+}
39
+
40
+// build a tree "add"ing values of the file fp
41
+void load_tree(FILE *fp, Tree *p_t) {
42
+    int val;
43
+    while (!feof(fp)) {
44
+        fscanf(fp, "%d ", &val);
45
+        add(p_t, val);
46
+    }
47
+
48
+}
49
+
50
+// print values of t in ascendant order (left-value-right)
51
+void print_lvr (Tree t) {
52
+    if (t != NULL) {
53
+        print_lvr(t->left);
54
+        printf("%d ", t->val);
55
+        print_lvr(t->right);
56
+    }
57
+}
58
+
59
+//Section 1.5
60
+void print_rec_edges(Tree t){
61
+    if (t != NULL) {
62
+        if (t->left != NULL) {
63
+            printf("%d %d\n", t->val, t->left->val);
64
+            print_rec_edges(t->left);
65
+        }
66
+        if (t->right != NULL) {
67
+            printf("%d %d\n", t->val, t->right->val);
68
+            print_rec_edges(t->right);
69
+        }
70
+
71
+    }
72
+}
73
+
74
+//PART II
75
+
76
+// pre: !isEmpty(t) & !isLeaf(t)
77
+// recursively prints arcs of the tree <value,left,right> into the file fp:
78
+// "value -> left;" if exist
79
+// "value -> right;" if exist
80
+// to do...
81
+void recursive_dot(Tree t, FILE *fp) {
82
+    if (t != NULL) {
83
+        if (t->left != NULL) {
84
+            fprintf(fp, "    %d -> %d;\n", t->val, t->left->val);
85
+            recursive_dot(t->left, fp);
86
+        }
87
+        if (t->right != NULL) {
88
+            fprintf(fp, "    %d -> %d;\n", t->val, t->right->val);
89
+            recursive_dot(t->right, fp);
90
+        }
91
+
92
+    }
93
+}
94
+
95
+
96
+// generate a .dot file for the tree
97
+// limits (no arcs) :
98
+//   isEmpty(t) => "empty" digraph
99
+//   isLeaf(t) => only one node
100
+// general case :
101
+//   calls recursive_dot which prints arcs
102
+void generate_dot (Tree t, FILE *fp) {
103
+  fprintf (fp, "digraph G {\n");
104
+  if (!isEmpty(t)) {
105
+    if (isLeaf(t)) {
106
+      fprintf(fp, "\t%d", t->val);
107
+    } else {
108
+      recursive_dot(t,fp);
109
+    }
110
+  }
111
+  fprintf (fp, "}\n");
112
+}

+ 47
- 0
TP8/trees.h View File

@@ -0,0 +1,47 @@
1
+/*
2
+ *  trees.h
3
+ *  
4
+ *
5
+ *  From a code of Bernard Carre
6
+ *
7
+ */
8
+
9
+#include <stdio.h>
10
+#include <stdlib.h>
11
+#include <stdbool.h>
12
+
13
+
14
+typedef struct node {
15
+	int val;
16
+	struct node *left;
17
+	struct node *right;
18
+}Node, *PtNode, *Tree;
19
+
20
+
21
+/*Construct a new tree from a value for the root node, a given left tree and a given right tree*/
22
+Tree cons(int , Tree , Tree );
23
+
24
+// make an empty tree
25
+Tree mkEmptyTree();
26
+
27
+/*Is the given tree empty ?*/
28
+bool isEmpty (Tree);
29
+
30
+/*Is the given tree a leaf ?*/
31
+bool isLeaf (Tree);
32
+
33
+/*Add a given integer in a binary search tree*/
34
+// do not verify the presence of x.
35
+void add(Tree *, int);
36
+
37
+/*Print the values of the tree in ascendant order*/
38
+void print_lvr (Tree);
39
+
40
+/* build a tree adding values of the file */
41
+void load_tree(FILE *, Tree *);
42
+
43
+void print_rec_edges(Tree t);
44
+
45
+/*PART 2*/
46
+
47
+void generate_dot (Tree , FILE *);