Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.75 KB | None | 0 0
  1. /*
  2. =============================================================
  3. Name : laib_5_es3.c
  4. Author : BCPTe
  5. Version : 1.0
  6. Copyright : ++NONE++
  7. Description : Laib_5 Exercise 3 - APA 19/20 PoliTO
  8. =============================================================
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <ctype.h>
  15.  
  16. #define MAX 20
  17. #define MAXC 15
  18. #define MAXENTRIES 200
  19. #define MAXFILE 100
  20.  
  21. typedef enum{
  22. r_search, r_code, r_date, r_partenza, r_capolinea, r_stampa, r_fine, r_err
  23. }comando_e;
  24.  
  25. typedef struct{
  26. char code[MAX+1];
  27. char departure[MAX+1];
  28. char arrival[MAX+1];
  29. int date[3];
  30. int datetoint;
  31. int hour_dep[3];
  32. int hour_arr[3];
  33. int delay;
  34. }info_log;
  35.  
  36. void STL(char *string);
  37. int fill_log(FILE *log, info_log **total_log, info_log ****ptr_sorted);
  38. comando_e leggiComando(comando_e *lastcmd);
  39. void selezionaDati(comando_e pick, comando_e lastcmd, info_log ****ptr_sorted, int total_entries);
  40. void order(info_log **ptr_sorted, int total_entries, int ord_type);
  41. void search(info_log **ptr_sorted, int total_entries);
  42. void printfunct(info_log **ptr_sorted, int total_entries);
  43.  
  44. int main(){
  45. int total_entries=0, i, j;
  46. char filename[MAX+1];
  47. comando_e pick, lastcmd=r_search;
  48. FILE *log;
  49. info_log *total_log, ***ptr_sorted;
  50.  
  51. printf("Enter filename: ");
  52. scanf("%s", filename);
  53.  
  54. log=fopen(filename, "r");
  55. if(log==NULL){
  56. /* if file doesn't exist exit with code error 99 */
  57. printf("Error: file doesn't exist!\n");
  58. exit(99);
  59. }
  60.  
  61. total_entries=fill_log(log, &total_log, &ptr_sorted);
  62. do{
  63. printf("\n");
  64. pick=leggiComando(&lastcmd);
  65. selezionaDati(pick, lastcmd, ptr_sorted, total_entries);
  66. }while(pick!=r_fine);
  67.  
  68. fclose(log);
  69.  
  70. /*
  71. // IF U WANT TO VERIFY THAT THE ORDERS TAKE PLACE AT THE SAME TIME, DELETE THE '/ * * /' AND COOHSE fine COMMAND! //
  72. // N.B.: U MUST FIRST WRITE ALL THE ORDER CODES //
  73. for(j=0 ; j<r_capolinea ; j++){
  74. for(i=0 ; i<total_entries ; i++)
  75. fprintf(stdout, "%s %s %s %d/%d/%d %d:%d:%d %d:%d:%d %d\n", ptr_sorted[j][i]->code, ptr_sorted[j][i]->departure, ptr_sorted[j][i]->arrival,
  76. ptr_sorted[j][i]->date[j], ptr_sorted[j][i]->date[1], ptr_sorted[j][i]->date[2], ptr_sorted[j][i]->hour_dep[j], ptr_sorted[j][i]->hour_dep[1], ptr_sorted[j][i]->hour_dep[2],
  77. ptr_sorted[j][i]->hour_arr[j], ptr_sorted[j][i]->hour_arr[1], ptr_sorted[j][i]->hour_arr[2], ptr_sorted[j][i]->delay);
  78. printf("\n\n");
  79. }
  80. */
  81.  
  82. return EXIT_SUCCESS;
  83. }
  84.  
  85. int fill_log(FILE *log, info_log **total_log, info_log ****ptr_sorted){
  86. int i=0, j, lengthmax=0, entries=0;
  87. char line[MAXFILE+1];
  88.  
  89. while(fgets(line, MAXFILE, log)!=NULL){
  90. if(strlen(line)>lengthmax)
  91. lengthmax=strlen(line);
  92. entries++;
  93. }
  94.  
  95. *ptr_sorted=malloc(r_capolinea*sizeof(info_log **));
  96. for(i=0 ; i<r_capolinea ; i++)
  97. (*ptr_sorted)[i]=malloc(entries*sizeof(info_log *));
  98.  
  99. *total_log=malloc(entries*sizeof(info_log));
  100.  
  101. rewind(log);
  102. while(fgets(line, MAXFILE, log)!=NULL){
  103. /* delete newline */
  104. line[strlen(line)-1]='\0';
  105. sscanf(line, "%s %s %s %d/%d/%d %d:%d:%d %d:%d:%d %d", total_log[i]->code, total_log[i]->departure, total_log[i]->arrival,
  106. &total_log[i]->date[0], &total_log[i]->date[1], &total_log[i]->date[2],
  107. &total_log[i]->hour_dep[0], &total_log[i]->hour_dep[1], &total_log[i]->hour_dep[2],
  108. &total_log[i]->hour_arr[0], &total_log[i]->hour_arr[1], &total_log[i]->hour_arr[2], &total_log[i]->delay);
  109. total_log[i]->datetoint=total_log[i]->date[0]+total_log[i]->date[1]*31+total_log[i]->date[2]*365;
  110. for(j=0 ; j<r_capolinea ; j++){
  111. ptr_sorted[j][i]=&total_log[i];
  112. }
  113. i++;
  114. }
  115.  
  116. printf("\nContents of the log file:\n");
  117. printf("-------------------------\n");
  118. for(j=0 ; j<i ; j++)
  119. printf("%s | %s -> %s\t| %d/%d/%d\t%d:%d:%d - %d:%d:%d\t%d\n", total_log[j]->code, total_log[j]->departure, total_log[j]->arrival,
  120. total_log[j]->date[0], total_log[j]->date[1], total_log[j]->date[2], total_log[j]->hour_dep[0], total_log[j]->hour_dep[1], total_log[j]->hour_dep[2],
  121. total_log[j]->hour_arr[0], total_log[j]->hour_arr[1], total_log[j]->hour_arr[2], total_log[j]->delay);
  122. printf("-------------------------\n");
  123.  
  124. return i;
  125. }
  126.  
  127. void STL(char *string){
  128. int i;
  129.  
  130. for(i=0 ; i<strlen(string) ; i++)
  131. string[i]=tolower(string[i]);
  132. }
  133.  
  134. comando_e leggiComando(comando_e *lastcmd){
  135. comando_e cmd;
  136. char line[MAXC+1];
  137. char table[r_err][MAXC+1]={"cerca", "codice", "data", "partenza", "capolinea", "stampa", "fine"};
  138.  
  139. printf("Enter sort/search command (cerca/codice/data/partenza/capolinea/stampa/fine): ");
  140. scanf("%s", line);
  141.  
  142. STL(line);
  143. cmd=r_search;
  144. while(cmd<r_err && strcmp(line, table[cmd])!=0)
  145. cmd++;
  146. if(cmd!=r_search && cmd!=r_stampa && cmd!=r_err) *lastcmd=cmd;
  147. return cmd;
  148. }
  149.  
  150. void selezionaDati(comando_e pick, comando_e lastcmd, info_log ****ptr_sorted, int total_entries){
  151. int ord_type;
  152. switch(pick){
  153. case r_search: search(ptr_sorted[0], total_entries);
  154. break;
  155. case r_code: ord_type=1; order(ptr_sorted[0], total_entries, ord_type); printf("Sort by code done!\n");
  156. break;
  157. case r_date: ord_type=2; order(ptr_sorted[1], total_entries, ord_type); printf("Sort by date done!\n");
  158. break;
  159. case r_partenza: ord_type=3; order(ptr_sorted[2], total_entries, ord_type); printf("Sort by departure done!\n");
  160. break;
  161. case r_capolinea: ord_type=4; order(ptr_sorted[3], total_entries, ord_type); printf("Sort by arrival done!\n");
  162. break;
  163. case r_stampa:
  164. if(lastcmd==r_code || lastcmd==r_search)
  165. printfunct(ptr_sorted[0], total_entries);
  166. else if(lastcmd==r_date)
  167. printfunct(ptr_sorted[1], total_entries);
  168. else if(lastcmd==r_partenza)
  169. printfunct(ptr_sorted[2], total_entries);
  170. else if(lastcmd==r_capolinea)
  171. printfunct(ptr_sorted[3], total_entries);
  172. break;
  173. case r_fine:
  174. printf("\nChiusura del programma in corso...\n");
  175. break;
  176. case r_err:
  177. default:
  178. printf("\nWrong command!\n");
  179. break;
  180. }
  181. }
  182.  
  183. void search(info_log **ptr_sorted, int total_entries){
  184. int i, found=0;
  185. char searched[MAX+1];
  186.  
  187. printf("WARNING: the search is carried out for convenience on the log ordered by code!\n");
  188. printf("Enter departure station to search: ");
  189. scanf("%s", searched);
  190.  
  191. printf("-------------------------\n");
  192.  
  193. /* linear search */
  194. for(i=0 ; i<total_entries ; i++){
  195. if(strstr(ptr_sorted[i]->departure, searched)!=NULL){
  196. found++;
  197. printf("%s | %s -> %s\t| %d/%d/%d\t%d:%d:%d - %d:%d:%d\t%d\n", ptr_sorted[i]->code, ptr_sorted[i]->departure, ptr_sorted[i]->arrival,
  198. ptr_sorted[i]->date[0], ptr_sorted[i]->date[1], ptr_sorted[i]->date[2], ptr_sorted[i]->hour_dep[0], ptr_sorted[i]->hour_dep[1], ptr_sorted[i]->hour_dep[2],
  199. ptr_sorted[i]->hour_arr[0], ptr_sorted[i]->hour_arr[1], ptr_sorted[i]->hour_arr[2], ptr_sorted[i]->delay);
  200. }
  201. }
  202.  
  203. if(found)
  204. printf("\n");
  205. printf("Found %d matches!\n", found);
  206. printf("-------------------------\n");
  207. }
  208.  
  209. void order(info_log *ptr_sorted[MAXENTRIES], int total_entries, int ord_type){
  210. int i, j, r=total_entries-1, flag=1;
  211. info_log *tmp;
  212.  
  213. for(i=0 ; i<r && flag==1 ; i++){
  214. flag=0;
  215. for(j=0 ; j<r-i ; j++){
  216. switch(ord_type){
  217. case 1: /* sort by code */
  218. if(strcmp(ptr_sorted[j]->code, ptr_sorted[j+1]->code)>0){
  219. flag=1;
  220. tmp=ptr_sorted[j];
  221. ptr_sorted[j]=ptr_sorted[j+1];
  222. ptr_sorted[j+1]=tmp;
  223. }
  224. break;
  225. case 2: /* sort by date */
  226. if(ptr_sorted[j]->datetoint>ptr_sorted[j+1]->datetoint){
  227. flag=1;
  228. tmp=ptr_sorted[j];
  229. ptr_sorted[j]=ptr_sorted[j+1];
  230. ptr_sorted[j+1]=tmp;
  231. }
  232. break;
  233. case 3: /* sort by departure */
  234. if(strcmp(ptr_sorted[j]->departure, ptr_sorted[j+1]->departure)>0){
  235. flag=1;
  236. tmp=ptr_sorted[j];
  237. ptr_sorted[j]=ptr_sorted[j+1];
  238. ptr_sorted[j+1]=tmp;
  239. }
  240. break;
  241. case 4: /* sort by arrival */
  242. if(strcmp(ptr_sorted[j]->arrival, ptr_sorted[j+1]->arrival)>0){
  243. flag=1;
  244. tmp=ptr_sorted[j];
  245. ptr_sorted[j]=ptr_sorted[j+1];
  246. ptr_sorted[j+1]=tmp;
  247. }
  248. break;
  249. }
  250. }
  251. }
  252. }
  253.  
  254. void printfunct(info_log *ptr_sorted[MAXENTRIES], int total_entries){
  255. int i;
  256. char command[MAX+1], filenm[MAX+1];
  257. FILE *fp;
  258.  
  259. printf("Enter print destination (stdout, file): ");
  260. scanf("%s", command);
  261. printf("\n");
  262. printf("-------------------------\n");
  263.  
  264. if(strcmp(command, "stdout")==0){ /* print on stdout */
  265. for(i=0 ; i<total_entries ; i++)
  266. printf("%s | %s -> %s\t| %d/%d/%d\t%d:%d:%d - %d:%d:%d\t%d\n", ptr_sorted[i]->code, ptr_sorted[i]->departure, ptr_sorted[i]->arrival,
  267. ptr_sorted[i]->date[0], ptr_sorted[i]->date[1], ptr_sorted[i]->date[2], ptr_sorted[i]->hour_dep[0], ptr_sorted[i]->hour_dep[1], ptr_sorted[i]->hour_dep[2],
  268. ptr_sorted[i]->hour_arr[0], ptr_sorted[i]->hour_arr[1], ptr_sorted[i]->hour_arr[2], ptr_sorted[i]->delay);
  269.  
  270. }
  271. else if(strcmp(command, "file")==0){ /* print on file */
  272. printf("Enter filename: ");
  273. scanf("%s", filenm);
  274. fp=fopen(filenm, "w");
  275. for(i=0 ; i<total_entries ; i++)
  276. fprintf(fp, "%s %s %s %d/%d/%d %d:%d:%d %d:%d:%d %d\n", ptr_sorted[i]->code, ptr_sorted[i]->departure, ptr_sorted[i]->arrival,
  277. ptr_sorted[i]->date[0], ptr_sorted[i]->date[1], ptr_sorted[i]->date[2], ptr_sorted[i]->hour_dep[0], ptr_sorted[i]->hour_dep[1], ptr_sorted[i]->hour_dep[2],
  278. ptr_sorted[i]->hour_arr[0], ptr_sorted[i]->hour_arr[1], ptr_sorted[i]->hour_arr[2], ptr_sorted[i]->delay);
  279. fclose(fp);
  280. }
  281. printf("\nPrint done!\n");
  282. printf("-------------------------\n");
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement