-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
executable file
·167 lines (140 loc) · 4.42 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
_________________________________________________
/ _________________________________ \
| == . | Gabriel Alves KUabara | |
| _ | Gabriel Freitas Vasconcelos | B |
| / \ | | A O |
| | O | | N° USP 11275043 | O |
| \_/ | N° USP 11819084 | |
| | | |
| | [email protected] | . . . |
| ::: | [email protected] | . . . |
| ::: |_________________________________| . . . |
| G A K |
\_________________________________________________/
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
#include "site.h"
#include "utils.h"
int count_char (char *string, char searched){
int count = 0;
for (int i = 0; i < strlen(string); i++)
if (string[i] == searched) count++;
return count;
}
SITE* create_site_from_googlebot(FILE* fp){
char* line = readLine(fp);
int nKeywords = 0;
nKeywords = count_char(line, ',')-3;
char* codeChar = strsep(&line, ",");
int code = atoi(codeChar);
char* name = strsep(&line, ",");
int relevance = atoi(strsep(&line, ","));
char* url = strsep(&line, ",");
char** keywords = malloc(nKeywords*sizeof(char*));
for (int i = 0; i < nKeywords; i++){
keywords[i] = strsep(&line, ",");
}
SITE* site = site_create(code, name, relevance, url, keywords, nKeywords);
free(codeChar);
free(keywords);
return site;
}
void operations(LIST *list){
while(TRUE){
printf("Please, type a command: ");
int op;
scanf("%d", &op);
switch(op){
//insert new site
case 1:
printf("Please, write all the site information as CSV: ");
getchar();
SITE *site = create_site_from_googlebot(stdin);
list_insert_site(list, site);
break;
//remove site
case 2:
printf("Type the site code you want to delete: ");
int code2;
scanf("%d", &code2);
list_remove_site(list, code2);
break;
//new key word
case 3:
printf("Type the site code you want to add a keyword: ");
int code3;
scanf("%d", &code3); getchar();
printf("Type the new keyword: ");
char *keyword = readLine(stdin);
list_insert_keyword(list, code3, keyword);
free(keyword); keyword = NULL;
break;
//update relevance
case 4:
printf("Type the site code you want to update relevance: ");
int code4;
scanf("%d", &code4);
printf("Type the new relevance: ");
int relevance;
scanf("%d", &relevance);
list_update_relevance(list, code4, relevance);
break;
case 5:
printf("Type your keyword and we search sites for you ٩(^‿^)۶ : ");
getchar();
char *str5 = readLine(stdin);
search_and_sort_sites_with_keyword(list, str5);
free(str5); str5 = NULL;
break;
case 6:
printf("Type your keyword and just wait for some suggestions ~(‾▿‾)~ : ");
getchar();
char *str6 = readLine(stdin);
sites_suggestions(list, str6);
free(str6); str6 = NULL;
break;
//get out:
case 7:
printf("\nOh no, this is a Good bye? (╥﹏╥)\n"
"Take care of yourself! See you again!!!\n");
return;
default:
printf("Please, can you type a valid operation number? ლ(ಠ益ಠლ)╯\n");
break;
}
}
}
void start_messages(){
printf("These are the possible commands for this program:\n\n");
printf("\t1 - Insert site\n");
printf("\t2 - Remove site\n");
printf("\t3 - Insert new keyword\n");
printf("\t4 - Update site relevance\n");
printf("\t5 - Search for sites with keyword\n");
printf("\t6 - Sites sugestions\n");
printf("\t7 - Exit Program\n\n");
}
int main() {
printf("Hello my friend, welcome to our program! Enjoy the trip! ༼ つ ◕_◕ ༽つ \n\n");
int nSites = 0;
LIST* list = list_create();
//reading all data
FILE *fp = fopen("googlebot.txt", "r");
printf("Reading from googlebot.txt...\n");
while (!feof(fp)){
SITE* site = create_site_from_googlebot(fp);
list_insert_site(list, site);
nSites++;
}
fclose(fp);
printf("Success Reading Input Data! Now, you have a list bro!!!\n\n");
start_messages(); //messages that show the commands
//list_print(list);
operations(list); //function that does all the operations required
list_erase(&list);
return 0;
}