#include #include #include #include #define EXIT_SUCCESS 0 #define EXIT_COMMANDLINE 1 #define EXIT_FILEACCESS 2 #define EXIT_INPUTFORMAT 3 #define EXIT_MEMORY 4 #define LUNGHEZZA 256 #define INFINITE 1e10; enum _boolean {FALSE = 0, TRUE = 1}; typedef enum _boolean boolean; typedef struct _point point; typedef point* PointVector; struct _point { int id; int x, y; }; void ReadCommandLine (int argc, char *argv[], char *InputFile); void LoadData (char *InputFile, int *pNumPoints, PointVector *pPoint); void FindClosestPair (int NumPoints, PointVector Point, point *P1Min, point *P2Min, double *pDist); int main (int argc, char *argv[]) { char InputFile[LUNGHEZZA]; int NumPoints; PointVector Point; point P1, P2; double Dist; // 1) Interpretazione della linea di comando ReadCommandLine(argc,argv,InputFile); // 2) Caricamento dei dati LoadData(InputFile,&NumPoints,&Point); // 3) Allocazione delle strutture dati ausiliarie // 4) Elaborazione FindClosestPair(NumPoints,Point,&P1,&P2,&Dist); // 5) Deallocazione delle strutture dati ausiliarie // 6) Stampa della soluzione printf("Le colonie %d e %d restano separate al massimo %d giorni dopo l'impianto\n", P1.id,P2.id,(int)(Dist/2.0)); // 7) Deallocazione delle strutture dati principali free(Point); return EXIT_SUCCESS; } void ReadCommandLine (int argc, char *argv[], char *InputFile) { if (argc != 2) { printf("The command line has a wrong format!\n"); printf("%s [InputFile]\n",argv[0],InputFile); exit(EXIT_COMMANDLINE); } strcpy(InputFile,argv[1]); } void LoadData (char *InputFile, int *pNumPoints, PointVector *pPoint) { FILE *fInputFile; int p; fInputFile = fopen(InputFile,"r"); if (fInputFile == NULL) { printf("File %s could not be opened!\n",InputFile); exit(EXIT_FILEACCESS); } if (fscanf(fInputFile,"%d",pNumPoints) != 1) { printf("The number of points could not be found in file %s!\n",InputFile); exit(EXIT_INPUTFORMAT); } *pPoint = (PointVector) calloc(*pNumPoints+1,sizeof(point)); if (*pPoint == NULL) { printf("Not enough memory to allocate the vector of points!\n"); exit(EXIT_MEMORY); } for (p = 1; p <= *pNumPoints; p++) { if (fscanf(fInputFile,"%d %d",&(*pPoint)[p].x,&(*pPoint)[p].y) != 2) { printf("The coordinates of point %d could not be found in file %s!\n",p,InputFile); exit(EXIT_INPUTFORMAT); } (*pPoint)[p].id = p; } fclose(fInputFile); } void FindClosestPair (int NumPoints, PointVector Point, point *P1Min, point *P2Min, double *pDist) { P1Min->id = 0; P2Min->id = 0; *pDist = 0.0; }