Soluzione 15 Settembre 2010
Main15Sett2010.c
—
C source code,
3 kB (3635 bytes)
Contenuto del file
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 20
typedef struct impianti_type{
int codice;
char nomecapo[N];
int budget;
}impianti;
typedef struct offerte_type{
int codice;
char nomecliente[N];
int offertaint;
}offerte;
//il codice impianto, il nome del progettista, il budget, il nome dell'offerente e la sua offerta
typedef struct value_tree{
int codice;
char nomecapo[N];
int budget;
char nomecliente[N];
int offerta;
}element;
typedef struct tree_type{
element value;
struct tree_type* left;
struct tree_type* right;
}item;
typedef item* tree;
//Funzioni primitive per il tipo di dato tree
int emptyTree(){
return NULL;
}
int empty(tree t){
if(t==NULL)
return 1;
else return 0;
}
element root(tree t){
return t->value;
}
tree left(tree t){
return t->left;
}
tree right(tree t){
return t->right;
}
tree cons(element e, tree left, tree right){
tree aux; aux=(tree)malloc(sizeof(item));
aux->value=e;
aux->left=left; aux->right=right;
return aux;
}
tree ins_ord(element e,tree t){
if(empty(t))
return cons(e,NULL,NULL);
else{
if( e.codice < root(t).codice)
t->left = ins_ord(e,left(t));
else
t->right = ins_ord(e,right(t));
return t;
}
}
//Ordinare tale vettore sul campo codice impianto e stampare a video il contenuto di V.
int ordinamento(impianti A,impianti B){
if(A.codice > B.codice)
return 1;
else{
if(A.codice < B.codice)
return -1;
else return 0;
}
}
int caricamento(impianti V[],FILE* p){
int maxarray=0;
int i=0;
maxarray = fread(V,sizeof(impianti),30,p);
qsort(V,maxarray,sizeof(impianti),ordinamento);
fclose(p);
return maxarray;
}
int caricamentoTree(impianti V[],int maxarray,FILE* o,tree T){
int i,trovato;
element aux;
offerte let; //*let = &let2;
//lettura del file, un dato alla volta
while( (fread(&let,sizeof(offerte),1,o)) > 0){
//ricerca all'interno dell'array V dell'offerta
trovato = 0; i=0;
while( (i<maxarray)&& (!trovato)){
if( V[i].codice ==let.codice )
//se l'offerta � minore del budget allora inserisci dentro l'albero tutti i dati;
//questo presuppone che ci siano diverse offerte > budget ma solo una che sta sotto il budget
//(spero di aver capito bene)
if( let.offertaint < V[i].budget ){
trovato=1;
aux.budget=V[i].budget; aux.codice=V[i].codice;
strcpy(aux.nomecapo,V[i].nomecapo);
strcpy(aux.nomecliente,let.nomecliente);
aux.offerta=let.offertaint;
T=ins_ord(aux,T);
}
i++;
}
}
fclose(o);
return T;
}
int ScriviOutput(tree T,FILE* p){
if(empty(T))
return 0;
else{
ScriviOutput(T->left,p);
fprintf(p,"\nCodice: %d\t Nomecapo: %s\t Budget: %d\t Nome offerente: %s\t Offerta: %d\n",
root(T).codice,root(T).nomecapo,root(T).budget,root(T).nomecliente,root(T).offerta);
ScriviOutput(T->right,p);
}
return 1;
}
int main(){
FILE* imp;
FILE* offr;
FILE* output;
tree T = NULL;
int maxarrayimp;
impianti V[29+1];
if( (imp = fopen("C:\\Temp\\VaccariMatteo\\VaccariMatteo\\impianti.bin","rb"))==NULL ){
printf("\nErrore nella lettura del file impianti.bin\n");
exit(1);
}
if ( (offr=fopen("C:\\Temp\\VaccariMatteo\\VaccariMatteo\\offerte.bin","rb")) == NULL){
printf("\nErrore nella lettura del file offerte.bin\n");
exit(1);
}
output=fopen("OUTPUT.TXT","wt");
maxarrayimp = caricamento(V,imp);
T = caricamentoTree(V,maxarrayimp,offr,T);
ScriviOutput(T,output);
fclose(output);
printf("\nFatto!\n\n");
return 1;
}
// TUTTO OK; pti 20