Ace of Hearts ha scritto sab, 10 giugno 2006 alle 02:08
Grazie mille Il Nero... Ma vorrei chiederti ancora una cosa... Non è che c'è un modo per tenere per punti una matrice di char? Mi servirebbe cosi...
Si può fare, ma dipende dal formato in cui ti servono e che cosa ci devi fare.
Ti serve il punteggio in forma di stringa ? Oppure ti serve di usare i char come se fossero numeri ?
Ti faccio le due possibilità:
a) Come stringa:
a1) Usando struct + qsort:
Codice:
struct Record
{
char Nome[80];
char Punteggio[30];
} Punteggi[200];
int CompareRecords(const void *a, const void *b)
{
return atoi((*(*Record)b).Punteggio) - atoi((*(*Record)a).Punteggio);
}
Poi, come prima, nel codice:
Codice:
qsort(Punteggi, 200, sizeof(Record), CompareRecords);
a2) Usando la funzione di sort "fatta in casa":
Codice:
char Nomi[100][80];
char Punteggi[100][30];
void OrdinaPunteggi(int numero)
{
for (int i = 0; i < numero - 1; i++)
{
int z = i;
for (int j = i + 1; j < numero; j++)
if (atoi(Punteggi[j]) > atoi(Punteggi[z]))
z = j;
char tempPunteggio[30]; char tempNome[80];
strcpy(tempPunteggio, Punteggi[z]);
strcpy(Punteggi[z], Punteggi[j]);
strcpy(Punteggi[j], tempPunteggio);
strcpy(tempNome, Nomi[z]);
strcpy(Nomi[z], Nomi[j]);
strcpy(Nomi[j], tempNome);
}
}
b) Come numeri:
Qui la soluzione varia molto a seconda di quello che vuoi come massimo punteggio possibile. Assumendo che come massimo punteggio ti basti 255, fai semplicemente:
b1) Con la struct ti limiti a cambiare la definizione della struct da:
Codice:
struct Record
{
char Nome[80];
int Punteggio;
}
a:
Codice:
struct Record
{
char Nome[80];
char Punteggio;
}
b2) Invece sempre col sort della nonna fatto in casa con tanto amore ()
Codice:
char Nomi[100][80];
char Punteggi[100];
void OrdinaPunteggi(int numero)
{
for (int i = 0; i < numero - 1; i++)
{
int z = i;
for (int j = i + 1; j < numero; j++)
if (Punteggi[j] > Punteggi[z])
z = j;
int tempPunteggio; char tempNome[80];
tempPunteggio = Punteggi[z];
Punteggi[z] = Punteggi[j];
Punteggi[j] = tempPunteggio;
strcpy(tempNome, Nomi[z]);
strcpy(Nomi[z], Nomi[j]);
strcpy(Nomi[j], tempNome);
}
}
Praticamente uguale a quello di prima, ma cambi da char a int.
C'è anche la possibilità che tu voglia punteggi maggiori di 255. In quel caso ti consiglio di usare un array di diciamo 2 char, e di scrivere il codice come se stessi trattando stringhe (vedi il caso a), sostituendo alla funzione atoi, che ho usato io, una funzione che ti scrivi tu per convertire queste sequenze di char in numeri interi, tipo:
Codice:
int CharToInt(char *val);
{
return val[0]*256 + val[1];
}
Per esempio.
Poi invece di usare strcpy, ti scrivi una funzioncina tua anche per la copia di questi valori-char:
Codice:
void CopyCharVal(char *Dest, char *Source)
{
Dest[0] = Source[0];
Dest[1] = Source[1];
}
E la usi al posto di strcpy.
In conclusione, a grandi linee:
Con la struct:
Codice:
struct Record
{
char Nome[80];
char Punteggio[2];
} Punteggi[200];
int CharToInt(char *val);
{
return val[0]*256 + val[1];
}
int CompareRecords(const void *a, const void *b)
{
return CharToInt((*(*Record)b).Punteggio) - CharToInt((*(*Record)a).Punteggio);
}
E poi, come al solito, nel codice:
Codice:
qsort(Punteggi, 200, sizeof(Record), CompareRecords);
Con il sort fatto in casa:
Codice:
int CharToInt(char *val);
{
return val[0]*256 + val[1];
}
void CopyCharVal(char *Dest, char *Source)
{
Dest[0] = Source[0];
Dest[1] = Source[1];
}
char Nomi[100][80];
char Punteggi[100][2];
void OrdinaPunteggi(int numero)
{
for (int i = 0; i < numero - 1; i++)
{
int z = i;
for (int j = i + 1; j < numero; j++)
if (CharToInt(Punteggi[j]) > CharToInt(Punteggi[z]))
z = j;
char tempPunteggio[2]; char tempNome[80];
CopyCharVal(tempPunteggio, Punteggi[z]);
CopyCharVal(Punteggi[z], Punteggi[j]);
CopyCharVal(Punteggi[j], tempPunteggio);
strcpy(tempNome, Nomi[z]);
strcpy(Nomi[z], Nomi[j]);
strcpy(Nomi[j], tempNome);
}
}
E amen.
Il tutto l'ho scritto un pò di corsa, quindi potrebbero esserci delle buddanate.