/* Vettore con contatore - Lista + Albero */
/******************************************************************************/
/* TipobaseABR */
/******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#define C 10
#define FLUSH while(getchar()!='\n')
/* materia */
typedef struct {
char materia[C];
char codice[C];
char professore[C];
}tipobaseABR;
void LeggiStringa(char s[], unsigned long dim) {
unsigned long i;
for(i=0;(s[i]=getchar())!='\n' && i<dim-1;i++);
s[i]='\0';
if(i==dim-1) FLUSH;
}
int ConfrontaABR(tipobaseABR a, tipobaseABR b) {
return strcmp(a.codice,b.codice);
}
void LeggiElementoABR(tipobaseABR * x) {
printf("\nInserisci nome materia:");
LeggiStringa(x->materia,C);
printf("\nInserisci codice materia:");
LeggiStringa(x->codice,C);
printf("\nInserisci nome del professore:");
LeggiStringa(x->professore,C);
}
void CercaElementoABR(tipobaseABR * x) {
printf("\nInserisci nome materia:");
LeggiStringa(x->materia,C);
printf("\nInserisci codice materia:");
LeggiStringa(x->codice,C);
}
void VisualizzaElementoABR(tipobaseABR x) {
printf("\nNome materia:%s",x.materia);
printf("\nCodice:%s",x.codice);
printf("\nNome professore:%s",x.professore);
}
/******************************************************************************/
/* Abr */
/******************************************************************************/
#define ALBEROVUOTO NULL
typedef short boolean;
typedef struct nodoABR {
tipobaseABR info;
struct nodoABR *leftchild,*rightchild;
}*abr;
void MakeNullABR(abr * n) {
*n=ALBEROVUOTO;
}
boolean EmptyABR(abr n) {
return(n==ALBEROVUOTO);
}
boolean FullABR(abr n) {
return 0;
}
abr LeftChild(abr n) {
if(!EmptyABR(n))
return(n->leftchild);
}
abr RightChild(abr n) {
if(!EmptyABR(n))
return(n->rightchild);
}
tipobaseABR Label(abr n) {
if(!EmptyABR(n))
return(n->info);
}
boolean Member(abr n,tipobaseABR x) {
if(EmptyABR(n)) return 0;
if(!ConfrontaABR(n->info,x)) return 1;
else if(ConfrontaABR(n->info,x) > 0) return Member(n->leftchild,x);
else return Member(n->rightchild,x);
}
void InsertABR(abr * n , tipobaseABR x) {
if(EmptyABR(*n)) {
(*n)=(struct nodoABR *)malloc(sizeof(struct nodoABR));
(*n)->info=x;
(*n)->leftchild=(*n)->rightchild=ALBEROVUOTO;
}
else if(ConfrontaABR((*n)->info,x) >0 ) InsertABR(&(*n)->leftchild,x);
else if(ConfrontaABR((*n)->info,x) <0 ) InsertABR(&(*n)->rightchild,x);
}
/******************************************************************************/
/* tipobaseList */
/******************************************************************************/
/*Studente*/
#define V 24
typedef struct {
char nome[C],cognome[C];
char via[V];
abr materia;
}tipobaseList;
int ConfrontaList(tipobaseList a , tipobaseList b) {
if(!strcmp(a.cognome,b.cognome)) return strcmp(a.nome,b.nome);
else return strcmp(a.cognome,b.cognome);
}
void AggiornaABR(tipobaseList *x,abr n ){
x->materia=n;
}
void CopiaABR(abr *n,tipobaseList x) {
*n=x.materia;
}
void LeggiElementoList(tipobaseList * x) {
FLUSH;
printf("\nInserisci nome studente:");
LeggiStringa(x->nome,C);
printf("\nInserisci cognome:");
LeggiStringa(x->cognome,C);
printf("\nInserisci via:");
LeggiStringa(x->via,V);
}
void CercaElementoList(tipobaseList *x) {
FLUSH;
printf("\nInserisci nome studente:");
LeggiStringa(x->nome,C);
printf("\nInserisci cognome:");
LeggiStringa(x->cognome,C);
}
void VisualizzaElementoList(tipobaseList x) {
FLUSH;
printf("\nNome Studente:%s",x.nome);
printf("\nCognome:%s",x.cognome);
printf("\nVia:%s",x.via);
}
/******************************************************************************/
/* List */
/******************************************************************************/
#define LISTAVUOTA NULL
typedef struct nodoList {
tipobaseList info;
struct nodoList * next;
}*list;
typedef list position;
position End(list l ) {
if(l==LISTAVUOTA) return (l);
while(l->next!=LISTAVUOTA){
l=l->next;
return(l);
}
}
position First(list l) {
return(LISTAVUOTA);
}
void MakeNullList(list *l) {
*l=LISTAVUOTA;
}
boolean EmptyList(list l) {
return(l==LISTAVUOTA);
}
boolean FullList(list l) {
return 0;
}
void InsertList(list *l, position p,tipobaseList x) {
struct nodoList *tmp;
if(!FullList(*l)) {
tmp=(struct nodoList *)malloc(sizeof(struct nodoList));
tmp->info=x;
if(p==LISTAVUOTA) {
tmp->next=*l;
*l=tmp;
}else{
tmp->next=p->next;
p->next=tmp;
}
}
}
void DeleteList(list * l, position p) {
struct nodoList * tmp;
if(!EmptyList(*l))
if(p==LISTAVUOTA) {
tmp=(*l)->next;
free(*l);
*l=tmp;
}else{
tmp=p->next;
p->next=tmp->next;
free(tmp);
}
}
position Locate(list l , tipobaseList x) {
if(!EmptyList(l)) {
if(!ConfrontaList(l->info,x)) return(LISTAVUOTA);
while(l->next!=LISTAVUOTA) {
if(!ConfrontaList(l->next->info,x)) return (l);
l=l->next;
}
return(l);
}
}
tipobaseList Retrieve(list l , position p) {
if(!EmptyList(l))
if(p==LISTAVUOTA) return(l->info);
else return(p->next->info);
}
position Next(list l , position p) {
if(p==LISTAVUOTA) return(l);
else return(p->next);
}
/******************************************************************************/
/* MAIN */
/******************************************************************************/
struct vettore {
unsigned int contatore;
list studenti;
};
void AllocaVettore(struct vettore ** , unsigned int );
void MakeNullVett(struct vettore *,unsigned int );
void Ins_studente(struct vettore *,tipobaseList ,unsigned int );
void Ins_materia(struct vettore * , tipobaseList ,unsigned int );
void Vis_studente(struct vettore *,tipobaseList , unsigned int );
main() {
tipobaseList studente;
unsigned int scelta,index=0,n=0;
struct nodoVettore *archivio;
do{
printf("\nMenu'");
printf("\n1-Alloca il vettore");
printf("\n2-Inserisci uno studente");
printf("\n3-Inserisci una materia");
printf("\n4-Visualizza studente e materie");
printf("\n5-Esci");
printf("\n-Scelta-->");
scanf("%u",&scelta);
switch(scelta) {
case 1:
printf("\nInserisci grandezza vettore : ");
scanf("%u",&n);
AllocaVettore(&archivio,n);
MakeNullVett(archivio,n);
printf("\nVettore di %u Allocato\n",n);
break;
case 2:
LeggiElementoList(&studente);
index=0;
Ins_studente(archivio,studente,index);
break;
case 3:
CercaElementoList(&studente);
index=0;
Ins_materia(archivio,studente,n);
break;
case 4:
CercaElementoList(&studente);
index=0;
Vis_studente(archivio,studente,n);
break;
case 5:
break;
default: printf("\nScelta non valida");
break;
}
}
while(scelta!=5);
}
void AllocaVettore(struct vettore **v,unsigned int n) {
*v=(struct vettore *)malloc(sizeof (struct vettore));
}
void MakeNullVett(struct vettore *v,unsigned int dim){
short i;
for(i=0;i<dim;i++){
v[i].contatore=0;
MakeNullList(&(v[i].studenti));}}
void Insord(list * l , tipobaseList x) {
position p,u;
tipobaseList tmp;
if(EmptyList(*l)) InsertList(l,First(*l),x);
else {
p=First(*l);
u=End(*l);
while(p!=u) {
tmp=Retrieve(*l,p);
if(ConfrontaList(tmp,x) >0 ) p=Next(*l,p);
}
InsertList(l,p,x);
}
}
void Ins_studente(struct vettore *v,tipobaseList x,unsigned int n){
position pos;
abr q;
short i;
for(i=0;i<n;i++) {
if(!EmptyList(v[i].studenti)) pos=Locate(v[i].studenti,x);
if(!EmptyList(v[i].studenti) && pos!=End(v[i].studenti));
else {
MakeNullABR(&q);
AggiornaABR(&x,q);
Insord(&(v[i].studenti),x);
(v[i].contatore)++ ;
printf("\n Studente inserito\n");}
}
}
void Ins_materia(struct vettore *v , tipobaseList x,unsigned int u) {
unsigned long i;
position pos;
abr n;
tipobaseABR materia;
for(i=0;i<u;i++) {
if(!EmptyList(v[i].studenti)) pos=Locate(v[i].studenti,x);
if(EmptyList(v[i].studenti) || pos==End(v[i].studenti) ) printf("\nStudente non trovato");
else {
x=Retrieve(v[i].studenti,pos);
LeggiElementoABR(&materia);
CopiaABR(&n,x);
MakeNullABR(&n);
InsertABR(&n,materia);
AggiornaABR(&x,n);
DeleteList(&(v[i].studenti),pos);
InsertList(&(v[i].studenti),pos,x);
}
}
}
void CercaElemento( abr a , tipobaseABR x ) { /*Cerca l'elemento nell'albero se lo trova lo visualizza*/
tipobaseABR tmp;
if(EmptyABR(a)) printf("\nElemento non trovato");
else {
tmp=Label(a);
if(!ConfrontaABR(tmp,x)) VisualizzaElementoABR(tmp);
else if(ConfrontaABR(tmp,x) > 0) CercaElemento(LeftChild(a),x);
else CercaElemento(RightChild(a),x);
}
}
void Vis_studente(struct vettore *v,tipobaseList x, unsigned int n){
position pos;
abr a;
tipobaseList tmp;
tipobaseABR materia;
unsigned int i;
for(i=0;i<n;i++){
if(EmptyList(v[i].studenti)) printf("\nStudente non trovato");
else{
pos=Locate(v[i].studenti,x);
if(pos!=End(v[i].studenti)){
x=Retrieve((v[i].studenti),pos);
VisualizzaElementoList(x);
CopiaABR(&a,x);
if(EmptyABR(a)) printf("\nNon ci sono materie registrate");
CercaElementoABR(&materia);
CercaElemento(a,materia);
}
}
}
}