Advertisement
Guest User

Zadatak „prinesi.com”¶

a guest
Dec 17th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define MAX_NAZIV 10+1
  6. #define MAX_VRSTA 20+1
  7.  
  8. typedef struct restoran_st {
  9.     char naziv[MAX_NAZIV];
  10.     char vrsta[MAX_VRSTA];
  11.     double ocena;
  12.  
  13.     struct restoran_st *next;
  14. } RESTORAN;
  15.  
  16. void init_list(RESTORAN **head) {
  17.     *head = NULL;
  18. }
  19.  
  20. void add_to_list(RESTORAN *new, RESTORAN **head) {
  21.     if(*head == NULL) { // list is empty
  22.         *head = new;
  23.         return;
  24.     }
  25.  
  26.     add_to_list(new, &((*head)->next));
  27. }
  28.  
  29. RESTORAN *create_new_item(char naziv[], char vrsta[], double ocena) {
  30.     RESTORAN *new = (RESTORAN *)malloc(sizeof(RESTORAN));
  31.     if (new == NULL) {
  32.         printf("Not enough RAM!\n");
  33.         exit(21);
  34.     }
  35.  
  36.     strcpy(new->naziv, naziv);
  37.     strcpy(new->vrsta, vrsta);
  38.     new->ocena = ocena;
  39.  
  40.     new->next = NULL;
  41.  
  42.     return new;
  43. }
  44.  
  45. void read_list_from(FILE *in, RESTORAN **head) {
  46.     char naziv[MAX_NAZIV];
  47.     char vrsta[MAX_VRSTA];
  48.     double ocena;
  49.  
  50.     while(fscanf(in, "%s %s %lf", naziv, vrsta, &ocena) != EOF) {
  51.         RESTORAN *new = create_new_item(naziv, vrsta, ocena);
  52.         add_to_list(new, head);
  53.     }
  54. }
  55.  
  56. void save_item_to(FILE *out, RESTORAN *x) {
  57.     fprintf(
  58.         out, "%3.1f %-10s %s\n",
  59.         x->ocena, x->naziv, x->vrsta
  60.     );
  61. }
  62.  
  63. void save_list_to(FILE *out, RESTORAN *head) {
  64.     if(head != NULL) {
  65.         save_item_to(out, head);
  66.         save_list_to(out, head->next);
  67.     }
  68. }
  69.  
  70. void destroy_list(RESTORAN **head) {
  71.     if(*head != NULL) {
  72.         destroy_list(&((*head)->next));
  73.         free(*head);
  74.         *head = NULL;
  75.     }
  76. }
  77.  
  78. FILE *safe_fopen(char *filename, char *mode, int error_code) {
  79.     FILE *fp = fopen(filename, mode);
  80.     if (fp == NULL) {
  81.         printf("Can't open '%s'!\n", filename);
  82.         exit(error_code);
  83.     }
  84.     return fp;
  85. }
  86.  
  87. RESTORAN *get_najbolji_restoran(RESTORAN *head, char vrsta[]) {
  88.     if (head == NULL) { // list is empty
  89.         return NULL;
  90.     }
  91.  
  92.     RESTORAN *best = NULL;
  93.     while(head != NULL) {
  94.         if (strcmp(head->vrsta, vrsta) == 0) {
  95.             // Gledamo samo restoran koji je odgovarajuce vrste
  96.             if (best == NULL) {
  97.                 // Pre ovoga nije bilo restorana koji su odgovarajuce vrste
  98.                 best = head;
  99.             } else if (head->ocena > best->ocena) {
  100.                 // Nadjen bolji restoran, koji je odgovarajuce vrste
  101.                 best = head;
  102.             }
  103.         }
  104.  
  105.         head = head->next;
  106.     }
  107.  
  108.     return best;
  109. }
  110.  
  111. int main(int arg_num, char *args[]) {
  112.     if (arg_num != 4) {
  113.         printf("USAGE: %s VRSTA IN_FILENAME OUT_FILENAME\n", args[0]);
  114.         exit(11);
  115.     }
  116.  
  117.     char *vrsta = args[1];
  118.     char *in_filename = args[2];
  119.     char *out_filename = args[3];
  120.  
  121.     FILE *in  = safe_fopen(in_filename,  "r", 1);
  122.     FILE *out = safe_fopen(out_filename, "w", 2);
  123.  
  124.     RESTORAN *head;
  125.     init_list(&head);
  126.  
  127.     read_list_from(in, &head);
  128.     save_list_to(out, head);
  129.  
  130.     RESTORAN *best = get_najbolji_restoran(head, vrsta);
  131.     if (best == NULL) {
  132.         fprintf(out, "\nU gradu ne postoji %s restoran!\n", vrsta);
  133.     } else {
  134.         fprintf(
  135.             out, "\nNajbolje ocenjen %s restoran u gradu je:\n",
  136.             best->vrsta
  137.         );
  138.         save_item_to(out, best);
  139.     }
  140.  
  141.     destroy_list(&head);
  142.  
  143.     fclose(in);
  144.     fclose(out);
  145.  
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement