codice:
/*
Note : Il file nome fisico docenti.dat contiene l'elenco dei
docenti(tipo Rdocente) che insegnano in un corso il cui
nome è Corso, mentre il file di nome fisico studenti.dat
contiene l'elenco degli studenti (tipo Rstudente) che
devono seguire quei corsi. I record degli studenti
contendono Cognome,Nome,dataN,Corsi. I due file sono
entrambi ordinati per key.
Scrivere una procedura che, per ogni docente, crei un
file, il cui nome coincide col cognome del docente,
contenente tutti gli studenti di quel corso(campi
nome, congnome, data di nascita) in ordine alfabetico
per cognome e nome e a parità per data di nascita.
COME POSSO FARE A FARE UN IMPUT SU FILE DI UN ARRAY DI ARRAY DI CARATTERI?
*/
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct Tpdata {
int giorno;
int mese;
int anno;
};
struct Rlibro{
int key;
char cognome[20];
char nome[20];
char corso[20];
};
struct Rstudente{
int key;
char cognome[20];
char nome[20];
Tpdata dataN;
char corso[20];
};
typedef Rlibro* Plibro;
typedef Rstudente* Pstudente;
void Inserimento (Rstudente &, char &);
void getfile(Pstudente [],int );
void putfile(Rstudente [],int ,int );
void insort(Pstudente [],int ,Pstudente);
double Datanum(Pstudente );
const char Nomefile[ ]="studente.dat";
const int Lrec=sizeof(Rlibro);
const int Numpers=6;
int main(){
char chr;
Rstudente libro;
/*int Numpers;
cout<<"Numero di libri da inserire: ";
cin>>Numpers;
fstream filepers;
filepers.open("studente.dat",ios::in|ios::out|ios::binary|ios::ate);
if (!filepers)
filepers.open("studente.dat",ios::out|ios::binary|ios::trunc);
while(Numpers>0){
Inserimento(libro,chr);
if (chr=='s') {
filepers.seekp(0,ios::end);
filepers.write((char*)&libro,Lrec);
Numpers--;
}}
filepers.close();*/
Pstudente PuntaP1[101],PuntaP2[101];
getfile(PuntaP1,Lrec);
//for(int k=1;k<=Numpers;k++){
// stampa(*PuntaP1[k]);
// }
system("pause");
//SortStud(PuntaP1,Numpers,true);
//putfile(PuntaP1,Numpers,Lrec);
system("pause");
return 0;
}
void Inserimento (Rstudente &pers1, char &ch)
{
cout << "INSERIMENTO LIBRI" << endl << endl;
pers1.key=100;
cout << "\nCognome= ";
cin >> pers1.nome;
cout << "Nome= ";
cin >> pers1.cognome;
/*cout << "\nEditore= ";
cin >> pers1.corso;*/
cout << "\nData Nascita= ";
cin >> pers1.dataN.giorno>>pers1.dataN.mese>>pers1.dataN.anno;
cout << "\nCorsi= ";
cin >> pers1.corso;
/*cout << "\nStato= ";
cin >> pers1.stato;*/
cout << "Salva (s/n)? ";
cin >> ch;
}
double Datanum(Pstudente x){
return x->dataN.anno*10000+x->dataN.mese*100+x->dataN.giorno;
}
void getfile(Pstudente PuntaP1[],int Lrec){
fstream file,file1;
Rlibro temp;
Rstudente temp1;
file.open("docente.dat",ios::in|ios::out|ios::binary|ios::ate);
if (!file)
file.open("docente.dat",ios::out|ios::binary|ios::trunc);
file1.open("studente.dat",ios::in|ios::out|ios::binary|ios::ate);
if (!file1)
file1.open("studente.dat",ios::out|ios::binary|ios::trunc);
int NumFileStud=file1.tellg()/sizeof(Rstudente);
int Numstud=file.tellg()/Lrec;
file.seekg(0,ios::beg);
int k=0,z=1;
for(int i=1;i<=Numstud;i++){
file.read((char*) &temp,Lrec);
cout<<temp.cognome<<endl;
system("pause");
file1.read((char*) &temp1,sizeof(Rstudente));
cout<<temp1.cognome<<endl;
system("pause");
while(k<NumFileStud && temp.key!=temp1.key){
file1.read((char*) &temp1,sizeof(Rstudente));
cout<<temp1.cognome<<endl;
system("pause");
}
while(k<NumFileStud && temp.key==temp1.key){
//Se array devo far cercare nell'array, come?
if(stricmp(temp.corso,temp1.corso)==0){
PuntaP1[z]=new Rstudente;
*PuntaP1[z]=temp1;
insort(PuntaP1,z,PuntaP1[z]);
z++;
file1.read((char*) &temp1,sizeof(Rstudente));
}
}
if(z>1){
fstream file2;
file2.open(temp.cognome,ios::in|ios::out|ios::binary|ios::ate);
if (!file)
file2.open(temp.cognome,ios::out|ios::binary|ios::trunc);
for(int n=1;n<=z;n++){
file2.write((char*) &*PuntaP1[i]->cognome,sizeof(char[20]));
file2.write((char*) &*PuntaP1[i]->nome,sizeof(char[20]));
file2.write((char*) &PuntaP1[i]->dataN.giorno,sizeof(int));
}file2.close();
}
file1.seekg(0,ios::beg);
}
file.close();
}
void insort(Pstudente Punt[],int n,Pstudente Temp){
int j;
Punt[0]=Temp;
j=n-1;
if(j>0 && stricmp(Punt[j]->cognome,Punt[0]->cognome)>0){
while(j>0 && stricmp(Punt[j]->cognome,Punt[0]->cognome)>0){
Punt[j+1]=Punt[j];
j--;
}
}
if(j>0 && stricmp(Punt[j]->cognome,Punt[0]->cognome)==0 &&
stricmp(Punt[j]->nome,Punt[0]->nome)<0){
while(j>0 && stricmp(Punt[j]->nome,Punt[0]->nome)<0){
Punt[j+1]=Punt[j];
j--;
}
}
if(stricmp(Punt[j]->nome,Punt[0]->nome)==0 && Datanum(Punt[j])<Datanum(Punt[0])){
while(Datanum(Punt[j])<Datanum(Punt[0])){
Punt[j+1]=Punt[j];
j--;
}
}
Punt[j+1]=Punt[0];
}
void putfile(Pstudente Punt[],int Numero,int Lrec){
fstream file;
file.open("libri2.dat",ios::out|ios::ate|ios::binary);
if (!file)
file.open("libri2.dat",ios::out|ios::binary|ios::trunc);
for(int i=0;i<Numero;i++){
file.write((char*) &*Punt[i],Lrec);
}
}