MartinSRB

prinesi.com

Dec 14th, 2021 (edited)
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define MAX_RESTORAN (10 + 1)
  6. #define MAX_KUHINJA (20 + 1)
  7.  
  8. typedef struct restoran_st{
  9.     char restoran[MAX_RESTORAN];
  10.     char kuhinja[MAX_KUHINJA];
  11.     float ocena;
  12.     struct restoran_st *sl;
  13. }RESTORAN;
  14.  
  15. void list_init(RESTORAN**);
  16. void delete_list(RESTORAN**);
  17. RESTORAN* make_restoran(char*,char*,float);
  18. void add_to_list(RESTORAN**,RESTORAN*);
  19.  
  20. FILE* safe_open(char*,char*);
  21. void load(FILE*,RESTORAN**);
  22. void save(FILE*,RESTORAN*,char*);
  23.  
  24. int main(int argc, char** argv){
  25.     if(argc != 4){
  26.         printf("Nepravilan poziv programa.");
  27.         return EXIT_FAILURE;
  28.     }else{
  29.         RESTORAN* glava;
  30.         FILE* file = safe_open(argv[2],"r");
  31.         list_init(&glava);
  32.         load(file,&glava);
  33.         fclose(file);
  34.         file = safe_open(argv[3],"w");
  35.         save(file,glava,argv[1]);
  36.         fclose(file);
  37.         delete_list(&glava);
  38.         return EXIT_SUCCESS;
  39.     }
  40. }
  41.  
  42. void list_init(RESTORAN** glava){
  43.     *glava = NULL;
  44.     return;
  45. }
  46.  
  47. void delete_list(RESTORAN** glava){
  48.     RESTORAN* t;
  49.     while(*glava != NULL){
  50.         t = *glava;
  51.         *glava = (*glava)->sl;
  52.         free(*glava);
  53.     }
  54.     return;
  55. }
  56.  
  57. RESTORAN* make_restoran(char* restoran,char* kuhinja,float ocena){
  58.     RESTORAN* new = (RESTORAN*)malloc(sizeof(RESTORAN));
  59.     if(new == NULL){
  60.         printf("Nije moguce zauzeti memoriju.");
  61.         exit(EXIT_FAILURE);
  62.     }
  63.     strcpy(new->restoran,restoran);
  64.     strcpy(new->kuhinja,kuhinja);
  65.     new->ocena = ocena;
  66.     new->sl = NULL;
  67.     return new;
  68. }
  69.  
  70. void add_to_list(RESTORAN** glava,RESTORAN* new){
  71.     if(*glava == NULL){
  72.         *glava = new;
  73.     }else{
  74.         RESTORAN* t = *glava;
  75.         while(t->sl != NULL){
  76.             t = t->sl;
  77.         }
  78.         t->sl = new;
  79.     }
  80.     return;
  81. }
  82.  
  83. FILE* safe_open(char* name,char* mode){
  84.     FILE* f = fopen(name,mode);
  85.     if(f == NULL){
  86.         printf("Nije moguce otvoriti fajl %s.",name);
  87.         exit(EXIT_FAILURE);
  88.     }
  89.     return f;
  90. }
  91.  
  92. void load(FILE* f,RESTORAN** glava){
  93.     char restoran[MAX_RESTORAN],
  94.          kuhinja[MAX_KUHINJA];
  95.     float ocena;
  96.     while(fscanf(f,"%s %s %f",
  97.                              restoran,
  98.                              kuhinja,
  99.                              &ocena) != EOF){
  100.         add_to_list(glava,make_restoran(restoran,kuhinja,ocena));
  101.     }
  102.     return;
  103. }
  104.  
  105. void save(FILE* f,RESTORAN* glava,char* param){
  106.     RESTORAN* t   = glava,
  107.             * max = NULL;
  108.     while(t != NULL){
  109.         fprintf(f,"%3.1f %-10s %s\n",
  110.                                   t->ocena,
  111.                                   t->restoran,
  112.                                   t->kuhinja);
  113.         if(strcmp(t->kuhinja,param) == 0){
  114.             if(max == NULL){
  115.                 max = t;
  116.             }else{
  117.                 if(max->ocena < t->ocena){
  118.                     max = t;
  119.                 }
  120.             }
  121.         }
  122.         t = t->sl;
  123.     }
  124.     if(max == NULL){
  125.         fprintf(f,"\nU gradu ne postoji %s restoran.",param);
  126.     }else{
  127.         fprintf(f,"\nNajbolje ocenjen %s restoran u gradu je:\n%3.1f %-10s %s",param,max->ocena,max->restoran,max->kuhinja);
  128.     }
  129.     return;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment