Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX_SIZE (30 + 1)
- #define UKRAS_LAMPICE "lampice"
- #define UKRAS_LAMPIONI "lampioni"
- #define UKRAS_JELKA "jelka"
- typedef struct ukras_st{
- char prodavnica[MAX_SIZE];
- char ukras[MAX_SIZE];
- char vrsta[MAX_SIZE];
- double cena;
- struct ukras_st *sl;
- }UKRAS;
- void list_init(UKRAS**);
- void delete_list(UKRAS**);
- UKRAS* make_ukras(char*,char*,char*,double);
- void add_to_list(UKRAS**,UKRAS*);
- UKRAS* find(UKRAS*,char*);
- FILE* safe_open(char*,char*);
- void load(FILE*,UKRAS**);
- void save(FILE*,UKRAS*);
- int main(int argc,char** argv){
- if(argc != 3){
- printf("Nepravilan poziv programa.");
- return EXIT_FAILURE;
- }else{
- UKRAS* glava;
- FILE* file = safe_open(argv[1],"r");
- list_init(&glava);
- load(file,&glava);
- fclose(file);
- file = safe_open(argv[2],"w");
- save(file,glava);
- fclose(file);
- delete_list(&glava);
- return EXIT_SUCCESS;
- }
- }
- void list_init(UKRAS** glava){
- *glava = NULL;
- return;
- }
- void delete_list(UKRAS** glava){
- UKRAS* tmp;
- while(*glava != NULL){
- tmp = *glava;
- *glava = (*glava)->sl;
- free(tmp);
- }
- return;
- }
- UKRAS* make_ukras(char* prodavnica,char* ukras,char* vrsta,double cena){
- UKRAS* new = (UKRAS*)malloc(sizeof(UKRAS));
- if(new == NULL){
- printf("Nije moguce zauzeti memoriju.");
- exit(EXIT_FAILURE);
- }
- strcpy(new->prodavnica,prodavnica);
- strcpy(new->ukras,ukras);
- strcpy(new->vrsta,vrsta);
- new->cena = cena;
- new->sl = NULL;
- return new;
- }
- void add_to_list(UKRAS** glava,UKRAS* new){
- if(*glava == NULL){
- *glava = new;
- }else{
- UKRAS* 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 otviriti fajl %s.",name);
- exit(EXIT_FAILURE);
- }
- return f;
- }
- void load(FILE* f,UKRAS** glava){
- char prodavnica[MAX_SIZE],
- ukras[MAX_SIZE],
- vrsta[MAX_SIZE];
- double cena;
- while(fscanf(f,"%s %s %s %lf",
- prodavnica,
- ukras,
- vrsta,
- &cena) != EOF){
- add_to_list(glava,make_ukras(prodavnica,ukras,vrsta,cena));
- }
- return;
- }
- void save(FILE* f,UKRAS* glava){
- UKRAS* min;
- double sum = 0;
- if((min = find(glava,UKRAS_JELKA)) != NULL){
- fprintf(f,"%.2lf %s %s\n",min->cena,min->ukras,min->prodavnica);
- sum += min->cena;
- }
- if((min = find(glava,UKRAS_LAMPIONI)) != NULL){
- fprintf(f,"%.2lf %s %s\n",min->cena,min->ukras,min->prodavnica);
- sum += min->cena;
- }
- if((min = find(glava,UKRAS_LAMPICE)) != NULL){
- fprintf(f,"%.2lf %s %s\n",min->cena,min->ukras,min->prodavnica);
- sum += min->cena;
- }
- fprintf(f,"\nUkupna cena je: %.2lf",sum);
- return;
- }
- UKRAS* find(UKRAS* glava,char* param){
- UKRAS* min = NULL;
- while(glava != NULL){
- if(strcmp(glava->vrsta,param) == 0){
- if(min == NULL){
- min = glava;
- }else{
- if(min->cena > glava->cena){
- min = glava;
- }
- }
- }
- glava = glava->sl;
- }
- return min;
- }
Add Comment
Please, Sign In to add comment