#include
#include
#include
using namespace std;
const int MAX=100;
struct t {
int hh, mm, ss;
int TotSecondi;
};
struct partec {
int pettorale;
string nome;
t tempo;
};
int ChiediDimensione();
Caricamento della tabella
void CaricaTabella(partec T[], int d);
Funzione di scambio
void Scambia(partec& a, partec& b);
void Ordina(partec T[], int d);
void StampaTabella(partec T[], int d);
int main()
{
int dim;
partec tab[MAX];
dim=ChiediDimensione();
CaricaTabella(tab,dim);
Ordina(tab,dim);
StampaTabella(tab,dim);
system("pause");
return 0;
}
int ChiediDimensione()
{
int d;
do{
cerr
cin>>d;
} while (dMAX);
return d;
}
void CaricaTabella(partec T[], int d)
{
for (int i=0; i
cerr
cin>>T.pettorale;
cerr
cin>>T.nome;
cerr
cin>>T.tempo.hh;
cerr
cin>>T.tempo.mm;
cerr
cin>>T.tempo.ss;
T.tempo.TotSecondi = T.tempo.hh*360 + T.tempo.mm*60 + T.tempo.ss;
}
}
void Scambia(partec& a, partec& b)
{
partec comodo;
comodo=a;
a=b;
b=comodo;
}
void Ordina(partec T[], int d)
{
for (int i=0; i
for (int j=i+1; j
if (T.tempo.TotSecondi>T[j].tempo.TotSecondi)
Scambia(T, T[j]);
}
}
}
void StampaTabella(partec T[], int d)
{
cout
for (int i=0; i
cout .pettorale
.nome
.tempo.hh
.tempo.mm
.tempo.ss
}
Domande da interrogazione
- What is the purpose of the `CaricaTabella` function in the code?
- How does the `Ordina` function organize the participants' data?
- What role does the `Scambia` function play in the sorting process?
The `CaricaTabella` function is designed to populate an array of `partec` structures with participant data, including their bib number, name, and race time in hours, minutes, and seconds. It also calculates the total time in seconds for each participant.
The `Ordina` function sorts the participants in ascending order based on their total race time in seconds. It uses a nested loop to compare each participant's time and swaps them if necessary to ensure the list is ordered from the fastest to the slowest.
The `Scambia` function is used to exchange the positions of two `partec` structures within the array. It is called by the `Ordina` function whenever two participants need to be swapped to maintain the correct order based on their race times.