Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX_RESTORAN (10 + 1)
- #define MAX_KUHINJA (20 + 1)
- typedef struct restoran_st{
- char restoran[MAX_RESTORAN];
- char kuhinja[MAX_KUHINJA];
- float ocena;
- struct restoran_st *sl;
- }RESTORAN;
- void list_init(RESTORAN**);
- void delete_list(RESTORAN**);
- RESTORAN* make_restoran(char*,char*,float);
- void add_to_list(RESTORAN**,RESTORAN*);
- FILE* safe_open(char*,char*);
- void load(FILE*,RESTORAN**);
- void save(FILE*,RESTORAN*,char*);
- int main(int argc, char** argv){
- if(argc != 4){
- printf("Nepravilan poziv programa.");
- return EXIT_FAILURE;
- }else{
- RESTORAN* glava;
- FILE* file = safe_open(argv[2],"r");
- list_init(&glava);
- load(file,&glava);
- fclose(file);
- file = safe_open(argv[3],"w");
- save(file,glava,argv[1]);
- fclose(file);
- delete_list(&glava);
- return EXIT_SUCCESS;
- }
- }
- void list_init(RESTORAN** glava){
- *glava = NULL;
- return;
- }
- void delete_list(RESTORAN** glava){
- RESTORAN* t;
- while(*glava != NULL){
- t = *glava;
- *glava = (*glava)->sl;
- free(*glava);
- }
- return;
- }
- RESTORAN* make_restoran(char* restoran,char* kuhinja,float ocena){
- RESTORAN* new = (RESTORAN*)malloc(sizeof(RESTORAN));
- if(new == NULL){
- printf("Nije moguce zauzeti memoriju.");
- exit(EXIT_FAILURE);
- }
- strcpy(new->restoran,restoran);
- strcpy(new->kuhinja,kuhinja);
- new->ocena = ocena;
- new->sl = NULL;
- return new;
- }
- void add_to_list(RESTORAN** glava,RESTORAN* new){
- if(*glava == NULL){
- *glava = new;
- }else{
- RESTORAN* t = *glava;
- while(t->sl != NULL){
- t = t->sl;
- }
- t->sl = new;
- }
- return;
- }
- FILE* safe_open(char* name,char* mode){
- FILE* f = fopen(name,mode);
- if(f == NULL){
- printf("Nije moguce otvoriti fajl %s.",name);
- exit(EXIT_FAILURE);
- }
- return f;
- }
- void load(FILE* f,RESTORAN** glava){
- char restoran[MAX_RESTORAN],
- kuhinja[MAX_KUHINJA];
- float ocena;
- while(fscanf(f,"%s %s %f",
- restoran,
- kuhinja,
- &ocena) != EOF){
- add_to_list(glava,make_restoran(restoran,kuhinja,ocena));
- }
- return;
- }
- void save(FILE* f,RESTORAN* glava,char* param){
- RESTORAN* t = glava,
- * max = NULL;
- while(t != NULL){
- fprintf(f,"%3.1f %-10s %s\n",
- t->ocena,
- t->restoran,
- t->kuhinja);
- if(strcmp(t->kuhinja,param) == 0){
- if(max == NULL){
- max = t;
- }else{
- if(max->ocena < t->ocena){
- max = t;
- }
- }
- }
- t = t->sl;
- }
- if(max == NULL){
- fprintf(f,"\nU gradu ne postoji %s restoran.",param);
- }else{
- fprintf(f,"\nNajbolje ocenjen %s restoran u gradu je:\n%3.1f %-10s %s",param,max->ocena,max->restoran,max->kuhinja);
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment