-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.c
149 lines (129 loc) · 4.54 KB
/
stats.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "stats.h"
#include "main.h"
void produce_stats(recipes_book *book, char *file_name, char *recipes_file_name) {
FILE *file = open_file(file_name, "w");
fprintf(file, "Le nombre de lignes dans le fichier d'entrée: %d\n", num_lines(recipes_file_name));
fprintf(file, "Le nombre de mots sans doublons: %d\n", num_unique_words(recipes_file_name));
fprintf(file, "Le nombre de mots avec doublons: %d\n", num_words(recipes_file_name));
fprintf(file, "La lettre la plus fréquente (sans considérer les doublons): %c\n", most_frequent_letter(recipes_file_name));
fprintf(file, "Le nombre de catégories: %d\n", num_categories(book));
fprintf(file, "Le nombre de recettes: %d\n", num_lines(recipes_file_name));
fprintf(file, "La catégorie la plus populaire (la plus fréquente): %s\n", category_with_most_recipes(book));
fprintf(file, "La catégorie qui a le plus grand nombre de recettes: %s\n", category_with_most_recipes(book));
fprintf(file, "La recette la plus longue (en termes de nombre de caractères): %s\n", longest_recipe(book));
close_file(file);
}
int num_lines(char *file_name) {
FILE *file = open_file(file_name, "r");
int count = 0;
char buffer[MAX_LINE_LENGHT];
while (fgets(buffer, MAX_LINE_LENGHT, file) != NULL)
count++;
close_file(file);
return count;
}
int num_unique_words(char *file_name) {
FILE *file = open_file(file_name, "r");
char *words[num_words(file_name)];
int count = 0;
char buffer[MAX_LINE_LENGHT];
while (fgets(buffer, MAX_LINE_LENGHT, file) != NULL) {
char *word = strtok(buffer, " ");
while (word != NULL) {
int found = 0;
for (int i = 0; i < count; i++)
if (strcmp(word, words[i]) == 0) {
found = 1;
break;
}
if (!found)
words[count++] = word;
word = strtok(NULL, " ");
}
}
close_file(file);
return count;
}
int num_words(char *recipes_file_name) {
FILE *file = open_file(recipes_file_name, "r");
int count = 0;
char buffer[MAX_LINE_LENGHT];
while (fgets(buffer, MAX_LINE_LENGHT, file) != NULL) {
char *word = strtok(buffer, " ");
while (word != NULL) {
count++;
word = strtok(NULL, " ");
}
}
close_file(file);
return count;
}
char most_frequent_letter(char *file_name) {
FILE *file = open_file(file_name, "r");
char *words[num_words(file_name)];
unsigned int count = 0;
char buffer[MAX_LINE_LENGHT];
while (fgets(buffer, MAX_LINE_LENGHT, file) != NULL) {
char *word = strtok(buffer, " ");
while (word != NULL) {
int found = 0;
unsigned int i;
for (i = 0; i < count; i++)
if (strcmp(word, words[i]) == 0) {
found = 1;
break;
}
if (!found)
words[count++] = word;
word = strtok(NULL, " ");
}
}
close_file(file);
unsigned int i, j;
int counts[26] = {0};
for (i = 0; i < count; ++i)
for (j = 0; j < (unsigned int) strlen(words[i]); ++j)
if (isalpha(words[i][j]))
counts[tolower(words[i][j]) - 'a']++;
int max = 0;
char letter = 'a';
for (int i = 0; i < 26; i++)
if (counts[i] > max) {
max = counts[i];
letter = 'a' + i;
}
return letter;
}
int num_categories(recipes_book *book) {
return book->num_categories;
}
char *category_with_most_recipes(recipes_book *book) {
char *category_with_most_recipes = NULL;
unsigned int max_num_recipes = 0;
struct category_node *current_category = book->first;
while (current_category != NULL) {
if (current_category->num_recipes > (unsigned int) max_num_recipes) {
category_with_most_recipes = current_category->name;
max_num_recipes = current_category->num_recipes;
}
current_category = current_category->next;
}
return category_with_most_recipes;
}
char *longest_recipe(recipes_book *book) {
char *longest_recipe = NULL;
unsigned int max_recipe_length = 0;
struct category_node *current_category = book->first;
while (current_category != NULL) {
struct recipe_node *current_recipe = current_category->recipes;
while (current_recipe != NULL) {
if ((unsigned int) strlen(current_recipe->name) > max_recipe_length) {
longest_recipe = current_recipe->name;
max_recipe_length = strlen(current_recipe->name);
}
current_recipe = current_recipe->next;
}
current_category = current_category->next;
}
return longest_recipe;
}