Non trascrivo il main che tanto funge soltanto da client per i due adt.
in arrivo:
Gruppo.h
#ifndef GRUPPO_H_INCLUDED
#define GRUPPO_H_INCLUDED
#include "Catalogo.h"
typedef struct group_wrapper *PTG;
typedef struct group_member *PMG;
typedef struct list_member *PME;
PTG group_wrapper_init();
PMG group_member_init();
PME list_member_init();
PTG read_group(char* fng, PTC swcg);
PMG search_list(char* sln, PTG swglf);
void mod_list(PTG swgm, PTC swcm);
void add_list(PTG swga, PTC swca);
void show_lists(PTG swgs, PTC swcs);
void show_list_total(PTG swgst, PTC swcst);
float list_total(PMG sgmlt, PTC swclt);
#endif // GRUPPO_H_INCLUDED
Gruppo.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Gruppo.h"
#include "Catalogo.h"
#define MAX 30
struct list_member
{
int index;
int qnt;
PME next_list_member;
};
struct group_member
{
char name_list[MAX];
PME head_list;
PME tail_list;
int size_list;
PMG next_group_member;
};
struct group_wrapper
{
PMG head_group;
PMG tail_group;
int size_group;
};
PTG group_wrapper_init()
{
PTG wrapp;
wrapp=(PTG)malloc(sizeof*wrapp);<-----------SEGMENTATIO FAULT HERE!!!
return wrapp;
}
PMG group_member_init()
{
PMG gmmb;
gmmb=(PMG)malloc(sizeof*gmmb);
return gmmb;
}
PME list_member_init()
{
PME lmmb;
lmmb=(PME)malloc(sizeof*lmmb);
return lmmb;
}
PTG read_group(char* fng, PTC swcg)
{
FILE* fp;
PTG swgr;
PMG sgm;
PME slm;
char temp[MAX];
int i, j;
swgr=group_wrapper_init();
swgr->head_group=group_member_init();
sgm=swgr->head_group;
fp=fopen(fng,"r");
fscanf(fp,"%d", &swgr->size_group);
for(i=0; i<swgr->size_group; i++)
{
sgm->head_list=list_member_init();
slm=sgm->head_list;
fscanf(fp,"%d", &sgm->size_list);
fscanf(fp,"%s", sgm->name_list);
for(j=0; j<sgm->size_list; j++)
{
fscanf(fp,"%s %d", temp, &slm->qnt);
slm->index=get_index(temp, swcg);
slm->next_list_member=NULL;
if(j==(sgm->size_list-1)) continue;
slm->next_list_member=list_member_init();
slm=slm->next_list_member;
}
sgm->tail_list=slm;
sgm->next_group_member=NULL;
if(i==(swgr->size_group-1)) continue;
sgm->next_group_member=group_member_init();
sgm=sgm->next_group_member;
}
swgr->tail_group=sgm;
return swgr;
}
PMG search_list(char* sln, PTG swglf)
{
int i;
PMG sgmlf=swglf->head_group;
for(i=0; i<swglf->size_group; i++)
{
if (strcmp(sln, sgmlf->name_list)==0)
return sgmlf;
sgmlf=sgmlf->next_group_member;
}
return NULL;
}
void mod_list(PTG swgm, PTC swcm)
{
char temp[MAX];
PMG sgms;
PME slms;
printf("Digita il nome della lista da modificare: ");
gets(temp);
sgms=search_list(temp, swgm);
while(sgms==NULL)
{
printf("Lista non trovata, ridigita: ");
gets(temp);
sgms=search_list(temp, swgm);
}
slms=sgms->tail_list;
slms->next_list_member=list_member_init();
slms=slms->next_list_member;
printf("Digita il nome seguito dalla quantita' dei prodotti che vuoi aggiungere, quindi digita FINE per terminare");
while(scanf("%s %d", temp, &slms->qnt)==2)
{
slms->index=get_index(temp, swcm);
if(slms->index==-1)
{
printf("Prodotto non presente nel catalogo, ridigita:");
continue;
}
sgms->tail_list=slms;
slms->next_list_member=list_member_init();
slms=slms->next_list_member;
sgms->size_list++;
}
sgms->tail_list->next_list_member=NULL;
}
void add_list(PTG swga, PTC swca)
{
char temp[MAX];
PMG smga;
PME smla;
swga->tail_group->next_group_member=group_member_init();
smga=swga->tail_group->next_group_member;
swga->tail_group=smga;
smga->next_group_member=NULL;
printf("Digita il nome della nuova lista");
gets(smga->name_list);
smga->head_list=list_member_init();
printf("Digita il nome seguito dalla quantita' dei prodotti che vuoi aggiungere, quindi digita FINE per terminare");
while(scanf("%s %d", temp, &smla->qnt)==2)
{
smla->index=get_index(temp, swca);
if(smla->index==-1)
{
printf("Prodotto non presente nel catalogo, ridigita:");
continue;
}
smga->tail_list=smla;
smla->next_list_member=list_member_init();
smla=smla->next_list_member;
smga->size_list++;
}
smga->tail_list->next_list_member=NULL;
swga->size_group++;
}
void show_lists(PTG swgs, PTC swcs)
{
int i=0;
float tot=0;
PMG smgh;
smgh=swgs->head_group;
for(i=0; i<swgs->size_group; i++)
{
printf("\n\nNome lista: %s", smgh->name_list);
tot+=list_total(smgh, swcs);
smgh=smgh->next_group_member;
}
printf("Totale costo gruppo d'acquisto: %f", tot);
}
void show_list_total(PTG swgst, PTC swcst)
{
char temp[MAX];
printf("Digita il nome della lista di cui vuoi visualizzare il prezzo: ");
gets(temp);
printf("Il totale della lista e': %lf", list_total(search_list(temp,swgst), swcst));
}
float list_total(PMG sgmlt, PTC swclt)
{
int j=0;
float subtot=0;
PME smlh;
smlh=sgmlt->head_list;
for(j=0; j<sgmlt->size_list; j++)
{
printf("\n%s %d", retrieve_name(smlh->index, swclt), smlh->qnt);
subtot+=retrieve_price(smlh->index, smlh->qnt, swclt);
smlh=smlh->next_list_member;
}
return subtot;
}
Catalogo.h
#ifndef CATALOGO_H_INCLUDED
#define CATALOGO_H_INCLUDED
typedef struct wrapper_catalog *PTC;
typedef struct catalog_member *PML;
typedef PML *PVL;
PTC wrapper_catalog_init();
PVL catalog_vect_init(int n);
PTC read_catalog(char* fnc);
int get_index(char* item_name, PTC swci);
char* retrieve_name(int sin, PTC swcn);
float retrieve_price(int sip, int sqnt, PTC swcp);
#endif // CATALOGO_H_INCLUDED
Catalogo.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Catalogo.h"
#define MAX 30
struct catalog_member
{
char name[MAX];
float price;
};
struct wrapper_catalog
{
PVL catalog_vect; //DOPPIO PUNTATORE
int size_catalog;
};
PTC wrapper_catalog_init()
{
PTC cwrapp;
cwrapp=(PTC)malloc(sizeof*cwrapp);
return cwrapp;
}
PVL catalog_vect_init(int n)
{
int i=0;
PVL cmmb;
cmmb=(PVL)malloc(n*sizeof**cmmb);
for(i=0; i<n; i++)
{
cmmb[i]=(PML)malloc(sizeof*cmmb);
}
return cmmb;
}
PTC read_catalog(char* fnc)
{
float temporal;
FILE* fp;
PTC swcr;
PVL svp;
int i;
swcr=wrapper_catalog_init();
fp=fopen(fnc,"r");
fscanf(fp,"%d", &swcr->size_catalog);
swcr->catalog_vect=catalog_vect_init(swcr->size_catalog);
svp=swcr->catalog_vect;
for(i=0; i<swcr->size_catalog; i++)
{
fscanf(fp,"%s %f", svp[i]->name, &temporal);
svp[i]->price=temporal;
}
fclose(fp);
return swcr;
}
int get_index(char* item_name, PTC swci)
{
int i=0;
for(i=0; i<swci->size_catalog; i++)
{
if(strcmp(item_name,swci->catalog_vect[i]->name)==0)
return i;
}
return -1;
}
char *retrieve_name(int sin, PTC swcn)
{
return swcn->catalog_vect[sin]->name;
}
float retrieve_price(int sip, int sqnt, PTC swcp)
{
float pht;
pht=sqnt*(swcp->catalog_vect[sip]->price);
return pht;
}