Saluti,
stasera sono di cattivo umore ...
Enzo enzo.gupi@xxxxxxxxxx [mlerlug/Lista ERLUG] wrote:
/* ---- casual.c ---- */
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(int argc, char *argv[])
{
int Start,End,Swapper,Result;
char* Err = NULL;
if (argc==3) {
Start = strtoul (argv[1],&Err,10);
Dato che Start è int ma strtoul() ritorna unsigned long,
anche supponendo che siano entrambi 32 bit per argv[1]
p. es. pari a "400000000" Start risulta negativo.
if (*Err) {
printf ("error: invalid value\n");
I messaggi d'errore andrebbero su stderr ...
return;
return cosa ?
}
End = strtoul (argv[2],&Err,10);
if (*Err) {
printf ("error: invalid value\n");
return;
}
if (Start>End) {
Swapper = Start;
Start = End;
End = Swapper;
}
Invece di scambiare i parametri, sarebbe meglio che si piantasse
con un bel messaggio di errore a mio parere; lo sforzo di mettere i
parametri in ordine è sicuramente inferiore a quello che occorre per
capire che un programma che utilizza questo gli sta passando i
parametri sbagliati.
srand (time (NULL));
Qui magari sarebbe meglio un'opzione -s che consenta di specificare
il seme o richiedere un seme pseudo casuale, come con time(), ma il
discorso si farebbe lungo.
Result = Start+(int)(((double)(End-Start+1)*rand())/(RAND_MAX+1.0));
printf ("%d\n",Result);
} else {
printf ("usage: casual <from> <to>\n");
}
}
Allego una mia rielaborazione del programma, disponibile sotto
licenza GPL. Non me ne voglia Enzo: il punto è che secondo me, noblesse
oblige, chi si propone come paladino dell'open source incorre
nell'obbligo di rendere disponibile il miglior sorgente che può. Del
codice che si limita a risolvere il problema può andare bene in un
contesto closed source. Naturalmente, mi aspetto che ora sarà il mio
codice ad essere preso a sassate ... ma così imparerò qualcosa di
nuovo.
Davide Bolcioni
--
There is no place like /home.
/*
* rand.c : a simple program to print a random integer in a given range
*
* Copyright (C) 2002 Davide Bolcioni
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <math.h>
static char* program_name;
void
die(int errno_code)
{
errno = errno_code;
(void) perror(program_name);
exit(1);
}
unsigned long
obtain_unsigned_long(const char* source)
{
unsigned long result;
char* end;
result = strtoul(source, &end, 10);
if (*end)
die(EINVAL);
return result;
}
int main(int argc, char** argv)
{
unsigned long start, end, result;
double n,d;
program_name = argv[0]; // Always works.
if (argc > 1) {
start = obtain_unsigned_long(argv[1]);
end = obtain_unsigned_long(argv[2]);
if (start >= end)
die(EINVAL);
srand(time(NULL));
n = rand();
d = RAND_MAX + 1.0;
result = start + (unsigned long)((end + 1 - start) * (n / d));
printf("%lu\n", result);
}
else {
(void) fprintf(stderr, "usage: %s start end\n", program_name);
}
return 0;
}
|