-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
117 lines (98 loc) · 2.86 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* First, the standard lib includes, alphabetically ordered */
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "util.h"
#include "abb.h" /* TAD abb */
void print_help(char *program_name) {
/* Print the usage help of this program. */
printf("Usage: %s <input file path>\n\n",
program_name);
}
char *parse_filepath(int argc, char *argv[]) {
/* Parse the filepath given by command line argument. */
char *result = NULL;
if (argc < 2) {
print_help(argv[0]);
exit(EXIT_FAILURE);
}
result = argv[1];
return (result);
}
abb abb_from_file(const char *filepath) {
FILE *file = NULL;
abb read_tree;
read_tree = abb_empty();
file = fopen(filepath, "r");
if (file == NULL) {
fprintf(stderr, "File does not exist.\n");
exit(EXIT_FAILURE);
}
unsigned int i = 0u;
unsigned int size = 0u;
int res = 0;
res = fscanf(file, " %u ", &size);
if (res != 1) {
fprintf(stderr, "Invalid format.\n");
exit(EXIT_FAILURE);
}
while (i < size) {
abb_elem elem;
res = fscanf(file," %d ", &(elem));
if (res != 1) {
fprintf(stderr, "Invalid array.\n");
exit(EXIT_FAILURE);
}
read_tree = abb_add(read_tree, elem);
++i;
}
fclose(file);
return read_tree;
}
int main(int argc, char *argv[]) {
char *filepath = NULL;
/* parse the filepath given in command line arguments */
filepath = parse_filepath(argc, argv);
/* parse the file to obtain an abb with the elements */
abb tree = abb_from_file(filepath);
/*dumping the tree*/
printAbb(tree);
printf("\n");
printf("raiz: %d\nminimo: %d\nmaximo: %d\nlength: %d\n", abb_root(tree),
abb_min(tree),
abb_max(tree),
abb_length(tree));
int opcion;
printf("1)ADD | 2)Remove | 3)Salir:\n");
scanf("%d", &opcion);
while(opcion != 3) {
if (opcion == 1) {
int add;
printf("Que elemento agregas?: ");
scanf("%d", &add);
tree = abb_add(tree, add);
} else if (opcion == 2) {
int del;
printf("Que elemento removes?: ");
scanf("%d", &del);
tree = abb_remove(tree, del);
}
printAbb(tree);
printf("\n");
printf("raiz: %d\nminimo: %d\nmaximo: %d\nlength: %d\n", abb_root(tree),
abb_min(tree),
abb_max(tree),
abb_length(tree));
printf("\n1)ADD | 2)Remove | 3)Salir:\n");
scanf("%d", &opcion);
}
printf("Arbol final: \n");
printAbb(tree);
printf("\n");
printf("raiz: %d\nminimo: %d\nmaximo: %d\nlength: %d\n", abb_root(tree),
abb_min(tree),
abb_max(tree),
abb_length(tree));
tree = abb_destroy(tree);
return (EXIT_SUCCESS);
}